Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/__tests__/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ describe("fetchRateLimits", () => {
expect(result.primary!.usedPercent).toBe(75)
expect(result.secondary!.usedPercent).toBe(30)
})

it("does not normalize usedPercent of exactly 1 (already 0-100 scale)", async () => {
const proc = createMockProcess()
;(spawn as unknown as Mock).mockReturnValue(proc)

const promise = fetchRateLimits("/usr/local/bin/codex")

await new Promise((r) => setTimeout(r, 0))
proc.stdout.emit("data", Buffer.from(JSON.stringify({ id: 1, result: {} }) + "\n"))
await new Promise((r) => setTimeout(r, 0))
proc.stdout.emit(
"data",
Buffer.from(
JSON.stringify({
id: 2,
result: { rateLimits: { primary: { usedPercent: 1 }, secondary: { usedPercent: 1 } } },
}) + "\n"
)
)

const result = await promise
expect(result.primary!.usedPercent).toBe(1)
expect(result.secondary!.usedPercent).toBe(1)
})
})

describe("readCache", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ export function fetchRateLimits(codexBinary: string): Promise<RateLimitSnapshot>

function normalizeUsedPercent(snapshot: RateLimitSnapshot): void {
if (snapshot.primary && snapshot.primary.usedPercent != null) {
if (snapshot.primary.usedPercent > 0 && snapshot.primary.usedPercent <= 1) {
if (snapshot.primary.usedPercent > 0 && snapshot.primary.usedPercent < 1) {
snapshot.primary.usedPercent *= 100
}
}
if (snapshot.secondary && snapshot.secondary.usedPercent != null) {
if (snapshot.secondary.usedPercent > 0 && snapshot.secondary.usedPercent <= 1) {
if (snapshot.secondary.usedPercent > 0 && snapshot.secondary.usedPercent < 1) {
snapshot.secondary.usedPercent *= 100
}
}
Expand Down
Loading