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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import { adopt } from "alchemy/AdoptPolicy"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 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 | // Canonical `list()` test (parent fan-out): secrets are sub-resources of a |
| 19 | // Secrets Store and there is no account-wide secret enumeration API, so |
| 20 | // `list()` enumerates every store and lists the secrets inside each. Deploy a |
| 21 | // store + secret, then assert the deployed secret appears in the result. |
| 22 | test.provider( |
| 23 | "list enumerates the deployed secret across stores", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* stack.destroy(); |
| 27 | |
| 28 | const deployed = yield* stack.deploy( |
| 29 | Effect.gen(function* () { |
| 30 | const store = yield* Cloudflare.SecretsStore.Store("ListSecretStore"); |
| 31 | return yield* Cloudflare.SecretsStore.Secret("ListSecret", { |
| 32 | store, |
| 33 | value: Redacted.make("sk-list-test-value"), |
| 34 | }); |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | const provider = yield* Provider.findProvider(Cloudflare.SecretsStore.Secret); |
| 39 | const all = yield* provider.list(); |
| 40 | |
| 41 | expect(all.some((s) => s.secretId === deployed.secretId)).toBe(true); |
| 42 | const found = all.find((s) => s.secretId === deployed.secretId)!; |
| 43 | expect(found.secretName).toEqual(deployed.secretName); |
| 44 | expect(found.storeId).toEqual(deployed.storeId); |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | }).pipe(logLevel), |
| 48 | { tags: ["provider:cloudflare", "provider:cloudflare:secretsstore", "live"] }, |
| 49 | ); |
| 50 | |
| 51 | class UnexpectedSecretValue extends Data.TaggedError("UnexpectedSecretValue")<{ |
| 52 | status: number; |
| 53 | value: string | undefined; |
| 54 | }> {} |
| 55 | |
| 56 | // Reads the secret back through a deployed Worker binding (the Secrets Store |
| 57 | // API never returns values). Fresh workers.dev URLs and freshly patched |
| 58 | // secrets take a moment to propagate, so retry until the expected value shows. |
| 59 | const readSecretValue = (url: string, expected: string) => |
| 60 | Effect.gen(function* () { |
| 61 | const client = yield* HttpClient.HttpClient; |
| 62 | const res = yield* client.get(url); |
| 63 | const value = res.status === 200 ? ((yield* res.json) as { value: string }).value : undefined; |
| 64 | if (value !== expected) { |
| 65 | return yield* Effect.fail(new UnexpectedSecretValue({ status: res.status, value })); |
| 66 | } |
| 67 | return value; |
| 68 | }).pipe( |
| 69 | Effect.retry({ |
| 70 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.spaced("5 seconds")]), |
| 71 | times: 20, |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | // #2441: changing only `value` updates the secret in place — same secret id, |
| 76 | // and a Worker bound to it reads the new value. |
| 77 | test.provider( |
| 78 | "changing value updates the secret in place", |
| 79 | (stack) => |
| 80 | Effect.gen(function* () { |
| 81 | yield* stack.destroy(); |
| 82 | |
| 83 | const deploy = (value: string) => |
| 84 | stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | const store = yield* Cloudflare.SecretsStore.Store("UpdateSecretStore"); |
| 87 | const secret = yield* Cloudflare.SecretsStore.Secret("UpdateSecret", { |
| 88 | store, |
| 89 | value: Redacted.make(value), |
| 90 | }); |
| 91 | const worker = yield* Cloudflare.Worker("UpdateSecretWorker", { |
| 92 | main: new URL("./fixtures/value-worker.ts", import.meta.url).pathname, |
| 93 | workersDev: { enabled: true, previewsEnabled: false }, |
| 94 | env: { MY_SECRET: secret }, |
| 95 | }); |
| 96 | return { secret, worker }; |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | const v1 = yield* deploy("sk-update-v1"); |
| 101 | expect(yield* readSecretValue(v1.worker.url!, "sk-update-v1")).toBe("sk-update-v1"); |
| 102 | |
| 103 | const v2 = yield* deploy("sk-update-v2"); |
| 104 | expect(v2.secret.secretId).toBe(v1.secret.secretId); |
| 105 | expect(v2.secret.secretName).toBe(v1.secret.secretName); |
| 106 | expect(yield* readSecretValue(v2.worker.url!, "sk-update-v2")).toBe("sk-update-v2"); |
| 107 | |
| 108 | yield* stack.destroy(); |
| 109 | }).pipe(logLevel), |
| 110 | { timeout: 300_000, tags: ["provider:cloudflare", "provider:cloudflare:secretsstore", "live"] }, |
| 111 | ); |
| 112 | |
| 113 | // A Secret with no state of its own is found by name. The store's listing |
| 114 | // order is not stable across pages, so the lookup searches by name, ordered |
| 115 | // by name. Deploy more than one page of secrets (the API pages by 20) whose |
| 116 | // names contain the target's and sort before it, so the search itself spans |
| 117 | // two pages, then adopt the target by name under a second logical id, which |
| 118 | // sends `read` through the lookup. |
| 119 | test.provider( |
| 120 | "adopts a secret by name from a store with more than one page of secrets", |
| 121 | (stack) => |
| 122 | Effect.gen(function* () { |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | const deploy = (adoptTarget: boolean) => |
| 126 | stack.deploy( |
| 127 | Effect.gen(function* () { |
| 128 | const store = yield* Cloudflare.SecretsStore.Store("PagedSecretStore"); |
| 129 | yield* Effect.forEach( |
| 130 | Array.from({ length: 21 }, (_, i) => i), |
| 131 | (i) => |
| 132 | Cloudflare.SecretsStore.Secret(`PagedFiller${i}`, { |
| 133 | store, |
| 134 | name: `filler-${i}-paged-secret-target`, |
| 135 | value: Redacted.make("filler"), |
| 136 | }), |
| 137 | { concurrency: "unbounded", discard: true }, |
| 138 | ); |
| 139 | const target = yield* Cloudflare.SecretsStore.Secret("PagedTarget", { |
| 140 | store, |
| 141 | name: testName("paged-secret-target"), |
| 142 | value: Redacted.make("sk-paged"), |
| 143 | }); |
| 144 | const adopted = adoptTarget |
| 145 | ? yield* Cloudflare.SecretsStore.Secret("PagedTargetAdopted", { |
| 146 | store, |
| 147 | name: testName("paged-secret-target"), |
| 148 | value: Redacted.make("sk-paged"), |
| 149 | }).pipe(adopt(true)) |
| 150 | : undefined; |
| 151 | return { target, adopted }; |
| 152 | }), |
| 153 | ); |
| 154 | |
| 155 | const { target } = yield* deploy(false); |
| 156 | const { adopted } = yield* deploy(true); |
| 157 | expect(adopted?.secretId).toEqual(target.secretId); |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | }).pipe(logLevel), |
| 161 | { |
| 162 | tags: ["provider:cloudflare", "provider:cloudflare:secretsstore", "live"], |
| 163 | timeout: 180_000, |
| 164 | }, |
| 165 | ); |
| 166 | |
| 167 | // #995: an interrupted first deploy can persist `creating` props whose parent |
| 168 | // Outputs were stripped to holes, so `olds.store` survives as `{}`. `read` |
| 169 | // must treat an unresolved parent reference like a missing one (return |
| 170 | // `undefined` → plan falls through to the create path) instead of handing |
| 171 | // `storeId: undefined` to the API and crashing every later plan with a |
| 172 | // SchemaError defect. |
| 173 | test.provider( |
| 174 | "read treats an unresolved parent store reference as missing", |
| 175 | () => |
| 176 | Effect.gen(function* () { |
| 177 | const provider = yield* Provider.findProvider(Cloudflare.SecretsStore.Secret); |
| 178 | const attr = yield* provider.read!({ |
| 179 | id: "OrphanSecret", |
| 180 | fqn: "OrphanSecret", |
| 181 | instanceId: "orphan-instance", |
| 182 | olds: { store: {} as never, value: Redacted.make("sk-orphan") }, |
| 183 | output: undefined, |
| 184 | }); |
| 185 | expect(attr).toBeUndefined(); |
| 186 | }).pipe(logLevel), |
| 187 | { tags: ["provider:cloudflare", "provider:cloudflare:secretsstore", "live"] }, |
| 188 | ); |