Files
clean-architecture-frontend…/tests/unit/error-classification.test.ts
DongHyeonkaandClaude Opus 5.5 aff5f43652 fix(contracts): drop unread error registry fields and pin the createFailure effect rule (07-04)
Nothing read severity, telemetryEvent or redaction, yet FE-REG-ERROR guarded
them as policy. They are removed and the registry baseline is re-approved.
A new test fails if createFailure stops clearing retryable for MAYBE_APPLIED (M1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 00:16:55 +09:00

128 lines
4.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
ERROR_REGISTRY,
createFailure,
kindForStatus,
normalizeUnknownFailure,
withFailureEffect,
} from "../../src/contracts/errors.ts";
describe("frontend failure classification", () => {
it("defines all stable error kinds with the four contract fields", () => {
expect(Object.keys(ERROR_REGISTRY)).toHaveLength(38);
for (const definition of Object.values(ERROR_REGISTRY)) {
expect(Object.keys(definition).sort()).toEqual([
"action",
"defaultRetryable",
"kind",
"userMessageKey",
]);
}
});
it.each([
[401, "AUTH_REQUIRED"],
[403, "FORBIDDEN"],
[404, "NOT_FOUND"],
[409, "CONFLICT"],
[422, "VALIDATION_REJECTED"],
[418, "UNKNOWN_CLIENT_FAILURE"],
[429, "RATE_LIMITED"],
[503, "SERVER_FAILURE"],
])("maps HTTP %i to %s", (status, kind) => {
expect(kindForStatus(status)).toBe(kind);
});
it("keeps re-login first for an auth failure whose effect is unknown (D-2)", () => {
expect(
createFailure("AUTH_REQUIRED", "CREATE", 0, { effect: "MAYBE_APPLIED" }),
).toMatchObject({ action: "reauth", retryable: false });
expect(
withFailureEffect(createFailure("AUTH_REQUIRED", "CREATE", 0), "MAYBE_APPLIED"),
).toMatchObject({ action: "reauth", retryable: false });
// Every other kind still sends an unknown effect to support.
expect(
createFailure("FORBIDDEN", "CREATE", 0, { effect: "MAYBE_APPLIED" }),
).toMatchObject({ action: "contact-support" });
});
it("never marks a failure retryable once the write may or did take effect (07-04)", () => {
expect(ERROR_REGISTRY.SERVER_FAILURE.defaultRetryable).toBe(true);
expect(createFailure("SERVER_FAILURE", "CREATE", 0).retryable).toBe(true);
expect(
createFailure("SERVER_FAILURE", "CREATE", 0, { effect: "MAYBE_APPLIED" }),
).toMatchObject({ retryable: false, action: "contact-support" });
expect(
createFailure("SERVER_FAILURE", "CREATE", 0, {
effect: "APPLIED_CONFIRMED",
}),
).toMatchObject({ retryable: false, action: "none" });
expect(
createFailure("SERVER_FAILURE", "CREATE", 0, { effect: "NOT_APPLIED" })
.retryable,
).toBe(true);
});
it("projects only allowlisted safe fields", () => {
const untrustedDetails = {
code: "TEMPORARY",
httpStatus: 503,
requestId: "request-1",
stack: "must not leak",
body: "must not leak",
authorization: "Bearer secret",
};
const result = createFailure(
"SERVER_FAILURE",
"LIST_ENTITIES",
0,
untrustedDetails,
);
expect(result).toMatchObject({
kind: "SERVER_FAILURE",
code: "TEMPORARY",
httpStatus: 503,
requestId: "request-1",
});
expect(JSON.stringify(result)).not.toMatch(/stack|body|Bearer|secret/);
});
it("bounds validation metadata even for direct failure callers", () => {
const oversizedPath = "p".repeat(121);
const oversizedCode = "C".repeat(81);
const validationIssues = [
{ path: oversizedPath, code: "TOO_LONG_PATH" },
{ path: "field", code: oversizedCode },
...Array.from({ length: 100 }, (_, index) => ({
path: `field.${index}`,
code: "INVALID",
})),
];
const result = createFailure("VALIDATION_REJECTED", "SAVE_ENTITY", 0, {
validationIssues,
});
expect(result.validationIssues).toHaveLength(50);
expect(result.validationIssues?.[0]).toEqual({
path: "field.0",
code: "INVALID",
});
expect(JSON.stringify(result.validationIssues)).not.toContain(oversizedPath);
expect(JSON.stringify(result.validationIssues)).not.toContain(oversizedCode);
});
it("normalizes any thrown value without leaking it", () => {
const secret = { token: "sensitive", nested: { rawBody: "private" } };
const result = normalizeUnknownFailure(secret);
expect(result).toMatchObject({
kind: "UNKNOWN_FAILURE",
causeClass: "object",
});
expect(JSON.stringify(result)).not.toMatch(/sensitive|private|token|rawBody/);
});
});