|
1 | | -import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 1 | +import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
2 | 2 | import { tmpdir } from 'node:os' |
3 | 3 | import { dirname, join } from 'node:path' |
4 | 4 | import { describe, expect, it } from 'vitest' |
@@ -90,6 +90,82 @@ describe('createConfigStore', () => { |
90 | 90 | expect(readFileSync(filePath, 'utf8')).not.toContain('private.example') |
91 | 91 | }) |
92 | 92 |
|
| 93 | + it('names why a rejected file was not used and keeps a copy when repairing it', () => { |
| 94 | + for (const [contents, problem] of [ |
| 95 | + ['{ "origin": "https://sim.example.com", }', 'unparseable'], |
| 96 | + ['[]', 'unparseable'], |
| 97 | + ['{ "origin": null }', 'unparseable'], |
| 98 | + ['{ "origin": "http://192.168.1.20:3000" }', 'invalid-origin'], |
| 99 | + ] as const) { |
| 100 | + const filePath = tempSettingsPath() |
| 101 | + writeFileSync(filePath, contents) |
| 102 | + const store = createConfigStore(filePath, {}) |
| 103 | + expect(store.getLoadProblem()?.kind).toBe(problem) |
| 104 | + |
| 105 | + expect(store.setOrigin(DEFAULT_ORIGIN).ok).toBe(true) |
| 106 | + expect(store.getLoadProblem()).toBeNull() |
| 107 | + const backups = readdirSync(dirname(filePath)).filter((name) => |
| 108 | + name.startsWith('settings.json.bak-') |
| 109 | + ) |
| 110 | + expect(backups).toHaveLength(1) |
| 111 | + expect(readFileSync(join(dirname(filePath), backups[0]), 'utf8')).toBe(contents) |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + it('reports no problem for a valid or missing file and keeps no copy when changing servers', () => { |
| 116 | + const filePath = tempSettingsPath() |
| 117 | + expect(createConfigStore(filePath, {}).getLoadProblem()).toBeNull() |
| 118 | + writeFileSync(filePath, JSON.stringify({ origin: 'https://sim.example.com' })) |
| 119 | + const store = createConfigStore(filePath, {}) |
| 120 | + expect(store.getLoadProblem()).toBeNull() |
| 121 | + expect(store.setOrigin('https://other.example.com').ok).toBe(true) |
| 122 | + expect(readdirSync(dirname(filePath))).toEqual(['settings.json']) |
| 123 | + }) |
| 124 | + |
| 125 | + it('retries a failed write instead of dropping every later change', () => { |
| 126 | + const filePath = tempSettingsPath() |
| 127 | + const parent = dirname(filePath) |
| 128 | + const store = createConfigStore(filePath, {}) |
| 129 | + rmSync(parent, { recursive: true }) |
| 130 | + writeFileSync(parent, 'not a directory') |
| 131 | + |
| 132 | + try { |
| 133 | + store.set('zoomLevel', 2) |
| 134 | + expect(store.flush()).toBe(false) |
| 135 | + |
| 136 | + rmSync(parent) |
| 137 | + mkdirSync(parent) |
| 138 | + store.set('trayEnabled', false) |
| 139 | + expect(store.flush()).toBe(true) |
| 140 | + expect(JSON.parse(readFileSync(filePath, 'utf8'))).toMatchObject({ |
| 141 | + zoomLevel: 2, |
| 142 | + trayEnabled: false, |
| 143 | + }) |
| 144 | + } finally { |
| 145 | + rmSync(parent, { recursive: true, force: true }) |
| 146 | + } |
| 147 | + }) |
| 148 | + |
| 149 | + it('reports a re-confirmed origin as saved only once pending changes reach disk', () => { |
| 150 | + const filePath = tempSettingsPath() |
| 151 | + const parent = dirname(filePath) |
| 152 | + const store = createConfigStore(filePath, {}) |
| 153 | + rmSync(parent, { recursive: true }) |
| 154 | + writeFileSync(parent, 'not a directory') |
| 155 | + |
| 156 | + try { |
| 157 | + store.set('computerUseAllowedApps', []) |
| 158 | + expect(store.setOrigin(DEFAULT_ORIGIN).ok).toBe(false) |
| 159 | + |
| 160 | + rmSync(parent) |
| 161 | + mkdirSync(parent) |
| 162 | + expect(store.setOrigin(DEFAULT_ORIGIN).ok).toBe(true) |
| 163 | + expect(JSON.parse(readFileSync(filePath, 'utf8')).computerUseAllowedApps).toEqual([]) |
| 164 | + } finally { |
| 165 | + rmSync(parent, { recursive: true, force: true }) |
| 166 | + } |
| 167 | + }) |
| 168 | + |
93 | 169 | it('keeps the active origin unchanged when its immediate write fails', () => { |
94 | 170 | const filePath = tempSettingsPath() |
95 | 171 | const parent = dirname(filePath) |
|
0 commit comments