t3-code-android-nightly/.repos/alchemy-effect/packages/alchemy/test/Cloudflare/Auth/AccountId.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

86 lines
2.8 KiB
TypeScript

import { validateAccountId } from "@/Cloudflare/Auth/AuthConfig.ts";
import { describe, expect, it } from "alchemy-test";
import * as Effect from "effect/Effect";
/**
* Production traces showed 242 distinct users hitting
* `InvalidRoute: Could not route to /accounts/workers/scripts/...`
* because an empty account ID was interpolated into the API path, plus
* placeholder values ("-", "dummy", "test", "mock") producing the same
* class of failure. `validateAccountId` turns all of those into a
* typed `AuthError` with an actionable message before any API call.
*/
describe(
"validateAccountId",
{
tags: ["unit", "provider:cloudflare", "provider:cloudflare:auth", "local"],
},
() => {
const valid = "2b29022c8dbd18353220a2f637eeba33";
it.effect("accepts a valid 32-hex account ID", () =>
Effect.gen(function* () {
const id = yield* validateAccountId(valid, "test");
expect(id).toBe(valid);
}),
);
it.effect("trims whitespace and lowercases", () =>
Effect.gen(function* () {
const id = yield* validateAccountId(
` ${valid.toUpperCase()} `,
"test",
);
expect(id).toBe(valid);
}),
);
it.effect("rejects undefined with an actionable message", () =>
Effect.gen(function* () {
const error = yield* validateAccountId(undefined, "test").pipe(
Effect.flip,
);
expect(error._tag).toBe("AuthError");
expect(error.message).toContain("missing");
expect(error.message).toContain("--reconfigure Cloudflare");
}),
);
it.effect("rejects the empty string", () =>
Effect.gen(function* () {
const error = yield* validateAccountId("", "test").pipe(Effect.flip);
expect(error.message).toContain("missing");
}),
);
it.effect("rejects whitespace-only values", () =>
Effect.gen(function* () {
const error = yield* validateAccountId(" ", "test").pipe(Effect.flip);
expect(error.message).toContain("missing");
}),
);
for (const placeholder of ["-", "dummy", "test", "mock", "abc123"]) {
it.effect(`rejects the placeholder value '${placeholder}'`, () =>
Effect.gen(function* () {
const error = yield* validateAccountId(placeholder, "test").pipe(
Effect.flip,
);
expect(error._tag).toBe("AuthError");
expect(error.message).toContain(placeholder);
expect(error.message).toContain("32 hex characters");
}),
);
}
it.effect("names the source of the bad value in the message", () =>
Effect.gen(function* () {
const error = yield* validateAccountId(
"dummy",
"stored for profile 'ci'",
).pipe(Effect.flip);
expect(error.message).toContain("stored for profile 'ci'");
}),
);
},
);