mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 18:10:18 +05:30
feat(migration): add v4-to-v5 settings migration runner with backup
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import { migrateV4ToV5, v4SettingsSchema } from '@/lib/migrations/v4-to-v5';
|
||||
|
||||
describe('migrateV4ToV5', () => {
|
||||
test('maps theme auto to system', () => {
|
||||
const out = migrateV4ToV5({ theme: 'auto', editorFontSize: 14 });
|
||||
expect(out.theme).toBe('system');
|
||||
expect(out.editorFontSize).toBe(14);
|
||||
});
|
||||
|
||||
test('drops unknown keys, applies defaults', () => {
|
||||
const out = migrateV4ToV5({ theme: 'light', weirdKey: 1 });
|
||||
expect(out.theme).toBe('light');
|
||||
expect(out.weirdKey).toBeUndefined();
|
||||
expect(out.tabSize).toBe(4); // default
|
||||
});
|
||||
|
||||
test('throws on invalid v4 input', () => {
|
||||
expect(() => migrateV4ToV5({ theme: 42 })).toThrow();
|
||||
});
|
||||
|
||||
test('is idempotent — running twice yields the same v5 output', () => {
|
||||
const v4 = { theme: 'dark', editorFontSize: 16, recentFiles: ['/a.md'] };
|
||||
const a = migrateV4ToV5(v4);
|
||||
const b = migrateV4ToV5(v4);
|
||||
expect(a).toEqual(b);
|
||||
});
|
||||
|
||||
test('exposes the v4 schema for use by main', () => {
|
||||
expect(v4SettingsSchema).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const { MigrationRunner } = require('../../../../src/main/updater/migration-runner');
|
||||
|
||||
function tmpDir() {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), 'mig-test-'));
|
||||
}
|
||||
|
||||
describe('MigrationRunner', () => {
|
||||
test('no-op when migration.version is already 5', () => {
|
||||
const dir = tmpDir();
|
||||
fs.writeFileSync(path.join(dir, 'settings.json'), JSON.stringify({ 'migration.version': 5 }));
|
||||
const r = new MigrationRunner({ dir, transform: (v) => v });
|
||||
expect(r.run()).toBe('skipped');
|
||||
fs.rmSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
test('runs migration on missing v5 marker, writes v5 file, backs up v4', () => {
|
||||
const dir = tmpDir();
|
||||
fs.writeFileSync(path.join(dir, 'settings.json'), JSON.stringify({ theme: 'light' }));
|
||||
const r = new MigrationRunner({ dir, transform: (v) => ({ ...v, theme: 'system', 'migration.version': 5 }) });
|
||||
expect(r.run()).toBe('migrated');
|
||||
expect(JSON.parse(fs.readFileSync(path.join(dir, 'settings.json'), 'utf-8'))['migration.version']).toBe(5);
|
||||
expect(fs.existsSync(path.join(dir, 'settings.v4.bak.json'))).toBe(true);
|
||||
fs.rmSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
test('returns fresh on missing file', () => {
|
||||
const dir = tmpDir();
|
||||
const r = new MigrationRunner({ dir, transform: () => ({ 'migration.version': 5 }) });
|
||||
expect(r.run()).toBe('fresh');
|
||||
expect(fs.existsSync(path.join(dir, 'settings.json'))).toBe(true);
|
||||
fs.rmSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
test('returns failed and preserves v4 on transform throw', () => {
|
||||
const dir = tmpDir();
|
||||
fs.writeFileSync(path.join(dir, 'settings.json'), JSON.stringify({ theme: 'light' }));
|
||||
const r = new MigrationRunner({ dir, transform: () => { throw new Error('boom'); } });
|
||||
expect(r.run()).toBe('failed');
|
||||
expect(fs.readFileSync(path.join(dir, 'settings.json'), 'utf-8')).toContain('light');
|
||||
fs.rmSync(dir, { recursive: true });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user