Files
clean-architecture-frontend…/tests/unit/diagnostics.test.ts

173 lines
5.0 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import {
createDiagnosticsAdapter,
getLastBootEvidence,
noOpDiagnostics,
recordBootFailure,
} from "../../src/adapters/diagnostics/bounded-diagnostics.ts";
import {
projectDiagnosticRecord,
safeErrorKind,
} from "../../src/contracts/diagnostics.ts";
describe("structured diagnostics contract", () => {
it("projects only registered, bounded context with deterministic time", () => {
const projected = projectDiagnosticRecord(
{
level: "info",
eventId: "route.changed",
context: { route_id: "APP_HOME", build_id: "build-a" },
},
() => 0,
);
expect(projected).toEqual({
success: true,
record: {
level: "info",
eventId: "route.changed",
timestamp: "1970-01-01T00:00:00.000Z",
context: { route_id: "APP_HOME", build_id: "build-a" },
},
});
expect(
projectDiagnosticRecord({
level: "error",
eventId: "ui.render.failed",
context: { raw_url: "https://example.test/private?token=secret" },
}),
).toEqual({ success: false, reason: "unknown-context" });
expect(
projectDiagnosticRecord({
level: "error",
eventId: "ui.render.failed",
context: { route_id: "private value with whitespace" },
}),
).toEqual({ success: false, reason: "invalid-context" });
});
it("bounds evidence and isolates throwing sinks and hostile error objects", () => {
const sink = vi.fn(() => {
throw new Error("sink-secret");
});
const adapter = createDiagnosticsAdapter({
maxEntries: 1,
now: () => 0,
sink,
});
const circular: Record<string, unknown> = { name: "TypeError" };
circular.self = circular;
const hostile = new Proxy(
{},
{
get() {
throw new Error("private getter");
},
ownKeys() {
throw new Error("private keys");
},
},
);
expect(safeErrorKind(circular)).toBe("TYPE_ERROR");
expect(safeErrorKind(hostile)).toBe("UNKNOWN_FAILURE");
expect(
projectDiagnosticRecord({
level: "error",
eventId: "ui.render.failed",
context: hostile,
}),
).toEqual({ success: false, reason: "serialization-failure" });
expect(() =>
adapter.record({
level: "info",
eventId: "route.changed",
context: { route_id: "APP_HOME" },
}),
).not.toThrow();
adapter.record({
level: "warn",
eventId: "cache.operation.failed",
context: { operation: "query", error_kind: "UNKNOWN_FAILURE" },
});
expect(adapter.entries()).toHaveLength(1);
expect(adapter.entries()[0]?.eventId).toBe("cache.operation.failed");
expect(adapter.dropped()).toEqual({
"queue-full": 1,
"sink-failure": 2,
});
expect(JSON.stringify(adapter.entries())).not.toMatch(
/sink-secret|private getter/,
);
});
it("creates safe pre-mount boot evidence and supports a true no-op", () => {
const circular: Record<string, unknown> = {
name: "TypeError",
token: "credential-value",
stack: "private-stack",
};
circular.self = circular;
expect(() =>
recordBootFailure(
circular,
{
buildId: "build-a",
configSchemaVersion: "1",
supportReference: "support-private",
},
() => 0,
),
).not.toThrow();
const evidence = getLastBootEvidence();
expect(evidence?.diagnostic?.eventId).toBe("app.boot.failed");
expect(evidence?.telemetry).toMatchObject({
eventName: "app.boot.failed",
timestamp: "1970-01-01T00:00:00.000Z",
});
expect(JSON.stringify(evidence)).not.toMatch(
/credential-value|private-stack|support-private/,
);
expect(() =>
noOpDiagnostics.record({
level: "error",
eventId: "app.boot.failed",
}),
).not.toThrow();
});
});
describe("bounded diagnostic dimensions", () => {
it.each([
["component_boundary", "route", true],
["component_boundary", "feature", true],
["component_boundary", "boot", true],
["component_boundary", "SomeComponentName", false],
["component_boundary", 1, false],
["queue_size_bucket", "0", true],
["queue_size_bucket", "1-10", true],
["queue_size_bucket", "51+", true],
["queue_size_bucket", "4096", false],
["queue_size_bucket", 3, false],
] as const)("admits %s=%s only from its finite set (%s)", (key, value, admitted) => {
const projected = projectDiagnosticRecord(
{ level: "warn", eventId: "ui.render.failed", context: { [key]: value } },
() => 0,
);
expect(projected.success).toBe(admitted);
});
});
describe("open-ended bucket labels", () => {
it("records the top buckets that contain a plus sign", () => {
for (const context of [{ queue_size_bucket: "51+" }, { attempt_count_bucket: "5+" }]) {
expect(
projectDiagnosticRecord({ level: "warn", eventId: "ui.render.failed", context }, () => 0),
).toMatchObject({ success: true, record: { context } });
}
});
});