Files
tennis-book/src/main/settings/CourtSettingsStore.test.ts
richarjiang f8c8c688a1 init
2026-07-15 17:12:04 +08:00

68 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { mkdtemp, readFile, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { describe, expect, it } from 'vitest'
import { CourtSettingsStore } from './CourtSettingsStore'
describe('CourtSettingsStore', () => {
it('未配置时返回对应球场的默认值', async () => {
const directory = await mkdtemp(join(tmpdir(), 'tennis-book-settings-'))
const store = new CourtSettingsStore(join(directory, 'court-settings.json'))
await expect(store.get('court-a', {
courtId: 'court-a'
})).resolves.toEqual({ courtId: 'court-a' })
})
it('按球场隔离并持久化访客凭证', async () => {
const directory = await mkdtemp(join(tmpdir(), 'tennis-book-settings-'))
const filePath = join(directory, 'court-settings.json')
const store = new CourtSettingsStore(filePath)
await store.update({
courtId: 'court-a',
visitorId: 'court-a-secret'
})
await store.update({ courtId: 'court-b' })
const reloaded = new CourtSettingsStore(filePath)
await expect(reloaded.get('court-a', {
courtId: 'court-a'
})).resolves.toEqual({
courtId: 'court-a',
visitorId: 'court-a-secret'
})
await expect(reloaded.get('court-b', {
courtId: 'court-b'
})).resolves.toEqual({ courtId: 'court-b' })
const persisted = JSON.parse(await readFile(filePath, 'utf8')) as unknown
expect(persisted).toEqual({
version: 1,
courts: {
'court-a': { visitorId: 'court-a-secret' },
'court-b': {}
}
})
})
it('忽略旧版暴露的场地 ID只迁移访客凭证', async () => {
const directory = await mkdtemp(join(tmpdir(), 'tennis-book-settings-legacy-'))
const filePath = join(directory, 'court-settings.json')
await writeFile(filePath, JSON.stringify({
version: 1,
courts: {
'court-a': { userId: '6038652', visitorId: 'legacy-secret' }
}
}))
const store = new CourtSettingsStore(filePath)
await expect(store.get('court-a', {
courtId: 'court-a'
})).resolves.toEqual({
courtId: 'court-a',
visitorId: 'legacy-secret'
})
})
})