Files
TREK/server/tests/unit/nest/sqlite-database-backup-check-copy.test.ts
T
Maurice 99c3c670a5 fix(demo): keep a demo baseline per engine and check it before every reset
The baseline was travel-baseline.db on both engines and went straight into
the swap, so a SQLite baseline left in the data directory made every hourly
reset of a Postgres demo fail inside pg_restore, and never got replaced. It
is now named after the engine's archive entry (travel-baseline.db on
SQLite, travel-baseline.pgdump on Postgres), and the backup port's new
checkCopy refuses one the engine cannot restore before the swap; the reset
then saves a fresh baseline and logs the reason instead of failing hourly.
2026-11-17 12:24:08 +01:00

44 lines
1.8 KiB
TypeScript

/**
* SqliteDatabaseBackup.checkCopy: a kept copy (the demo baseline) is put back
* only when it is a SQLite file; a Postgres dump an engine switch left under
* the same name, an empty file or a missing one is refused with a reason.
*/
import { SqliteDatabaseBackup } from '../../../src/nest/backup/sqlite-database-backup';
import type { DatabaseLifecycle } from '../../../src/nest/database/database-lifecycle.service';
import Database from 'better-sqlite3';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
let dir: string;
const backup = new SqliteDatabaseBackup({ file: ':memory:' } as unknown as DatabaseLifecycle, {} as never);
beforeEach(() => {
dir = mkdtempSync(path.join(tmpdir(), 'trek-check-copy-'));
});
afterEach(() => {
rmSync(dir, { recursive: true, force: true });
});
describe('SqliteDatabaseBackup.checkCopy', () => {
it('SQLCOPY-001: a SQLite file passes', async () => {
const file = path.join(dir, 'travel-baseline.db');
const db = new Database(file);
db.exec('CREATE TABLE t (x INTEGER)');
db.close();
await expect(backup.checkCopy(file)).resolves.toBeNull();
});
it('SQLCOPY-002: anything else is refused, naming the file', async () => {
const dump = path.join(dir, 'dump.db');
writeFileSync(dump, 'PGDMP\u0001\u000e\u0000 not sqlite at all');
await expect(backup.checkCopy(dump)).resolves.toBe(`${dump} is not a SQLite database`);
const empty = path.join(dir, 'empty.db');
writeFileSync(empty, '');
await expect(backup.checkCopy(empty)).resolves.toBe(`${empty} is not a SQLite database`);
await expect(backup.checkCopy(path.join(dir, 'missing.db'))).resolves.toMatch(/missing\.db cannot be read/);
});
});