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,29 @@
|
||||
import Koa from "koa";
|
||||
import { createServer } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import { apirouter } from "../../src/webapi/apirouter.js";
|
||||
import type { BackendDbInterface } from "../../src/db/types/DbInterface.js";
|
||||
|
||||
export interface TestServer {
|
||||
url: string;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
export async function createTestServer(
|
||||
db: BackendDbInterface
|
||||
): Promise<TestServer> {
|
||||
const app = new Koa();
|
||||
const router = apirouter(db);
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
const server = createServer(app.callback());
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const { port } = server.address() as AddressInfo;
|
||||
return {
|
||||
url: `http://127.0.0.1:${port}`,
|
||||
close: () =>
|
||||
new Promise<void>((resolve, reject) =>
|
||||
server.close((err) => (err ? reject(err) : resolve()))
|
||||
),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { getWrappedDb } from "../../src/db/sqlite/impl/index.js";
|
||||
import { SqliteMigrator } from "../../src/db/sqlite/SqliteMigrator.js";
|
||||
import { SqliteInterface } from "../../src/db/sqlite/SqliteInterface.js";
|
||||
import type { WrappedDb } from "../../src/db/sqlite/impl/types.js";
|
||||
|
||||
export interface TestDb {
|
||||
db: SqliteInterface;
|
||||
wrapped: WrappedDb;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
export async function createTestDb(): Promise<TestDb> {
|
||||
const wrapped = getWrappedDb("node", ":memory:", {});
|
||||
const migrator = new SqliteMigrator(wrapped);
|
||||
await migrator.migrateTo(3);
|
||||
const db = new SqliteInterface(wrapped);
|
||||
return { db, wrapped, close: () => wrapped.destroy() };
|
||||
}
|
||||
Reference in New Issue
Block a user