Files
abode/test/tools/convertError.test.ts
T
codingetandCodex 31d4636dde
CI / install-and-build (pull_request) Successful in 1m29s
CI / format (pull_request) Successful in 51s
CI / typecheck-source (pull_request) Successful in 41s
CI / typecheck-tests (pull_request) Successful in 39s
CI / test (pull_request) Successful in 51s
CI / lint (pull_request) Successful in 21s
ci: add pull request quality gates
Co-Authored-By: gpt-5.6-terra <noreply@openai.com>
2026-07-22 21:50:29 +00:00

77 lines
2.4 KiB
TypeScript

import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { convertError } from "../../src/webapi/middleware/convertError.js";
import {
NotFoundAbodeError,
NotAuthorizedAbodeError,
ConflictAbodeError,
InvalidAbodeError,
ReadonlyAbodeError,
} from "../../src/db/types/errors.js";
function makeCtx() {
return { status: 200, body: null as unknown };
}
async function runWithError(err: unknown) {
const ctx = makeCtx();
await convertError(ctx as any, async () => {
throw err;
});
return ctx;
}
describe("convertError middleware", () => {
it("does not interfere when next succeeds", async () => {
const ctx = makeCtx();
let nextCalled = false;
await convertError(ctx as any, async () => {
nextCalled = true;
});
assert.equal(nextCalled, true);
assert.equal(ctx.status, 200);
});
it("NotFoundAbodeError → 404 not_found", async () => {
const ctx = await runWithError(new NotFoundAbodeError());
assert.equal(ctx.status, 404);
assert.deepEqual(ctx.body, { ok: false, error: "not_found" });
});
it("NotAuthorizedAbodeError → 401 not_authorized", async () => {
const ctx = await runWithError(new NotAuthorizedAbodeError());
assert.equal(ctx.status, 401);
assert.deepEqual(ctx.body, { ok: false, error: "not_authorized" });
});
it("ConflictAbodeError → 409 conflict", async () => {
const ctx = await runWithError(new ConflictAbodeError());
assert.equal(ctx.status, 409);
assert.deepEqual(ctx.body, { ok: false, error: "conflict" });
});
it("InvalidAbodeError → 400 invalid", async () => {
const ctx = await runWithError(new InvalidAbodeError());
assert.equal(ctx.status, 400);
assert.deepEqual(ctx.body, { ok: false, error: "invalid" });
});
it("ReadonlyAbodeError → 403 readonly", async () => {
const ctx = await runWithError(new ReadonlyAbodeError());
assert.equal(ctx.status, 403);
assert.deepEqual(ctx.body, { ok: false, error: "readonly" });
});
it("unknown Error → 500 unknown", async () => {
const ctx = await runWithError(new Error("something went wrong"));
assert.equal(ctx.status, 500);
assert.deepEqual(ctx.body, { ok: false, error: "unknown" });
});
it("non-Error thrown → 500 unknown", async () => {
const ctx = await runWithError("string error");
assert.equal(ctx.status, 500);
assert.deepEqual(ctx.body, { ok: false, error: "unknown" });
});
});