Replace legacy sqlite adapter tests with persistence module tests
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
2cf729e36c
commit
ee2211b5ea
3 files changed
+104
-43
No files matched your search
@@ -0,0 +1,69 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { runPersistenceMigrations } from "../migrator";
|
||||
import { runWithSqlClient } from "../runtime";
|
||||
import { openPersistenceSqliteDatabase } from "../sqliteLayer";
|
||||
import { appendStateEvent, listStateEventsAfterSeq, readLastStateSeq } from "./stateEventsRepo";
|
||||
|
||||
describe("stateEventsRepo", () => {
|
||||
test("appends and lists ordered state events", () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-persistence-state-events-repo-"));
|
||||
const dbPath = path.join(dir, "state.sqlite");
|
||||
try {
|
||||
const db = openPersistenceSqliteDatabase(dbPath);
|
||||
try {
|
||||
runPersistenceMigrations(db);
|
||||
const createdAt = new Date().toISOString();
|
||||
const first = runWithSqlClient(
|
||||
db,
|
||||
appendStateEvent({
|
||||
eventType: "project.upsert",
|
||||
entityId: "project-1",
|
||||
payload: {
|
||||
project: {
|
||||
id: "project-1",
|
||||
name: "Project One",
|
||||
cwd: "/tmp/project-one",
|
||||
scripts: [],
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
},
|
||||
},
|
||||
createdAt,
|
||||
}),
|
||||
() => {
|
||||
throw new Error("Expected Effect SQL client to be available");
|
||||
},
|
||||
);
|
||||
const second = runWithSqlClient(
|
||||
db,
|
||||
appendStateEvent({
|
||||
eventType: "project.delete",
|
||||
entityId: "project-1",
|
||||
payload: { projectId: "project-1" },
|
||||
createdAt,
|
||||
}),
|
||||
() => {
|
||||
throw new Error("Expected Effect SQL client to be available");
|
||||
},
|
||||
);
|
||||
|
||||
const lastSeq = runWithSqlClient(db, readLastStateSeq, () => 0);
|
||||
const events = runWithSqlClient(db, listStateEventsAfterSeq(first.seq - 1), () => []);
|
||||
|
||||
expect(first.seq).toBeGreaterThan(0);
|
||||
expect(second.seq).toBeGreaterThan(first.seq);
|
||||
expect(lastSeq).toBe(second.seq);
|
||||
expect(events.map((event) => event.eventType)).toEqual(["project.upsert", "project.delete"]);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { runPersistenceMigrations } from "./migrator";
|
||||
import { openPersistenceSqliteDatabase } from "./sqliteLayer";
|
||||
|
||||
describe("persistence sqlite layer", () => {
|
||||
test("opens sqlite database and runs migrations idempotently", () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-persistence-sqlite-layer-"));
|
||||
const dbPath = path.join(dir, "state.sqlite");
|
||||
try {
|
||||
const db = openPersistenceSqliteDatabase(dbPath);
|
||||
try {
|
||||
runPersistenceMigrations(db);
|
||||
runPersistenceMigrations(db);
|
||||
|
||||
db.prepare(
|
||||
"INSERT INTO metadata (key, value_json) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value_json = excluded.value_json;",
|
||||
).run("app.settings.v1", JSON.stringify({ theme: "dark" }));
|
||||
|
||||
const row = db.prepare("SELECT value_json FROM metadata WHERE key = ? LIMIT 1;").get(
|
||||
"app.settings.v1",
|
||||
) as { value_json?: string } | undefined;
|
||||
expect(row?.value_json).toBe(JSON.stringify({ theme: "dark" }));
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,43 +0,0 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { openSqliteDatabase } from "./sqliteAdapter";
|
||||
|
||||
describe("sqliteAdapter", () => {
|
||||
it("opens a database and runs basic statements", () => {
|
||||
const db = openSqliteDatabase(":memory:");
|
||||
db.exec("CREATE TABLE sample (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT NOT NULL);");
|
||||
|
||||
const insertResult = db.prepare("INSERT INTO sample (value) VALUES (?);").run("hello") as {
|
||||
changes?: number;
|
||||
lastInsertRowid?: number;
|
||||
};
|
||||
expect(insertResult.changes).toBe(1);
|
||||
|
||||
const row = db.prepare("SELECT value FROM sample WHERE id = ?;").get(1) as
|
||||
| { value?: string }
|
||||
| undefined;
|
||||
expect(row?.value).toBe("hello");
|
||||
|
||||
const rows = db.prepare("SELECT value FROM sample ORDER BY id ASC;").all() as Array<{
|
||||
value?: string;
|
||||
}>;
|
||||
expect(rows.map((entry) => entry.value)).toEqual(["hello"]);
|
||||
|
||||
db.close();
|
||||
});
|
||||
|
||||
it("throws a helpful error when node:sqlite is unavailable", () => {
|
||||
const missingModuleError = new Error("No such built-in module: node:sqlite");
|
||||
(missingModuleError as NodeJS.ErrnoException).code = "ERR_UNKNOWN_BUILTIN_MODULE";
|
||||
const requireFn = vi.fn((specifier: string) => {
|
||||
if (specifier === "node:sqlite") {
|
||||
throw missingModuleError;
|
||||
}
|
||||
throw new Error(`Unexpected module request: ${specifier}`);
|
||||
});
|
||||
|
||||
expect(() => openSqliteDatabase(":memory:", requireFn as never, false)).toThrow(
|
||||
"@effect/sql-sqlite-node is unavailable",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user