mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-08-29 18:31:33 +00:00
DatabaseContext handed every repository a raw better-sqlite3 handle alongside
drizzle, and three of them used it for retention queries built on datetime('now',
?) — a SQLite-only function. That handle is the one thing standing between the
repository layer and a second engine.
Drop it. The two time-based prunes compute their cutoff in JS against the
CURRENT_TIMESTAMP text format, which every engine writes the same way and which
compares correctly as a string; the health-history prune becomes a select of the
rows to keep followed by a NOT IN delete. All three turn async, so their two
callers await them.
Name the dialect rather than repeating a string literal, so adding an engine is
one edit instead of a search.
Tests built their schema through context.sqlite?.exec(). Optional chaining meant
removing the field type-checked cleanly and then silently created no tables, so
the fixture now owns exec() and a raw handle for direct assertions — schema setup
belongs to the test harness, not to the interface repositories consume.
No behaviour change, and no Postgres yet: this only removes the coupling that
would have to be undone first.
147 lines
3.9 KiB
TypeScript
147 lines
3.9 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
import { TestSqliteDatabase } from "./test-support.js";
|
|
import { OpenTabRepository } from "../../../database/repositories/open-tab-repository.js";
|
|
|
|
describe("OpenTabRepository", () => {
|
|
let adapter: TestSqliteDatabase | null = null;
|
|
|
|
afterEach(async () => {
|
|
if (adapter) {
|
|
await adapter.close();
|
|
adapter = null;
|
|
}
|
|
});
|
|
|
|
async function createRepository(
|
|
onWrite?: () => void | Promise<void>,
|
|
): Promise<OpenTabRepository> {
|
|
adapter = new TestSqliteDatabase();
|
|
const context = await adapter.connect();
|
|
adapter.exec(`
|
|
CREATE TABLE users (
|
|
id TEXT PRIMARY KEY,
|
|
username TEXT NOT NULL,
|
|
password_hash TEXT NOT NULL,
|
|
is_admin INTEGER NOT NULL DEFAULT 0,
|
|
is_oidc INTEGER NOT NULL DEFAULT 0
|
|
);
|
|
|
|
CREATE TABLE user_open_tabs (
|
|
id TEXT PRIMARY KEY,
|
|
user_id TEXT NOT NULL,
|
|
tab_type TEXT NOT NULL,
|
|
host_id INTEGER,
|
|
label TEXT NOT NULL,
|
|
tab_order INTEGER NOT NULL DEFAULT 0,
|
|
backend_session_id TEXT,
|
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
INSERT INTO users (id, username, password_hash)
|
|
VALUES ('user-1', 'alice', 'hash'), ('user-2', 'bob', 'hash');
|
|
`);
|
|
|
|
return new OpenTabRepository(context, onWrite);
|
|
}
|
|
|
|
it("lists recent tabs ordered by tab order", async () => {
|
|
const repo = await createRepository();
|
|
|
|
await repo.upsertForUser(
|
|
"user-1",
|
|
{
|
|
id: "tab-old",
|
|
tabType: "terminal",
|
|
label: "Old",
|
|
tabOrder: 1,
|
|
},
|
|
"2026-06-27T00:00:00.000Z",
|
|
);
|
|
await repo.upsertForUser(
|
|
"user-1",
|
|
{
|
|
id: "tab-new",
|
|
tabType: "stats",
|
|
label: "New",
|
|
tabOrder: 0,
|
|
},
|
|
"2026-06-27T01:00:00.000Z",
|
|
);
|
|
await repo.upsertForUser(
|
|
"user-2",
|
|
{
|
|
id: "other",
|
|
tabType: "terminal",
|
|
label: "Other",
|
|
tabOrder: 0,
|
|
},
|
|
"2026-06-27T02:00:00.000Z",
|
|
);
|
|
|
|
expect(
|
|
(await repo.listRecentForUser("user-1", "2026-06-27T00:30:00.000Z")).map(
|
|
(tab) => tab.id,
|
|
),
|
|
).toEqual(["tab-new"]);
|
|
});
|
|
|
|
it("upserts tabs and preserves backend session when omitted", async () => {
|
|
const repo = await createRepository();
|
|
|
|
await repo.upsertForUser("user-1", {
|
|
id: "tab-1",
|
|
tabType: "terminal",
|
|
hostId: 1,
|
|
label: "Server",
|
|
tabOrder: 0,
|
|
backendSessionId: "session-1",
|
|
});
|
|
await repo.upsertForUser("user-1", {
|
|
id: "tab-1",
|
|
tabType: "terminal",
|
|
hostId: 2,
|
|
label: "Server renamed",
|
|
tabOrder: 1,
|
|
});
|
|
|
|
expect(
|
|
(await repo.listRecentForUser("user-1", "2000-01-01T00:00:00.000Z"))[0],
|
|
).toMatchObject({
|
|
id: "tab-1",
|
|
hostId: 2,
|
|
label: "Server renamed",
|
|
tabOrder: 1,
|
|
backendSessionId: "session-1",
|
|
});
|
|
});
|
|
|
|
it("replaces, updates, and deletes tabs for a user", async () => {
|
|
let writeCount = 0;
|
|
const repo = await createRepository(() => {
|
|
writeCount += 1;
|
|
});
|
|
|
|
await repo.replaceForUser("user-1", [
|
|
{ id: "tab-1", tabType: "terminal", label: "One", tabOrder: 1 },
|
|
{ id: "tab-2", tabType: "settings", label: "Two", tabOrder: 0 },
|
|
]);
|
|
expect(
|
|
(await repo.listRecentForUser("user-1", "2000-01-01T00:00:00.000Z")).map(
|
|
(tab) => tab.id,
|
|
),
|
|
).toEqual(["tab-2", "tab-1"]);
|
|
|
|
expect(
|
|
await repo.updateForUser("user-1", "missing", { label: "Missing" }),
|
|
).toBe(false);
|
|
expect(
|
|
await repo.updateForUser("user-1", "tab-1", { label: "Renamed" }),
|
|
).toBe(true);
|
|
expect(await repo.deleteForUser("user-1", "tab-2")).toBe(1);
|
|
expect(await repo.deleteByUserId("user-1")).toBe(1);
|
|
expect(await repo.deleteByUserId("user-1")).toBe(0);
|
|
expect(writeCount).toBe(4);
|
|
});
|
|
});
|