"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = require("path");
const backup_js_1 = require("./backup.js");
let tmpDir;
(0, vitest_1.beforeEach)(() => {
    tmpDir = (0, fs_1.mkdtempSync)((0, path_1.join)((0, os_1.tmpdir)(), 'backup-test-'));
    // Seed some data directories with content
    const dirs = ['config', 'characters', 'templates', 'queue'];
    for (const d of dirs) {
        (0, fs_1.mkdirSync)((0, path_1.resolve)(tmpDir, d), { recursive: true });
    }
    (0, fs_1.writeFileSync)((0, path_1.resolve)(tmpDir, 'config', 'settings.json'), JSON.stringify({ version: 1 }));
    (0, fs_1.writeFileSync)((0, path_1.resolve)(tmpDir, 'characters', 'char1.json'), JSON.stringify({ id: 'char1' }));
    (0, fs_1.writeFileSync)((0, path_1.resolve)(tmpDir, 'templates', 'tpl1.json'), JSON.stringify({ id: 'tpl1' }));
});
(0, vitest_1.afterEach)(() => {
    (0, fs_1.rmSync)(tmpDir, { recursive: true, force: true });
});
(0, vitest_1.describe)('createBackup', () => {
    (0, vitest_1.it)('should create a ZIP file in the backups directory', async () => {
        const info = await (0, backup_js_1.createBackup)(tmpDir);
        (0, vitest_1.expect)(info.filename).toMatch(/^backup-.*\.zip$/);
        (0, vitest_1.expect)((0, fs_1.existsSync)(info.path)).toBe(true);
        (0, vitest_1.expect)(info.sizeBytes).toBeGreaterThan(0);
        (0, vitest_1.expect)(info.checksum).toHaveLength(64); // SHA-256 hex
    });
    (0, vitest_1.it)('should create a .sha256 checksum file alongside the ZIP', async () => {
        const info = await (0, backup_js_1.createBackup)(tmpDir);
        const checksumPath = `${info.path}.sha256`;
        (0, vitest_1.expect)((0, fs_1.existsSync)(checksumPath)).toBe(true);
        const storedChecksum = (0, fs_1.readFileSync)(checksumPath, 'utf-8').trim();
        (0, vitest_1.expect)(storedChecksum).toBe(info.checksum);
    });
});
(0, vitest_1.describe)('restoreBackup', () => {
    (0, vitest_1.it)('should restore data from a backup ZIP', async () => {
        const info = await (0, backup_js_1.createBackup)(tmpDir);
        // Modify data after backup
        (0, fs_1.writeFileSync)((0, path_1.resolve)(tmpDir, 'config', 'settings.json'), JSON.stringify({ version: 999 }));
        await (0, backup_js_1.restoreBackup)(info.path, tmpDir);
        // Data should be restored to original
        const restored = JSON.parse((0, fs_1.readFileSync)((0, path_1.resolve)(tmpDir, 'config', 'settings.json'), 'utf-8'));
        (0, vitest_1.expect)(restored.version).toBe(1);
    });
    (0, vitest_1.it)('should throw on checksum mismatch', async () => {
        const info = await (0, backup_js_1.createBackup)(tmpDir);
        // Corrupt the checksum file
        (0, fs_1.writeFileSync)(`${info.path}.sha256`, 'badhash', 'utf-8');
        await (0, vitest_1.expect)((0, backup_js_1.restoreBackup)(info.path, tmpDir)).rejects.toThrow('Backup checksum verification failed');
    });
    (0, vitest_1.it)('should rollback on extraction failure', async () => {
        // Create a backup first so we have valid data
        await (0, backup_js_1.createBackup)(tmpDir);
        // Write known data
        (0, fs_1.writeFileSync)((0, path_1.resolve)(tmpDir, 'config', 'settings.json'), JSON.stringify({ version: 42 }));
        // Try to restore from a non-existent/invalid zip
        const badZip = (0, path_1.resolve)(tmpDir, 'bad.zip');
        (0, fs_1.writeFileSync)(badZip, 'not a zip');
        await (0, vitest_1.expect)((0, backup_js_1.restoreBackup)(badZip, tmpDir)).rejects.toThrow();
        // Original data should be rolled back
        const data = JSON.parse((0, fs_1.readFileSync)((0, path_1.resolve)(tmpDir, 'config', 'settings.json'), 'utf-8'));
        (0, vitest_1.expect)(data.version).toBe(42);
    });
});
(0, vitest_1.describe)('listBackups', () => {
    (0, vitest_1.it)('should return empty array when no backups exist', async () => {
        const list = await (0, backup_js_1.listBackups)(tmpDir);
        (0, vitest_1.expect)(list).toEqual([]);
    });
    (0, vitest_1.it)('should list all backups sorted newest first', async () => {
        const b1 = await (0, backup_js_1.createBackup)(tmpDir);
        // Small delay to ensure different timestamps
        await new Promise(r => setTimeout(r, 50));
        const b2 = await (0, backup_js_1.createBackup)(tmpDir);
        const list = await (0, backup_js_1.listBackups)(tmpDir);
        (0, vitest_1.expect)(list).toHaveLength(2);
        // Newest first
        (0, vitest_1.expect)(list[0].filename).toBe(b2.filename);
        (0, vitest_1.expect)(list[1].filename).toBe(b1.filename);
    });
});
(0, vitest_1.describe)('verifyBackup', () => {
    (0, vitest_1.it)('should return true for a valid backup', async () => {
        const info = await (0, backup_js_1.createBackup)(tmpDir);
        const valid = await (0, backup_js_1.verifyBackup)(info.path);
        (0, vitest_1.expect)(valid).toBe(true);
    });
    (0, vitest_1.it)('should return false when checksum file is missing', async () => {
        const info = await (0, backup_js_1.createBackup)(tmpDir);
        (0, fs_1.rmSync)(`${info.path}.sha256`);
        const valid = await (0, backup_js_1.verifyBackup)(info.path);
        (0, vitest_1.expect)(valid).toBe(false);
    });
    (0, vitest_1.it)('should return false when checksum does not match', async () => {
        const info = await (0, backup_js_1.createBackup)(tmpDir);
        (0, fs_1.writeFileSync)(`${info.path}.sha256`, 'tampered', 'utf-8');
        const valid = await (0, backup_js_1.verifyBackup)(info.path);
        (0, vitest_1.expect)(valid).toBe(false);
    });
});
//# sourceMappingURL=backup.test.js.map