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>
86 lines
3.1 KiB
TypeScript
86 lines
3.1 KiB
TypeScript
import { describe, it, before, after } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { WrappedNodeSqliteDb } from "../../../src/db/sqlite/impl/node-sqlite.js";
|
|
import { SqliteMigrator } from "../../../src/db/sqlite/SqliteMigrator.js";
|
|
import { migrations } from "../../../src/db/sqlite/migrations/index.js";
|
|
|
|
describe("SqliteMigrator", () => {
|
|
it("listAvailableMigrations returns all known migrations", () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
const available = migrator.listAvailableMigrations();
|
|
assert.ok(Array.isArray(available));
|
|
assert.ok(available.length >= 3, "at least 3 migrations");
|
|
const ids = available.map((m) => m.id);
|
|
assert.ok(ids.includes(1), "migration 1 present");
|
|
assert.ok(ids.includes(2), "migration 2 present");
|
|
assert.ok(ids.includes(3), "migration 3 present");
|
|
db.destroy();
|
|
});
|
|
|
|
it("listAppliedMigrations returns empty array on fresh db", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
const applied = await migrator.listAppliedMigrations();
|
|
assert.deepEqual(applied, []);
|
|
db.destroy();
|
|
});
|
|
|
|
it("migrateTo(1) applies first migration", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
await migrator.migrateTo(1);
|
|
const applied = await migrator.listAppliedMigrations();
|
|
assert.equal(applied.length, 1);
|
|
assert.equal(applied[0].id, 1);
|
|
assert.ok(applied[0].name, "migration has a name");
|
|
assert.ok(applied[0].applied_at, "migration has applied_at");
|
|
db.destroy();
|
|
});
|
|
|
|
it("migrateTo(3) applies all three migrations in order", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
await migrator.migrateTo(3);
|
|
const applied = await migrator.listAppliedMigrations();
|
|
assert.equal(applied.length, 3);
|
|
assert.deepEqual(
|
|
applied.map((m) => m.id),
|
|
[1, 2, 3]
|
|
);
|
|
db.destroy();
|
|
});
|
|
|
|
it("migrateTo(3) twice is idempotent (nothing to do)", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
await migrator.migrateTo(3);
|
|
await migrator.migrateTo(3);
|
|
const applied = await migrator.listAppliedMigrations();
|
|
assert.equal(applied.length, 3);
|
|
db.destroy();
|
|
});
|
|
|
|
it("migrateTo with unknown id throws", async () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
await assert.rejects(
|
|
() => migrator.migrateTo(9999),
|
|
/No known migration with id 9999/
|
|
);
|
|
db.destroy();
|
|
});
|
|
|
|
it("listAvailableMigrations names match migration objects", () => {
|
|
const db = new WrappedNodeSqliteDb(":memory:");
|
|
const migrator = new SqliteMigrator(db);
|
|
const available = migrator.listAvailableMigrations();
|
|
for (const { id, name } of available) {
|
|
const migration = migrations.find((m) => m.id === id);
|
|
assert.ok(migration, `migration ${id} exists`);
|
|
assert.equal(migration.name, name);
|
|
}
|
|
db.destroy();
|
|
});
|
|
});
|