The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Fiber from "effect/Fiber"; |
| 4 | import * as HttpApiError from "effect/http-api/HttpApiError"; |
| 5 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 6 | import * as HttpClientError from "effect/http/HttpClientError"; |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Logger from "effect/Logger"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import * as TestClock from "effect/testing/TestClock"; |
| 13 | import { |
| 14 | checkHttpStateStoreAuth, |
| 15 | describeStateStoreFailure, |
| 16 | makeHttpStateStore, |
| 17 | } from "@/State/HttpStateStore.ts"; |
| 18 | |
| 19 | /** |
| 20 | * Hermetic tests driven by the production failure modes observed in |
| 21 | * Axiom (`prod-traces`, spans `state_store.*`): |
| 22 | * |
| 23 | * - `StateStoreError` with an EMPTY message (31 hits / 14 users on |
| 24 | * beta.57–59) — thrown from `mapStateStoreError` when the client |
| 25 | * error carried no message (e.g. the no-content 401). |
| 26 | * - opaque `Decode error (500 PUT <url>)` — worker 5xx bodies that |
| 27 | * fail response decoding. |
| 28 | * - `Transport error (GET .../state/stacks)` / `fetch failed` — not |
| 29 | * retried by `checkHttpStateStoreAuth`. |
| 30 | * |
| 31 | * All HTTP traffic is stubbed through the `FetchHttpClient.Fetch` |
| 32 | * reference — no sockets, no cloud. |
| 33 | */ |
| 34 | |
| 35 | /** Minimal fetch signature — Bun's `typeof fetch` also demands `preconnect`. */ |
| 36 | type FetchStub = (input: string | URL | Request, init?: RequestInit) => Promise<Response>; |
| 37 | |
| 38 | /** Build an HttpClient layer whose transport is the given fetch stub. */ |
| 39 | const stubHttpClient = (stub: FetchStub) => |
| 40 | FetchHttpClient.layer.pipe( |
| 41 | Layer.provide(Layer.succeed(FetchHttpClient.Fetch, stub as typeof globalThis.fetch)), |
| 42 | ); |
| 43 | |
| 44 | const json = (body: unknown, status = 200) => |
| 45 | new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); |
| 46 | |
| 47 | const sampleValue = { fqn: "stack/scope/a", props: { hello: "world" } }; |
| 48 | |
| 49 | const makeStore = makeHttpStateStore({ |
| 50 | url: "https://state-store.test", |
| 51 | authToken: "token", |
| 52 | id: "test-http", |
| 53 | }); |
| 54 | |
| 55 | describe("describeStateStoreFailure", { tags: ["unit", "local"] }, () => { |
| 56 | it("never returns an empty message", () => { |
| 57 | class Empty extends Error { |
| 58 | readonly _tag = "SomeTaggedError"; |
| 59 | constructor() { |
| 60 | super(""); |
| 61 | } |
| 62 | } |
| 63 | const message = describeStateStoreFailure(new Empty()); |
| 64 | expect(message.length).toBeGreaterThan(0); |
| 65 | }); |
| 66 | |
| 67 | it("maps Unauthorized errors to an actionable message", () => { |
| 68 | const message = describeStateStoreFailure(new HttpApiError.Unauthorized()); |
| 69 | expect(message).toContain("unauthorized"); |
| 70 | expect(message).toContain("alchemy profile edit"); |
| 71 | }); |
| 72 | |
| 73 | it("appends the HTTP status when the error carries a response", () => { |
| 74 | const request = HttpClientRequest.put("https://state-store.test"); |
| 75 | const response = HttpClientResponse.fromWeb(request, new Response(null, { status: 500 })); |
| 76 | const error = new HttpClientError.HttpClientError({ |
| 77 | reason: new HttpClientError.StatusCodeError({ request, response }), |
| 78 | }); |
| 79 | expect(describeStateStoreFailure(error)).toContain("500"); |
| 80 | }); |
| 81 | |
| 82 | it("omits arbitrary error and cause messages", () => { |
| 83 | const cause = new Error("secret cause"); |
| 84 | const outer = new Error("secret message"); |
| 85 | outer.cause = cause; |
| 86 | const message = describeStateStoreFailure(outer); |
| 87 | expect(message).not.toContain("secret"); |
| 88 | expect(message.length).toBeGreaterThan(0); |
| 89 | }); |
| 90 | |
| 91 | it("omits non-Error failures that may contain state", () => { |
| 92 | expect(describeStateStoreFailure("secret state")).not.toContain("secret"); |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | describe("makeHttpStateStore", { tags: ["unit", "local"] }, () => { |
| 97 | // Regression: https://github.com/reve-ai/kommunikasie/commit/f2e7320ff261833092b84d8aa25e3563710661e8 |
| 98 | // State encoding unwraps Redacted values before HTTP. Neither the request |
| 99 | // object nor transport/decoder messages may escape through diagnostics. |
| 100 | for (const failure of ["status", "decode", "transport"] as const) { |
| 101 | it.effect(`does not expose serialized state after a ${failure} failure`, () => { |
| 102 | const secret = "STATE_PAYLOAD_SENTINEL_91f4"; |
| 103 | const logs: string[] = []; |
| 104 | let requestBody = ""; |
| 105 | const logger = Logger.make(({ cause, message }) => { |
| 106 | logs.push(JSON.stringify({ cause, message })); |
| 107 | }); |
| 108 | const stub: FetchStub = async (_input, init) => { |
| 109 | requestBody = await new Response(init?.body).text(); |
| 110 | if (failure === "transport") throw new Error(secret); |
| 111 | return new Response(secret, { |
| 112 | status: failure === "status" ? 400 : 200, |
| 113 | headers: { "content-type": "application/json" }, |
| 114 | }); |
| 115 | }; |
| 116 | return Effect.gen(function* () { |
| 117 | const store = yield* makeStore; |
| 118 | const fiber = yield* store |
| 119 | .set({ |
| 120 | stack: "s", |
| 121 | stage: "dev", |
| 122 | fqn: "secret", |
| 123 | value: { |
| 124 | kind: "action", |
| 125 | actionType: "SecretState", |
| 126 | namespace: undefined, |
| 127 | fqn: "secret", |
| 128 | logicalId: "secret", |
| 129 | status: "ran", |
| 130 | downstream: [], |
| 131 | inputHash: "input-hash", |
| 132 | input: { token: Redacted.make(secret) }, |
| 133 | output: null, |
| 134 | }, |
| 135 | }) |
| 136 | .pipe(Effect.flip, Effect.forkChild); |
| 137 | yield* TestClock.adjust("30 seconds"); |
| 138 | const error = yield* Fiber.join(fiber); |
| 139 | expect(requestBody).toContain(secret); |
| 140 | expect(JSON.stringify({ error, logs })).not.toContain(secret); |
| 141 | expect(error.cause).toBeUndefined(); |
| 142 | expect(error.message).not.toContain(secret); |
| 143 | expect(error.message.length).toBeGreaterThan(0); |
| 144 | }).pipe(Effect.provide(stubHttpClient(stub)), Effect.provide(Logger.layer([logger]))); |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | it.live("retries transient 5xx and then succeeds", () => { |
| 149 | let calls = 0; |
| 150 | const stub: FetchStub = async () => { |
| 151 | calls++; |
| 152 | return calls < 3 ? new Response("internal error", { status: 500 }) : json(sampleValue); |
| 153 | }; |
| 154 | return Effect.gen(function* () { |
| 155 | const store = yield* makeStore; |
| 156 | const result = yield* store.set({ |
| 157 | stack: "s", |
| 158 | stage: "dev", |
| 159 | fqn: "stack/scope/a", |
| 160 | value: sampleValue as never, |
| 161 | }); |
| 162 | expect(result).toEqual(sampleValue); |
| 163 | expect(calls).toBe(3); |
| 164 | }).pipe(Effect.provide(stubHttpClient(stub))); |
| 165 | }); |
| 166 | |
| 167 | it.live( |
| 168 | "surfaces a persistent 5xx as a StateStoreError with status context", |
| 169 | () => { |
| 170 | let calls = 0; |
| 171 | const stub: FetchStub = async () => { |
| 172 | calls++; |
| 173 | return new Response("secrets store binding unavailable", { status: 500 }); |
| 174 | }; |
| 175 | return Effect.gen(function* () { |
| 176 | const store = yield* makeStore; |
| 177 | const error = yield* store |
| 178 | .set({ stack: "s", stage: "dev", fqn: "stack/scope/a", value: sampleValue as never }) |
| 179 | .pipe(Effect.flip); |
| 180 | expect(error._tag).toBe("StateStoreError"); |
| 181 | expect(error.message.trim().length).toBeGreaterThan(0); |
| 182 | expect(error.message).toContain("500"); |
| 183 | // initial attempt + 5 bounded retries |
| 184 | expect(calls).toBe(6); |
| 185 | }).pipe(Effect.provide(stubHttpClient(stub))); |
| 186 | }, |
| 187 | 30_000, |
| 188 | ); |
| 189 | |
| 190 | // #1833: a failure behind the auth middleware (a Durable Object call, a |
| 191 | // Secrets Store read) used to encode as its `Unauthorized` and surface as an |
| 192 | // empty 500. The store now answers a typed StateStoreFailure. |
| 193 | it.live( |
| 194 | "retries a StateStoreFailure, then names the failed operation", |
| 195 | () => { |
| 196 | let calls = 0; |
| 197 | const stub: FetchStub = async () => { |
| 198 | calls++; |
| 199 | return json( |
| 200 | { _tag: "StateStoreFailure", operation: "getState", reason: "RpcCallError" }, |
| 201 | 500, |
| 202 | ); |
| 203 | }; |
| 204 | return Effect.gen(function* () { |
| 205 | const store = yield* makeStore; |
| 206 | const error = yield* store |
| 207 | .get({ stack: "s", stage: "dev", fqn: "stack/scope/a" }) |
| 208 | .pipe(Effect.flip); |
| 209 | expect(error.message).toBe( |
| 210 | "State store failed to getState (RpcCallError); its logs have the cause.", |
| 211 | ); |
| 212 | expect(error.http).toEqual({ status: 500 }); |
| 213 | // initial attempt + 5 bounded retries |
| 214 | expect(calls).toBe(6); |
| 215 | }).pipe(Effect.provide(stubHttpClient(stub))); |
| 216 | }, |
| 217 | 30_000, |
| 218 | ); |
| 219 | |
| 220 | it.live("the credential check retries a StateStoreFailure", () => { |
| 221 | let calls = 0; |
| 222 | const stub: FetchStub = async () => { |
| 223 | calls++; |
| 224 | return calls < 3 |
| 225 | ? json({ _tag: "StateStoreFailure", operation: "listStacks", reason: "RpcCallError" }, 500) |
| 226 | : json([]); |
| 227 | }; |
| 228 | return checkHttpStateStoreAuth({ url: "https://state-store.test", authToken: "token" }).pipe( |
| 229 | Effect.map((authorized) => { |
| 230 | expect(authorized).toBe(true); |
| 231 | expect(calls).toBe(3); |
| 232 | }), |
| 233 | Effect.provide(stubHttpClient(stub)), |
| 234 | ); |
| 235 | }); |
| 236 | |
| 237 | it.live("maps a no-content 401 to a non-empty, actionable error", () => { |
| 238 | const stub: FetchStub = async () => new Response(null, { status: 401 }); |
| 239 | return Effect.gen(function* () { |
| 240 | const store = yield* makeStore; |
| 241 | const error = yield* store.listStacks().pipe(Effect.flip); |
| 242 | expect(error._tag).toBe("StateStoreError"); |
| 243 | expect(error.message.trim().length).toBeGreaterThan(0); |
| 244 | expect(error.message).toContain("unauthorized"); |
| 245 | expect(error.message).toContain("alchemy profile edit"); |
| 246 | }).pipe(Effect.provide(stubHttpClient(stub))); |
| 247 | }); |
| 248 | |
| 249 | it.live( |
| 250 | "maps a non-JSON 200 body to a non-empty StateStoreError instead of a bare SyntaxError", |
| 251 | () => { |
| 252 | const stub: FetchStub = async () => |
| 253 | new Response("Alchemy State Store", { |
| 254 | status: 200, |
| 255 | headers: { "content-type": "application/json" }, |
| 256 | }); |
| 257 | return Effect.gen(function* () { |
| 258 | const store = yield* makeStore; |
| 259 | const error = yield* store.listStacks().pipe(Effect.flip); |
| 260 | expect(error._tag).toBe("StateStoreError"); |
| 261 | expect(error.message.trim().length).toBeGreaterThan(0); |
| 262 | }).pipe(Effect.provide(stubHttpClient(stub))); |
| 263 | }, |
| 264 | ); |
| 265 | }); |
| 266 | |
| 267 | describe("checkHttpStateStoreAuth", { tags: ["unit", "local"] }, () => { |
| 268 | const check = checkHttpStateStoreAuth({ url: "https://state-store.test", authToken: "token" }); |
| 269 | |
| 270 | it.live("retries transport-level failures before succeeding", () => { |
| 271 | let calls = 0; |
| 272 | const stub: FetchStub = async () => { |
| 273 | calls++; |
| 274 | if (calls < 3) throw new TypeError("fetch failed"); |
| 275 | return json([]); |
| 276 | }; |
| 277 | return Effect.gen(function* () { |
| 278 | const isAuthed = yield* check; |
| 279 | expect(isAuthed).toBe(true); |
| 280 | expect(calls).toBe(3); |
| 281 | }).pipe(Effect.provide(stubHttpClient(stub))); |
| 282 | }); |
| 283 | |
| 284 | it.live("returns false on 401 without retrying", () => { |
| 285 | let calls = 0; |
| 286 | const stub: FetchStub = async () => { |
| 287 | calls++; |
| 288 | return new Response(null, { status: 401 }); |
| 289 | }; |
| 290 | return Effect.gen(function* () { |
| 291 | const isAuthed = yield* check; |
| 292 | expect(isAuthed).toBe(false); |
| 293 | expect(calls).toBe(1); |
| 294 | }).pipe(Effect.provide(stubHttpClient(stub))); |
| 295 | }); |
| 296 | }); |