70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { mkdtemp } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { tmpdir } from 'node:os'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { MonitorTaskStore } from './MonitorTaskStore'
|
|
|
|
describe('MonitorTaskStore', () => {
|
|
it('持久化监控任务和已提醒的空场集合', async () => {
|
|
const directory = await mkdtemp(join(tmpdir(), 'tennis-book-monitors-'))
|
|
const filePath = join(directory, 'monitor-tasks.json')
|
|
const store = new MonitorTaskStore(filePath)
|
|
await store.add({
|
|
id: 'task-1',
|
|
courtId: 'fansibote-fuzhong',
|
|
targetDate: '2026-07-16',
|
|
startTime: '08:00',
|
|
endTime: '09:00',
|
|
enabled: true,
|
|
createdAt: '2026-07-15T10:00:00.000Z',
|
|
seenAvailableSlotIds: ['slot-1']
|
|
})
|
|
|
|
const reloaded = new MonitorTaskStore(filePath)
|
|
await expect(reloaded.list()).resolves.toEqual([
|
|
expect.objectContaining({ id: 'task-1', seenAvailableSlotIds: ['slot-1'] })
|
|
])
|
|
})
|
|
|
|
it('并发创建相同监控范围时只持久化一个任务', async () => {
|
|
const directory = await mkdtemp(join(tmpdir(), 'tennis-book-monitors-unique-'))
|
|
const store = new MonitorTaskStore(join(directory, 'monitor-tasks.json'))
|
|
const baseTask = {
|
|
courtId: 'fansibote-fuzhong',
|
|
targetDate: '2026-07-16',
|
|
startTime: '08:00',
|
|
endTime: '09:00',
|
|
enabled: true,
|
|
createdAt: '2026-07-15T10:00:00.000Z',
|
|
seenAvailableSlotIds: []
|
|
}
|
|
const results = await Promise.allSettled([
|
|
store.addUnique({ id: 'task-1', ...baseTask }),
|
|
store.addUnique({ id: 'task-2', ...baseTask })
|
|
])
|
|
|
|
expect(results.filter((result) => result.status === 'fulfilled')).toHaveLength(1)
|
|
await expect(store.list()).resolves.toHaveLength(1)
|
|
})
|
|
|
|
it('持久化没有指定日期的持续监控任务', async () => {
|
|
const directory = await mkdtemp(join(tmpdir(), 'tennis-book-monitors-recurring-'))
|
|
const filePath = join(directory, 'monitor-tasks.json')
|
|
await new MonitorTaskStore(filePath).add({
|
|
id: 'task-recurring',
|
|
courtId: 'fansibote-fuzhong',
|
|
startTime: '08:00',
|
|
endTime: '09:00',
|
|
enabled: true,
|
|
createdAt: '2026-07-15T10:00:00.000Z',
|
|
seenAvailableSlotIds: []
|
|
})
|
|
|
|
const tasks = await new MonitorTaskStore(filePath).list()
|
|
expect(tasks).toEqual([
|
|
expect.objectContaining({ id: 'task-recurring', enabled: true })
|
|
])
|
|
expect(tasks[0]).not.toHaveProperty('targetDate')
|
|
})
|
|
})
|