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

main
ts 182 lines 5.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 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 { 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 // DEX synthetic tests require the Digital Experience Monitoring
16 // entitlement. On the standard testing account
17 // `POST /accounts/{id}/dex/devices/dex_tests` fails with HTTP 403 code
18 // 11007 "dex.api.entitlements.missing" — surfaced as the typed
19 // `Forbidden` error. The lifecycle test is gated behind an entitled
20 // account supplied via env; the probe test always runs and pins the
21 // typed tag.
22 const entitled = !!process.env.CLOUDFLARE_TEST_DEX;
23
24 const TEST_NAME = testName("alchemy-test-dex");
25
26 test.provider.skipIf(entitled)(
27 "unentitled accounts surface the typed Forbidden entitlement error",
28 (stack) =>
29 Effect.gen(function* () {
30 const { accountId } = yield* yield* CloudflareEnvironment;
31
32 yield* stack.destroy();
33
34 const error = yield* zeroTrust
35 .createDeviceDexTest({
36 accountId,
37 name: "alchemy-dex-entitlement-probe",
38 enabled: true,
39 interval: "0h30m0s",
40 data: { host: "https://example.com", kind: "http", method: "GET" },
41 })
42 .pipe(Effect.flip);
43 expect(error._tag).toEqual("Forbidden");
44 if (error._tag === "Forbidden") {
45 expect(error.message).toContain("dex.api.entitlements.missing");
46 }
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 {
51 tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"],
52 timeout: 90_000,
53 },
54 );
55
56 test.provider.skipIf(!entitled)(
57 "create, update in place, and destroy a DEX test",
58 (stack) =>
59 Effect.gen(function* () {
60 const { accountId } = yield* yield* CloudflareEnvironment;
61
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* Cloudflare.Devices.DeviceDexTest("AppHealth", {
67 name: TEST_NAME,
68 data: {
69 host: "https://app.example.com/health",
70 kind: "http",
71 method: "GET",
72 },
73 interval: "0h30m0s",
74 description: "v1",
75 });
76 }),
77 );
78
79 expect(created.testId).toBeDefined();
80 expect(created.accountId).toEqual(accountId);
81 expect(created.name).toEqual(TEST_NAME);
82 expect(created.interval).toEqual("0h30m0s");
83 expect(created.enabled).toEqual(true);
84
85 const live = yield* zeroTrust.getDeviceDexTest({
86 accountId,
87 dexTestId: created.testId,
88 });
89 expect(live.name).toEqual(TEST_NAME);
90
91 // Interval + description update converges in place — same test id.
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* Cloudflare.Devices.DeviceDexTest("AppHealth", {
95 name: TEST_NAME,
96 data: {
97 host: "https://app.example.com/health",
98 kind: "http",
99 method: "GET",
100 },
101 interval: "1h0m0s",
102 description: "v2",
103 enabled: false,
104 });
105 }),
106 );
107 expect(updated.testId).toEqual(created.testId);
108 expect(updated.interval).toEqual("1h0m0s");
109 expect(updated.description).toEqual("v2");
110 expect(updated.enabled).toEqual(false);
111
112 yield* stack.destroy();
113
114 const gone = yield* zeroTrust
115 .getDeviceDexTest({ accountId, dexTestId: created.testId })
116 .pipe(Effect.flip);
117 expect(gone._tag).toEqual("DexTestNotFound");
118 }).pipe(logLevel),
119 {
120 tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"],
121 timeout: 90_000,
122 },
123 );
124
125 // Canonical `list()` test (account-scoped collection). The lifecycle requires
126 // the DEX entitlement, so the deploy + presence assertion is gated behind an
127 // entitled account. On unentitled accounts the provider's `list()` catches the
128 // typed `Forbidden` (dex.api.entitlements.missing) and returns `[]`; the ungated
129 // half of this test asserts that well-typed empty result so a clean skip run
130 // still exercises `list()`.
131 test.provider.skipIf(!entitled)(
132 "list enumerates the deployed DEX test",
133 (stack) =>
134 Effect.gen(function* () {
135 yield* stack.destroy();
136
137 const deployed = yield* stack.deploy(
138 Effect.gen(function* () {
139 return yield* Cloudflare.Devices.DeviceDexTest("ListResource", {
140 name: TEST_NAME,
141 data: {
142 host: "https://app.example.com/health",
143 kind: "http",
144 method: "GET",
145 },
146 interval: "0h30m0s",
147 description: "list",
148 });
149 }),
150 );
151
152 const provider = yield* Provider.findProvider(Cloudflare.Devices.DeviceDexTest);
153 const all = yield* provider.list();
154
155 expect(all.some((t) => t.testId === deployed.testId)).toBe(true);
156
157 yield* stack.destroy();
158 }).pipe(logLevel),
159 {
160 tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"],
161 timeout: 90_000,
162 },
163 );
164
165 test.provider.skipIf(entitled)(
166 "list returns a well-typed empty array on unentitled accounts",
167 (stack) =>
168 Effect.gen(function* () {
169 yield* stack.destroy();
170
171 const provider = yield* Provider.findProvider(Cloudflare.Devices.DeviceDexTest);
172 const all = yield* provider.list();
173
174 expect(Array.isArray(all)).toBe(true);
175
176 yield* stack.destroy();
177 }).pipe(logLevel),
178 {
179 tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"],
180 timeout: 90_000,
181 },
182 );