@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 217 lines 7.56 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 vulnScanner from "@distilled.cloud/cloudflare/vulnerability-scanner";
7 import { adopt } from "alchemy/AdoptPolicy";
8 import * as Provider from "alchemy/Provider";
9 import * as Effect from "effect/Effect";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Schedule from "effect/Schedule";
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 per-test names — same on every run, disjoint between tests.
21 const NAME_DEFAULT = testName("alchemy-vulnscanner-target-default");
22 const NAME_UPDATE = testName("alchemy-vulnscanner-target-update");
23 const NAME_LIST = testName("alchemy-vulnscanner-target-list");
24
25 const resolveZoneId = Effect.gen(function* () {
26 const { accountId } = yield* yield* CloudflareEnvironment;
27 const zone = yield* findZoneByName({ accountId, name: zoneName });
28 if (!zone) {
29 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
30 }
31 return zone.id;
32 });
33
34 const accountIdOf = Effect.gen(function* () {
35 const { accountId } = yield* yield* CloudflareEnvironment;
36 return accountId;
37 });
38
39 // Freshly minted scoped tokens propagate eventually-consistently across
40 // Cloudflare's edge; ride out 403 blips on out-of-band verification calls.
41 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
42
43 const getTargetEnvironment = (accountId: string, targetEnvironmentId: string) =>
44 vulnScanner.getTargetEnvironment({ accountId, targetEnvironmentId }).pipe(
45 Effect.retry({
46 while: (e) => e._tag === "Forbidden",
47 schedule: forbiddenRetrySchedule,
48 times: 8,
49 }),
50 );
51
52 // Typed "is it gone?" probe — `TargetEnvironmentNotFound` (code 11001) maps
53 // to undefined; anything else propagates.
54 const findTargetEnvironment = (accountId: string, targetEnvironmentId: string) =>
55 getTargetEnvironment(accountId, targetEnvironmentId).pipe(
56 Effect.catchTag("TargetEnvironmentNotFound", () => Effect.succeed(undefined)),
57 );
58
59 test.provider(
60 "create, verify, and destroy a target environment",
61 (stack) =>
62 Effect.gen(function* () {
63 const zoneId = yield* resolveZoneId;
64 const accountId = yield* accountIdOf;
65
66 yield* stack.destroy();
67
68 const env = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* Cloudflare.VulnerabilityScanner.VulnScannerTargetEnvironment("DefaultEnv", {
71 name: NAME_DEFAULT,
72 zoneId,
73 description: "alchemy test target environment",
74 }).pipe(adopt(true));
75 }),
76 );
77
78 expect(env.targetEnvironmentId).toBeDefined();
79 expect(env.accountId).toEqual(accountId);
80 expect(env.name).toEqual(NAME_DEFAULT);
81 expect(env.zoneId).toEqual(zoneId);
82 expect(env.description).toEqual("alchemy test target environment");
83
84 // Out-of-band verification against the live API.
85 const live = yield* getTargetEnvironment(accountId, env.targetEnvironmentId);
86 expect(live.id).toEqual(env.targetEnvironmentId);
87 expect(live.name).toEqual(NAME_DEFAULT);
88 expect(live.target.zoneTag).toEqual(zoneId);
89 expect(live.description).toEqual("alchemy test target environment");
90
91 yield* stack.destroy();
92
93 const gone = yield* findTargetEnvironment(accountId, env.targetEnvironmentId);
94 expect(gone).toBeUndefined();
95 }).pipe(logLevel),
96 {
97 tags: [
98 "provider:cloudflare",
99 "provider:cloudflare:vulnerabilityscanner",
100 "provider:cloudflare:zone",
101 "live",
102 ],
103 },
104 );
105
106 test.provider(
107 "updating name and description patches in place; clearing description",
108 (stack) =>
109 Effect.gen(function* () {
110 const zoneId = yield* resolveZoneId;
111 const accountId = yield* accountIdOf;
112
113 yield* stack.destroy();
114
115 const initial = yield* stack.deploy(
116 Effect.gen(function* () {
117 return yield* Cloudflare.VulnerabilityScanner.VulnScannerTargetEnvironment("UpdateEnv", {
118 name: NAME_UPDATE,
119 zoneId,
120 description: "v1",
121 }).pipe(adopt(true));
122 }),
123 );
124
125 expect(initial.description).toEqual("v1");
126
127 const updated = yield* stack.deploy(
128 Effect.gen(function* () {
129 return yield* Cloudflare.VulnerabilityScanner.VulnScannerTargetEnvironment("UpdateEnv", {
130 name: `${NAME_UPDATE}-renamed`,
131 zoneId,
132 description: "v2",
133 }).pipe(adopt(true));
134 }),
135 );
136
137 // Same environment updated in place — not a replacement.
138 expect(updated.targetEnvironmentId).toEqual(initial.targetEnvironmentId);
139 expect(updated.name).toEqual(`${NAME_UPDATE}-renamed`);
140 expect(updated.description).toEqual("v2");
141
142 const live = yield* getTargetEnvironment(accountId, updated.targetEnvironmentId);
143 expect(live.name).toEqual(`${NAME_UPDATE}-renamed`);
144 expect(live.description).toEqual("v2");
145
146 // Omitting the description clears it (PUT with explicit null).
147 const cleared = yield* stack.deploy(
148 Effect.gen(function* () {
149 return yield* Cloudflare.VulnerabilityScanner.VulnScannerTargetEnvironment("UpdateEnv", {
150 name: `${NAME_UPDATE}-renamed`,
151 zoneId,
152 }).pipe(adopt(true));
153 }),
154 );
155
156 expect(cleared.targetEnvironmentId).toEqual(initial.targetEnvironmentId);
157 expect(cleared.description).toBeUndefined();
158
159 const liveCleared = yield* getTargetEnvironment(accountId, cleared.targetEnvironmentId);
160 expect(liveCleared.description ?? undefined).toBeUndefined();
161
162 yield* stack.destroy();
163
164 const gone = yield* findTargetEnvironment(accountId, initial.targetEnvironmentId);
165 expect(gone).toBeUndefined();
166 }).pipe(logLevel),
167 {
168 tags: [
169 "provider:cloudflare",
170 "provider:cloudflare:vulnerabilityscanner",
171 "provider:cloudflare:zone",
172 "live",
173 ],
174 },
175 );
176
177 // Canonical `list()` test (account collection): deploy a target environment,
178 // then enumerate the account-scoped collection and assert the deployed item
179 // appears in the exhaustively-paginated result.
180 test.provider(
181 "list enumerates the deployed target environment",
182 (stack) =>
183 Effect.gen(function* () {
184 const zoneId = yield* resolveZoneId;
185
186 yield* stack.destroy();
187
188 const env = yield* stack.deploy(
189 Effect.gen(function* () {
190 return yield* Cloudflare.VulnerabilityScanner.VulnScannerTargetEnvironment("ListEnv", {
191 name: NAME_LIST,
192 zoneId,
193 }).pipe(adopt(true));
194 }),
195 );
196
197 const provider = yield* Provider.findProvider(
198 Cloudflare.VulnerabilityScanner.VulnScannerTargetEnvironment,
199 );
200 const all = yield* provider.list();
201
202 expect(all.some((e) => e.targetEnvironmentId === env.targetEnvironmentId)).toBe(true);
203 const found = all.find((e) => e.targetEnvironmentId === env.targetEnvironmentId);
204 expect(found?.name).toEqual(NAME_LIST);
205 expect(found?.zoneId).toEqual(zoneId);
206
207 yield* stack.destroy();
208 }).pipe(logLevel),
209 {
210 tags: [
211 "provider:cloudflare",
212 "provider:cloudflare:vulnerabilityscanner",
213 "provider:cloudflare:zone",
214 "live",
215 ],
216 },
217 );