diff --git a/src/main/rate-limits/codex-fetcher-pty-settle.test.ts b/src/main/rate-limits/codex-fetcher-pty-settle.test.ts new file mode 100644 index 000000000..5c7291411 --- /dev/null +++ b/src/main/rate-limits/codex-fetcher-pty-settle.test.ts @@ -0,0 +1,74 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const { childSpawnMock, resolveCodexCommandMock, ptySpawnMock } = vi.hoisted(() => ({ + childSpawnMock: vi.fn(), + resolveCodexCommandMock: vi.fn(), + ptySpawnMock: vi.fn() +})) + +vi.mock('node:child_process', () => ({ + spawn: childSpawnMock +})) + +vi.mock('../codex-cli/command', () => ({ + resolveCodexCommand: resolveCodexCommandMock +})) + +vi.mock('node-pty', () => ({ + spawn: ptySpawnMock +})) + +import { fetchCodexRateLimits } from './codex-fetcher' + +function makeDisposable() { + return { dispose: vi.fn() } +} + +describe('fetchCodexRateLimits PTY settle timers', () => { + beforeEach(() => { + vi.useFakeTimers() + vi.clearAllMocks() + resolveCodexCommandMock.mockReturnValue('codex') + }) + + it('coalesces the PTY fallback status settle timer while output keeps streaming', async () => { + const ptyHandlers: { onData?: (data: string) => void } = {} + + childSpawnMock.mockImplementation(() => { + throw new Error('rpc unavailable') + }) + ptySpawnMock.mockReturnValue({ + onData: vi.fn((callback) => { + ptyHandlers.onData = callback + return makeDisposable() + }), + onExit: vi.fn(() => makeDisposable()), + write: vi.fn(), + kill: vi.fn() + }) + + const resultPromise = fetchCodexRateLimits() + await vi.advanceTimersByTimeAsync(0) + + const onPtyData = ptyHandlers.onData + if (!onPtyData) { + throw new Error('PTY data handler was not registered') + } + + onPtyData('>') + expect(vi.getTimerCount()).toBe(1) + + onPtyData('5h limit: 17%\n') + onPtyData('Weekly limit: 23%\n') + onPtyData('still rendering\n') + expect(vi.getTimerCount()).toBe(2) + + await vi.advanceTimersByTimeAsync(500) + + await expect(resultPromise).resolves.toMatchObject({ + session: { usedPercent: 17 }, + weekly: { usedPercent: 23 }, + status: 'ok' + }) + }) +}) diff --git a/src/main/rate-limits/codex-fetcher.ts b/src/main/rate-limits/codex-fetcher.ts index f7c89f2de..5ac0e3c8c 100644 --- a/src/main/rate-limits/codex-fetcher.ts +++ b/src/main/rate-limits/codex-fetcher.ts @@ -398,6 +398,7 @@ async function fetchViaPty(options?: FetchCodexRateLimitsOptions): Promise | null = null const term = pty.spawn(spawnFile, spawnArgs, { name: 'xterm-256color', @@ -414,6 +415,10 @@ async function fetchViaPty(options?: FetchCodexRateLimitsOptions): Promise { if (!resolved) { resolved = true + if (settleTimer) { + clearTimeout(settleTimer) + settleTimer = null + } cleanupHiddenRateLimitPty(term, termDisposables, { kill: true }) resolve({ provider: 'codex', @@ -442,8 +447,11 @@ async function fetchViaPty(options?: FetchCodexRateLimitsOptions): Promise { + if (sentStatus && !settleTimer && (FIVE_HOUR_RE.test(output) || WEEKLY_RE.test(output))) { + // Why: after status text is parseable the TUI may continue streaming + // chunks; one settle timer is enough to let the panel finish flushing. + settleTimer = setTimeout(() => { + settleTimer = null if (resolved) { return } @@ -475,6 +483,10 @@ async function fetchViaPty(options?: FetchCodexRateLimitsOptions): Promise { cleanupHiddenRateLimitPty(term, termDisposables, { kill: false }) + if (settleTimer) { + clearTimeout(settleTimer) + settleTimer = null + } if (!resolved) { resolved = true clearTimeout(timeout)