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 Stream from "effect/Stream"; |
| 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 | // Deterministic identifiers — the portal id is the API identity, so reruns |
| 17 | // converge on the same portal instead of leaking. The two cases run |
| 18 | // concurrently, so they MUST use distinct portal ids AND distinct hostnames: |
| 19 | // a hostname is a per-account unique constraint, so a shared hostname makes the |
| 20 | // two deploys race on "This hostname is already in use in a different MCP |
| 21 | // Portal". |
| 22 | const PORTAL_ID = testName("alc-mcp-portal"); |
| 23 | const HOSTNAME = `${testName("alchemy-test-mcp")}.alchemy-test-2.us`; |
| 24 | const LIST_PORTAL_ID = testName("alc-mcp-portal-list"); |
| 25 | const LIST_HOSTNAME = `${testName("alchemy-test-mcp-list")}.alchemy-test-2.us`; |
| 26 | |
| 27 | // Read a portal out-of-band, mapping "gone" to undefined. |
| 28 | const getLivePortal = (accountId: string, id: string) => |
| 29 | zeroTrust |
| 30 | .readAccessAiControlMcpPortal({ accountId, id }) |
| 31 | .pipe(Effect.catchTag("McpPortalNotFound", () => Effect.succeed(undefined))); |
| 32 | |
| 33 | // Delete every portal currently occupying `hostname`, regardless of its portal |
| 34 | // id. A per-stack `destroy()` only removes the portal tracked under this |
| 35 | // stack's logical id, so an orphan left by a previously crashed run keeps the |
| 36 | // hostname (a per-account unique constraint) reserved and the next create |
| 37 | // fails with `McpPortalHostnameInUse`. Sweeping by hostname makes the suite |
| 38 | // self-healing and leaves no dangling resources behind. |
| 39 | const cleanupPortalsByHostname = (accountId: string, hostname: string) => |
| 40 | zeroTrust.listAccessAiControlMcpPortals.pages({ accountId }).pipe( |
| 41 | Stream.runCollect, |
| 42 | Effect.map((chunk) => |
| 43 | Array.from(chunk).flatMap((page) => |
| 44 | (page.result ?? []).filter((p) => p.hostname === hostname), |
| 45 | ), |
| 46 | ), |
| 47 | Effect.flatMap((portals) => |
| 48 | Effect.forEach(portals, (p) => |
| 49 | zeroTrust |
| 50 | .deleteAccessAiControlMcpPortal({ accountId, id: p.id }) |
| 51 | .pipe(Effect.catchTag("McpPortalNotFound", () => Effect.void)), |
| 52 | ), |
| 53 | ), |
| 54 | Effect.catchTag("Forbidden", () => Effect.void), |
| 55 | ); |
| 56 | |
| 57 | test.provider( |
| 58 | "create, update in place, and destroy an MCP portal", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 62 | |
| 63 | yield* stack.destroy(); |
| 64 | yield* cleanupPortalsByHostname(accountId, HOSTNAME); |
| 65 | |
| 66 | const portal = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* Cloudflare.Access.McpPortal("AiPortal", { |
| 69 | portalId: PORTAL_ID, |
| 70 | hostname: HOSTNAME, |
| 71 | description: "alchemy mcp portal v1", |
| 72 | }); |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(portal.portalId).toEqual(PORTAL_ID); |
| 77 | expect(portal.accountId).toEqual(accountId); |
| 78 | expect(portal.hostname).toEqual(HOSTNAME); |
| 79 | expect(portal.name).toEqual(PORTAL_ID); |
| 80 | expect(portal.description).toEqual("alchemy mcp portal v1"); |
| 81 | // Observed server-side default (no allowCodeMode requested). |
| 82 | expect(portal.allowCodeMode).toEqual(true); |
| 83 | expect(portal.secureWebGateway).toEqual(false); |
| 84 | |
| 85 | const live = yield* getLivePortal(accountId, PORTAL_ID); |
| 86 | expect(live?.id).toEqual(PORTAL_ID); |
| 87 | expect(live?.hostname).toEqual(HOSTNAME); |
| 88 | |
| 89 | // Name + description update converges in place — same portal id. |
| 90 | const updated = yield* stack.deploy( |
| 91 | Effect.gen(function* () { |
| 92 | return yield* Cloudflare.Access.McpPortal("AiPortal", { |
| 93 | portalId: PORTAL_ID, |
| 94 | name: testName("Alchemy AI Portal"), |
| 95 | hostname: HOSTNAME, |
| 96 | description: "alchemy mcp portal v2", |
| 97 | secureWebGateway: true, |
| 98 | }); |
| 99 | }), |
| 100 | ); |
| 101 | expect(updated.portalId).toEqual(PORTAL_ID); |
| 102 | expect(updated.name).toEqual(testName("Alchemy AI Portal")); |
| 103 | expect(updated.description).toEqual("alchemy mcp portal v2"); |
| 104 | expect(updated.secureWebGateway).toEqual(true); |
| 105 | |
| 106 | const liveUpdated = yield* getLivePortal(accountId, PORTAL_ID); |
| 107 | expect(liveUpdated?.name).toEqual(testName("Alchemy AI Portal")); |
| 108 | expect(liveUpdated?.secureWebGateway).toEqual(true); |
| 109 | |
| 110 | // No-op redeploy keeps the same portal without drift. |
| 111 | const noop = yield* stack.deploy( |
| 112 | Effect.gen(function* () { |
| 113 | return yield* Cloudflare.Access.McpPortal("AiPortal", { |
| 114 | portalId: PORTAL_ID, |
| 115 | name: testName("Alchemy AI Portal"), |
| 116 | hostname: HOSTNAME, |
| 117 | description: "alchemy mcp portal v2", |
| 118 | secureWebGateway: true, |
| 119 | }); |
| 120 | }), |
| 121 | ); |
| 122 | expect(noop.portalId).toEqual(PORTAL_ID); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | |
| 126 | const afterDestroy = yield* getLivePortal(accountId, PORTAL_ID); |
| 127 | expect(afterDestroy).toBeUndefined(); |
| 128 | }).pipe(logLevel), |
| 129 | { |
| 130 | tags: ["provider:cloudflare", "provider:cloudflare:access", "live"], |
| 131 | timeout: 90_000, |
| 132 | }, |
| 133 | ); |
| 134 | |
| 135 | // Canonical `list()` test (account collection): deploy a portal, then resolve |
| 136 | // the provider via the typed helper and assert the deployed portal appears in |
| 137 | // the exhaustively-paginated result. |
| 138 | test.provider( |
| 139 | "list enumerates the deployed MCP portal", |
| 140 | (stack) => |
| 141 | Effect.gen(function* () { |
| 142 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 143 | |
| 144 | yield* stack.destroy(); |
| 145 | yield* cleanupPortalsByHostname(accountId, LIST_HOSTNAME); |
| 146 | |
| 147 | const deployed = yield* stack.deploy( |
| 148 | Effect.gen(function* () { |
| 149 | return yield* Cloudflare.Access.McpPortal("ListPortal", { |
| 150 | portalId: LIST_PORTAL_ID, |
| 151 | hostname: LIST_HOSTNAME, |
| 152 | }); |
| 153 | }), |
| 154 | ); |
| 155 | |
| 156 | const provider = yield* Provider.findProvider(Cloudflare.Access.McpPortal); |
| 157 | const all = yield* provider.list(); |
| 158 | |
| 159 | expect(all.some((p) => p.portalId === deployed.portalId)).toBe(true); |
| 160 | |
| 161 | yield* stack.destroy(); |
| 162 | |
| 163 | const afterDestroy = yield* getLivePortal(accountId, LIST_PORTAL_ID); |
| 164 | expect(afterDestroy).toBeUndefined(); |
| 165 | }).pipe(logLevel), |
| 166 | { |
| 167 | tags: ["provider:cloudflare", "provider:cloudflare:access", "live"], |
| 168 | timeout: 90_000, |
| 169 | }, |
| 170 | ); |