115 lines
3.5 KiB
TypeScript
115 lines
3.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
ERROR_REGISTRY,
|
|
createFailure,
|
|
kindForStatus,
|
|
normalizeUnknownFailure,
|
|
} 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("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/);
|
|
});
|
|
});
|