The best way to control Claude Code, Codex and more from any computer or phone

main
ts 127 lines 4.52 KB
Raw
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 SECRET_A =
19 "kyTZf6QHTPVdpDjLWWbYO7DI3Z6f3wWvECDCtMHEOSomCnq0db4DBzowg4QH51jJZUw5n4nGYNGmkJhCfn+9Ag==";
20 const SECRET_B =
21 "8nNZTjsM1Yu0aFYNYTcMM2KFCDc2cMKQz3mNkLV0XzUgDgQ4Mc7XhhSh0RUFhd6OYzz6Vl5hUTUVUYz1JBdt0g==";
22
23 // Ride out fresh-token 403 blips on out-of-band calls.
24 const retryForbidden = <A, E extends { _tag: string }, R>(eff: Effect.Effect<A, E, R>) =>
25 eff.pipe(
26 Effect.retry({
27 while: (e) => e._tag === "Forbidden",
28 schedule: Schedule.exponential("500 millis"),
29 times: 8,
30 }),
31 );
32
33 const getTsig = (accountId: string, tsigId: string) =>
34 retryForbidden(dns.getZoneTransferTsig({ accountId, tsigId }));
35
36 // A deleted TSIG surfaces as the typed `TsigNotFound` (HTTP 404).
37 const expectGone = (accountId: string, tsigId: string) =>
38 getTsig(accountId, tsigId).pipe(
39 Effect.flatMap(() => Effect.fail({ _tag: "TsigNotDeleted" } as const)),
40 Effect.catchTag("TsigNotFound", () => Effect.void),
41 Effect.retry({
42 while: (e) => e._tag === "TsigNotDeleted",
43 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
44 }),
45 );
46
47 test.provider(
48 "create, rotate the secret in place, and delete a TSIG",
49 (stack) =>
50 Effect.gen(function* () {
51 const { accountId } = yield* yield* CloudflareEnvironment;
52
53 yield* stack.destroy();
54
55 const created = yield* stack.deploy(
56 Cloudflare.DNS.ZoneTransferTsig("TestTsig", {
57 name: `${testName("alchemy-dnszt-tsig-test")}.`,
58 algo: "hmac-sha512.",
59 secret: Redacted.make(SECRET_A),
60 }),
61 );
62 expect(created.tsigId).toBeDefined();
63 expect(created.accountId).toEqual(accountId);
64 expect(created.name).toEqual(`${testName("alchemy-dnszt-tsig-test")}.`);
65 expect(created.algo).toEqual("hmac-sha512.");
66 // The secret is never persisted in attributes.
67 expect("secret" in created).toBe(false);
68
69 // Out-of-band verify via the SDK.
70 const live = yield* getTsig(accountId, created.tsigId);
71 expect(live.name).toEqual(`${testName("alchemy-dnszt-tsig-test")}.`);
72 expect(live.secret).toEqual(SECRET_A);
73
74 // Rotate the secret in place — same physical TSIG.
75 const rotated = yield* stack.deploy(
76 Cloudflare.DNS.ZoneTransferTsig("TestTsig", {
77 name: `${testName("alchemy-dnszt-tsig-test")}.`,
78 algo: "hmac-sha512.",
79 secret: Redacted.make(SECRET_B),
80 }),
81 );
82 expect(rotated.tsigId).toEqual(created.tsigId);
83
84 const liveRotated = yield* getTsig(accountId, created.tsigId);
85 expect(liveRotated.secret).toEqual(SECRET_B);
86
87 yield* stack.destroy();
88 yield* expectGone(accountId, created.tsigId);
89
90 // Re-running destroy is idempotent.
91 yield* stack.destroy();
92 }).pipe(logLevel),
93 {
94 tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"],
95 timeout: 120_000,
96 },
97 );
98
99 // Canonical `list()` test (account-scoped collection): deploy a real TSIG,
100 // resolve the provider from context via the typed `findProvider`, call
101 // `list()`, and assert the deployed TSIG appears in the exhaustively-paginated
102 // result.
103 test.provider(
104 "list enumerates the deployed TSIG",
105 (stack) =>
106 Effect.gen(function* () {
107 yield* stack.destroy();
108
109 const deployed = yield* stack.deploy(
110 Cloudflare.DNS.ZoneTransferTsig("ListTsig", {
111 algo: "hmac-sha512.",
112 secret: Redacted.make(SECRET_A),
113 }),
114 );
115
116 const provider = yield* Provider.findProvider(Cloudflare.DNS.ZoneTransferTsig);
117 const all = yield* provider.list();
118
119 expect(all.some((t) => t.tsigId === deployed.tsigId)).toBe(true);
120
121 yield* stack.destroy();
122 }).pipe(logLevel),
123 {
124 tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"],
125 timeout: 120_000,
126 },
127 );