Adds 215 tests across three tiers using node:test + node:assert/strict (no new test framework dependencies): - test/tools/ — middleware and utility tests (token, hash, authenticate, jsonBody, convertError, validators) - test/shared/ — DbInterface/BackendDbInterface contract suites reusable across backends (users, abodes, residents, apikeys, sessions, auth) - test/backends/sqlite/ — SQLite-private tests (sql builder, WrappedDb, migrator) + shared suites via SqliteInterface - test/backends/api/ — ApiInterface unit tests + shared suites via a live Koa server backed by SQLite Also fixes four bugs uncovered by the tests: - ApiInterface: path params leaked into query string (slice(1) fix) - ApiInterface: calling res.json() on 204 No Content responses - SqliteInterface.updateResident: missing comma in SET clause - WrappedBetterSqlite3Db: readonly:undefined rejected by better-sqlite3 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
110 lines
3.6 KiB
TypeScript
110 lines
3.6 KiB
TypeScript
import { describe, it, before, after } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import type { DbInterface } from "../../src/db/types/DbInterface.js";
|
|
import { NotFoundAbodeError, InvalidAbodeError } from "../../src/db/types/errors.js";
|
|
import { hashPassword } from "../../src/util/hash.js";
|
|
|
|
export function runAbodeTests(
|
|
name: string,
|
|
getDb: () => Promise<{ db: DbInterface; close(): void }>
|
|
): void {
|
|
describe(`${name}: abodes`, async () => {
|
|
let db: DbInterface;
|
|
let close: () => void;
|
|
let ctxUid: string;
|
|
|
|
before(async () => {
|
|
({ db, close } = await getDb());
|
|
const pw = await hashPassword("abode-ctx");
|
|
const user = await db.createUser({
|
|
email: `abode-ctx-${Date.now()}@test.example`,
|
|
name: "Abode Ctx User",
|
|
password: pw,
|
|
flags: {},
|
|
});
|
|
ctxUid = user.uid;
|
|
});
|
|
|
|
after(() => close());
|
|
|
|
it("createAbode returns an Abode with expected fields", async () => {
|
|
const abode = await db.createAbode({ name: "Test Abode" }, { uid: ctxUid });
|
|
assert.ok(abode.aid, "has aid");
|
|
assert.equal(abode.name, "Test Abode");
|
|
assert.ok(abode.created_at);
|
|
assert.ok(abode.updated_at);
|
|
});
|
|
|
|
it("getAbodeById returns the created abode", async () => {
|
|
const created = await db.createAbode({ name: "ById Abode" }, { uid: ctxUid });
|
|
const found = await db.getAbodeById(created.aid);
|
|
assert.equal(found.aid, created.aid);
|
|
assert.equal(found.name, "ById Abode");
|
|
});
|
|
|
|
it("getAbodeById throws NotFoundAbodeError for unknown aid", async () => {
|
|
await assert.rejects(
|
|
() => db.getAbodeById("00000000-0000-0000-0000-000000000000"),
|
|
(err) => {
|
|
assert.ok(err instanceof NotFoundAbodeError);
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
|
|
it("listAbodes includes the created abode", async () => {
|
|
const created = await db.createAbode(
|
|
{ name: `Listed Abode ${Date.now()}` },
|
|
{ uid: ctxUid }
|
|
);
|
|
const abodes = await db.listAbodes();
|
|
assert.ok(Array.isArray(abodes));
|
|
const found = abodes.find((a) => a.aid === created.aid);
|
|
assert.ok(found, "created abode appears in listAbodes");
|
|
});
|
|
|
|
it("updateAbode updates the name", async () => {
|
|
const created = await db.createAbode({ name: "Before" }, { uid: ctxUid });
|
|
const updated = await db.updateAbode(
|
|
{ aid: created.aid, name: "After" },
|
|
{ uid: ctxUid }
|
|
);
|
|
assert.equal(updated.aid, created.aid);
|
|
assert.equal(updated.name, "After");
|
|
});
|
|
|
|
it("updateAbode with no fields throws InvalidAbodeError", async () => {
|
|
const created = await db.createAbode({ name: "No Update" }, { uid: ctxUid });
|
|
await assert.rejects(
|
|
() => db.updateAbode({ aid: created.aid }, { uid: ctxUid }),
|
|
(err) => {
|
|
assert.ok(err instanceof InvalidAbodeError);
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
|
|
it("deleteAbodeById removes the abode", async () => {
|
|
const created = await db.createAbode({ name: "To Delete" }, { uid: ctxUid });
|
|
await db.deleteAbodeById(created.aid);
|
|
await assert.rejects(
|
|
() => db.getAbodeById(created.aid),
|
|
(err) => {
|
|
assert.ok(err instanceof NotFoundAbodeError);
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
|
|
it("deleteAbodeById throws NotFoundAbodeError for unknown aid", async () => {
|
|
await assert.rejects(
|
|
() => db.deleteAbodeById("00000000-0000-0000-0000-000000000000"),
|
|
(err) => {
|
|
assert.ok(err instanceof NotFoundAbodeError);
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
});
|
|
}
|