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

main
ts 176 lines 6.91 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 cloudforceOne from "@distilled.cloud/cloudflare/cloudforce-one";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Cloudforce One attack-surface scans are entitlement-gated: on the standard
16 // testing account every `/cloudforce-one/scans/config` call fails with the
17 // typed `Unauthorized` error ("needs cfone.port_scan entitlement"). The full
18 // lifecycle test below is gated behind an explicit opt-in env flag for
19 // entitled accounts; the probe test always runs and pins the typed tag.
20 const entitled = !!process.env.CLOUDFLARE_TEST_CLOUDFORCE_ONE;
21
22 // Probe once per suite: either the account can list scan configs (entitled)
23 // or the call fails with the typed `Unauthorized` tag.
24 const probeEntitlement = (accountId: string) =>
25 cloudforceOne.listScanConfigs({ accountId }).pipe(
26 Effect.as(true),
27 Effect.catchTag("Unauthorized", () => Effect.succeed(false)),
28 );
29
30 // Unentitlement probe — pins the typed Unauthorized rejection ("needs cfone.port_scan
31 // entitlement") and skips on entitled accounts, where the API would accept the calls.
32 test.provider.skipIf(entitled)(
33 "unentitled accounts surface the typed Unauthorized error",
34 (stack) =>
35 Effect.gen(function* () {
36 const { accountId } = yield* yield* CloudflareEnvironment;
37
38 yield* stack.destroy();
39
40 const canList = yield* probeEntitlement(accountId);
41 if (canList) {
42 // Entitled account — nothing to assert here; the lifecycle test
43 // covers the real behavior.
44 yield* Effect.logInfo("account is cloudforce-one entitled; probe test is a no-op");
45 return;
46 }
47
48 // The typed tag — not UnknownCloudflareError, not a status check.
49 const listError = yield* cloudforceOne.listScanConfigs({ accountId }).pipe(Effect.flip);
50 expect(listError._tag).toEqual("Unauthorized");
51 expect(listError.message).toContain("cfone.port_scan");
52
53 const createError = yield* cloudforceOne
54 .createScanConfig({ accountId, ips: ["1.1.1.1/32"], frequency: 0 })
55 .pipe(Effect.flip);
56 expect(createError._tag).toEqual("Unauthorized");
57
58 yield* stack.destroy();
59 }).pipe(logLevel),
60 { tags: ["provider:cloudflare", "provider:cloudflare:cloudforceone", "live"] },
61 );
62
63 // Poll the account list until the config id disappears — there is no
64 // get-by-id endpoint, so "gone" means absent from the list.
65 const expectGone = (accountId: string, configId: string) =>
66 cloudforceOne.listScanConfigs({ accountId }).pipe(
67 Effect.flatMap((page) =>
68 page.result.some((c) => c.id === configId)
69 ? Effect.fail({ _tag: "ScanConfigNotDeleted" } as const)
70 : Effect.void,
71 ),
72 Effect.retry({
73 while: (e) => e._tag === "ScanConfigNotDeleted",
74 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
75 }),
76 );
77
78 // Requires the Cloudforce One attack-surface scan (cfone.port_scan) entitlement —
79 // unentitled accounts fail with the typed Unauthorized. Unlock with CLOUDFLARE_TEST_CLOUDFORCE_ONE=1.
80 test.provider.skipIf(!entitled)(
81 "create a scan config, update ports in place, delete",
82 (stack) =>
83 Effect.gen(function* () {
84 const { accountId } = yield* yield* CloudflareEnvironment;
85
86 yield* stack.destroy();
87
88 // Create — one-off scan of a single documentation address.
89 const created = yield* stack.deploy(
90 Cloudflare.CloudforceOne.ScanConfig("TestScanConfig", {
91 ips: ["1.1.1.1/32"],
92 frequency: 0,
93 }),
94 );
95 expect(created.configId).toBeTruthy();
96 expect(created.accountId).toEqual(accountId);
97 expect(created.ips).toEqual(["1.1.1.1/32"]);
98 expect(created.frequency).toEqual(0);
99
100 // Out-of-band verification — the config shows up in the account list.
101 const listed = yield* cloudforceOne.listScanConfigs({ accountId });
102 expect(listed.result.map((c) => c.id)).toContain(created.configId);
103
104 // In-place update — change the port list; the config id is stable.
105 const updated = yield* stack.deploy(
106 Cloudflare.CloudforceOne.ScanConfig("TestScanConfig", {
107 ips: ["1.1.1.1/32"],
108 frequency: 0,
109 ports: ["1-80", "443"],
110 }),
111 );
112 expect(updated.configId).toEqual(created.configId);
113 expect([...updated.ports].sort()).toEqual(["1-80", "443"]);
114
115 // No-op redeploy — same props, same config id.
116 const noop = yield* stack.deploy(
117 Cloudflare.CloudforceOne.ScanConfig("TestScanConfig", {
118 ips: ["1.1.1.1/32"],
119 frequency: 0,
120 ports: ["1-80", "443"],
121 }),
122 );
123 expect(noop.configId).toEqual(created.configId);
124
125 yield* stack.destroy();
126
127 yield* expectGone(accountId, created.configId);
128
129 // Destroy again — delete is idempotent (typed ScanConfigNotFound).
130 yield* stack.destroy();
131 }).pipe(logLevel),
132 { tags: ["provider:cloudflare", "provider:cloudflare:cloudforceone", "live"] },
133 );
134
135 // Read-only list assertion — always runs. On unentitled accounts the provider's
136 // list() catches the typed `Unauthorized` (needs cfone.port_scan) and returns
137 // [], so this stays green everywhere.
138 test.provider(
139 "list returns an array of scan configs",
140 (stack) =>
141 Effect.gen(function* () {
142 yield* stack.destroy();
143
144 const provider = yield* Provider.findProvider(Cloudflare.CloudforceOne.ScanConfig);
145 const all = yield* provider.list();
146 expect(Array.isArray(all)).toBe(true);
147
148 yield* stack.destroy();
149 }).pipe(logLevel),
150 { tags: ["provider:cloudflare", "provider:cloudflare:cloudforceone", "live"] },
151 );
152
153 // Full enumeration — gated on the cfone.port_scan entitlement. Unlock with
154 // CLOUDFLARE_TEST_CLOUDFORCE_ONE=1. Unentitled accounts surface the typed
155 // Unauthorized on the deploy (createScanConfig), so the mutation is gated.
156 test.provider.skipIf(!entitled)(
157 "list enumerates the deployed scan config",
158 (stack) =>
159 Effect.gen(function* () {
160 yield* stack.destroy();
161
162 const deployed = yield* stack.deploy(
163 Cloudflare.CloudforceOne.ScanConfig("ListScanConfig", {
164 ips: ["1.1.1.1/32"],
165 frequency: 0,
166 }),
167 );
168
169 const provider = yield* Provider.findProvider(Cloudflare.CloudforceOne.ScanConfig);
170 const all = yield* provider.list();
171 expect(all.some((c) => c.configId === deployed.configId)).toBe(true);
172
173 yield* stack.destroy();
174 }).pipe(logLevel),
175 { tags: ["provider:cloudflare", "provider:cloudflare:cloudforceone", "live"] },
176 );