Files
kandev/apps/web/lib/api/domains/queue-api.test.ts
T
José Almeida 83abe6d743 fix(queue): propagate transfer error + tighten WSError narrowing
- `Service.TransferSession` now returns an error so callers can fail
  closed instead of silently leaving entries orphaned on the previous
  session. The two existing call sites pick the right policy:
  `switchSessionForStep` (reusing an existing session) propagates the
  error so the workflow switch aborts; the new-session path logs and
  continues, since the new session is already committed and rolling
  back here would leave a half-switched workflow.
- `asWSError` now also narrows on `message` / `details`, so WS payloads
  without a `code` field are still mapped to a meaningful `Error.message`
  via `rethrowQueueError` (instead of "[object Object]"). Native
  `Error` instances explicitly skip the WS-error branch so they pass
  through untouched, preserving the existing pass-through guarantee.
- `queue-api.test.ts`: `expect.assertions(N)` on the catch-only tests
  so a missing throw fails the test instead of silently passing.
2026-05-10 21:22:00 +01:00

83 lines
2.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { QueueFullError, QueueEntryNotFoundError, rethrowQueueError } from "./queue-api";
describe("rethrowQueueError", () => {
it("maps queue_full errors to QueueFullError carrying the cap metadata", () => {
expect(() =>
rethrowQueueError({
code: "queue_full",
message: "Queue is full",
details: { queue_size: 7, max: 10 },
}),
).toThrow(QueueFullError);
expect.assertions(5);
try {
rethrowQueueError({
code: "queue_full",
details: { queue_size: 9, max: 10 },
});
} catch (err) {
const qf = err as QueueFullError;
expect(qf).toBeInstanceOf(QueueFullError);
expect(qf.queueSize).toBe(9);
expect(qf.max).toBe(10);
expect(qf.code).toBe("queue_full");
}
});
it("defaults missing queue_size / max to 0 when details are sparse", () => {
expect.assertions(3);
try {
rethrowQueueError({ code: "queue_full" });
} catch (err) {
const qf = err as QueueFullError;
expect(qf).toBeInstanceOf(QueueFullError);
expect(qf.queueSize).toBe(0);
expect(qf.max).toBe(0);
}
});
it("maps entry_not_found errors to QueueEntryNotFoundError", () => {
expect(() =>
rethrowQueueError({
code: "entry_not_found",
message: "Already drained",
}),
).toThrow(QueueEntryNotFoundError);
});
it("rethrows non-queue WS errors as plain Error instances", () => {
let caught: unknown;
try {
rethrowQueueError({ code: "internal_error", message: "Boom" });
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(Error);
expect((caught as Error).message).toContain("Boom");
});
it("preserves Error instances supplied by the WS client", () => {
const original = new Error("boom");
let caught: unknown;
try {
rethrowQueueError(original);
} catch (err) {
caught = err;
}
expect(caught).toBe(original);
});
it("wraps non-Error non-WSError values in an Error so callers can rely on stack traces", () => {
let caught: unknown;
try {
rethrowQueueError("just a string");
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(Error);
expect((caught as Error).message).toBe("just a string");
});
});