Files

57 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

import test from 'node:test'
import assert from 'node:assert/strict'
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { parseUsageLine, readLatestUsage } from '../electron/usage-reader.js'
test('parses primary and secondary Codex rate limit windows', () => {
const line = JSON.stringify({
timestamp: '2026-07-16T08:00:00.000Z',
type: 'event_msg',
payload: {
type: 'token_count',
rate_limits: {
primary: { window_minutes: 300, used_percent: 32, resets_at: 1784203200 },
secondary: { window_minutes: 10080, used_percent: 59, resets_at: 1784808000 },
},
},
})
const snapshots = parseUsageLine(line, 'rollout-test.jsonl')
assert.equal(snapshots.length, 2)
assert.equal(snapshots[0].remainingPercent, 68)
assert.equal(snapshots[1].remainingPercent, 41)
assert.equal(snapshots[0].sourceFile, 'rollout-test.jsonl')
})
test('ignores malformed or unrelated lines', () => {
assert.deepEqual(parseUsageLine('{broken'), [])
assert.deepEqual(parseUsageLine(JSON.stringify({ type: 'event_msg', payload: { type: 'message' } })), [])
})
test('does not expose an expired five-hour window', () => {
const sessionsPath = mkdtempSync(path.join(os.tmpdir(), 'codex-halo-test-'))
const now = Date.now()
const line = JSON.stringify({
timestamp: new Date(now).toISOString(),
type: 'event_msg',
payload: {
type: 'token_count',
rate_limits: {
primary: { window_minutes: 300, used_percent: 31, resets_at: Math.floor(now / 1000) - 30 },
secondary: { window_minutes: 10080, used_percent: 79, resets_at: Math.floor(now / 1000) + 3600 },
},
},
})
try {
writeFileSync(path.join(sessionsPath, 'rollout-test.jsonl'), line)
const snapshot = readLatestUsage(sessionsPath)
assert.equal(snapshot.shortWindow, null)
assert.equal(snapshot.weekWindow.remainingPercent, 21)
} finally {
rmSync(sessionsPath, { recursive: true, force: true })
}
})