import test from 'node:test'; import assert from 'node:assert/strict'; import { mkdtemp, mkdir, writeFile, readFile, rm, symlink, stat } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join, dirname } from 'node:path'; import AdmZip from 'adm-zip'; import { createSiteBackup, siteBackupFilename } from '../server/site-backup.mjs'; test('backup is a portable current workspace, with UTF-8 paths and exact media bytes', async t => { const root = await mkdtemp(join(tmpdir(), 'backup-fixture-')); t.after(() => rm(root, { recursive: true, force: true })); const files = { 'content/pages/home.json': Buffer.from('{"title":"Current"}'), 'templates/pages/home/shell.html': Buffer.from('current'), 'assets/фото ровера.bin': Buffer.from(Array.from({ length: 700000 }, (_, i) => i % 251)), '.htaccess': Buffer.from('DirectoryIndex index.html'), 'empty.txt': Buffer.alloc(0), }; const privateFiles = ['.cms-backups/old.zip', '.git/config', '.env', '.env.local', 'demo.secrets.json', 'assets/.trash/old.png', 'assets/._photo', 'node_modules/package/a.js']; for (const [name, value] of [...Object.entries(files), ...privateFiles.map(name => [name, Buffer.from('private')])]) { await mkdir(dirname(join(root, name)), { recursive: true }); await writeFile(join(root, name), value); } const backup = await createSiteBackup(root, { required: ['content/pages/home.json'] }); try { const zip = new AdmZip(await readFile(backup.path)); assert.equal(zip.test(), true); assert.deepEqual(zip.getEntries().map(e => e.entryName).sort(), Object.keys(files).sort()); for (const [name, value] of Object.entries(files)) assert.deepEqual(zip.readFile(name), value, name); assert.equal(backup.size, (await stat(backup.path)).size); assert.equal((await stat(backup.path)).mode & 0o777, 0o600); } finally { await backup.cleanup(); } await assert.rejects(stat(backup.path)); assert.equal(await readFile(join(root, '.env'), 'utf8'), 'private'); await assert.rejects(createSiteBackup(root, { required: ['missing.json'] }), /неполный/); await symlink(join(root, '.env'), join(root, 'linked-secret')); await assert.rejects(createSiteBackup(root), /символическими/); await rm(join(root, 'linked-secret')); const abort = new AbortController(); abort.abort(); await assert.rejects(createSiteBackup(root, { signal: abort.signal }), { name: 'AbortError' }); }); test('backup filename identifies the selected site and Moscow calendar date', () => { assert.equal(siteBackupFilename('mission-core', new Date('2026-11-05T20:59:00Z')), 'NODEDC_SITE_2026-11-05_MISSION_CORE_TECH.zip'); assert.equal(siteBackupFilename('nodedc', new Date('2026-11-05T21:01:00Z')), 'NODEDC_SITE_2026-11-06_NODEDC_TECH.zip'); });