Files
abode/test/backends/api/index.test.ts
T
codingetandClaude da4e597f73 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>
2026-06-30 11:33:32 +00:00

40 lines
1.3 KiB
TypeScript

import { createTestDb } from "../../helpers/sqlite.js";
import { createTestServer } from "../../helpers/koa.js";
import { ApiInterface } from "../../../src/db/api/ApiInterface.js";
import { hashPassword } from "../../../src/util/hash.js";
import { runUserTests } from "../../shared/users.js";
import { runAbodeTests } from "../../shared/abodes.js";
import { runResidentTests } from "../../shared/residents.js";
import { runApikeyTests } from "../../shared/apikeys.js";
const AUTH_EMAIL = "api-auth@test.example";
const AUTH_PASSWORD = "api-auth-password";
async function getApiDb() {
const { db: sqliteDb, close: closeSqlite } = await createTestDb();
const pw = await hashPassword(AUTH_PASSWORD);
await sqliteDb.createUser({
email: AUTH_EMAIL,
name: "API Auth User",
password: pw,
flags: {},
});
const server = await createTestServer(sqliteDb);
const authHeader = "Basic " + btoa(`${AUTH_EMAIL}:${AUTH_PASSWORD}`);
const api = new ApiInterface(server.url, {
headers: { Authorization: authHeader },
});
return {
db: api,
close: async () => {
await server.close();
closeSqlite();
},
};
}
runUserTests("api", getApiDb);
runAbodeTests("api", getApiDb);
runResidentTests("api", getApiDb);
runApikeyTests("api", getApiDb);