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

main
ts 103 lines 4.2 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 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 // The scoped API token the test harness mints propagates eventually-
16 // consistently across Cloudflare's edge — ride out 403 blips
17 // (`Forbidden`, declared in the distilled error union) on the test's
18 // own out-of-band verification calls.
19 const getSettings = (accountId: string) =>
20 zeroTrust.getDeviceSetting({ accountId }).pipe(
21 Effect.retry({
22 while: (e) => e._tag === "Forbidden",
23 schedule: Schedule.exponential("500 millis"),
24 times: 8,
25 }),
26 );
27
28 test.provider(
29 "patches disableForTime and restores the original value on destroy",
30 (stack) =>
31 Effect.gen(function* () {
32 const { accountId } = yield* yield* CloudflareEnvironment;
33
34 yield* stack.destroy();
35
36 // Capture the live singleton before Alchemy manages it; the
37 // capture-and-restore contract must put it back on destroy.
38 const before = yield* getSettings(accountId);
39 const beforeDisable = before.disableForTime ?? null;
40
41 // Step 1 — set the WARP override-code time limit. `disableForTime`
42 // is the lowest-risk field on the singleton: it only affects how
43 // long manually-issued override codes last.
44 const a = yield* stack.deploy(
45 Cloudflare.Devices.DeviceSettings("Devices", { disableForTime: 3600 }),
46 );
47 expect(a.accountId).toEqual(accountId);
48 expect(a.disableForTime).toEqual(3600);
49 expect(a.initialSettings.disableForTime ?? null).toEqual(beforeDisable);
50
51 const live1 = yield* getSettings(accountId);
52 expect(live1.disableForTime).toEqual(3600);
53
54 // Step 2 — update in place; the initial snapshot must be preserved,
55 // not re-captured from the now-managed state.
56 const b = yield* stack.deploy(
57 Cloudflare.Devices.DeviceSettings("Devices", { disableForTime: 7200 }),
58 );
59 expect(b.disableForTime).toEqual(7200);
60 expect(b.initialSettings.disableForTime ?? null).toEqual(beforeDisable);
61
62 const live2 = yield* getSettings(accountId);
63 expect(live2.disableForTime).toEqual(7200);
64
65 // Step 3 — a no-op redeploy must not lose the snapshot either.
66 const c = yield* stack.deploy(
67 Cloudflare.Devices.DeviceSettings("Devices", { disableForTime: 7200 }),
68 );
69 expect(c.disableForTime).toEqual(7200);
70 expect(c.initialSettings.disableForTime ?? null).toEqual(beforeDisable);
71
72 // Destroy — the singleton must be restored to its pre-managed state.
73 yield* stack.destroy();
74 const after = yield* getSettings(accountId);
75 expect(after.disableForTime ?? null).toEqual(beforeDisable);
76 }).pipe(logLevel),
77 { tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"] },
78 );
79
80 // Canonical `list()` test (account-scoped singleton): there is exactly one
81 // device-settings object per account and no enumeration API, so `list()`
82 // reads the single singleton and returns a one-element Attributes array.
83 test.provider(
84 "list returns the account's device settings singleton",
85 (stack) =>
86 Effect.gen(function* () {
87 const { accountId } = yield* yield* CloudflareEnvironment;
88
89 yield* stack.destroy();
90
91 const provider = yield* Provider.findProvider(Cloudflare.Devices.DeviceSettings);
92 const all = yield* provider.list();
93
94 // Exactly one element — the account-wide singleton — well-typed as
95 // DeviceSettings["Attributes"].
96 expect(all.length).toEqual(1);
97 expect(all[0].accountId).toEqual(accountId);
98 expect(all[0].initialSettings).toBeDefined();
99
100 yield* stack.destroy();
101 }).pipe(logLevel),
102 { tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"] },
103 );