Files
clean-architecture-frontend…/tests/unit/error-classification.test.ts
T
DongHyeonkaandClaude Opus 5.5 2afee67150 fix(http): let the package classify command 401/403/429 and keep re-login first (03-02)
These statuses skipped the package classifier and always became
MAYBE_APPLIED, so an expired session ended in "contact support" or the
unknown-result screen. The descriptor gains optional classifyStatus, the
reference create declares NOT_APPLIED, and AUTH_REQUIRED keeps reauth.

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

129 lines
4.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
ERROR_REGISTRY,
createFailure,
kindForStatus,
normalizeUnknownFailure,
withFailureEffect,
} from "../../src/contracts/errors.ts";
import { DIAGNOSTIC_EVENT_REGISTRY } from "../../src/contracts/diagnostics.ts";
import { TELEMETRY_REGISTRY } from "../../src/contracts/telemetry.ts";
describe("frontend failure classification", () => {
it("defines all stable error kinds with the seven contract fields", () => {
expect(Object.keys(ERROR_REGISTRY)).toHaveLength(38);
for (const definition of Object.values(ERROR_REGISTRY)) {
expect(definition).toEqual(
expect.objectContaining({
kind: expect.any(String),
defaultRetryable: expect.any(Boolean),
severity: expect.any(String),
userMessageKey: expect.any(String),
action: expect.any(String),
telemetryEvent: expect.any(String),
redaction: expect.any(Array),
}),
);
}
});
it("references only registered observability events", () => {
const registeredEvents = new Set([
...Object.keys(DIAGNOSTIC_EVENT_REGISTRY),
...Object.keys(TELEMETRY_REGISTRY),
]);
for (const definition of Object.values(ERROR_REGISTRY)) {
expect(registeredEvents.has(definition.telemetryEvent)).toBe(true);
}
});
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("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/);
});
});