44 lines
1.7 KiB
TypeScript
44 lines
1.7 KiB
TypeScript
// 发送后的运行态投影:把刚启动的 run 合进列表,并用事件上的服务端时间。
|
|
import { describe, expect, it } from 'vitest'
|
|
import type { Run } from '../src/contracts'
|
|
import { activeRun, eventTime, mergeStartedRun } from '../src/store'
|
|
|
|
function run(id: string, status: Run['status'], over: Partial<Run> = {}): Run {
|
|
return {
|
|
id, conversationId: 'c', projectId: 'p', kind: 'conversation', status,
|
|
createdAt: '2026-10-03T00:00:00Z', ...over,
|
|
}
|
|
}
|
|
|
|
describe('mergeStartedRun', () => {
|
|
it('adds a pending run so the composer treats the conversation as active', () => {
|
|
const merged = mergeStartedRun([], run('r1', 'pending'))
|
|
expect(activeRun(merged)?.id).toBe('r1')
|
|
})
|
|
|
|
it('keeps an existing record when the same id arrives again', () => {
|
|
const settled = run('r1', 'completed', { phase: 'done' })
|
|
const merged = mergeStartedRun([settled], run('r1', 'pending'))
|
|
expect(merged).toHaveLength(1)
|
|
expect(merged[0].status).toBe('completed')
|
|
expect(activeRun(merged)).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('eventTime', () => {
|
|
it('parses normal ISO server timestamps', () => {
|
|
const iso = '2026-10-03T14:50:25.073Z'
|
|
expect(eventTime({ ts: iso }, 0)).toBe(Date.parse(iso))
|
|
expect(eventTime({ ts: iso }, 42)).toBe(Date.parse(iso))
|
|
})
|
|
|
|
it('falls back to now when timestamp is missing, invalid or zero-time 0001-01-01T00:00:00Z', () => {
|
|
expect(eventTime({}, 42)).toBe(42)
|
|
expect(eventTime({ ts: undefined }, 42)).toBe(42)
|
|
expect(eventTime({ ts: '' }, 42)).toBe(42)
|
|
expect(eventTime({ ts: 'not-a-time' }, 42)).toBe(42)
|
|
expect(eventTime({ ts: '0001-01-01T00:00:00Z' }, 42)).toBe(42)
|
|
expect(eventTime({ ts: '1970-01-01T00:00:00Z' }, 42)).toBe(42)
|
|
})
|
|
})
|