68 lines
3.6 KiB
TypeScript
68 lines
3.6 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||
import { checkFreshTables, createTestDb, testDatabaseUrl, type TestDb } from "khb_common";
|
||
import { collect, selectLoaders } from "../collect.js";
|
||
import type { TableLoader } from "./snapshot-round.js";
|
||
|
||
const url = testDatabaseUrl();
|
||
const d = url ? describe : describe.skip;
|
||
|
||
const jobLoader = (names: string[], fail = false): TableLoader => ({
|
||
table: "jt_job",
|
||
columns: ["id", "number", "name", "status", "closed_on"],
|
||
load: async () => {
|
||
if (fail) throw new Error("llmtest-boom");
|
||
return names.map((n, i) => ({ id: `llmtest-${i}`, number: null, name: n, status: "approved", closed_on: null }));
|
||
},
|
||
});
|
||
const typeLoader = (): TableLoader => ({
|
||
table: "jt_task_type", columns: ["id", "name"], load: async () => [{ id: "llmtest-t1", name: "llmtest-Type" }],
|
||
});
|
||
|
||
d("раунд зеркалирования (временная БД)", () => {
|
||
let db: TestDb;
|
||
beforeAll(async () => { db = await createTestDb(url!); });
|
||
afterAll(async () => { await db?.drop(); });
|
||
|
||
it("загружает таблицы, пишет снапшот и статусы по таблицам", async () => {
|
||
const [res] = await collect(db.pool, { jt: [jobLoader(["A", "B"]), typeLoader()] });
|
||
expect(res!.status).toBe("ok");
|
||
expect((await db.pool.query("select count(*)::int n from jt_job")).rows[0].n).toBe(2);
|
||
const st = await db.pool.query("select table_name, status, row_count from sync_table_status order by table_name");
|
||
expect(st.rows).toEqual([
|
||
{ table_name: "jt_job", status: "ok", row_count: 2 },
|
||
{ table_name: "jt_task_type", status: "ok", row_count: 1 },
|
||
]);
|
||
const snap = await db.pool.query("select had_errors, requested_tables from sync_snapshot");
|
||
expect(snap.rows[0].had_errors).toBe(false);
|
||
});
|
||
|
||
it("следующий раунд переносит старые данные в _hist", async () => {
|
||
await collect(db.pool, { jt: [jobLoader(["C"])] });
|
||
expect((await db.pool.query("select name from jt_job")).rows.map((r) => r.name)).toEqual(["C"]);
|
||
expect((await db.pool.query("select count(*)::int n from jt_job_hist")).rows[0].n).toBe(2);
|
||
});
|
||
|
||
it("ошибка одной таблицы не трогает остальные и не меняет её текущие данные", async () => {
|
||
const [res] = await collect(db.pool, { jt: [jobLoader([], true), typeLoader()] });
|
||
expect(res!.status).toBe("error");
|
||
expect(res!.tables.find((t) => t.name === "jt_job")!.error).toBe("llmtest-boom");
|
||
expect((await db.pool.query("select name from jt_job")).rows.map((r) => r.name)).toEqual(["C"]); // не тронута
|
||
expect((await db.pool.query("select had_errors, error_count from sync_snapshot order by started_at desc limit 1")).rows[0])
|
||
.toEqual({ had_errors: true, error_count: 1 });
|
||
});
|
||
|
||
it("потребитель с нужными таблицами: сбой jt_job блокирует, сбой чужой таблицы - нет", async () => {
|
||
const blocked = await checkFreshTables(db.pool, ["jt_job", "jt_task_type"]);
|
||
expect(blocked.find((s) => s.table === "jt_job")).toMatchObject({ ok: false, reason: "error" });
|
||
expect(blocked.find((s) => s.table === "jt_task_type")!.ok).toBe(true);
|
||
const unaffected = await checkFreshTables(db.pool, ["jt_task_type"]);
|
||
expect(unaffected.every((s) => s.ok)).toBe(true);
|
||
});
|
||
|
||
it("раздельный сбор по списку таблиц", () => {
|
||
const sel = selectLoaders({ jt: [jobLoader([]), typeLoader()] }, { tables: ["jt_task_type"] });
|
||
expect(sel).toHaveLength(1);
|
||
expect(sel[0]!.loaders.map((l) => l.table)).toEqual(["jt_task_type"]);
|
||
});
|
||
});
|