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, it } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import { Credentials, apiTokenCredentials } from "@distilled.cloud/cloudflare/Credentials"; |
| 5 | import * as secretsStore from "@distilled.cloud/cloudflare/secrets-store"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpBody from "effect/http/HttpBody"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Redacted from "effect/Redacted"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 15 | |
| 16 | /** |
| 17 | * Test harness that captures the outbound `HttpClientRequest` and lets |
| 18 | * the caller plug in a canned `Response`. The motivating regression |
| 19 | * (Cloudflare's `secrets_store/stores` POST receiving an array body |
| 20 | * and rejecting it with `invalid_json_body`) was invisible to the type |
| 21 | * system because the upstream SDK schema modeled the body as |
| 22 | * `{ name }[]`. The fix is to upgrade `@distilled.cloud/cloudflare` to |
| 23 | * a release with the corrected single-object schema; these tests pin |
| 24 | * the wire format so a future SDK regression is caught immediately. |
| 25 | */ |
| 26 | interface Captured { |
| 27 | url: string; |
| 28 | method: string; |
| 29 | contentType: string | undefined; |
| 30 | bodyJson: unknown; |
| 31 | authorization: string | undefined; |
| 32 | } |
| 33 | |
| 34 | const harness = (response: Response) => { |
| 35 | let captured: Captured | undefined; |
| 36 | const client = HttpClient.make((request) => |
| 37 | Effect.sync(() => { |
| 38 | const body = request.body as HttpBody.HttpBody; |
| 39 | const bodyText = body._tag === "Uint8Array" ? new TextDecoder().decode(body.body) : ""; |
| 40 | captured = { |
| 41 | url: request.url, |
| 42 | method: request.method, |
| 43 | contentType: body._tag === "Uint8Array" ? body.contentType : undefined, |
| 44 | bodyJson: bodyText ? JSON.parse(bodyText) : undefined, |
| 45 | authorization: request.headers.authorization, |
| 46 | }; |
| 47 | return HttpClientResponse.fromWeb(request, response); |
| 48 | }), |
| 49 | ); |
| 50 | const layer = Layer.mergeAll( |
| 51 | Layer.succeed(HttpClient.HttpClient, client), |
| 52 | Layer.succeed( |
| 53 | Credentials, |
| 54 | Effect.succeed(apiTokenCredentials({ apiToken: Redacted.make("test-token") })), |
| 55 | ), |
| 56 | ); |
| 57 | return { layer, get: () => captured! }; |
| 58 | }; |
| 59 | |
| 60 | const successResponse = () => |
| 61 | new Response( |
| 62 | JSON.stringify({ |
| 63 | success: true, |
| 64 | errors: [], |
| 65 | messages: [], |
| 66 | result: { |
| 67 | id: "store-id-123", |
| 68 | name: "default_secrets_store", |
| 69 | created: "2026-01-01T00:00:00Z", |
| 70 | modified: "2026-01-01T00:00:00Z", |
| 71 | }, |
| 72 | }), |
| 73 | { status: 200, headers: { "content-type": "application/json" } }, |
| 74 | ); |
| 75 | |
| 76 | const errorResponse = (status: number, errors: Array<{ code: number; message: string }>) => |
| 77 | new Response(JSON.stringify({ success: false, errors, messages: [], result: null }), { |
| 78 | status, |
| 79 | headers: { "content-type": "application/json" }, |
| 80 | }); |
| 81 | |
| 82 | it.live( |
| 83 | "createStore POSTs a single JSON object body (regression: invalid_json_body)", |
| 84 | () => |
| 85 | Effect.gen(function* () { |
| 86 | const { layer, get } = harness(successResponse()); |
| 87 | |
| 88 | const result = yield* Effect.gen(function* () { |
| 89 | const create = yield* secretsStore.createStore; |
| 90 | return yield* create({ accountId: "acct-abc", name: "default_secrets_store" }); |
| 91 | }).pipe(Effect.provide(layer)); |
| 92 | |
| 93 | expect(result.id).toBe("store-id-123"); |
| 94 | expect(result.name).toBe("default_secrets_store"); |
| 95 | |
| 96 | const sent = get(); |
| 97 | expect(sent.method).toBe("POST"); |
| 98 | expect(sent.url).toBe( |
| 99 | "https://api.cloudflare.com/client/v4/accounts/acct-abc/secrets_store/stores", |
| 100 | ); |
| 101 | expect(sent.contentType).toMatch(/application\/json/); |
| 102 | expect(sent.authorization).toBe("Bearer test-token"); |
| 103 | |
| 104 | // Cloudflare's REST API expects `{"name": "..."}` and rejects |
| 105 | // `[{"name": "..."}]` with code 1001 `invalid_json_body`. |
| 106 | // Earlier `@distilled.cloud/cloudflare` releases sent the array |
| 107 | // shape; this test pins the corrected single-object body. |
| 108 | expect(sent.bodyJson).toEqual({ name: "default_secrets_store" }); |
| 109 | expect(Array.isArray(sent.bodyJson)).toBe(false); |
| 110 | }), |
| 111 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:secretsstore", "local"] }, |
| 112 | ); |
| 113 | |
| 114 | it.live( |
| 115 | "createStore surfaces MaximumStoresExceeded as a tagged error so callers can catchTag it", |
| 116 | () => |
| 117 | Effect.gen(function* () { |
| 118 | const { layer } = harness( |
| 119 | errorResponse(409, [{ code: 1003, message: "maximum_stores_exceeded" }]), |
| 120 | ); |
| 121 | |
| 122 | const result = yield* Effect.gen(function* () { |
| 123 | const create = yield* secretsStore.createStore; |
| 124 | return yield* create({ accountId: "acct-abc", name: "x" }).pipe( |
| 125 | Effect.catchTag("MaximumStoresExceeded", (e) => Effect.succeed(e)), |
| 126 | ); |
| 127 | }).pipe(Effect.provide(layer)); |
| 128 | |
| 129 | // Assert on the tag, not `instanceof`: distilled errors are defined as |
| 130 | // `class X extends applyErrorMatchers(Schema.TaggedError(...))`, so |
| 131 | // the decoder constructs instances of the inner schema class — they |
| 132 | // carry the right `_tag` but are not `instanceof` the exported subclass. |
| 133 | const err = result as secretsStore.MaximumStoresExceeded; |
| 134 | expect(err._tag).toBe("MaximumStoresExceeded"); |
| 135 | expect(err.code).toBe(1003); |
| 136 | expect(err.message).toBe("maximum_stores_exceeded"); |
| 137 | }), |
| 138 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:secretsstore", "local"] }, |
| 139 | ); |
| 140 | |
| 141 | // Canonical `list()` test (account-scoped collection). Deploy/adopt the |
| 142 | // account's Secrets Store, then enumerate via the provider's `list()` and |
| 143 | // assert the deployed store's id is present in the exhaustively-paginated |
| 144 | // result. Bracketed with `stack.destroy()` at start and end; note the |
| 145 | // SecretsStore provider's `delete` is an intentional no-op (account-level |
| 146 | // infra), so destroy never tears down the shared store. |
| 147 | test.provider( |
| 148 | "list enumerates the deployed secrets store", |
| 149 | (stack) => |
| 150 | Effect.gen(function* () { |
| 151 | yield* stack.destroy(); |
| 152 | |
| 153 | const deployed = yield* stack.deploy( |
| 154 | Effect.gen(function* () { |
| 155 | return yield* Cloudflare.SecretsStore.Store("ListStore"); |
| 156 | }), |
| 157 | ); |
| 158 | |
| 159 | const provider = yield* Provider.findProvider(Cloudflare.SecretsStore.Store); |
| 160 | const all = yield* provider.list(); |
| 161 | |
| 162 | expect(all.length).toBeGreaterThan(0); |
| 163 | expect(all.some((s) => s.storeId === deployed.storeId)).toBe(true); |
| 164 | |
| 165 | yield* stack.destroy(); |
| 166 | }), |
| 167 | { tags: ["provider:cloudflare", "provider:cloudflare:secretsstore", "live"] }, |
| 168 | ); |