CI / install-and-build (pull_request) Successful in 1m29s
CI / format (pull_request) Successful in 51s
CI / typecheck-source (pull_request) Successful in 41s
CI / typecheck-tests (pull_request) Successful in 39s
CI / test (pull_request) Successful in 51s
CI / lint (pull_request) Successful in 21s
Co-Authored-By: gpt-5.6-terra <noreply@openai.com>
125 lines
3.7 KiB
TypeScript
125 lines
3.7 KiB
TypeScript
import { describe, it, before, after } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import type { DbInterface } from "../../src/db/types/DbInterface.js";
|
|
import {
|
|
NotFoundAbodeError,
|
|
InvalidAbodeError,
|
|
} from "../../src/db/types/errors.js";
|
|
import { hashPassword } from "../../src/util/hash.js";
|
|
|
|
export function runAbodeTests(
|
|
name: string,
|
|
getDb: () => Promise<{ db: DbInterface; close(): void }>,
|
|
): void {
|
|
describe(`${name}: abodes`, async () => {
|
|
let db: DbInterface;
|
|
let close: () => void;
|
|
let ctxUid: string;
|
|
|
|
before(async () => {
|
|
({ db, close } = await getDb());
|
|
const pw = await hashPassword("abode-ctx");
|
|
const user = await db.createUser({
|
|
email: `abode-ctx-${Date.now()}@test.example`,
|
|
name: "Abode Ctx User",
|
|
password: pw,
|
|
flags: {},
|
|
});
|
|
ctxUid = user.uid;
|
|
});
|
|
|
|
after(() => close());
|
|
|
|
it("createAbode returns an Abode with expected fields", async () => {
|
|
const abode = await db.createAbode(
|
|
{ name: "Test Abode" },
|
|
{ uid: ctxUid },
|
|
);
|
|
assert.ok(abode.aid, "has aid");
|
|
assert.equal(abode.name, "Test Abode");
|
|
assert.ok(abode.created_at);
|
|
assert.ok(abode.updated_at);
|
|
});
|
|
|
|
it("getAbodeById returns the created abode", async () => {
|
|
const created = await db.createAbode(
|
|
{ name: "ById Abode" },
|
|
{ uid: ctxUid },
|
|
);
|
|
const found = await db.getAbodeById(created.aid);
|
|
assert.equal(found.aid, created.aid);
|
|
assert.equal(found.name, "ById Abode");
|
|
});
|
|
|
|
it("getAbodeById throws NotFoundAbodeError for unknown aid", async () => {
|
|
await assert.rejects(
|
|
() => db.getAbodeById("00000000-0000-0000-0000-000000000000"),
|
|
(err) => {
|
|
assert.ok(err instanceof NotFoundAbodeError);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
it("listAbodes includes the created abode", async () => {
|
|
const created = await db.createAbode(
|
|
{ name: `Listed Abode ${Date.now()}` },
|
|
{ uid: ctxUid },
|
|
);
|
|
const abodes = await db.listAbodes();
|
|
assert.ok(Array.isArray(abodes));
|
|
const found = abodes.find((a) => a.aid === created.aid);
|
|
assert.ok(found, "created abode appears in listAbodes");
|
|
});
|
|
|
|
it("updateAbode updates the name", async () => {
|
|
const created = await db.createAbode({ name: "Before" }, { uid: ctxUid });
|
|
const updated = await db.updateAbode(
|
|
{ aid: created.aid, name: "After" },
|
|
{ uid: ctxUid },
|
|
);
|
|
assert.equal(updated.aid, created.aid);
|
|
assert.equal(updated.name, "After");
|
|
});
|
|
|
|
it("updateAbode with no fields throws InvalidAbodeError", async () => {
|
|
const created = await db.createAbode(
|
|
{ name: "No Update" },
|
|
{ uid: ctxUid },
|
|
);
|
|
await assert.rejects(
|
|
() => db.updateAbode({ aid: created.aid }, { uid: ctxUid }),
|
|
(err) => {
|
|
assert.ok(err instanceof InvalidAbodeError);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
it("deleteAbodeById removes the abode", async () => {
|
|
const created = await db.createAbode(
|
|
{ name: "To Delete" },
|
|
{ uid: ctxUid },
|
|
);
|
|
await db.deleteAbodeById(created.aid);
|
|
await assert.rejects(
|
|
() => db.getAbodeById(created.aid),
|
|
(err) => {
|
|
assert.ok(err instanceof NotFoundAbodeError);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
it("deleteAbodeById throws NotFoundAbodeError for unknown aid", async () => {
|
|
await assert.rejects(
|
|
() => db.deleteAbodeById("00000000-0000-0000-0000-000000000000"),
|
|
(err) => {
|
|
assert.ok(err instanceof NotFoundAbodeError);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
});
|
|
}
|