|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' |
| 2 | +import { mkdtempSync, rmSync, writeFileSync, readFileSync, existsSync, readdirSync } from 'fs' |
| 3 | +import { join } from 'path' |
| 4 | +import { tmpdir } from 'os' |
| 5 | + |
| 6 | +// Point persistence at a throwaway userData dir per test. |
| 7 | +let DIR = '' |
| 8 | +vi.mock('./paths', () => ({ |
| 9 | + userDataDir: () => DIR |
| 10 | +})) |
| 11 | + |
| 12 | +import { |
| 13 | + loadConfig, |
| 14 | + saveConfigSync, |
| 15 | + getConfigLoadError, |
| 16 | + validateConfigFile, |
| 17 | + discardCorruptConfigAndReset |
| 18 | +} from './persistence' |
| 19 | + |
| 20 | +const configPath = (): string => join(DIR, 'config.json') |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + DIR = mkdtempSync(join(tmpdir(), 'harness-cfg-')) |
| 24 | +}) |
| 25 | +afterEach(() => { |
| 26 | + rmSync(DIR, { recursive: true, force: true }) |
| 27 | +}) |
| 28 | + |
| 29 | +describe('loadConfig corrupt-file handling', () => { |
| 30 | + it('a missing file is a clean default load (no error, writes enabled)', () => { |
| 31 | + const cfg = loadConfig() |
| 32 | + expect(getConfigLoadError()).toBeNull() |
| 33 | + expect(cfg.repoRoots).toEqual([]) |
| 34 | + // Writes work after a clean load. |
| 35 | + saveConfigSync({ ...cfg, repoRoots: ['/x'] }) |
| 36 | + expect(JSON.parse(readFileSync(configPath(), 'utf-8')).repoRoots).toEqual(['/x']) |
| 37 | + }) |
| 38 | + |
| 39 | + it('malformed JSON: returns defaults, records error, quarantines, suspends writes', () => { |
| 40 | + writeFileSync(configPath(), '{ this is not json') |
| 41 | + const cfg = loadConfig() |
| 42 | + |
| 43 | + // Falls back to defaults rather than throwing. |
| 44 | + expect(cfg.repoRoots).toEqual([]) |
| 45 | + |
| 46 | + // Error captured. |
| 47 | + const err = getConfigLoadError() |
| 48 | + expect(err).not.toBeNull() |
| 49 | + expect(err?.configPath).toBe(configPath()) |
| 50 | + expect(err?.backupPath).toBeTruthy() |
| 51 | + |
| 52 | + // Quarantine copy holds the original bad content. |
| 53 | + expect(readFileSync(err!.backupPath!, 'utf-8')).toBe('{ this is not json') |
| 54 | + |
| 55 | + // Writes are suspended — the bad original survives for hand-editing. |
| 56 | + saveConfigSync({ ...cfg, repoRoots: ['/should-not-persist'] }) |
| 57 | + expect(readFileSync(configPath(), 'utf-8')).toBe('{ this is not json') |
| 58 | + }) |
| 59 | + |
| 60 | + it('validateConfigFile reflects the on-disk state without applying it', () => { |
| 61 | + writeFileSync(configPath(), 'garbage') |
| 62 | + loadConfig() |
| 63 | + expect(validateConfigFile().ok).toBe(false) |
| 64 | + |
| 65 | + // Simulate the user fixing the file by hand. |
| 66 | + writeFileSync(configPath(), JSON.stringify({ repoRoots: ['/fixed'] })) |
| 67 | + expect(validateConfigFile()).toEqual({ ok: true }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('discardCorruptConfigAndReset re-enables writes and persists defaults', () => { |
| 71 | + writeFileSync(configPath(), 'nope') |
| 72 | + loadConfig() |
| 73 | + expect(getConfigLoadError()).not.toBeNull() |
| 74 | + |
| 75 | + const fresh = discardCorruptConfigAndReset() |
| 76 | + expect(getConfigLoadError()).toBeNull() |
| 77 | + |
| 78 | + // Default config written over the bad file, and it parses. |
| 79 | + const onDisk = JSON.parse(readFileSync(configPath(), 'utf-8')) |
| 80 | + expect(onDisk.repoRoots).toEqual([]) |
| 81 | + expect(fresh.connections?.[0].kind).toBe('local') |
| 82 | + |
| 83 | + // Writes resumed. |
| 84 | + saveConfigSync({ ...fresh, repoRoots: ['/y'] }) |
| 85 | + expect(JSON.parse(readFileSync(configPath(), 'utf-8')).repoRoots).toEqual(['/y']) |
| 86 | + }) |
| 87 | + |
| 88 | + it('atomic write leaves no stray .tmp file behind', () => { |
| 89 | + const cfg = loadConfig() |
| 90 | + saveConfigSync({ ...cfg, repoRoots: ['/z'] }) |
| 91 | + const leftovers = readdirSync(DIR).filter((f) => f.endsWith('.tmp')) |
| 92 | + expect(leftovers).toEqual([]) |
| 93 | + expect(existsSync(configPath())).toBe(true) |
| 94 | + }) |
| 95 | +}) |
0 commit comments