2026-07-04 20:00:51 +04:00
|
|
|
import {describe, it, expect, afterEach} from 'vitest';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import os from 'os';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import {backupDatabase} from '../backup-database.js';
|
2026-07-04 11:38:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const tmpDirs = [];
|
2026-07-04 11:38:26 +04:00
|
|
|
|
|
|
|
|
function makeTmpDir() {
|
2026-07-04 20:00:51 +04:00
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'backup-database-test-'));
|
|
|
|
|
tmpDirs.push(dir);
|
|
|
|
|
return dir;
|
2026-07-04 11:38:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('backupDatabase', () => {
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
for (const dir of tmpDirs.splice(0)) {
|
2026-07-04 20:00:51 +04:00
|
|
|
fs.rmSync(dir, {recursive: true, force: true});
|
2026-07-04 11:38:26 +04:00
|
|
|
}
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
2026-07-04 11:38:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
it('ne fait rien quand le fichier de base n\'existe pas', () => {
|
|
|
|
|
const root = makeTmpDir();
|
2026-07-04 11:38:26 +04:00
|
|
|
const result = backupDatabase({
|
|
|
|
|
dbPath: path.join(root, 'inexistant.db'),
|
|
|
|
|
backupsDir: path.join(root, 'backups')
|
2026-07-04 20:00:51 +04:00
|
|
|
});
|
2026-07-04 11:38:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
expect(result).toBeNull();
|
|
|
|
|
expect(fs.existsSync(path.join(root, 'backups'))).toBe(false);
|
|
|
|
|
});
|
2026-07-04 11:38:26 +04:00
|
|
|
|
|
|
|
|
it('copie le fichier de base dans le dossier de sauvegardes', () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const root = makeTmpDir();
|
|
|
|
|
const dbPath = path.join(root, 'data.db');
|
|
|
|
|
fs.writeFileSync(dbPath, 'contenu-de-la-base');
|
|
|
|
|
const backupsDir = path.join(root, 'backups');
|
2026-07-04 11:38:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const result = backupDatabase({dbPath, backupsDir});
|
2026-07-04 11:38:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
expect(result).not.toBeNull();
|
|
|
|
|
expect(fs.existsSync(result)).toBe(true);
|
|
|
|
|
expect(fs.readFileSync(result, 'utf8')).toBe('contenu-de-la-base');
|
|
|
|
|
});
|
2026-07-04 11:38:26 +04:00
|
|
|
|
|
|
|
|
it('ne conserve que les 8 sauvegardes les plus récentes', () => {
|
2026-07-04 20:00:51 +04:00
|
|
|
const root = makeTmpDir();
|
|
|
|
|
const dbPath = path.join(root, 'data.db');
|
|
|
|
|
fs.writeFileSync(dbPath, 'contenu');
|
|
|
|
|
const backupsDir = path.join(root, 'backups');
|
|
|
|
|
fs.mkdirSync(backupsDir, {recursive: true});
|
2026-07-04 11:38:26 +04:00
|
|
|
|
|
|
|
|
for (let i = 0; i < 10; i++) {
|
2026-07-04 20:00:51 +04:00
|
|
|
fs.writeFileSync(path.join(backupsDir, `data-2020-01-0${i}.db`), 'ancien');
|
2026-07-04 11:38:26 +04:00
|
|
|
}
|
|
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
backupDatabase({dbPath, backupsDir});
|
2026-07-04 11:38:26 +04:00
|
|
|
|
2026-07-04 20:00:51 +04:00
|
|
|
const remaining = fs.readdirSync(backupsDir).filter(name => name.startsWith('data-'));
|
|
|
|
|
expect(remaining).toHaveLength(8);
|
|
|
|
|
});
|
|
|
|
|
});
|