Use shared clock for active usage timers

This commit is contained in:
Novick Yuan
2026-05-28 14:30:15 +08:00
parent 6de684df59
commit 9b5c3ecd23
5 changed files with 140 additions and 36 deletions
@@ -0,0 +1,56 @@
import { afterEach, describe, expect, it } from 'vitest'
import { createApp, type App } from 'vue'
import ElapsedTimeText from '../ElapsedTimeText.vue'
const mountedApps: Array<{ app: App, root: HTMLElement }> = []
function mountElapsedTimeText(props: Record<string, unknown>) {
const root = document.createElement('div')
document.body.appendChild(root)
const app = createApp(ElapsedTimeText, props)
app.mount(root)
mountedApps.push({ app, root })
return root
}
afterEach(() => {
for (const { app, root } of mountedApps.splice(0)) {
app.unmount()
root.remove()
}
})
describe('ElapsedTimeText', () => {
it('uses the supplied display clock for active requests', () => {
const root = mountElapsedTimeText({
createdAt: '2026-05-28T12:00:00Z',
status: 'streaming',
responseTimeMs: 10_000,
displayNowMs: Date.parse('2026-05-28T12:00:40Z'),
})
expect(root.textContent).toBe('40.00s')
})
it('keeps terminal requests pinned to the backend final duration', () => {
const root = mountElapsedTimeText({
createdAt: '2026-05-28T12:00:00Z',
status: 'completed',
responseTimeMs: 42_340,
displayNowMs: Date.parse('2026-05-28T12:01:30Z'),
})
expect(root.textContent).toBe('42.34s')
})
it('clamps active elapsed time at zero when the display clock is behind', () => {
const root = mountElapsedTimeText({
createdAt: '2026-05-28T12:00:40Z',
status: 'pending',
displayNowMs: Date.parse('2026-05-28T12:00:00Z'),
})
expect(root.textContent).toBe('0.00s')
})
})
@@ -91,8 +91,16 @@ vi.mock('lucide-vue-next', async () => {
vi.mock('../ElapsedTimeText.vue', () => ({
default: defineComponent({
name: 'ElapsedTimeTextStub',
setup() {
return () => h('span', 'elapsed')
props: {
displayNowMs: {
type: Number,
default: null,
},
},
setup(props) {
return () => h('span', {
'data-display-now-ms': props.displayNowMs == null ? '' : String(props.displayNowMs),
}, 'elapsed')
},
}),
}))
@@ -243,6 +251,16 @@ describe('UsageRecordsTable', () => {
expect(root.querySelector('[data-active-latency-state="waiting-first-byte"]')).toBeNull()
})
it('passes the shared display clock to active elapsed text', () => {
const root = mountUsageRecordsTable([buildRecord({
status: 'streaming',
response_time_ms: null,
first_byte_time_ms: 500,
})], { displayNowMs: 1_779_999_000_000 })
expect(root.querySelector('[data-display-now-ms="1779999000000"]')).not.toBeNull()
})
it('shows failed when Codex image progress fails before the usage record finalizes', () => {
const root = mountUsageRecordsTable([buildRecord({
status: 'pending',