import { test } from 'node:test'; import assert from 'node:assert/strict'; import { mkdir, mkdtemp, readdir, rm, stat, writeFile } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { nextRun, retention, snapshotName, takeSnapshot, verifyFiles, sha256File } from './cluster-poc-backup.mjs'; import { restoreScript } from './cluster-poc.mjs'; test('snapshots are taken daily at 03:20 Beijing time and named by their UTC time', () => { assert.equal(nextRun(new Date('2026-11-01T10:00:00Z')).toISOString(), '2026-11-01T19:20:00.000Z'); assert.equal(nextRun(new Date('2026-11-01T19:20:00Z')).toISOString(), '2026-11-02T19:20:00.000Z'); assert.equal(nextRun(new Date('2026-12-31T23:00:00Z')).toISOString(), '2027-01-01T19:20:00.000Z'); assert.equal(snapshotName(new Date('2026-11-01T19:20:05.123Z')), '20261101T192005Z'); }); test('the newest complete snapshots are kept; older ones and leftover partial ones go', () => { const names = ['20261103T192000Z', '20261101T192000Z', '20261102T192000Z', '20261104T192000Z']; const { keep, remove } = retention([...names, '.partial-20261105T192000Z', '.lock', 'last.json'], 2); assert.deepEqual(keep, ['20261103T192000Z', '20261104T192000Z']); assert.deepEqual(remove, ['20261101T192000Z', '20261102T192000Z', '.partial-20261105T192000Z']); assert.deepEqual(retention(names.slice(0, 1), 7).remove, []); }); test('a snapshot is restored only when every file has its recorded size and SHA-256', async t => { const directory = await mkdtemp(path.join(os.tmpdir(), 'wf-backup-verify-')); t.after(() => rm(directory, { recursive: true, force: true })); await writeFile(path.join(directory, 'git.tgz'), 'archive'); const files = { 'git.tgz': { bytes: 7, sha256: await sha256File(path.join(directory, 'git.tgz')) } }; await verifyFiles(directory, files); await assert.rejects(verifyFiles(directory, { ...files, 'minio.tgz': { bytes: 1, sha256: '' } }), /missing/); await assert.rejects(verifyFiles(directory, { 'git.tgz': { ...files['git.tgz'], bytes: 8 } }), /bytes/); await writeFile(path.join(directory, 'git.tgz'), 'archivX'); await assert.rejects(verifyFiles(directory, files), /SHA-256/); }); test('a snapshot that fails leaves no partial snapshot or lock behind, and a held lock stops a second one', async t => { const directory = await mkdtemp(path.join(os.tmpdir(), 'wf-backup-fail-')); t.after(() => rm(directory, { recursive: true, force: true })); const root = path.join(directory, 'backups'); // Nothing listens on port 9: the database dump fails first. const database = { host: '127.0.0.1', port: 9, user: 'wishforge', database: 'wishforge' }; await assert.rejects(takeSnapshot({ root, data: directory, database })); assert.deepEqual(await readdir(root), []); await mkdir(path.join(root, '.lock')); await assert.rejects(takeSnapshot({ root, data: directory, database }), /Another snapshot/); assert.ok(await stat(path.join(root, '.lock'))); }); test('a restore checks the snapshot before it empties anything, and unpacks only what the snapshot holds', () => { const script = restoreScript('20261102T192000Z', ['git', 'minio']).split('\n'); const verify = script.findIndex(line => line.includes('cluster-poc-backup.mjs verify')); const firstReset = script.findIndex(line => line.includes('-mindepth 1 -delete')); assert.ok(verify >= 0 && verify < firstReset); assert.ok(script.indexOf('touch /tmp/verified') < firstReset); assert.ok(script.some(line => line.startsWith("mkdir -p 'runner-state-fanyu'"))); assert.ok(!script.some(line => line.includes("-C 'runner-state-fanyu'"))); assert.ok(!script.some(line => /backups'? &&/.test(line) && line.includes('-delete')), 'snapshots are never emptied'); assert.equal(script.at(-2), 'touch /tmp/restored'); assert.throws(() => restoreScript('../x'), /Not a snapshot/); assert.throws(() => restoreScript('20261102T192000Z', ['postgres']), /Unknown archive/); });