fix(config-backup): a restore after a reset keeps the retention, the mode probe cannot write through a planted symlink, the form shows the retention in use

This commit is contained in:
Devvyn committed 2026-10-20 19:31:55 +08:00
1 parent c982fa1ce5
commit 241886d44d
7 files changed
+176 -22

No files matched your search

+44
View File
@@ -62,3 +62,47 @@ describe('restoreConfigBackup (Settings UI)', () => {
expect(confirms[0]).toMatch(/users\.json, \.env\)/);
});
});
describe('loadConfigBackups (retention fields)', () => {
function load(saved: Record<string, unknown>) {
const inputs: Record<string, { value: string; placeholder: string }> = {
appSettingsConfigBackupKeepCount: { value: '20', placeholder: '' },
appSettingsConfigBackupKeepDays: { value: '30', placeholder: '' },
appSettingsConfigBackupDir: { value: '', placeholder: '' },
};
function CodemanApp(this: unknown) {}
const ctx = vm.createContext({
CodemanApp,
window: {},
document: { getElementById: (id: string) => inputs[id] ?? null },
escapeHtml: (s: string) => s,
});
vm.runInContext(SRC, ctx);
const app = Object.create((CodemanApp as unknown as { prototype: object }).prototype) as Record<string, unknown>;
Object.assign(app, {
$: () => ({ textContent: '', innerHTML: '' }),
loadAppSettingsFromStorage: () => saved,
_api: async () => ({
ok: true,
json: async () => ({
success: true,
data: { settings: { keepCount: 100, keepDays: 0 }, dir: '/d', defaultDir: '/d', backups: [] },
}),
}),
});
return { app, inputs };
}
it('shows the retention the passes use when settings.json has none of the keys (a reset file)', async () => {
const { app, inputs } = load({});
await (app.loadConfigBackups as () => Promise<void>)();
expect(inputs.appSettingsConfigBackupKeepCount.value).toBe(100);
expect(inputs.appSettingsConfigBackupKeepDays.value).toBe(0);
});
it('leaves the form alone when the saved settings state the retention', async () => {
const { app, inputs } = load({ configBackupKeepCount: 7, configBackupKeepDays: 9 });
await (app.loadConfigBackups as () => Promise<void>)();
expect(inputs.appSettingsConfigBackupKeepCount.value).toBe('20'); // the open-settings code owns it
});
});
+43
View File
@@ -32,6 +32,7 @@ import {
resolveConfigBackupDir,
restoreConfigBackup,
configBackupOrigin,
latestBackupRetention,
sweepConfigBackupTemps,
} from '../src/config-backup.js';
@@ -541,6 +542,19 @@ describe('restore trusts only what this module wrote', () => {
expect(() => restoreConfigBackup({ dataDir: data, backupDir: backups, id, keepsModes: false })).toThrow(/symlink/);
});
it('the mode probe never writes through a planted symlink or trips over a planted directory', () => {
put('settings.json', '{"a":1}');
const { id } = made();
const target = join(root, 'victim.txt');
writeFileSync(target, 'precious', { mode: 0o644 });
symlinkSync(target, join(backups, `.mode-probe-${process.pid}`));
mkdirSync(join(backups, '.mode-probe-0'));
expect(() => restoreConfigBackup({ dataDir: data, backupDir: backups, id })).not.toThrow();
expect(() => diffConfigBackups({ dataDir: data, backupDir: backups, from: id, to: 'current' })).not.toThrow();
expect(readFileSync(target, 'utf8')).toBe('precious');
expect(statSync(target).mode & 0o777).toBe(0o644);
});
it('compare applies the same check to a snapshot file', () => {
put('settings.json', '{"a":1}');
const { id } = made();
@@ -559,6 +573,35 @@ describe('restore trusts only what this module wrote', () => {
expect(() => restoreConfigBackup({ dataDir: data, backupDir: backups, id })).not.toThrow();
});
it('keeps the backup retention when the live settings.json was reset and has none of the four keys', () => {
put('settings.json', JSON.stringify({ displayName: 'old', configBackupKeepCount: 100, configBackupKeepDays: 0 }));
const { id } = made();
put('settings.json', '{}');
restoreConfigBackup({ dataDir: data, backupDir: backups, id });
expect(JSON.parse(readFileSync(join(data, 'settings.json'), 'utf8'))).toEqual({
displayName: 'old',
configBackupKeepCount: 100,
configBackupKeepDays: 0,
});
});
it('latestBackupRetention skips a newer snapshot that recorded none, and a pre-restore copy records it', () => {
put('settings.json', '{"a":1}');
createConfigBackup({ dataDir: data, backupDir: backups, reason: 'auto', now: T0, keepCount: 100, keepDays: 0 });
put('settings.json', '{"a":2}');
createConfigBackup({ dataDir: data, backupDir: backups, reason: 'auto', now: T0 + 1000 }); // an older build
const origin = configBackupOrigin(data);
expect(latestBackupRetention(backups, origin)).toEqual({ keepCount: 100, keepDays: 0 });
const id = listConfigBackups(backups, origin).at(-1)!.id;
const r = restoreConfigBackup({ dataDir: data, backupDir: backups, id, now: T0 + 2000 });
expect(r.safetySnapshot).toBeTruthy();
const pre = listConfigBackups(backups, origin).find((b) => b.id === r.safetySnapshot)!;
expect(JSON.parse(readFileSync(join(backups, pre.id, 'manifest.json'), 'utf8'))).toMatchObject({
keepCount: 100,
keepDays: 0,
});
});
it('keeps the current folder and retention when settings.json is restored', () => {
put('settings.json', JSON.stringify({ displayName: 'old', configBackupDir: '/old', configBackupKeepCount: 5 }));
const { id } = made();
+26 -2
View File
@@ -189,12 +189,16 @@ describe('retention survives an unreadable or reset settings.json', () => {
const BASE = Date.UTC(2025, 0, 1);
const dir = () => defaultConfigBackupDir(getDataDir());
/** 30 snapshots a day apart, made under a long-history retention (100 copies, no age limit). */
function seed() {
function seed(withKeys = true) {
settings({ configBackupKeepCount: 100, configBackupKeepDays: 0 });
for (let i = 0; i < 30; i++) {
writeFileSync(
SETTINGS_PATH,
JSON.stringify({ displayName: `v${i}`, configBackupKeepCount: 100, configBackupKeepDays: 0 })
JSON.stringify(
withKeys
? { displayName: `v${i}`, configBackupKeepCount: 100, configBackupKeepDays: 0 }
: { displayName: `v${i}` }
)
);
createConfigBackup({
dataDir: getDataDir(),
@@ -226,6 +230,26 @@ describe('retention survives an unreadable or reset settings.json', () => {
expect(listConfigBackups(dir())).toHaveLength(31);
});
it.each([true, false])(
'a restore after a reset keeps the retention (the backup %s the four keys)',
async (withKeys) => {
seed(withKeys);
const oldest = listConfigBackups(dir()).at(-1)!.id;
writeFileSync(SETTINGS_PATH, '{}'); // the reset
expect((await runConfigBackup('auto'))?.removed).toEqual([]);
const { app } = await createRouteTestHarness(registerConfigBackupRoutes);
const res = await app.inject({ method: 'POST', url: `/api/config-backups/${oldest}/restore` });
expect(res.statusCode).toBe(200);
// the restored file carries the retention, or the recorded one still applies via the pre-restore copy
const out = await runConfigBackup('auto');
expect(out?.removed).toEqual([]);
expect(listConfigBackups(dir()).length).toBeGreaterThanOrEqual(32);
const status = (await app.inject({ method: 'GET', url: '/api/config-backups' })).json().data.settings;
expect(status).toMatchObject({ keepCount: 100, keepDays: 0 }); // what the form shows is what the pass uses
}
);
it('a settings.json that sets the retention still wins', async () => {
seed();
writeFileSync(SETTINGS_PATH, JSON.stringify({ configBackupKeepCount: 5, configBackupKeepDays: 0 }));