Files
ForgeFlow/tests/configuration-backup.test.mjs
T

46 lines
2.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import backupModule from '../src/main/configuration-backup.cjs';
import configModule from '../src/main/config-store.cjs';
const { sanitizeConfiguration, createEncryptedBackup, readEncryptedBackup } = backupModule;
const { ConfigStore } = configModule;
test('configuration backups exclude credentials and operation history', () => {
const clean = sanitizeConfiguration({
gitea: { baseUrl: 'https://gitea.test', encryptedToken: 'secret-token' },
servers: [{ id: 'server', host: 'unraid.test', encryptedPassword: 'password', encryptedPassphrase: 'passphrase' }],
operations: [{ id: 'operation', sha: 'a'.repeat(40) }],
preferences: { autoRefresh: true }
});
assert.equal(clean.gitea.encryptedToken, null);
assert.equal('encryptedPassword' in clean.servers[0], false);
assert.equal('encryptedPassphrase' in clean.servers[0], false);
assert.deepEqual(clean.operations, []);
});
test('configuration backups round-trip with authenticated encryption', () => {
const serialized = createEncryptedBackup({ workspaceRoots: ['C:/Projects'], gitea: { encryptedToken: 'secret' } }, 'correct horse battery staple');
assert.doesNotMatch(serialized, /C:\/Projects|secret/);
const restored = readEncryptedBackup(serialized, 'correct horse battery staple');
assert.deepEqual(restored.configuration.workspaceRoots, ['C:/Projects']);
assert.equal(restored.configuration.gitea.encryptedToken, null);
assert.throws(() => readEncryptedBackup(serialized, 'incorrect passphrase'), /could not be decrypted/i);
});
test('recovery snapshots preserve the exact in-memory configuration before a mutation', async (context) => {
const directory = await mkdtemp(path.join(tmpdir(), 'forgeflow-config-snapshot-'));
context.after(() => rm(directory, { recursive: true, force: true }));
const store = new ConfigStore(directory);
store.data.workspaceRoots = ['C:/Projects'];
store.data.deploymentProfiles = { 'jens/example': [{ id: 'production', provider: 'gitea-actions' }] };
await store.save();
const before = `${JSON.stringify(store.data, null, 2)}\n`;
const snapshot = await store.createRecoverySnapshot('server reconciliation / production');
assert.equal(await readFile(snapshot.filePath, 'utf8'), before);
assert.equal(snapshot.reason, 'server-reconciliation-production');
});