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 flagship from "@distilled.cloud/cloudflare/flagship"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Data from "effect/Data"; |
| 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 | class AppStillExists extends Data.TaggedError("AppStillExists") {} |
| 18 | |
| 19 | class AppNotListedYet extends Data.TaggedError("AppNotListedYet") {} |
| 20 | |
| 21 | // A deleted app surfaces as `FlagshipAppNotFound` — that's the success |
| 22 | // condition here. |
| 23 | const expectAppGone = (accountId: string, appId: string) => |
| 24 | flagship.getApp({ accountId, appId }).pipe( |
| 25 | Effect.flatMap(() => Effect.fail(new AppStillExists())), |
| 26 | Effect.retry({ |
| 27 | while: (e): e is AppStillExists => e instanceof AppStillExists, |
| 28 | schedule: Schedule.max([Schedule.exponential("250 millis"), Schedule.recurs(10)]), |
| 29 | }), |
| 30 | Effect.catchTag("FlagshipAppNotFound", () => Effect.void), |
| 31 | ); |
| 32 | |
| 33 | test.provider( |
| 34 | "create, update, delete a Flagship app", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 38 | |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const initial = yield* stack.deploy( |
| 42 | Effect.gen(function* () { |
| 43 | const app = yield* Cloudflare.Flagship.App("App", { name: "alchemy-test-flagship-app" }); |
| 44 | return { app }; |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | expect(initial.app.appId).toBeDefined(); |
| 49 | expect(initial.app.accountId).toEqual(accountId); |
| 50 | expect(initial.app.name).toEqual("alchemy-test-flagship-app"); |
| 51 | |
| 52 | // Verify out-of-band via the API. |
| 53 | const live = yield* flagship.getApp({ accountId, appId: initial.app.appId }); |
| 54 | expect(live.name).toEqual("alchemy-test-flagship-app"); |
| 55 | |
| 56 | // Rename in place — same app id. |
| 57 | const renamed = yield* stack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | const app = yield* Cloudflare.Flagship.App("App", { |
| 60 | name: testName("alchemy-test-flagship-app-v2"), |
| 61 | }); |
| 62 | return { app }; |
| 63 | }), |
| 64 | ); |
| 65 | expect(renamed.app.appId).toEqual(initial.app.appId); |
| 66 | expect(renamed.app.name).toEqual(testName("alchemy-test-flagship-app-v2")); |
| 67 | |
| 68 | const liveRenamed = yield* flagship.getApp({ accountId, appId: initial.app.appId }); |
| 69 | expect(liveRenamed.name).toEqual(testName("alchemy-test-flagship-app-v2")); |
| 70 | |
| 71 | // Redeploying identical props is a no-op (still the same app). |
| 72 | const noop = yield* stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | const app = yield* Cloudflare.Flagship.App("App", { |
| 75 | name: testName("alchemy-test-flagship-app-v2"), |
| 76 | }); |
| 77 | return { app }; |
| 78 | }), |
| 79 | ); |
| 80 | expect(noop.app.appId).toEqual(initial.app.appId); |
| 81 | |
| 82 | yield* stack.destroy(); |
| 83 | |
| 84 | yield* expectAppGone(accountId, initial.app.appId); |
| 85 | }).pipe(logLevel), |
| 86 | { tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"] }, |
| 87 | ); |
| 88 | |
| 89 | test.provider( |
| 90 | "recreates an app after out-of-band delete", |
| 91 | (stack) => |
| 92 | Effect.gen(function* () { |
| 93 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | |
| 97 | const initial = yield* stack.deploy( |
| 98 | Effect.gen(function* () { |
| 99 | const app = yield* Cloudflare.Flagship.App("HealApp", { |
| 100 | name: testName("alchemy-test-flagship-heal"), |
| 101 | }); |
| 102 | return { app }; |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | // Delete the app out-of-band. A redeploy with identical props is a |
| 107 | // planner no-op, so change a prop to force reconcile — it must observe |
| 108 | // the app as missing and recreate it instead of failing on a 404. |
| 109 | yield* flagship.deleteApp({ accountId, appId: initial.app.appId }); |
| 110 | |
| 111 | const healed = yield* stack.deploy( |
| 112 | Effect.gen(function* () { |
| 113 | const app = yield* Cloudflare.Flagship.App("HealApp", { |
| 114 | name: testName("alchemy-test-flagship-heal-v2"), |
| 115 | }); |
| 116 | return { app }; |
| 117 | }), |
| 118 | ); |
| 119 | |
| 120 | expect(healed.app.appId).not.toEqual(initial.app.appId); |
| 121 | expect(healed.app.name).toEqual(testName("alchemy-test-flagship-heal-v2")); |
| 122 | |
| 123 | const live = yield* flagship.getApp({ accountId, appId: healed.app.appId }); |
| 124 | expect(live.name).toEqual(testName("alchemy-test-flagship-heal-v2")); |
| 125 | |
| 126 | yield* stack.destroy(); |
| 127 | |
| 128 | yield* expectAppGone(accountId, healed.app.appId); |
| 129 | }).pipe(logLevel), |
| 130 | { tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"] }, |
| 131 | ); |
| 132 | |
| 133 | test.provider( |
| 134 | "list enumerates the deployed Flagship app", |
| 135 | (stack) => |
| 136 | Effect.gen(function* () { |
| 137 | yield* stack.destroy(); |
| 138 | |
| 139 | const deployed = yield* stack.deploy( |
| 140 | Effect.gen(function* () { |
| 141 | const app = yield* Cloudflare.Flagship.App("ListApp", { |
| 142 | name: testName("alchemy-test-flagship-list"), |
| 143 | }); |
| 144 | return { app }; |
| 145 | }), |
| 146 | ); |
| 147 | |
| 148 | const provider = yield* Provider.findProvider(Cloudflare.Flagship.App); |
| 149 | |
| 150 | // The flagship apps list endpoint is eventually consistent: a freshly |
| 151 | // created app is strongly readable by id via getApp but takes a few |
| 152 | // seconds to surface in the account-wide list. Retry until it appears. |
| 153 | const all = yield* provider.list().pipe( |
| 154 | Effect.flatMap((apps) => |
| 155 | apps.some((a) => a.appId === deployed.app.appId) |
| 156 | ? Effect.succeed(apps) |
| 157 | : Effect.fail(new AppNotListedYet()), |
| 158 | ), |
| 159 | Effect.retry({ |
| 160 | while: (e): e is AppNotListedYet => e instanceof AppNotListedYet, |
| 161 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(8)]), |
| 162 | }), |
| 163 | ); |
| 164 | |
| 165 | expect(all.some((a) => a.appId === deployed.app.appId)).toBe(true); |
| 166 | const found = all.find((a) => a.appId === deployed.app.appId); |
| 167 | expect(found?.name).toEqual(testName("alchemy-test-flagship-list")); |
| 168 | expect(found?.accountId).toEqual(deployed.app.accountId); |
| 169 | |
| 170 | yield* stack.destroy(); |
| 171 | }).pipe(logLevel), |
| 172 | { tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"] }, |
| 173 | ); |