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 { poll } from "alchemy/Util/poll"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { testName } from "../../names.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | class FlagStillExists extends Data.TaggedError("FlagStillExists") {} |
| 19 | |
| 20 | // A deleted flag surfaces as `FlagshipFlagNotFound` (or, when the parent app |
| 21 | // was destroyed too, `FlagshipAppNotFound`) — either way it's gone. |
| 22 | const expectFlagGone = (accountId: string, appId: string, flagKey: string) => |
| 23 | flagship.getAppFlag({ accountId, appId, flagKey }).pipe( |
| 24 | Effect.flatMap(() => Effect.fail(new FlagStillExists())), |
| 25 | Effect.retry({ |
| 26 | while: (e): e is FlagStillExists => e instanceof FlagStillExists, |
| 27 | schedule: Schedule.max([Schedule.exponential("250 millis"), Schedule.recurs(10)]), |
| 28 | }), |
| 29 | Effect.catchTag(["FlagshipFlagNotFound", "FlagshipAppNotFound"], () => Effect.void), |
| 30 | ); |
| 31 | |
| 32 | test.provider( |
| 33 | "create, update, delete a flag in an app", |
| 34 | (stack) => |
| 35 | Effect.gen(function* () { |
| 36 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 37 | |
| 38 | yield* stack.destroy(); |
| 39 | |
| 40 | const initial = yield* stack.deploy( |
| 41 | Effect.gen(function* () { |
| 42 | const app = yield* Cloudflare.Flagship.App("FlagApp", { |
| 43 | name: testName("alchemy-test-flagship-flags"), |
| 44 | }); |
| 45 | const flag = yield* Cloudflare.Flagship.Flag("Flag", { |
| 46 | appId: app.appId, |
| 47 | key: "alchemy-test-flag", |
| 48 | defaultVariation: "off", |
| 49 | variations: { off: false, on: true }, |
| 50 | }); |
| 51 | return { app, flag }; |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | expect(initial.flag.accountId).toEqual(accountId); |
| 56 | expect(initial.flag.appId).toEqual(initial.app.appId); |
| 57 | expect(initial.flag.key).toEqual("alchemy-test-flag"); |
| 58 | expect(initial.flag.enabled).toBe(true); |
| 59 | expect(initial.flag.defaultVariation).toEqual("off"); |
| 60 | expect(initial.flag.variations).toEqual({ off: false, on: true }); |
| 61 | expect(initial.flag.rules).toEqual([]); |
| 62 | |
| 63 | // Verify out-of-band via the API. |
| 64 | const live = yield* flagship.getAppFlag({ |
| 65 | accountId, |
| 66 | appId: initial.app.appId, |
| 67 | flagKey: "alchemy-test-flag", |
| 68 | }); |
| 69 | expect(live.enabled).toBe(true); |
| 70 | expect(live.defaultVariation).toEqual("off"); |
| 71 | expect(live.type).toEqual("boolean"); |
| 72 | |
| 73 | // Update mutable props in place — same key, same app. |
| 74 | const updated = yield* stack.deploy( |
| 75 | Effect.gen(function* () { |
| 76 | const app = yield* Cloudflare.Flagship.App("FlagApp", { |
| 77 | name: testName("alchemy-test-flagship-flags"), |
| 78 | }); |
| 79 | const flag = yield* Cloudflare.Flagship.Flag("Flag", { |
| 80 | appId: app.appId, |
| 81 | key: "alchemy-test-flag", |
| 82 | enabled: false, |
| 83 | defaultVariation: "on", |
| 84 | variations: { off: false, on: true }, |
| 85 | description: "managed by alchemy", |
| 86 | rules: [ |
| 87 | { |
| 88 | priority: 1, |
| 89 | conditions: [{ attribute: "country", operator: "equals", value: "US" }], |
| 90 | serveVariation: "on", |
| 91 | rollout: { percentage: 50 }, |
| 92 | }, |
| 93 | ], |
| 94 | }); |
| 95 | return { app, flag }; |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | expect(updated.flag.key).toEqual("alchemy-test-flag"); |
| 100 | expect(updated.flag.appId).toEqual(initial.app.appId); |
| 101 | expect(updated.flag.enabled).toBe(false); |
| 102 | expect(updated.flag.defaultVariation).toEqual("on"); |
| 103 | expect(updated.flag.description).toEqual("managed by alchemy"); |
| 104 | expect(updated.flag.rules).toEqual([ |
| 105 | { |
| 106 | priority: 1, |
| 107 | conditions: [{ attribute: "country", operator: "equals", value: "US" }], |
| 108 | serveVariation: "on", |
| 109 | rollout: { percentage: 50 }, |
| 110 | }, |
| 111 | ]); |
| 112 | |
| 113 | const liveUpdated = yield* flagship.getAppFlag({ |
| 114 | accountId, |
| 115 | appId: initial.app.appId, |
| 116 | flagKey: "alchemy-test-flag", |
| 117 | }); |
| 118 | expect(liveUpdated.enabled).toBe(false); |
| 119 | expect(liveUpdated.defaultVariation).toEqual("on"); |
| 120 | expect(liveUpdated.rules).toHaveLength(1); |
| 121 | |
| 122 | // Redeploying identical props is a no-op. |
| 123 | const noop = yield* stack.deploy( |
| 124 | Effect.gen(function* () { |
| 125 | const app = yield* Cloudflare.Flagship.App("FlagApp", { |
| 126 | name: testName("alchemy-test-flagship-flags"), |
| 127 | }); |
| 128 | const flag = yield* Cloudflare.Flagship.Flag("Flag", { |
| 129 | appId: app.appId, |
| 130 | key: "alchemy-test-flag", |
| 131 | enabled: false, |
| 132 | defaultVariation: "on", |
| 133 | variations: { off: false, on: true }, |
| 134 | description: "managed by alchemy", |
| 135 | rules: [ |
| 136 | { |
| 137 | priority: 1, |
| 138 | conditions: [{ attribute: "country", operator: "equals", value: "US" }], |
| 139 | serveVariation: "on", |
| 140 | rollout: { percentage: 50 }, |
| 141 | }, |
| 142 | ], |
| 143 | }); |
| 144 | return { app, flag }; |
| 145 | }), |
| 146 | ); |
| 147 | expect(noop.flag.updatedAt).toEqual(updated.flag.updatedAt); |
| 148 | |
| 149 | yield* stack.destroy(); |
| 150 | |
| 151 | yield* expectFlagGone(accountId, initial.app.appId, "alchemy-test-flag"); |
| 152 | }).pipe(logLevel), |
| 153 | { tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"] }, |
| 154 | ); |
| 155 | |
| 156 | test.provider( |
| 157 | "replaces the flag when the key changes", |
| 158 | (stack) => |
| 159 | Effect.gen(function* () { |
| 160 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 161 | |
| 162 | yield* stack.destroy(); |
| 163 | |
| 164 | const initial = yield* stack.deploy( |
| 165 | Effect.gen(function* () { |
| 166 | const app = yield* Cloudflare.Flagship.App("ReplaceApp", { |
| 167 | name: testName("alchemy-test-flagship-replace"), |
| 168 | }); |
| 169 | const flag = yield* Cloudflare.Flagship.Flag("ReplaceFlag", { |
| 170 | appId: app.appId, |
| 171 | key: "alchemy-test-flag-a", |
| 172 | defaultVariation: "off", |
| 173 | variations: { off: false, on: true }, |
| 174 | }); |
| 175 | return { app, flag }; |
| 176 | }), |
| 177 | ); |
| 178 | expect(initial.flag.key).toEqual("alchemy-test-flag-a"); |
| 179 | |
| 180 | // Changing the key is a replacement: a new flag is created and the old |
| 181 | // one is deleted. |
| 182 | const replaced = yield* stack.deploy( |
| 183 | Effect.gen(function* () { |
| 184 | const app = yield* Cloudflare.Flagship.App("ReplaceApp", { |
| 185 | name: testName("alchemy-test-flagship-replace"), |
| 186 | }); |
| 187 | const flag = yield* Cloudflare.Flagship.Flag("ReplaceFlag", { |
| 188 | appId: app.appId, |
| 189 | key: "alchemy-test-flag-b", |
| 190 | defaultVariation: "off", |
| 191 | variations: { off: false, on: true }, |
| 192 | }); |
| 193 | return { app, flag }; |
| 194 | }), |
| 195 | ); |
| 196 | |
| 197 | expect(replaced.flag.key).toEqual("alchemy-test-flag-b"); |
| 198 | yield* expectFlagGone(accountId, initial.app.appId, "alchemy-test-flag-a"); |
| 199 | |
| 200 | yield* stack.destroy(); |
| 201 | |
| 202 | yield* expectFlagGone(accountId, initial.app.appId, "alchemy-test-flag-b"); |
| 203 | }).pipe(logLevel), |
| 204 | { tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"] }, |
| 205 | ); |
| 206 | |
| 207 | test.provider( |
| 208 | "recreates a flag after out-of-band delete", |
| 209 | (stack) => |
| 210 | Effect.gen(function* () { |
| 211 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 212 | |
| 213 | yield* stack.destroy(); |
| 214 | |
| 215 | const initial = yield* stack.deploy( |
| 216 | Effect.gen(function* () { |
| 217 | const app = yield* Cloudflare.Flagship.App("HealApp", { |
| 218 | name: testName("alchemy-test-flagship-heal-flag"), |
| 219 | }); |
| 220 | const flag = yield* Cloudflare.Flagship.Flag("HealFlag", { |
| 221 | appId: app.appId, |
| 222 | key: "alchemy-test-flag-heal", |
| 223 | defaultVariation: "off", |
| 224 | variations: { off: false, on: true }, |
| 225 | }); |
| 226 | return { app, flag }; |
| 227 | }), |
| 228 | ); |
| 229 | |
| 230 | // Delete the flag out-of-band. A redeploy with identical props is a |
| 231 | // planner no-op, so change a prop to force reconcile — it must observe |
| 232 | // the flag as missing and recreate it instead of failing on a 404. |
| 233 | yield* flagship.deleteAppFlag({ |
| 234 | accountId, |
| 235 | appId: initial.app.appId, |
| 236 | flagKey: "alchemy-test-flag-heal", |
| 237 | }); |
| 238 | |
| 239 | const healed = yield* stack.deploy( |
| 240 | Effect.gen(function* () { |
| 241 | const app = yield* Cloudflare.Flagship.App("HealApp", { |
| 242 | name: testName("alchemy-test-flagship-heal-flag"), |
| 243 | }); |
| 244 | const flag = yield* Cloudflare.Flagship.Flag("HealFlag", { |
| 245 | appId: app.appId, |
| 246 | key: "alchemy-test-flag-heal", |
| 247 | enabled: false, |
| 248 | defaultVariation: "off", |
| 249 | variations: { off: false, on: true }, |
| 250 | }); |
| 251 | return { app, flag }; |
| 252 | }), |
| 253 | ); |
| 254 | |
| 255 | expect(healed.flag.key).toEqual("alchemy-test-flag-heal"); |
| 256 | expect(healed.flag.enabled).toBe(false); |
| 257 | |
| 258 | const live = yield* flagship.getAppFlag({ |
| 259 | accountId, |
| 260 | appId: initial.app.appId, |
| 261 | flagKey: "alchemy-test-flag-heal", |
| 262 | }); |
| 263 | expect(live.enabled).toBe(false); |
| 264 | |
| 265 | yield* stack.destroy(); |
| 266 | |
| 267 | yield* expectFlagGone(accountId, initial.app.appId, "alchemy-test-flag-heal"); |
| 268 | }).pipe(logLevel), |
| 269 | { tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"] }, |
| 270 | ); |
| 271 | |
| 272 | test.provider( |
| 273 | "list enumerates the deployed flag", |
| 274 | (stack) => |
| 275 | Effect.gen(function* () { |
| 276 | yield* stack.destroy(); |
| 277 | |
| 278 | const deployed = yield* stack.deploy( |
| 279 | Effect.gen(function* () { |
| 280 | const app = yield* Cloudflare.Flagship.App("ListApp", {}); |
| 281 | const flag = yield* Cloudflare.Flagship.Flag("ListFlag", { |
| 282 | appId: app.appId, |
| 283 | key: "alchemy-test-flag-list", |
| 284 | defaultVariation: "off", |
| 285 | variations: { off: false, on: true }, |
| 286 | }); |
| 287 | return { app, flag }; |
| 288 | }), |
| 289 | ); |
| 290 | |
| 291 | const provider = yield* Provider.findProvider(Cloudflare.Flagship.Flag); |
| 292 | |
| 293 | // The flag itself is readable immediately (see the other tests' direct |
| 294 | // `getAppFlag` right after deploy). What lags is the account-wide |
| 295 | // `list()` path: the provider has no account-wide flag enumeration, so it |
| 296 | // goes `listApps -> listAppFlags`, and a freshly-created app appears in |
| 297 | // the account-wide `listApps` slower than the flag does under its own |
| 298 | // parent. Poll the full path until that app-list consistency catches up. |
| 299 | const all = yield* poll({ |
| 300 | description: "list() includes the deployed flag", |
| 301 | effect: provider.list(), |
| 302 | predicate: (all) => |
| 303 | all.some((f) => f.appId === deployed.app.appId && f.key === deployed.flag.key), |
| 304 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(30)]), |
| 305 | }); |
| 306 | |
| 307 | expect(all.some((f) => f.appId === deployed.app.appId && f.key === deployed.flag.key)).toBe( |
| 308 | true, |
| 309 | ); |
| 310 | |
| 311 | yield* stack.destroy(); |
| 312 | }).pipe(logLevel), |
| 313 | // The `listApps` consistency poll above is bounded at ~90s (30 x 3s) plus |
| 314 | // per-iteration `list()` latency, on top of two deploys; size the test over |
| 315 | // that bounded worst case rather than the 120s default. |
| 316 | { tags: ["provider:cloudflare", "provider:cloudflare:flagship", "live"], timeout: 180_000 }, |
| 317 | ); |