Files
TREK/server/tests/unit/nest/backup-archive-version.test.ts
T
Maurice b2f2aef016 test(server): seed the small specs through the repositories
The suites with one to three raw statements left now seed and read their
rows through the ORM factories instead of hand-written SQLite on the
better-sqlite3 handle. A legacy statement that serves as a parity oracle,
the schema introspection and the bare backup file keep their raw SQL and
say why with a test-sql-allow marker. Two database mocks still answered a
getPlaceWithTags the module no longer exports; that dead branch goes too,
and the budget seam suite stubs the reservations repository directly
instead of a fake prepare().
2026-10-08 18:46:13 +02:00

48 lines
2.0 KiB
TypeScript

import Database from 'better-sqlite3';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { checkBackupDatabase } from '../../../src/nest/backup/backup-archive';
describe('checkBackupDatabase and the release that made the backup', () => {
let dir: string;
beforeEach(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-backup-version-'));
});
afterEach(() => {
fs.rmSync(dir, { recursive: true, force: true });
});
const backupWith = (recorded: string[]): void => {
const db = new Database(path.join(dir, 'travel.db'));
for (const table of ['users', 'trips', 'trip_members', 'places', 'days']) db.exec(`CREATE TABLE ${table} (id INTEGER)`);
db.exec('CREATE TABLE mikro_orm_migrations (id INTEGER PRIMARY KEY, name TEXT, executed_at TEXT)');
// test-sql-allow: a bare backup file before any boot, which no ORM is bound to.
const insert = db.prepare('INSERT INTO mikro_orm_migrations (name) VALUES (?)');
for (const name of recorded) insert.run(name);
db.close();
};
it('BACKUPVER-001: refuses a backup a newer TREK made, before anything is swapped', () => {
backupWith(['Migration20200101000000_baseline_schema', 'Migration20990101000000_from_a_newer_release']);
expect(checkBackupDatabase(dir)).toEqual({
error: 'This backup was made by a newer TREK version. Update TREK before restoring it.',
status: 400,
});
});
it('BACKUPVER-002: accepts a backup this release or an older one made', () => {
backupWith(['Migration20200101000000_baseline_schema', 'Migration20200101042000_tours']);
expect(checkBackupDatabase(dir)).toBeNull();
});
it('BACKUPVER-003: accepts a pre-ORM backup without the migrations table', () => {
const db = new Database(path.join(dir, 'travel.db'));
for (const table of ['users', 'trips', 'trip_members', 'places', 'days']) db.exec(`CREATE TABLE ${table} (id INTEGER)`);
db.close();
expect(checkBackupDatabase(dir)).toBeNull();
});
});