The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { validateAccountId } from "@alchemy.run/cloudflare/Auth/AuthConfig"; |
| 2 | import { describe, expect, it } from "@alchemy.run/test"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | /** |
| 6 | * Production traces showed 242 distinct users hitting |
| 7 | * `InvalidRoute: Could not route to /accounts/workers/scripts/...` |
| 8 | * because an empty account ID was interpolated into the API path, plus |
| 9 | * placeholder values ("-", "dummy", "test", "mock") producing the same |
| 10 | * class of failure. `validateAccountId` turns all of those into a |
| 11 | * typed `AuthError` with an actionable message before any API call. |
| 12 | */ |
| 13 | describe( |
| 14 | "validateAccountId", |
| 15 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:auth", "local"] }, |
| 16 | () => { |
| 17 | const valid = "2b29022c8dbd18353220a2f637eeba33"; |
| 18 | |
| 19 | it.effect("accepts a valid 32-hex account ID", () => |
| 20 | Effect.gen(function* () { |
| 21 | const id = yield* validateAccountId(valid, "test"); |
| 22 | expect(id).toBe(valid); |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | it.effect("trims whitespace and lowercases", () => |
| 27 | Effect.gen(function* () { |
| 28 | const id = yield* validateAccountId(` ${valid.toUpperCase()} `, "test"); |
| 29 | expect(id).toBe(valid); |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | it.effect("rejects undefined with an actionable message", () => |
| 34 | Effect.gen(function* () { |
| 35 | const error = yield* validateAccountId(undefined, "test").pipe(Effect.flip); |
| 36 | expect(error._tag).toBe("AuthError"); |
| 37 | expect(error.message).toContain("missing"); |
| 38 | expect(error.message).toContain("--reconfigure Cloudflare"); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | it.effect("rejects the empty string", () => |
| 43 | Effect.gen(function* () { |
| 44 | const error = yield* validateAccountId("", "test").pipe(Effect.flip); |
| 45 | expect(error.message).toContain("missing"); |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | it.effect("rejects whitespace-only values", () => |
| 50 | Effect.gen(function* () { |
| 51 | const error = yield* validateAccountId(" ", "test").pipe(Effect.flip); |
| 52 | expect(error.message).toContain("missing"); |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | for (const placeholder of ["-", "dummy", "test", "mock", "abc123"]) { |
| 57 | it.effect(`rejects the placeholder value '${placeholder}'`, () => |
| 58 | Effect.gen(function* () { |
| 59 | const error = yield* validateAccountId(placeholder, "test").pipe(Effect.flip); |
| 60 | expect(error._tag).toBe("AuthError"); |
| 61 | expect(error.message).toContain(placeholder); |
| 62 | expect(error.message).toContain("32 hex characters"); |
| 63 | }), |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | it.effect("names the source of the bad value in the message", () => |
| 68 | Effect.gen(function* () { |
| 69 | const error = yield* validateAccountId("dummy", "stored for profile 'ci'").pipe( |
| 70 | Effect.flip, |
| 71 | ); |
| 72 | expect(error.message).toContain("stored for profile 'ci'"); |
| 73 | }), |
| 74 | ); |
| 75 | }, |
| 76 | ); |