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

main
ts 123 lines 5.12 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { describe, 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 const { test } = Test.make({ providers: Cloudflare.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 // The mutating cases toggle the account's WARP default profile and restore
14 // the captured baseline. The standing test account has Zero Trust (the
15 // ungated `list` case below already requires it), so they run by default;
16 // set CLOUDFLARE_TEST_DEVICES=0 to skip them on an account you must not touch.
17 const ENABLED = process.env.CLOUDFLARE_TEST_DEVICES !== "0";
18 const skip = !ENABLED;
19
20 // Both cases mutate the same account-level WARP default device profile
21 // singleton; run them serially so they don't corrupt each other's captured
22 // baseline under the global concurrent test config.
23 describe.sequential(
24 "DefaultProfile",
25 { tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"] },
26 () => {
27 test.provider.skipIf(skip)(
28 "reads the existing default device profile without mutating it",
29 (stack) =>
30 Effect.gen(function* () {
31 const { accountId } = yield* yield* CloudflareEnvironment;
32
33 const before = yield* zeroTrust.getDevicePolicyDefault({ accountId });
34
35 const profile = yield* stack.deploy(
36 Effect.gen(function* () {
37 return yield* Cloudflare.Devices.DeviceDefaultProfile("Default", {});
38 }),
39 );
40
41 expect(profile.accountId).toEqual(accountId);
42 expect(["include", "exclude"]).toContain(profile.mode);
43
44 // Nothing supplied => no PATCH, no PUTs => observed state matches before.
45 const after = yield* zeroTrust.getDevicePolicyDefault({ accountId });
46 expect(after.captivePortal ?? null).toEqual(before.captivePortal ?? null);
47 expect(after.allowedToLeave ?? null).toEqual(before.allowedToLeave ?? null);
48
49 // Singleton: delete is a no-op, so destroy must NOT remove the profile.
50 yield* stack.destroy();
51 const stillThere = yield* zeroTrust.getDevicePolicyDefault({ accountId });
52 expect(stillThere).toBeDefined();
53 }).pipe(logLevel),
54 );
55
56 test.provider.skipIf(skip)("toggles captivePortal and restores the original value", (stack) =>
57 Effect.gen(function* () {
58 const { accountId } = yield* yield* CloudflareEnvironment;
59
60 const original = yield* zeroTrust.getDevicePolicyDefault({ accountId });
61 const originalCaptive = original.captivePortal ?? 180;
62
63 // Step 1 — set to 180 seconds.
64 const a = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* Cloudflare.Devices.DeviceDefaultProfile("Default", {
67 captivePortal: 180,
68 });
69 }),
70 );
71 expect(a.captivePortal).toEqual(180);
72 const live1 = yield* zeroTrust.getDevicePolicyDefault({ accountId });
73 expect(live1.captivePortal).toEqual(180);
74
75 // Step 2 — flip to 360.
76 const b = yield* stack.deploy(
77 Effect.gen(function* () {
78 return yield* Cloudflare.Devices.DeviceDefaultProfile("Default", {
79 captivePortal: 360,
80 });
81 }),
82 );
83 expect(b.captivePortal).toEqual(360);
84 const live2 = yield* zeroTrust.getDevicePolicyDefault({ accountId });
85 expect(live2.captivePortal).toEqual(360);
86
87 // Restore via a final deploy so the account is not left mutated.
88 yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* Cloudflare.Devices.DeviceDefaultProfile("Default", {
91 captivePortal: originalCaptive,
92 });
93 }),
94 );
95
96 yield* stack.destroy();
97 }).pipe(logLevel),
98 );
99
100 // Canonical `list()` test (account-scoped singleton): there is exactly one
101 // default device profile per account and no enumeration API, so `list()`
102 // reads the single profile and returns it as a one-element array — exactly
103 // mirroring `read`. Read-only, so it is NOT gated behind the mutation env
104 // var; it only requires the account to have Zero Trust / WARP entitlement.
105 test.provider("list returns the singleton default device profile", (stack) =>
106 Effect.gen(function* () {
107 const { accountId } = yield* yield* CloudflareEnvironment;
108
109 yield* stack.destroy();
110
111 const provider = yield* Provider.findProvider(Cloudflare.Devices.DeviceDefaultProfile);
112 const all = yield* provider.list();
113
114 // Account singleton: exactly one element, well-typed Attributes.
115 expect(all.length).toEqual(1);
116 expect(all[0].accountId).toEqual(accountId);
117 expect(["include", "exclude"]).toContain(all[0].mode);
118
119 yield* stack.destroy();
120 }).pipe(logLevel),
121 );
122 },
123 );