@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 173 lines 6.36 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 zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { testName } from "../../names.ts";
10
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Ride out 403 blips (`Forbidden`) while the harness-minted token
16 // propagates across Cloudflare's edge.
17 const getList = (accountId: string, listId: string) =>
18 zeroTrust.getGatewayList({ accountId, listId }).pipe(
19 Effect.retry({
20 while: (e) => e._tag === "Forbidden",
21 schedule: Schedule.exponential("500 millis"),
22 times: 8,
23 }),
24 );
25
26 // A deleted list surfaces as `GatewayListNotFound` (Cloudflare code 2218).
27 const expectGone = (accountId: string, listId: string) =>
28 getList(accountId, listId).pipe(
29 Effect.flatMap(() => Effect.fail({ _tag: "ListNotDeleted" } as const)),
30 Effect.catchTag("GatewayListNotFound", () => Effect.void),
31 Effect.retry({
32 while: (e) => e._tag === "ListNotDeleted",
33 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
34 }),
35 );
36
37 const itemValues = (items: ReadonlyArray<{ value: string }>) => items.map((i) => i.value).sort();
38
39 test.provider(
40 "create, verify, and destroy a DOMAIN list",
41 (stack) =>
42 Effect.gen(function* () {
43 const { accountId } = yield* yield* CloudflareEnvironment;
44
45 yield* stack.destroy();
46
47 const list = yield* stack.deploy(
48 Cloudflare.Gateway.List("BasicList", {
49 name: testName("alchemy-zt-list-basic"),
50 type: "DOMAIN",
51 description: "alchemy test list",
52 items: [{ value: "a.alchemy-test.example" }, { value: "b.alchemy-test.example" }],
53 }),
54 );
55
56 expect(list.listId).toBeTruthy();
57 expect(list.accountId).toEqual(accountId);
58 expect(list.type).toEqual("DOMAIN");
59 expect(itemValues(list.items)).toEqual(["a.alchemy-test.example", "b.alchemy-test.example"]);
60
61 const live = yield* getList(accountId, list.listId);
62 expect(live.name).toEqual(testName("alchemy-zt-list-basic"));
63 expect(live.description).toEqual("alchemy test list");
64 expect(itemValues((live.items ?? []) as { value: string }[])).toEqual([
65 "a.alchemy-test.example",
66 "b.alchemy-test.example",
67 ]);
68
69 yield* stack.destroy();
70 yield* expectGone(accountId, list.listId);
71 }).pipe(logLevel),
72 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
73 );
74
75 test.provider(
76 "update items, description, and name in place",
77 (stack) =>
78 Effect.gen(function* () {
79 const { accountId } = yield* yield* CloudflareEnvironment;
80
81 yield* stack.destroy();
82
83 const initial = yield* stack.deploy(
84 Cloudflare.Gateway.List("UpdateList", {
85 name: testName("alchemy-zt-list-update"),
86 type: "DOMAIN",
87 items: [{ value: "keep.alchemy-test.example" }, { value: "remove.alchemy-test.example" }],
88 }),
89 );
90
91 const updated = yield* stack.deploy(
92 Cloudflare.Gateway.List("UpdateList", {
93 name: testName("alchemy-zt-list-update-v2"),
94 type: "DOMAIN",
95 description: "now with a description",
96 items: [{ value: "keep.alchemy-test.example" }, { value: "add.alchemy-test.example" }],
97 }),
98 );
99
100 // Same list mutated in place — not a replacement.
101 expect(updated.listId).toEqual(initial.listId);
102 expect(updated.name).toEqual(testName("alchemy-zt-list-update-v2"));
103 expect(itemValues(updated.items)).toEqual([
104 "add.alchemy-test.example",
105 "keep.alchemy-test.example",
106 ]);
107
108 const live = yield* getList(accountId, updated.listId);
109 expect(live.name).toEqual(testName("alchemy-zt-list-update-v2"));
110 expect(live.description).toEqual("now with a description");
111 expect(itemValues((live.items ?? []) as { value: string }[])).toEqual([
112 "add.alchemy-test.example",
113 "keep.alchemy-test.example",
114 ]);
115
116 // Redeploying identical props is a no-op (still the same list).
117 const noop = yield* stack.deploy(
118 Cloudflare.Gateway.List("UpdateList", {
119 name: testName("alchemy-zt-list-update-v2"),
120 type: "DOMAIN",
121 description: "now with a description",
122 items: [{ value: "keep.alchemy-test.example" }, { value: "add.alchemy-test.example" }],
123 }),
124 );
125 expect(noop.listId).toEqual(initial.listId);
126
127 yield* stack.destroy();
128 yield* expectGone(accountId, initial.listId);
129 }).pipe(logLevel),
130 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
131 );
132
133 test.provider(
134 "changing the type replaces the list",
135 (stack) =>
136 Effect.gen(function* () {
137 const { accountId } = yield* yield* CloudflareEnvironment;
138
139 yield* stack.destroy();
140
141 const domainList = yield* stack.deploy(
142 Cloudflare.Gateway.List("ReplaceList", {
143 name: testName("alchemy-zt-list-replace"),
144 type: "DOMAIN",
145 items: [{ value: "x.alchemy-test.example" }],
146 }),
147 );
148
149 // The name is the resource's cold-read identity, so a replacement
150 // (type change) pairs with a rename — keeping the old name would make
151 // the engine find the doomed sibling and refuse to adopt it.
152 const ipList = yield* stack.deploy(
153 Cloudflare.Gateway.List("ReplaceList", {
154 name: testName("alchemy-zt-list-replace-ip"),
155 type: "IP",
156 items: [{ value: "203.0.113.1" }],
157 }),
158 );
159
160 // Type is immutable — the engine must have created a new list.
161 expect(ipList.listId).not.toEqual(domainList.listId);
162 expect(ipList.type).toEqual("IP");
163
164 const live = yield* getList(accountId, ipList.listId);
165 expect(live.type).toEqual("IP");
166 // The old list was deleted by the replacement.
167 yield* expectGone(accountId, domainList.listId);
168
169 yield* stack.destroy();
170 yield* expectGone(accountId, ipList.listId);
171 }).pipe(logLevel),
172 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
173 );