Files
abode/test/backends/sqlite/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

39 lines
1.4 KiB
TypeScript

import type { BackendDbInterface } from "../../../src/db/types/DbInterface.js";
import { SqliteInterface } from "../../../src/db/sqlite/SqliteInterface.js";
import { createTestDb } from "../../helpers/sqlite.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";
import { runSessionTests } from "../../shared/sessions.js";
import { runAuthTests } from "../../shared/auth.js";
async function createExpiredApikey(
db: BackendDbInterface,
uid: string
): Promise<`at_${string}`> {
const si = db as SqliteInterface;
const token = (`at_${"e".repeat(32)}`) as `at_${string}`;
const kid = crypto.randomUUID();
const { sql } = si._;
si._.db.run(sql`
INSERT INTO "apikeys"("uid", "kid", "token", "name", "permissions", "expires_at")
VALUES(
${{ uuid: uid }},
${{ uuid: kid }},
${{ text: token }},
${{ text: "Expired Key" }},
${{ jsonb: {} }},
${{ date: new Date(Date.now() - 10000).toISOString() }}
)
`);
return token;
}
runUserTests("sqlite", createTestDb);
runAbodeTests("sqlite", createTestDb);
runResidentTests("sqlite", createTestDb);
runApikeyTests("sqlite", createTestDb);
runSessionTests("sqlite", createTestDb);
runAuthTests("sqlite", createTestDb, createExpiredApikey);