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 { adopt } from "alchemy/AdoptPolicy"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { testName } from "../../names.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | // The scoped API token the test harness mints propagates eventually- |
| 18 | // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`, |
| 19 | // declared in the distilled error union) on out-of-band verification calls. |
| 20 | const getVnet = (accountId: string, virtualNetworkId: string) => |
| 21 | zeroTrust.getNetworkVirtualNetwork({ accountId, virtualNetworkId }).pipe( |
| 22 | Effect.retry({ |
| 23 | while: (e) => e._tag === "Forbidden", |
| 24 | schedule: Schedule.exponential("500 millis"), |
| 25 | times: 8, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | // A destroyed virtual network either 404s (`VirtualNetworkNotFound`, code |
| 30 | // 1046) or is soft-deleted with `deletedAt` set. |
| 31 | const expectGone = (accountId: string, virtualNetworkId: string) => |
| 32 | getVnet(accountId, virtualNetworkId).pipe( |
| 33 | Effect.flatMap((vnet) => |
| 34 | vnet.deletedAt ? Effect.void : Effect.fail({ _tag: "VnetNotDeleted" } as const), |
| 35 | ), |
| 36 | Effect.catchTag("VirtualNetworkNotFound", () => Effect.void), |
| 37 | Effect.retry({ |
| 38 | while: (e) => e._tag === "VnetNotDeleted", |
| 39 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | test.provider( |
| 44 | "create, verify, and destroy a virtual network", |
| 45 | (stack) => |
| 46 | Effect.gen(function* () { |
| 47 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 48 | |
| 49 | yield* stack.destroy(); |
| 50 | |
| 51 | const vnet = yield* stack.deploy( |
| 52 | Cloudflare.Tunnel.VirtualNetwork("BasicVnet", { |
| 53 | name: testName("alchemy-zt-vnet-basic"), |
| 54 | comment: "alchemy test vnet", |
| 55 | }).pipe(adopt(true)), |
| 56 | ); |
| 57 | |
| 58 | expect(vnet.virtualNetworkId).toBeTruthy(); |
| 59 | expect(vnet.accountId).toEqual(accountId); |
| 60 | expect(vnet.name).toEqual(testName("alchemy-zt-vnet-basic")); |
| 61 | expect(vnet.comment).toEqual("alchemy test vnet"); |
| 62 | expect(vnet.isDefaultNetwork).toBe(false); |
| 63 | |
| 64 | const live = yield* getVnet(accountId, vnet.virtualNetworkId); |
| 65 | expect(live.name).toEqual(testName("alchemy-zt-vnet-basic")); |
| 66 | expect(live.comment).toEqual("alchemy test vnet"); |
| 67 | |
| 68 | yield* stack.destroy(); |
| 69 | yield* expectGone(accountId, vnet.virtualNetworkId); |
| 70 | }).pipe(logLevel), |
| 71 | { tags: ["provider:cloudflare", "provider:cloudflare:tunnel", "live"] }, |
| 72 | ); |
| 73 | |
| 74 | test.provider( |
| 75 | "update name and comment in place (same id)", |
| 76 | (stack) => |
| 77 | Effect.gen(function* () { |
| 78 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | |
| 82 | const initial = yield* stack.deploy( |
| 83 | Cloudflare.Tunnel.VirtualNetwork("UpdateVnet", { |
| 84 | name: testName("alchemy-zt-vnet-update"), |
| 85 | comment: "v1", |
| 86 | }).pipe(adopt(true)), |
| 87 | ); |
| 88 | |
| 89 | const updated = yield* stack.deploy( |
| 90 | Cloudflare.Tunnel.VirtualNetwork("UpdateVnet", { |
| 91 | name: testName("alchemy-zt-vnet-update-v2"), |
| 92 | comment: "v2", |
| 93 | }).pipe(adopt(true)), |
| 94 | ); |
| 95 | |
| 96 | // Same vnet mutated in place — not a replacement. |
| 97 | expect(updated.virtualNetworkId).toEqual(initial.virtualNetworkId); |
| 98 | expect(updated.name).toEqual(testName("alchemy-zt-vnet-update-v2")); |
| 99 | expect(updated.comment).toEqual("v2"); |
| 100 | |
| 101 | const live = yield* getVnet(accountId, updated.virtualNetworkId); |
| 102 | expect(live.name).toEqual(testName("alchemy-zt-vnet-update-v2")); |
| 103 | expect(live.comment).toEqual("v2"); |
| 104 | |
| 105 | // Redeploying identical props is a no-op. |
| 106 | const noop = yield* stack.deploy( |
| 107 | Cloudflare.Tunnel.VirtualNetwork("UpdateVnet", { |
| 108 | name: testName("alchemy-zt-vnet-update-v2"), |
| 109 | comment: "v2", |
| 110 | }).pipe(adopt(true)), |
| 111 | ); |
| 112 | expect(noop.virtualNetworkId).toEqual(initial.virtualNetworkId); |
| 113 | |
| 114 | yield* stack.destroy(); |
| 115 | yield* expectGone(accountId, initial.virtualNetworkId); |
| 116 | }).pipe(logLevel), |
| 117 | { tags: ["provider:cloudflare", "provider:cloudflare:tunnel", "live"] }, |
| 118 | ); |
| 119 | |
| 120 | test.provider( |
| 121 | "recreates after out-of-band delete", |
| 122 | (stack) => |
| 123 | Effect.gen(function* () { |
| 124 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 125 | |
| 126 | yield* stack.destroy(); |
| 127 | |
| 128 | const vnet = yield* stack.deploy( |
| 129 | Cloudflare.Tunnel.VirtualNetwork("HealVnet", { |
| 130 | name: testName("alchemy-zt-vnet-heal"), |
| 131 | comment: "v1", |
| 132 | }).pipe(adopt(true)), |
| 133 | ); |
| 134 | |
| 135 | yield* zeroTrust |
| 136 | .deleteNetworkVirtualNetwork({ accountId, virtualNetworkId: vnet.virtualNetworkId }) |
| 137 | .pipe( |
| 138 | Effect.retry({ |
| 139 | while: (e) => e._tag === "Forbidden", |
| 140 | schedule: Schedule.exponential("500 millis"), |
| 141 | times: 8, |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | // Change a prop to force reconcile — it must observe the vnet as |
| 146 | // missing and recreate it instead of failing on a 404. |
| 147 | const healed = yield* stack.deploy( |
| 148 | Cloudflare.Tunnel.VirtualNetwork("HealVnet", { |
| 149 | name: testName("alchemy-zt-vnet-heal"), |
| 150 | comment: "v2", |
| 151 | }).pipe(adopt(true)), |
| 152 | ); |
| 153 | |
| 154 | expect(healed.virtualNetworkId).not.toEqual(vnet.virtualNetworkId); |
| 155 | expect(healed.comment).toEqual("v2"); |
| 156 | const live = yield* getVnet(accountId, healed.virtualNetworkId); |
| 157 | expect(live.comment).toEqual("v2"); |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | yield* expectGone(accountId, healed.virtualNetworkId); |
| 161 | }).pipe(logLevel), |
| 162 | { tags: ["provider:cloudflare", "provider:cloudflare:tunnel", "live"] }, |
| 163 | ); |
| 164 | |
| 165 | test.provider( |
| 166 | "list enumerates the deployed virtual network", |
| 167 | (stack) => |
| 168 | Effect.gen(function* () { |
| 169 | yield* stack.destroy(); |
| 170 | |
| 171 | const deployed = yield* stack.deploy( |
| 172 | Cloudflare.Tunnel.VirtualNetwork("ListVnet", { |
| 173 | name: testName("alchemy-zt-vnet-list"), |
| 174 | comment: "alchemy list test vnet", |
| 175 | }).pipe(adopt(true)), |
| 176 | ); |
| 177 | |
| 178 | const provider = yield* Provider.findProvider(Cloudflare.Tunnel.VirtualNetwork); |
| 179 | const all = yield* provider.list(); |
| 180 | |
| 181 | expect(all.some((v) => v.virtualNetworkId === deployed.virtualNetworkId)).toBe(true); |
| 182 | const found = all.find((v) => v.virtualNetworkId === deployed.virtualNetworkId); |
| 183 | expect(found?.name).toEqual(testName("alchemy-zt-vnet-list")); |
| 184 | |
| 185 | yield* stack.destroy(); |
| 186 | yield* expectGone(deployed.accountId, deployed.virtualNetworkId); |
| 187 | }).pipe(logLevel), |
| 188 | { tags: ["provider:cloudflare", "provider:cloudflare:tunnel", "live"] }, |
| 189 | ); |