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 { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as workers from "@distilled.cloud/cloudflare/workers"; |
| 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 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | // Freshly-minted scoped API tokens propagate eventually-consistently across |
| 15 | // Cloudflare's edge — ride out intermittent 403s on the test's own |
| 16 | // out-of-band calls by retrying the typed `Forbidden` error. |
| 17 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 18 | |
| 19 | const getLiveSetting = Effect.gen(function* () { |
| 20 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 21 | return yield* workers.getAccountSetting({ accountId }).pipe( |
| 22 | Effect.retry({ |
| 23 | while: (e) => e._tag === "Forbidden", |
| 24 | schedule: forbiddenRetrySchedule, |
| 25 | times: 8, |
| 26 | }), |
| 27 | ); |
| 28 | }); |
| 29 | |
| 30 | // `getAccountSetting` is eventually consistent: right after a PUT it can keep |
| 31 | // echoing the pre-write `greenCompute` for a few seconds (more often under the |
| 32 | // concurrent suite). Poll the live singleton until it reflects the value we |
| 33 | // just wrote before asserting, bounded so a genuinely-stuck write fails fast. |
| 34 | const waitForGreenCompute = (expected: boolean | undefined) => |
| 35 | getLiveSetting.pipe( |
| 36 | Effect.flatMap((live) => |
| 37 | (live.greenCompute ?? false) === (expected ?? false) |
| 38 | ? Effect.succeed(live) |
| 39 | : Effect.fail({ _tag: "GreenComputeNotApplied" as const }), |
| 40 | ), |
| 41 | Effect.retry({ |
| 42 | while: (e) => e._tag === "GreenComputeNotApplied", |
| 43 | schedule: Schedule.spaced("2 seconds"), |
| 44 | times: 15, |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | // Both cases mutate the same account-level Workers settings singleton; run them serially so they don't corrupt each other's captured baseline under the global concurrent test config. |
| 49 | describe.sequential( |
| 50 | "AccountSetting", |
| 51 | { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] }, |
| 52 | () => { |
| 53 | test.provider( |
| 54 | "flips green compute, updates in place, and restores the baseline on destroy", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | // Observe the account's baseline out-of-band. The singleton always |
| 60 | // exists, so the test works relative to whatever the account has. |
| 61 | const baseline = yield* getLiveSetting; |
| 62 | const baselineGreen = baseline.greenCompute ?? false; |
| 63 | const flipped = !baselineGreen; |
| 64 | |
| 65 | // Create — pin green compute to the opposite of the baseline. |
| 66 | const created = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* Cloudflare.Workers.AccountSetting("AccountSetting", { |
| 69 | greenCompute: flipped, |
| 70 | }); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | expect(created.greenCompute).toEqual(flipped); |
| 75 | // The pre-management value was captured for restore-on-destroy. |
| 76 | expect(created.initialGreenCompute).toEqual(baseline.greenCompute); |
| 77 | |
| 78 | // Out-of-band verify the live account state (poll through edge lag). |
| 79 | const live = yield* waitForGreenCompute(flipped); |
| 80 | expect(live.greenCompute ?? false).toEqual(flipped); |
| 81 | |
| 82 | // Update in place — set it back to the baseline value. Same |
| 83 | // singleton, no replacement; the captured initial value survives. |
| 84 | const updated = yield* stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | return yield* Cloudflare.Workers.AccountSetting("AccountSetting", { |
| 87 | greenCompute: baselineGreen, |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(updated.greenCompute ?? false).toEqual(baselineGreen); |
| 93 | expect(updated.initialGreenCompute).toEqual(baseline.greenCompute); |
| 94 | |
| 95 | const liveAfterUpdate = yield* waitForGreenCompute(baselineGreen); |
| 96 | expect(liveAfterUpdate.greenCompute ?? false).toEqual(baselineGreen); |
| 97 | |
| 98 | // Destroy — restores the pre-management values (already at the |
| 99 | // baseline here, so this also exercises the idempotent no-op path). |
| 100 | yield* stack.destroy(); |
| 101 | |
| 102 | const restored = yield* waitForGreenCompute(baselineGreen); |
| 103 | expect(restored.greenCompute ?? false).toEqual(baselineGreen); |
| 104 | expect(restored.defaultUsageModel).toEqual(baseline.defaultUsageModel); |
| 105 | }).pipe(logLevel), |
| 106 | { timeout: 120_000 }, |
| 107 | ); |
| 108 | |
| 109 | test.provider( |
| 110 | "no-op deploy when desired settings already match the live account", |
| 111 | (stack) => |
| 112 | Effect.gen(function* () { |
| 113 | yield* stack.destroy(); |
| 114 | |
| 115 | const baseline = yield* getLiveSetting; |
| 116 | |
| 117 | // Deploy with the account's current values — reconcile observes no |
| 118 | // drift and skips the PUT entirely. |
| 119 | const setting = yield* stack.deploy( |
| 120 | Effect.gen(function* () { |
| 121 | return yield* Cloudflare.Workers.AccountSetting("NoopSetting", { |
| 122 | defaultUsageModel: baseline.defaultUsageModel ?? undefined, |
| 123 | greenCompute: baseline.greenCompute ?? undefined, |
| 124 | }); |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | expect(setting.defaultUsageModel).toEqual(baseline.defaultUsageModel ?? undefined); |
| 129 | expect(setting.greenCompute).toEqual(baseline.greenCompute ?? undefined); |
| 130 | expect(setting.initialDefaultUsageModel).toEqual(baseline.defaultUsageModel ?? undefined); |
| 131 | expect(setting.initialGreenCompute).toEqual(baseline.greenCompute ?? undefined); |
| 132 | |
| 133 | // Destroy — initial values match the live state, so restore is a |
| 134 | // no-op and the account is left untouched. |
| 135 | yield* stack.destroy(); |
| 136 | |
| 137 | const after = yield* getLiveSetting; |
| 138 | expect(after.defaultUsageModel).toEqual(baseline.defaultUsageModel); |
| 139 | expect(after.greenCompute).toEqual(baseline.greenCompute); |
| 140 | }).pipe(logLevel), |
| 141 | { timeout: 120_000 }, |
| 142 | ); |
| 143 | |
| 144 | // Canonical `list()` test (account-scoped singleton): there is no |
| 145 | // collection API for the Workers account settings, so `list()` reads the |
| 146 | // single account-wide object and returns it as a one-element array. The |
| 147 | // singleton always exists, so no deploy is needed to observe it. |
| 148 | test.provider( |
| 149 | "list returns the account settings singleton", |
| 150 | (stack) => |
| 151 | Effect.gen(function* () { |
| 152 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 153 | |
| 154 | const provider = yield* Provider.findProvider(Cloudflare.Workers.AccountSetting); |
| 155 | const all = yield* provider.list(); |
| 156 | |
| 157 | expect(all.length).toEqual(1); |
| 158 | expect(all[0].accountId).toEqual(accountId); |
| 159 | // The element is a full Attributes shape (same as `read` produces). |
| 160 | expect(all[0].initialGreenCompute).toEqual(all[0].greenCompute); |
| 161 | expect(all[0].initialDefaultUsageModel).toEqual(all[0].defaultUsageModel); |
| 162 | |
| 163 | // `stack` is unused (the singleton always exists), but keep the destroy |
| 164 | // bookend so the harness state stays clean. |
| 165 | yield* stack.destroy(); |
| 166 | }).pipe(logLevel), |
| 167 | { timeout: 120_000 }, |
| 168 | ); |
| 169 | }, |
| 170 | ); |