The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 mcn from "@distilled.cloud/cloudflare/magic-cloud-networking"; |
| 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 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Magic Cloud Networking is an entitlement-gated add-on (Magic WAN family). |
| 16 | // On the standard testing account every MCN call fails with the typed |
| 17 | // `FeatureNotEnabled` error (HTTP 403, Cloudflare code 1012 "feature not |
| 18 | // enabled"). The full lifecycle test below is gated behind an explicit |
| 19 | // opt-in env flag for entitled accounts; the probe test always runs and |
| 20 | // pins the typed tag. |
| 21 | const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_CLOUD_NETWORKING; |
| 22 | |
| 23 | // The scoped API token the test harness mints propagates eventually- |
| 24 | // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`, |
| 25 | // declared in the distilled error union) on out-of-band calls. The retry |
| 26 | // is bounded so an unentitled account fails fast with the typed tag. |
| 27 | const getIntegration = (accountId: string, providerId: string) => |
| 28 | mcn.getCloudIntegration({ accountId, providerId }).pipe( |
| 29 | Effect.retry({ |
| 30 | while: (e) => e._tag === "Forbidden", |
| 31 | schedule: Schedule.exponential("500 millis"), |
| 32 | times: 5, |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | // Poll until the integration is gone after destroy. Cloudflare answers GET |
| 37 | // for a missing integration with the typed `CloudIntegrationNotFound` (404). |
| 38 | const expectGone = (accountId: string, providerId: string) => |
| 39 | getIntegration(accountId, providerId).pipe( |
| 40 | Effect.flatMap(() => Effect.fail({ _tag: "IntegrationNotDeleted" } as const)), |
| 41 | Effect.catchTag("CloudIntegrationNotFound", () => Effect.void), |
| 42 | Effect.retry({ |
| 43 | while: (e) => e._tag === "IntegrationNotDeleted", |
| 44 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | test.provider( |
| 49 | "unentitled accounts surface the typed FeatureNotEnabled error", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 53 | |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const canList = yield* mcn.listCloudIntegrations({ accountId }).pipe( |
| 57 | Effect.as(true), |
| 58 | Effect.catchTag("FeatureNotEnabled", () => Effect.succeed(false)), |
| 59 | ); |
| 60 | if (canList) { |
| 61 | // Entitled account — the gated lifecycle test covers real behavior. |
| 62 | yield* Effect.logInfo("account is MCN-entitled; probe test is a no-op"); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // The typed tag — not UnknownCloudflareError, not a status check. |
| 67 | const error = yield* mcn.listCloudIntegrations({ accountId }).pipe(Effect.flip); |
| 68 | expect(error._tag).toEqual("FeatureNotEnabled"); |
| 69 | |
| 70 | const createError = yield* mcn |
| 71 | .createCloudIntegration({ accountId, cloudType: "AWS", friendlyName: "alchemy-mcn-probe" }) |
| 72 | .pipe(Effect.flip); |
| 73 | expect(createError._tag).toEqual("FeatureNotEnabled"); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | }).pipe(logLevel), |
| 77 | { tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"] }, |
| 78 | ); |
| 79 | |
| 80 | test.provider( |
| 81 | "list returns a well-typed array of integrations", |
| 82 | (stack) => |
| 83 | Effect.gen(function* () { |
| 84 | yield* stack.destroy(); |
| 85 | |
| 86 | const provider = yield* Provider.findProvider( |
| 87 | Cloudflare.MagicCloudNetworking.CloudIntegration, |
| 88 | ); |
| 89 | const all = yield* provider.list(); |
| 90 | |
| 91 | // On an unentitled account `list()` catches the typed `FeatureNotEnabled` |
| 92 | // tag and yields `[]`; on an entitled account it enumerates every |
| 93 | // integration. Either way the result is the full Attributes array. |
| 94 | expect(Array.isArray(all)).toBe(true); |
| 95 | for (const item of all) { |
| 96 | expect(typeof item.integrationId).toBe("string"); |
| 97 | expect(typeof item.accountId).toBe("string"); |
| 98 | } |
| 99 | |
| 100 | yield* stack.destroy(); |
| 101 | }).pipe(logLevel), |
| 102 | { tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"] }, |
| 103 | ); |
| 104 | |
| 105 | test.provider.skipIf(!entitled)( |
| 106 | "list enumerates the deployed integration", |
| 107 | (stack) => |
| 108 | Effect.gen(function* () { |
| 109 | yield* stack.destroy(); |
| 110 | |
| 111 | const deployed = yield* stack.deploy( |
| 112 | Cloudflare.MagicCloudNetworking.CloudIntegration("ListIntegration", { |
| 113 | cloudType: "AWS", |
| 114 | friendlyName: "alchemy-mcn-cloud-integration-list", |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | const provider = yield* Provider.findProvider( |
| 119 | Cloudflare.MagicCloudNetworking.CloudIntegration, |
| 120 | ); |
| 121 | const all = yield* provider.list(); |
| 122 | |
| 123 | expect(all.some((x) => x.integrationId === deployed.integrationId)).toBe(true); |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | }).pipe(logLevel), |
| 127 | { |
| 128 | tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"], |
| 129 | timeout: 120_000, |
| 130 | }, |
| 131 | ); |
| 132 | |
| 133 | test.provider.skipIf(!entitled)( |
| 134 | "registers an AWS integration, updates it in place, and destroys it", |
| 135 | (stack) => |
| 136 | Effect.gen(function* () { |
| 137 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 138 | |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const integration = yield* stack.deploy( |
| 142 | Cloudflare.MagicCloudNetworking.CloudIntegration("Aws", { |
| 143 | cloudType: "AWS", |
| 144 | friendlyName: "alchemy-mcn-cloud-integration", |
| 145 | description: "alchemy cloud integration test", |
| 146 | }), |
| 147 | ); |
| 148 | |
| 149 | expect(integration.integrationId).toBeDefined(); |
| 150 | expect(integration.accountId).toEqual(accountId); |
| 151 | expect(integration.cloudType).toEqual("AWS"); |
| 152 | expect(integration.friendlyName).toEqual("alchemy-mcn-cloud-integration"); |
| 153 | // No credentials are wired yet — the integration is pending setup. |
| 154 | expect(integration.lifecycleState).toEqual("PENDING_SETUP"); |
| 155 | |
| 156 | // Out-of-band verification via the distilled API. |
| 157 | const live = yield* getIntegration(accountId, integration.integrationId); |
| 158 | expect(live.friendlyName).toEqual("alchemy-mcn-cloud-integration"); |
| 159 | expect(live.cloudType).toEqual("AWS"); |
| 160 | |
| 161 | // Update mutable props in place — same integrationId. |
| 162 | const updated = yield* stack.deploy( |
| 163 | Cloudflare.MagicCloudNetworking.CloudIntegration("Aws", { |
| 164 | cloudType: "AWS", |
| 165 | friendlyName: "alchemy-mcn-cloud-integration-v2", |
| 166 | description: "alchemy cloud integration test v2", |
| 167 | }), |
| 168 | ); |
| 169 | |
| 170 | expect(updated.integrationId).toEqual(integration.integrationId); |
| 171 | expect(updated.friendlyName).toEqual("alchemy-mcn-cloud-integration-v2"); |
| 172 | expect(updated.description).toEqual("alchemy cloud integration test v2"); |
| 173 | |
| 174 | const liveUpdated = yield* getIntegration(accountId, integration.integrationId); |
| 175 | expect(liveUpdated.friendlyName).toEqual("alchemy-mcn-cloud-integration-v2"); |
| 176 | |
| 177 | yield* stack.destroy(); |
| 178 | |
| 179 | yield* expectGone(accountId, integration.integrationId); |
| 180 | }).pipe(logLevel), |
| 181 | { |
| 182 | tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"], |
| 183 | timeout: 120_000, |
| 184 | }, |
| 185 | ); |
| 186 | |
| 187 | test.provider.skipIf(!entitled)( |
| 188 | "replaces the integration when cloudType changes", |
| 189 | (stack) => |
| 190 | Effect.gen(function* () { |
| 191 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 192 | |
| 193 | yield* stack.destroy(); |
| 194 | |
| 195 | const initial = yield* stack.deploy( |
| 196 | Cloudflare.MagicCloudNetworking.CloudIntegration("ReplaceIntegration", { |
| 197 | cloudType: "AWS", |
| 198 | friendlyName: "alchemy-mcn-cloud-integration-replace", |
| 199 | }), |
| 200 | ); |
| 201 | |
| 202 | // cloudType is the provider identity — changing it must produce a |
| 203 | // brand-new integration (new integrationId). |
| 204 | const replaced = yield* stack.deploy( |
| 205 | Cloudflare.MagicCloudNetworking.CloudIntegration("ReplaceIntegration", { |
| 206 | cloudType: "GOOGLE", |
| 207 | friendlyName: "alchemy-mcn-cloud-integration-replace", |
| 208 | }), |
| 209 | ); |
| 210 | |
| 211 | expect(replaced.integrationId).not.toEqual(initial.integrationId); |
| 212 | expect(replaced.cloudType).toEqual("GOOGLE"); |
| 213 | |
| 214 | yield* expectGone(accountId, initial.integrationId); |
| 215 | |
| 216 | yield* stack.destroy(); |
| 217 | |
| 218 | yield* expectGone(accountId, replaced.integrationId); |
| 219 | }).pipe(logLevel), |
| 220 | { |
| 221 | tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"], |
| 222 | timeout: 120_000, |
| 223 | }, |
| 224 | ); |