68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
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'
|
||
})
|
||
})
|
||
})
|