The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as dns from "@distilled.cloud/cloudflare/dns"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { testName } from "../../names.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | // Deterministic key material (this is test-only, not a real secret). |
| 18 | const TSIG_SECRET = |
| 19 | "kyTZf6QHTPVdpDjLWWbYO7DI3Z6f3wWvECDCtMHEOSomCnq0db4DBzowg4QH51jJZUw5n4nGYNGmkJhCfn+9Ag=="; |
| 20 | |
| 21 | // Ride out fresh-token 403 blips on out-of-band calls. |
| 22 | const retryForbidden = <A, E extends { _tag: string }, R>(eff: Effect.Effect<A, E, R>) => |
| 23 | eff.pipe( |
| 24 | Effect.retry({ |
| 25 | while: (e) => e._tag === "Forbidden", |
| 26 | schedule: Schedule.exponential("500 millis"), |
| 27 | times: 8, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | const getPeer = (accountId: string, peerId: string) => |
| 32 | retryForbidden(dns.getZoneTransferPeer({ accountId, peerId })); |
| 33 | |
| 34 | // A deleted peer surfaces as the typed `PeerNotFound` (HTTP 404). |
| 35 | const expectGone = (accountId: string, peerId: string) => |
| 36 | getPeer(accountId, peerId).pipe( |
| 37 | Effect.flatMap(() => Effect.fail({ _tag: "PeerNotDeleted" } as const)), |
| 38 | Effect.catchTag("PeerNotFound", () => Effect.void), |
| 39 | Effect.retry({ |
| 40 | while: (e) => e._tag === "PeerNotDeleted", |
| 41 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | test.provider( |
| 46 | "create with connection settings, update in place, and delete a peer", |
| 47 | (stack) => |
| 48 | Effect.gen(function* () { |
| 49 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 50 | |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | // Create applies name-only POST followed by a PUT with the |
| 54 | // connection settings. |
| 55 | const created = yield* stack.deploy( |
| 56 | Cloudflare.DNS.ZoneTransferPeer("TestPeer", { |
| 57 | name: testName("alchemy-dnszt-peer-test"), |
| 58 | ip: "192.0.2.53", |
| 59 | port: 53, |
| 60 | }), |
| 61 | ); |
| 62 | expect(created.peerId).toBeDefined(); |
| 63 | expect(created.accountId).toEqual(accountId); |
| 64 | expect(created.name).toEqual(testName("alchemy-dnszt-peer-test")); |
| 65 | expect(created.ip).toEqual("192.0.2.53"); |
| 66 | expect(created.port).toEqual(53); |
| 67 | |
| 68 | // Out-of-band verify via the SDK. |
| 69 | const live = yield* getPeer(accountId, created.peerId); |
| 70 | expect(live.name).toEqual(testName("alchemy-dnszt-peer-test")); |
| 71 | expect(live.ip).toEqual("192.0.2.53"); |
| 72 | expect(live.port).toEqual(53); |
| 73 | |
| 74 | // Update the connection settings in place — same physical peer. |
| 75 | const updated = yield* stack.deploy( |
| 76 | Cloudflare.DNS.ZoneTransferPeer("TestPeer", { |
| 77 | name: testName("alchemy-dnszt-peer-test-renamed"), |
| 78 | ip: "198.51.100.53", |
| 79 | port: 5353, |
| 80 | }), |
| 81 | ); |
| 82 | expect(updated.peerId).toEqual(created.peerId); |
| 83 | expect(updated.name).toEqual(testName("alchemy-dnszt-peer-test-renamed")); |
| 84 | expect(updated.ip).toEqual("198.51.100.53"); |
| 85 | expect(updated.port).toEqual(5353); |
| 86 | |
| 87 | const liveUpdated = yield* getPeer(accountId, created.peerId); |
| 88 | expect(liveUpdated.ip).toEqual("198.51.100.53"); |
| 89 | expect(liveUpdated.port).toEqual(5353); |
| 90 | |
| 91 | yield* stack.destroy(); |
| 92 | yield* expectGone(accountId, created.peerId); |
| 93 | |
| 94 | // Re-running destroy is idempotent. |
| 95 | yield* stack.destroy(); |
| 96 | }).pipe(logLevel), |
| 97 | { |
| 98 | tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"], |
| 99 | timeout: 120_000, |
| 100 | }, |
| 101 | ); |
| 102 | |
| 103 | test.provider( |
| 104 | "wires a TSIG reference through to the peer", |
| 105 | (stack) => |
| 106 | Effect.gen(function* () { |
| 107 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | const { peer, tsig } = yield* stack.deploy( |
| 112 | Effect.gen(function* () { |
| 113 | const tsig = yield* Cloudflare.DNS.ZoneTransferTsig("PeerTsig", { |
| 114 | algo: "hmac-sha512.", |
| 115 | secret: Redacted.make(TSIG_SECRET), |
| 116 | }); |
| 117 | const peer = yield* Cloudflare.DNS.ZoneTransferPeer("TsigPeer", { |
| 118 | ip: "192.0.2.54", |
| 119 | tsigId: tsig.tsigId, |
| 120 | }); |
| 121 | return { peer, tsig }; |
| 122 | }), |
| 123 | ); |
| 124 | expect(peer.tsigId).toEqual(tsig.tsigId); |
| 125 | |
| 126 | const live = yield* getPeer(accountId, peer.peerId); |
| 127 | expect(live.tsigId).toEqual(tsig.tsigId); |
| 128 | |
| 129 | yield* stack.destroy(); |
| 130 | yield* expectGone(accountId, peer.peerId); |
| 131 | }).pipe(logLevel), |
| 132 | { |
| 133 | tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"], |
| 134 | timeout: 120_000, |
| 135 | }, |
| 136 | ); |
| 137 | |
| 138 | test.provider( |
| 139 | "list enumerates the deployed peer", |
| 140 | (stack) => |
| 141 | Effect.gen(function* () { |
| 142 | yield* stack.destroy(); |
| 143 | |
| 144 | const deployed = yield* stack.deploy( |
| 145 | Cloudflare.DNS.ZoneTransferPeer("ListPeer", { |
| 146 | ip: "192.0.2.55", |
| 147 | port: 53, |
| 148 | }), |
| 149 | ); |
| 150 | |
| 151 | const provider = yield* Provider.findProvider(Cloudflare.DNS.ZoneTransferPeer); |
| 152 | const all = yield* provider.list(); |
| 153 | |
| 154 | expect(all.some((p) => p.peerId === deployed.peerId)).toBe(true); |
| 155 | |
| 156 | yield* stack.destroy(); |
| 157 | }).pipe(logLevel), |
| 158 | { |
| 159 | tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"], |
| 160 | timeout: 120_000, |
| 161 | }, |
| 162 | ); |