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 getNetwork = (accountId: string, networkId: string) => |
| 21 | zeroTrust.getDeviceNetwork({ accountId, networkId }).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, networkId: string) => |
| 30 | getNetwork(accountId, networkId).pipe( |
| 31 | Effect.flatMap((n) => |
| 32 | // The managed-networks GET returns a nullable result rather than a |
| 33 | // 404 once the network is deleted — treat both shapes as gone. |
| 34 | n.networkId == null ? Effect.void : Effect.fail({ _tag: "NetworkNotDeleted" } as const), |
| 35 | ), |
| 36 | Effect.catchTag("DeviceNetworkNotFound", () => Effect.void), |
| 37 | Effect.retry({ |
| 38 | while: (e) => e._tag === "NetworkNotDeleted", |
| 39 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | const SHA_A = "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c" as const; |
| 44 | const SHA_B = "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730" as const; |
| 45 | |
| 46 | test.provider( |
| 47 | "create, update in place, and delete a managed network", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 51 | |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | const network = yield* stack.deploy( |
| 55 | Cloudflare.Devices.DeviceManagedNetwork("Office", { |
| 56 | name: testName("alchemy-test-managed-network"), |
| 57 | config: { tlsSockaddr: "192.0.2.1:443", sha256: SHA_A }, |
| 58 | }), |
| 59 | ); |
| 60 | |
| 61 | expect(network.networkId).toBeTruthy(); |
| 62 | expect(network.accountId).toEqual(accountId); |
| 63 | expect(network.name).toEqual(testName("alchemy-test-managed-network")); |
| 64 | expect(network.type).toEqual("tls"); |
| 65 | expect(network.config.tlsSockaddr).toEqual("192.0.2.1:443"); |
| 66 | expect(network.config.sha256).toEqual(SHA_A); |
| 67 | |
| 68 | // Out-of-band verify against the live API. |
| 69 | const live = yield* getNetwork(accountId, network.networkId); |
| 70 | expect(live.name).toEqual(testName("alchemy-test-managed-network")); |
| 71 | expect(live.config?.tlsSockaddr).toEqual("192.0.2.1:443"); |
| 72 | |
| 73 | // Update config in place — same networkId. |
| 74 | const updated = yield* stack.deploy( |
| 75 | Cloudflare.Devices.DeviceManagedNetwork("Office", { |
| 76 | name: testName("alchemy-test-managed-network"), |
| 77 | config: { tlsSockaddr: "192.0.2.2:443", sha256: SHA_B }, |
| 78 | }), |
| 79 | ); |
| 80 | expect(updated.networkId).toEqual(network.networkId); |
| 81 | expect(updated.config.tlsSockaddr).toEqual("192.0.2.2:443"); |
| 82 | expect(updated.config.sha256).toEqual(SHA_B); |
| 83 | |
| 84 | const live2 = yield* getNetwork(accountId, network.networkId); |
| 85 | expect(live2.config?.tlsSockaddr).toEqual("192.0.2.2:443"); |
| 86 | |
| 87 | // Redeploying identical props is a no-op (same network). |
| 88 | const noop = yield* stack.deploy( |
| 89 | Cloudflare.Devices.DeviceManagedNetwork("Office", { |
| 90 | name: testName("alchemy-test-managed-network"), |
| 91 | config: { tlsSockaddr: "192.0.2.2:443", sha256: SHA_B }, |
| 92 | }), |
| 93 | ); |
| 94 | expect(noop.networkId).toEqual(network.networkId); |
| 95 | |
| 96 | yield* stack.destroy(); |
| 97 | yield* expectGone(accountId, network.networkId); |
| 98 | }).pipe(logLevel), |
| 99 | { tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"] }, |
| 100 | ); |
| 101 | |
| 102 | test.provider( |
| 103 | "list enumerates the deployed managed network", |
| 104 | (stack) => |
| 105 | Effect.gen(function* () { |
| 106 | yield* stack.destroy(); |
| 107 | |
| 108 | const deployed = yield* stack.deploy( |
| 109 | Cloudflare.Devices.DeviceManagedNetwork("ListResource", { |
| 110 | config: { tlsSockaddr: "192.0.2.3:443", sha256: SHA_A }, |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | const provider = yield* Provider.findProvider(Cloudflare.Devices.DeviceManagedNetwork); |
| 115 | const all = yield* provider.list(); |
| 116 | |
| 117 | expect(all.some((n) => n.networkId === deployed.networkId)).toBe(true); |
| 118 | |
| 119 | yield* stack.destroy(); |
| 120 | yield* expectGone(deployed.accountId, deployed.networkId); |
| 121 | }).pipe(logLevel), |
| 122 | { tags: ["provider:cloudflare", "provider:cloudflare:devices", "live"] }, |
| 123 | ); |