* Classify Codex app-server chatgpt-auth-required as an auth error When auth.json holds only an OPENAI_API_KEY (no ChatGPT tokens), the app-server RPC rejects account/rateLimits/read with "chatgpt authentication required to read rate limits". That string matched none of CODEX_AUTH_ERROR_PATTERNS, so fetchCodexRateLimits fell through to the hidden PTY /status probe, which cannot render usage for such accounts and burned the full 15s PTY timeout on every refresh cycle, surfacing as a permanent "Refresh failed — PTY timeout" status chip. Classify the message as an auth error so the RPC result is returned directly (fast, accurate) and no PTY is spawned. * Keep auth-required usage errors from rendering as a rate-limit Limited label The new Codex app-server error 'chatgpt authentication required to read rate limits' mentions rate limits only as the object it failed to read, but the status bar's rate-limit classifier matched the phrase and labeled the chip 'Limited'. Classify authentication-required messages as auth failures so they get the standard softened refresh copy instead.
181 lines
5.1 KiB
TypeScript
181 lines
5.1 KiB
TypeScript
import { EventEmitter } from 'node:events'
|
|
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
|
|
}))
|
|
|
|
// Auth gate is covered separately; these tests assume a signed-in Codex.
|
|
vi.mock('./codex-auth-presence', () => ({
|
|
probeCodexAuthPresence: vi.fn(() => 'present')
|
|
}))
|
|
|
|
import { fetchCodexRateLimits } from './codex-fetcher'
|
|
|
|
function makeDisposable() {
|
|
return { dispose: vi.fn() }
|
|
}
|
|
|
|
function makeRpcChild() {
|
|
const child = new EventEmitter() as EventEmitter & {
|
|
stdout: EventEmitter
|
|
stderr: EventEmitter
|
|
stdin: { write: ReturnType<typeof vi.fn> }
|
|
kill: ReturnType<typeof vi.fn>
|
|
}
|
|
child.stdout = new EventEmitter()
|
|
child.stderr = new EventEmitter()
|
|
child.stdin = { write: vi.fn() }
|
|
child.kill = vi.fn()
|
|
return child
|
|
}
|
|
|
|
describe('fetchCodexRateLimits auth errors', () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers()
|
|
vi.clearAllMocks()
|
|
resolveCodexCommandMock.mockReturnValue('codex')
|
|
})
|
|
|
|
it('returns Codex RPC auth refresh errors without masking them behind PTY fallback', async () => {
|
|
const rpcChild = makeRpcChild()
|
|
const authError =
|
|
'Your access token could not be refreshed because your refresh token was already used. Please log out and sign in again.'
|
|
|
|
childSpawnMock.mockReturnValue(rpcChild)
|
|
rpcChild.stdin.write.mockImplementation((line: string) => {
|
|
const msg = JSON.parse(line) as { id?: number; method?: string }
|
|
if (msg.method === 'initialize') {
|
|
setTimeout(() => {
|
|
rpcChild.stdout.emit(
|
|
'data',
|
|
Buffer.from(`${JSON.stringify({ jsonrpc: '2.0', id: msg.id, result: {} })}\n`)
|
|
)
|
|
}, 0)
|
|
}
|
|
if (msg.method === 'account/rateLimits/read') {
|
|
setTimeout(() => {
|
|
rpcChild.stdout.emit(
|
|
'data',
|
|
Buffer.from(
|
|
`${JSON.stringify({
|
|
jsonrpc: '2.0',
|
|
id: msg.id,
|
|
error: { code: -32000, message: authError }
|
|
})}\n`
|
|
)
|
|
)
|
|
}, 0)
|
|
}
|
|
})
|
|
|
|
const resultPromise = fetchCodexRateLimits()
|
|
await vi.advanceTimersByTimeAsync(1)
|
|
await vi.advanceTimersByTimeAsync(1)
|
|
|
|
await expect(resultPromise).resolves.toMatchObject({
|
|
provider: 'codex',
|
|
session: null,
|
|
weekly: null,
|
|
status: 'error',
|
|
error: authError
|
|
})
|
|
expect(ptySpawnMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns the app-server chatgpt-auth-required error without spawning the PTY probe', async () => {
|
|
const rpcChild = makeRpcChild()
|
|
const authError = 'chatgpt authentication required to read rate limits'
|
|
|
|
childSpawnMock.mockReturnValue(rpcChild)
|
|
rpcChild.stdin.write.mockImplementation((line: string) => {
|
|
const msg = JSON.parse(line) as { id?: number; method?: string }
|
|
if (msg.method === 'initialize') {
|
|
setTimeout(() => {
|
|
rpcChild.stdout.emit(
|
|
'data',
|
|
Buffer.from(`${JSON.stringify({ jsonrpc: '2.0', id: msg.id, result: {} })}\n`)
|
|
)
|
|
}, 0)
|
|
}
|
|
if (msg.method === 'account/rateLimits/read') {
|
|
setTimeout(() => {
|
|
rpcChild.stdout.emit(
|
|
'data',
|
|
Buffer.from(
|
|
`${JSON.stringify({
|
|
jsonrpc: '2.0',
|
|
id: msg.id,
|
|
error: { code: -32600, message: authError }
|
|
})}\n`
|
|
)
|
|
)
|
|
}, 0)
|
|
}
|
|
})
|
|
|
|
const resultPromise = fetchCodexRateLimits()
|
|
await vi.advanceTimersByTimeAsync(1)
|
|
await vi.advanceTimersByTimeAsync(1)
|
|
|
|
await expect(resultPromise).resolves.toMatchObject({
|
|
provider: 'codex',
|
|
session: null,
|
|
weekly: null,
|
|
status: 'error',
|
|
error: authError
|
|
})
|
|
expect(ptySpawnMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('preserves Codex PTY auth errors when the CLI exits before status is available', async () => {
|
|
const ptyHandlers: { onData?: (data: string) => void; onExit?: () => void } = {}
|
|
const authError =
|
|
'Error loading configuration: Your authentication session could not be refreshed automatically.'
|
|
|
|
childSpawnMock.mockImplementation(() => {
|
|
throw new Error('rpc unavailable')
|
|
})
|
|
ptySpawnMock.mockReturnValue({
|
|
onData: vi.fn((callback) => {
|
|
ptyHandlers.onData = callback
|
|
return makeDisposable()
|
|
}),
|
|
onExit: vi.fn((callback) => {
|
|
ptyHandlers.onExit = callback
|
|
return makeDisposable()
|
|
}),
|
|
write: vi.fn(),
|
|
kill: vi.fn()
|
|
})
|
|
|
|
const resultPromise = fetchCodexRateLimits()
|
|
await vi.advanceTimersByTimeAsync(0)
|
|
|
|
ptyHandlers.onData?.(`${authError}\n`)
|
|
ptyHandlers.onExit?.()
|
|
|
|
await expect(resultPromise).resolves.toMatchObject({
|
|
provider: 'codex',
|
|
session: null,
|
|
weekly: null,
|
|
status: 'error',
|
|
error: authError
|
|
})
|
|
})
|
|
})
|