The best way to control Claude Code, Codex and more from any computer or phone
| 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 | import { testName } from "../../names.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | // The scoped API token the test harness mints propagates eventually- |
| 17 | // consistently across Cloudflare's edge — ride out 403 blips |
| 18 | // (`Forbidden`, declared in the distilled error union) on the test's |
| 19 | // own out-of-band verification calls. |
| 20 | const getProfile = (accountId: string, policyId: string) => |
| 21 | zeroTrust.getDevicePolicyCustom({ accountId, policyId }).pipe( |
| 22 | Effect.retry({ |
| 23 | while: (e) => e._tag === "Forbidden", |
| 24 | schedule: Schedule.exponential("500 millis"), |
| 25 | times: 8, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | const expectGone = (accountId: string, policyId: string) => |
| 30 | getProfile(accountId, policyId).pipe( |
| 31 | Effect.flatMap(() => Effect.fail({ _tag: "ProfileNotDeleted" } as const)), |
| 32 | // A missing profile surfaces as `DevicePolicyNotFound` (Cloudflare |
| 33 | // error code 2052) — that's the success condition here. |
| 34 | Effect.catchTag("DevicePolicyNotFound", () => Effect.void), |
| 35 | Effect.retry({ |
| 36 | while: (e) => e._tag === "ProfileNotDeleted", |
| 37 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | test.provider( |
| 42 | "create, update in place, and delete a custom device profile", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | |
| 49 | const profile = yield* stack.deploy( |
| 50 | Cloudflare.Devices.DeviceCustomProfile("Contractors", { |
| 51 | name: testName("alchemy-test-custom-profile"), |
| 52 | match: 'identity.email == "contractor@alchemy-test-2.us"', |
| 53 | precedence: 12010, |
| 54 | description: "Alchemy test profile", |
| 55 | switchLocked: true, |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | expect(profile.policyId).toBeTruthy(); |
| 60 | expect(profile.accountId).toEqual(accountId); |
| 61 | expect(profile.name).toEqual(testName("alchemy-test-custom-profile")); |
| 62 | expect(profile.match).toEqual('identity.email == "contractor@alchemy-test-2.us"'); |
| 63 | expect(profile.description).toEqual("Alchemy test profile"); |
| 64 | expect(profile.switchLocked).toEqual(true); |
| 65 | expect(profile.default).toEqual(false); |
| 66 | |
| 67 | // Out-of-band verify against the live API. |
| 68 | const live = yield* getProfile(accountId, profile.policyId); |
| 69 | expect(live?.name).toEqual(testName("alchemy-test-custom-profile")); |
| 70 | expect(live?.switchLocked).toEqual(true); |
| 71 | |
| 72 | // Update mutable props (body + split-tunnel exclude list) in place — |
| 73 | // same policyId. |
| 74 | const updated = yield* stack.deploy( |
| 75 | Cloudflare.Devices.DeviceCustomProfile("Contractors", { |
| 76 | name: testName("alchemy-test-custom-profile"), |
| 77 | match: 'identity.email == "contractor@alchemy-test-2.us"', |
| 78 | precedence: 12010, |
| 79 | description: "Alchemy test profile v2", |
| 80 | switchLocked: false, |
| 81 | exclude: [{ address: "10.99.0.0/16", description: "test range" }], |
| 82 | }), |
| 83 | ); |
| 84 | expect(updated.policyId).toEqual(profile.policyId); |
| 85 | expect(updated.description).toEqual("Alchemy test profile v2"); |
| 86 | expect(updated.switchLocked).toEqual(false); |
| 87 | expect(updated.exclude.some((e) => e.address === "10.99.0.0/16")).toEqual(true); |
| 88 | |
| 89 | // Out-of-band verify the exclude list endpoint. |
| 90 | const excludes = yield* zeroTrust |
| 91 | .getDevicePolicyCustomExclude({ accountId, policyId: profile.policyId }) |
| 92 | .pipe( |
| 93 | Effect.retry({ |
| 94 | while: (e) => e._tag === "Forbidden", |
| 95 | schedule: Schedule.exponential("500 millis"), |
| 96 | times: 8, |
| 97 | }), |
| 98 | ); |
| 99 | expect( |
| 100 | (excludes.result ?? []).some((e) => "address" in e && e.address === "10.99.0.0/16"), |
| 101 | ).toEqual(true); |
| 102 | |
| 103 | yield* stack.destroy(); |
| 104 | yield* expectGone(accountId, profile.policyId); |
| 105 | }).pipe(logLevel), |
| 106 | { tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"] }, |
| 107 | ); |
| 108 | |
| 109 | test.provider( |
| 110 | "list enumerates the deployed custom device profile", |
| 111 | (stack) => |
| 112 | Effect.gen(function* () { |
| 113 | yield* stack.destroy(); |
| 114 | |
| 115 | const deployed = yield* stack.deploy( |
| 116 | Cloudflare.Devices.DeviceCustomProfile("ListProfile", { |
| 117 | name: testName("alchemy-test-custom-profile-list"), |
| 118 | match: 'identity.email == "list@alchemy-test-2.us"', |
| 119 | precedence: 12011, |
| 120 | description: "Alchemy list() test profile", |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | const provider = yield* Provider.findProvider(Cloudflare.Devices.DeviceCustomProfile); |
| 125 | const all = yield* provider.list(); |
| 126 | |
| 127 | // Exhaustively paginated account collection must contain the profile we |
| 128 | // just deployed, hydrated into the exact `read` Attributes shape. |
| 129 | const found = all.find((p) => p.policyId === deployed.policyId); |
| 130 | expect(found).toBeDefined(); |
| 131 | expect(found?.name).toEqual(testName("alchemy-test-custom-profile-list")); |
| 132 | expect(found?.match).toEqual('identity.email == "list@alchemy-test-2.us"'); |
| 133 | expect(found?.accountId).toEqual(deployed.accountId); |
| 134 | expect(found?.default).toEqual(false); |
| 135 | |
| 136 | yield* stack.destroy(); |
| 137 | }).pipe(logLevel), |
| 138 | { tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"] }, |
| 139 | ); |