You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.3 KiB
89 lines
3.3 KiB
import { afterEach, describe, expect, it } from 'vitest'
|
|
import { mkdtempSync, rmSync, writeFileSync, readdirSync, readFileSync, mkdirSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { JsonStore, Signal } from '@template/core'
|
|
import { settingsSchema } from '@template/contracts'
|
|
|
|
const directories: string[] = []
|
|
function location(): string {
|
|
const dir = mkdtempSync(join(tmpdir(), 'workshop-unit-'))
|
|
directories.push(dir)
|
|
return join(dir, 'settings.json')
|
|
}
|
|
afterEach(() => {
|
|
for (const dir of directories.splice(0)) rmSync(dir, { recursive: true, force: true })
|
|
})
|
|
const defaults = settingsSchema.parse({})
|
|
|
|
describe('validated atomic settings', () => {
|
|
it('adds defaults for new fields and drops obsolete fields', () => {
|
|
const file = location()
|
|
writeFileSync(file, JSON.stringify({ theme: 'dark', obsolete: true }))
|
|
const store = new JsonStore(file, settingsSchema, defaults)
|
|
expect(store.get()).toEqual({ ...defaults, theme: 'dark' })
|
|
})
|
|
|
|
it('backs up invalid data and restores a readable defaults file', () => {
|
|
const file = location()
|
|
writeFileSync(file, '{broken')
|
|
const store = new JsonStore(file, settingsSchema, defaults)
|
|
expect(store.recoveredFromBackup).toBe(true)
|
|
expect(readdirSync(join(file, '..')).filter((name) => name.endsWith('.bak'))).toHaveLength(1)
|
|
expect(JSON.parse(readFileSync(file, 'utf8'))).toEqual(defaults)
|
|
})
|
|
|
|
it('persists before notifying and isolates snapshots', () => {
|
|
const file = location()
|
|
const store = new JsonStore(file, settingsSchema, defaults)
|
|
const seen: string[] = []
|
|
const stop = store.changed.subscribe((value) => {
|
|
seen.push(value.theme)
|
|
expect(JSON.parse(readFileSync(file, 'utf8')).theme).toBe(value.theme)
|
|
})
|
|
store.set({ theme: 'dark' })
|
|
stop()
|
|
store.set({ language: 'en' })
|
|
const snapshot = store.get()
|
|
snapshot.window.width = 720
|
|
expect(store.get().window.width).toBe(defaults.window.width)
|
|
expect(seen).toEqual(['dark'])
|
|
expect(readdirSync(join(file, '..')).some((name) => name.endsWith('.tmp'))).toBe(false)
|
|
})
|
|
|
|
it('rejects invalid writes without changing memory or disk', () => {
|
|
const file = location()
|
|
const store = new JsonStore(file, settingsSchema, defaults)
|
|
store.set({ theme: 'light' })
|
|
const previous = readFileSync(file, 'utf8')
|
|
expect(() => store.set({ window: { width: -1, height: 700 } })).toThrow()
|
|
expect(store.get().theme).toBe('light')
|
|
expect(readFileSync(file, 'utf8')).toBe(previous)
|
|
})
|
|
|
|
it('does not commit in-memory changes when disk replacement fails', () => {
|
|
const file = location()
|
|
const store = new JsonStore(file, settingsSchema, defaults)
|
|
mkdirSync(file)
|
|
expect(() => store.set({ theme: 'dark' })).toThrow()
|
|
expect(store.get()).toEqual(defaults)
|
|
expect(readdirSync(join(file, '..')).some((name) => name.endsWith('.tmp'))).toBe(false)
|
|
})
|
|
|
|
it('releases observers and still notifies other observers after one fails', () => {
|
|
const signal = new Signal<number>()
|
|
let received = 0
|
|
const stop = signal.subscribe(() => {
|
|
throw new Error('bad listener')
|
|
})
|
|
signal.subscribe((value) => {
|
|
received = value
|
|
})
|
|
expect(() => signal.emit(2)).toThrow('Signal listeners failed')
|
|
expect(received).toBe(2)
|
|
stop()
|
|
stop()
|
|
signal.clear()
|
|
expect(signal.size).toBe(0)
|
|
})
|
|
})
|
|
|