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

main
ts 143 lines 5.39 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as dns from "@distilled.cloud/cloudflare/dns";
7 import { Action } from "alchemy/Action";
8 import { adopt } from "alchemy/AdoptPolicy";
9 import * as Effect from "effect/Effect";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Stream from "effect/Stream";
12 import { testName } from "../../names.ts";
13
14 const { test } = Test.make({ providers: Cloudflare.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
19
20 // Deterministic record name — reused on every run (never derive from
21 // Date.now()/random), owns its own subdomain so it never collides with the
22 // managed-Record tests.
23 const RECORD_NAME = `${testName("alchemy-dnslocal")}.${zoneName}`;
24 const RECORD_TYPE = "TXT";
25 const RECORD_CONTENT = '"alchemy-dnslocal-seed"';
26
27 const resolveZoneId = Effect.gen(function* () {
28 const { accountId } = yield* yield* CloudflareEnvironment;
29 const zone = yield* findZoneByName({ accountId, name: zoneName });
30 if (!zone) {
31 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
32 }
33 return zone.id;
34 });
35
36 // Delete every record matching (name, type) — used to clear leftovers from
37 // interrupted runs and to guarantee cleanup on finish.
38 const purgeRecords = (zoneId: string) =>
39 dns.listRecords
40 .items({
41 zoneId,
42 name: { exact: RECORD_NAME },
43 type: RECORD_TYPE,
44 })
45 .pipe(
46 Stream.filter((r) => r.name === RECORD_NAME && r.type === RECORD_TYPE),
47 Stream.runCollect,
48 Effect.map((chunk) => Array.from(chunk)),
49 Effect.flatMap(
50 Effect.forEach((r) =>
51 dns.deleteRecord({ zoneId, dnsRecordId: r.id }).pipe(Effect.catch(() => Effect.void)),
52 ),
53 ),
54 );
55
56 const findRecord = (zoneId: string) =>
57 dns.listRecords.items({ zoneId, name: { exact: RECORD_NAME }, type: RECORD_TYPE }).pipe(
58 Stream.filter((r) => r.name === RECORD_NAME && r.type === RECORD_TYPE),
59 Stream.runCollect,
60 Effect.map((chunk) => Array.from(chunk)[0]),
61 );
62
63 // Binding a DNS zone inside an Action via `ReadWriteDnsLocal` — the local
64 // (current-credentials) implementation of the `ReadWriteDns` binding. Exercises
65 // the write (create), read (get), list, and delete client surface plus the
66 // deferred zone-id accessor (`yield* zone.zoneId` resolved at apply time).
67 test.provider(
68 "ReadWriteDnsLocal: create, read, list and delete a TXT record from an Action",
69 (stack) =>
70 Effect.gen(function* () {
71 const zoneId = yield* resolveZoneId;
72
73 yield* stack.destroy();
74 // Clear leftovers from interrupted runs (Cloudflare allows duplicate
75 // records for the same name/type).
76 yield* purgeRecords(zoneId);
77
78 const out = yield* stack
79 .deploy(
80 Effect.gen(function* () {
81 // Adopt the standing test zone. Zones default to `retain` on
82 // removal, so `stack.destroy()` never deletes it.
83 const zone = yield* Cloudflare.Zone.Zone("DnsLocalZone", {
84 name: zoneName,
85 }).pipe(adopt(true));
86
87 const Seed = Action(
88 "Seed",
89 Effect.gen(function* () {
90 const dnsClient = yield* Cloudflare.DNS.ReadWriteDns(zone);
91 // Accessor — resolved at apply time against the tracker.
92 const zoneIdAccessor = yield* zone.zoneId;
93
94 return Effect.fn(function* () {
95 const created = yield* dnsClient.createDnsRecord({
96 type: RECORD_TYPE,
97 name: RECORD_NAME,
98 content: RECORD_CONTENT,
99 ttl: 60,
100 });
101
102 const record = yield* dnsClient.getDnsRecord(created.id);
103
104 const listed = yield* dnsClient.listDnsRecords({
105 type: RECORD_TYPE,
106 name: { exact: RECORD_NAME },
107 });
108
109 yield* dnsClient.deleteDnsRecord(created.id);
110
111 return {
112 zoneId: yield* zoneIdAccessor,
113 recordId: created.id,
114 content: record.content,
115 listedContent: listed.result.find((r) => r.id === created.id)?.content,
116 listedCount: listed.result.length,
117 };
118 });
119 }).pipe(Effect.provide(Cloudflare.DNS.ReadWriteDnsLocal)),
120 );
121
122 return yield* Seed({});
123 }),
124 )
125 .pipe(Effect.ensuring(purgeRecords(zoneId).pipe(Effect.ignore)));
126
127 expect(out.zoneId).toEqual(zoneId);
128 expect(out.recordId).toBeTruthy();
129 expect(out.content).toEqual(RECORD_CONTENT);
130 expect(out.listedContent).toEqual(RECORD_CONTENT);
131 expect(out.listedCount).toBeGreaterThan(0);
132
133 // The Action deleted the record — it is gone out-of-band.
134 const gone = yield* findRecord(zoneId);
135 expect(gone).toBeUndefined();
136
137 yield* stack.destroy();
138 }).pipe(logLevel),
139 {
140 tags: ["provider:cloudflare", "provider:cloudflare:dns", "provider:cloudflare:zone", "live"],
141 timeout: 120_000,
142 },
143 );