Files
suaveplan/packages/networking/drive/drive-template/tests/error-codes.test.ts
2026-05-27 23:47:53 +01:00

76 lines
2.3 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import {
DriveTemplateError,
DTP_CODES,
type DtpErrorCode,
} from "../src/error-codes.js";
describe("@suaveplan/drive-template — DTP_CODES registry", () => {
it("is frozen", { timeout: 5000 }, () => {
expect(Object.isFrozen(DTP_CODES)).toBe(true);
});
it("every code value starts with the DTP. namespace", {
timeout: 5000,
}, () => {
for (const value of Object.values(DTP_CODES)) {
expect(value).toMatch(/^DTP\./);
}
});
it("includes every documented rejection path", { timeout: 5000 }, () => {
const expected = [
"TEMPLATE_VERSION_UNSUPPORTED",
"TEMPLATE_EXPIRED",
"TEMPLATE_STALE",
"TEMPLATE_REPLAY",
"TEMPLATE_UNTRUSTED_ISSUER",
"TEMPLATE_SIG_INVALID",
"TEMPLATE_TOO_DEEP",
"TEMPLATE_MALFORMED",
"CDN_BAD_SIGNATURE",
"KEY_GRANT_REJECTED",
] as const;
for (const key of expected) {
expect(DTP_CODES).toHaveProperty(key);
}
});
});
describe("@suaveplan/drive-template — DriveTemplateError class", () => {
it("preserves the code", { timeout: 5000 }, () => {
const err = new DriveTemplateError(
DTP_CODES.TEMPLATE_EXPIRED,
"test message"
);
expect(err.code).toBe(DTP_CODES.TEMPLATE_EXPIRED);
expect(err.message).toBe("test message");
});
it("threads cause and context through SuaveplanError options", {
timeout: 5000,
}, () => {
const cause = new Error("inner");
const err = new DriveTemplateError(
DTP_CODES.TEMPLATE_SIG_INVALID,
"bad sig",
{
cause,
context: { issuer: "alice" },
}
);
expect(err.cause).toBe(cause);
expect(err.context).toEqual({ issuer: "alice" });
});
it("permits all codes via DtpErrorCode union", { timeout: 5000 }, () => {
const codes: DtpErrorCode[] = Object.values(DTP_CODES);
expect(codes.length).toBeGreaterThanOrEqual(10);
for (const code of codes) {
const err = new DriveTemplateError(code, "ok");
expect(err.code).toBe(code);
}
});
});