feat(tests): add unit and integration test suite (node:test)
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>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import type { BackendDbInterface } from "../../src/db/types/DbInterface.js";
|
||||
import { NotFoundAbodeError } from "../../src/db/types/errors.js";
|
||||
import { hashPassword } from "../../src/util/hash.js";
|
||||
|
||||
export function runSessionTests(
|
||||
name: string,
|
||||
getDb: () => Promise<{ db: BackendDbInterface; close(): void }>
|
||||
): void {
|
||||
describe(`${name}: sessions`, async () => {
|
||||
let db: BackendDbInterface;
|
||||
let close: () => void;
|
||||
let uid: string;
|
||||
|
||||
before(async () => {
|
||||
({ db, close } = await getDb());
|
||||
const pw = await hashPassword("session-password");
|
||||
const user = await db.createUser({
|
||||
email: `session-user-${Date.now()}@test.example`,
|
||||
name: "Session User",
|
||||
password: pw,
|
||||
flags: {},
|
||||
});
|
||||
uid = user.uid;
|
||||
});
|
||||
|
||||
after(() => close());
|
||||
|
||||
it("createSession returns an as_ token", async () => {
|
||||
const token = await db.createSession(uid);
|
||||
assert.ok(token.startsWith("as_"), `token starts with as_: ${token}`);
|
||||
assert.equal(token.length, 35, "as_ + 32 hex chars");
|
||||
});
|
||||
|
||||
it("getUserBySession returns the correct user", async () => {
|
||||
const token = await db.createSession(uid);
|
||||
const user = await db.getUserBySession(token);
|
||||
assert.equal(user.uid, uid);
|
||||
});
|
||||
|
||||
it("getUserBySession throws NotFoundAbodeError for unknown token", async () => {
|
||||
const fakeToken = `as_${"0".repeat(32)}` as `as_${string}`;
|
||||
await assert.rejects(
|
||||
() => db.getUserBySession(fakeToken),
|
||||
(err) => {
|
||||
assert.ok(err instanceof NotFoundAbodeError);
|
||||
return true;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it("deleteSessionsByUser invalidates all sessions for user", async () => {
|
||||
const token = await db.createSession(uid);
|
||||
await db.deleteSessionsByUser(uid);
|
||||
await assert.rejects(
|
||||
() => db.getUserBySession(token),
|
||||
(err) => {
|
||||
assert.ok(err instanceof NotFoundAbodeError);
|
||||
return true;
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user