68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
|
|
import { DMP_CODES, DmpError, type DmpErrorCode } from "../src/error-codes.js";
|
|
|
|
describe("@suaveplan/drive-mesh-protocol — DMP_CODES registry", () => {
|
|
it("is frozen", { timeout: 5000 }, () => {
|
|
expect(Object.isFrozen(DMP_CODES)).toBe(true);
|
|
});
|
|
|
|
it("every code value starts with the DMP. namespace", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
for (const value of Object.values(DMP_CODES)) {
|
|
expect(value).toMatch(/^DMP\./);
|
|
}
|
|
});
|
|
|
|
it("includes every documented rejection path", { timeout: 5000 }, () => {
|
|
const expected = [
|
|
"EMPTY_REQUEST",
|
|
"UNKNOWN_REQUEST_KIND",
|
|
"MAX_CHUNKS_EXCEEDED",
|
|
"BITSET_LENGTH_MISMATCH",
|
|
"BITSET_PADDING_NONZERO",
|
|
"UNORDERED_RESPONSE",
|
|
"GENERATION_STALE",
|
|
"CODEC_MISMATCH",
|
|
] as const;
|
|
for (const key of expected) {
|
|
expect(DMP_CODES).toHaveProperty(key);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("@suaveplan/drive-mesh-protocol — DmpError class", () => {
|
|
it("preserves the code", { timeout: 5000 }, () => {
|
|
const err = new DmpError(DMP_CODES.EMPTY_REQUEST, "test message");
|
|
expect(err.code).toBe(DMP_CODES.EMPTY_REQUEST);
|
|
expect(err.message).toBe("test message");
|
|
});
|
|
|
|
it("threads cause and context through SuaveplanError options", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
const cause = new Error("inner");
|
|
const err = new DmpError(
|
|
DMP_CODES.MAX_CHUNKS_EXCEEDED,
|
|
"cap exceeded",
|
|
{
|
|
cause,
|
|
context: { requested: 4096, allowed: 1024 },
|
|
}
|
|
);
|
|
expect(err.cause).toBe(cause);
|
|
expect(err.context).toEqual({ requested: 4096, allowed: 1024 });
|
|
});
|
|
|
|
it("permits all codes via DmpErrorCode union", { timeout: 5000 }, () => {
|
|
// Type-level check: every code is assignable to the discriminator.
|
|
const codes: DmpErrorCode[] = Object.values(DMP_CODES);
|
|
expect(codes.length).toBeGreaterThanOrEqual(8);
|
|
for (const code of codes) {
|
|
const err = new DmpError(code, "ok");
|
|
expect(err.code).toBe(code);
|
|
}
|
|
});
|
|
});
|