|
1 | | -import { describe, expect, it, vi } from "vitest"; |
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { promises as fs } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
2 | 5 | import type { UsageLedgerRow, UsageSummary } from "../lib/usage/index.js"; |
3 | 6 | import { runUsageCommand } from "../lib/codex-manager/commands/usage.js"; |
| 7 | +import { removeWithRetry } from "./helpers/remove-with-retry.js"; |
4 | 8 |
|
5 | 9 | function makeSummary(overrides: Partial<UsageSummary> = {}): UsageSummary { |
6 | 10 | return { |
@@ -228,3 +232,135 @@ describe("usage command", () => { |
228 | 232 | }); |
229 | 233 | }); |
230 | 234 |
|
| 235 | +describe("usage command --since parsing", () => { |
| 236 | + const NOW = 1_750_000_000_000; |
| 237 | + |
| 238 | + afterEach(() => { |
| 239 | + vi.useRealTimers(); |
| 240 | + }); |
| 241 | + |
| 242 | + async function sinceSentToLedger( |
| 243 | + args: string[], |
| 244 | + ): Promise<number | Date | string | undefined> { |
| 245 | + let captured: number | Date | string | undefined; |
| 246 | + const exitCode = await runUsageCommand(args, { |
| 247 | + logInfo: vi.fn(), |
| 248 | + summarizeUsage: async (query) => { |
| 249 | + captured = query?.since; |
| 250 | + return makeSummary(); |
| 251 | + }, |
| 252 | + }); |
| 253 | + expect(exitCode).toBe(0); |
| 254 | + return captured; |
| 255 | + } |
| 256 | + |
| 257 | + it("resolves relative durations against the current clock", async () => { |
| 258 | + vi.useFakeTimers(); |
| 259 | + vi.setSystemTime(NOW); |
| 260 | + |
| 261 | + await expect(sinceSentToLedger(["--since", "30m"])).resolves.toBe( |
| 262 | + NOW - 30 * 60_000, |
| 263 | + ); |
| 264 | + await expect(sinceSentToLedger(["--since=24h"])).resolves.toBe( |
| 265 | + NOW - 24 * 3_600_000, |
| 266 | + ); |
| 267 | + await expect(sinceSentToLedger(["--since", "7d"])).resolves.toBe( |
| 268 | + NOW - 7 * 86_400_000, |
| 269 | + ); |
| 270 | + await expect(sinceSentToLedger(["--since", "2W"])).resolves.toBe( |
| 271 | + NOW - 2 * 604_800_000, |
| 272 | + ); |
| 273 | + }); |
| 274 | + |
| 275 | + it("passes epoch numbers through as numbers and dates as strings", async () => { |
| 276 | + await expect(sinceSentToLedger(["--since", "12345"])).resolves.toBe(12345); |
| 277 | + await expect(sinceSentToLedger(["--since", " 2026-01-02 "])).resolves.toBe( |
| 278 | + "2026-01-02", |
| 279 | + ); |
| 280 | + }); |
| 281 | + |
| 282 | + it("rejects --since without a value", async () => { |
| 283 | + const logError = vi.fn(); |
| 284 | + const logInfo = vi.fn(); |
| 285 | + const exitCode = await runUsageCommand(["--since"], { logError, logInfo }); |
| 286 | + expect(exitCode).toBe(1); |
| 287 | + expect(String(logError.mock.calls[0]?.[0])).toContain( |
| 288 | + "Missing value for --since", |
| 289 | + ); |
| 290 | + // parse failures also re-print the usage help |
| 291 | + expect(String(logInfo.mock.calls[0]?.[0])).toContain("Usage:"); |
| 292 | + }); |
| 293 | +}); |
| 294 | + |
| 295 | +describe("usage command default file writer", () => { |
| 296 | + let tempDir: string; |
| 297 | + |
| 298 | + afterEach(async () => { |
| 299 | + vi.restoreAllMocks(); |
| 300 | + await removeWithRetry(tempDir, { recursive: true, force: true }); |
| 301 | + }); |
| 302 | + |
| 303 | + function deps(overrides: { logError?: ReturnType<typeof vi.fn> } = {}) { |
| 304 | + return { |
| 305 | + logInfo: vi.fn(), |
| 306 | + logError: vi.fn(), |
| 307 | + getCwd: () => tempDir, |
| 308 | + summarizeUsage: async () => makeSummary(), |
| 309 | + ...overrides, |
| 310 | + }; |
| 311 | + } |
| 312 | + |
| 313 | + it("writes the rendered report atomically into nested directories", async () => { |
| 314 | + tempDir = await fs.mkdtemp(join(tmpdir(), "codex-usage-write-")); |
| 315 | + const commandDeps = deps(); |
| 316 | + const exitCode = await runUsageCommand( |
| 317 | + ["--out", join("reports", "usage.txt")], |
| 318 | + commandDeps, |
| 319 | + ); |
| 320 | + |
| 321 | + expect(exitCode).toBe(0); |
| 322 | + const written = await fs.readFile( |
| 323 | + join(tempDir, "reports", "usage.txt"), |
| 324 | + "utf8", |
| 325 | + ); |
| 326 | + expect(written).toContain("Usage summary by model"); |
| 327 | + expect(written.endsWith("\n")).toBe(true); |
| 328 | + // the staged .tmp file must be consumed by the rename |
| 329 | + const leftovers = await fs.readdir(join(tempDir, "reports")); |
| 330 | + expect(leftovers).toEqual(["usage.txt"]); |
| 331 | + }); |
| 332 | + |
| 333 | + it("retries the final rename on transient EBUSY and still succeeds", async () => { |
| 334 | + tempDir = await fs.mkdtemp(join(tmpdir(), "codex-usage-retry-")); |
| 335 | + const renameSpy = vi |
| 336 | + .spyOn(fs, "rename") |
| 337 | + .mockRejectedValueOnce(Object.assign(new Error("busy"), { code: "EBUSY" })); |
| 338 | + const exitCode = await runUsageCommand(["--out", "usage.txt"], deps()); |
| 339 | + |
| 340 | + expect(exitCode).toBe(0); |
| 341 | + expect(renameSpy).toHaveBeenCalledTimes(2); |
| 342 | + const written = await fs.readFile(join(tempDir, "usage.txt"), "utf8"); |
| 343 | + expect(written).toContain("Usage summary by model"); |
| 344 | + expect(await fs.readdir(tempDir)).toEqual(["usage.txt"]); |
| 345 | + }); |
| 346 | + |
| 347 | + it("fails fast on non-retryable rename errors and removes the staged temp file", async () => { |
| 348 | + tempDir = await fs.mkdtemp(join(tmpdir(), "codex-usage-fail-")); |
| 349 | + vi.spyOn(fs, "rename").mockRejectedValue( |
| 350 | + Object.assign(new Error("no space"), { code: "ENOSPC" }), |
| 351 | + ); |
| 352 | + const logError = vi.fn(); |
| 353 | + const exitCode = await runUsageCommand( |
| 354 | + ["--out", "usage.txt"], |
| 355 | + deps({ logError }), |
| 356 | + ); |
| 357 | + |
| 358 | + expect(exitCode).toBe(1); |
| 359 | + expect(String(logError.mock.calls[0]?.[0])).toContain( |
| 360 | + "Failed to write usage report: no space", |
| 361 | + ); |
| 362 | + // neither the target nor any secret/staging .tmp file may survive |
| 363 | + expect(await fs.readdir(tempDir)).toEqual([]); |
| 364 | + }); |
| 365 | +}); |
| 366 | + |
0 commit comments