Skip to content

fix(test): isolate usage-log fixtures from the real OpenCodex home - #997

Open
Yuxin-Qiao wants to merge 1 commit into
lidge-jun:devfrom
Yuxin-Qiao:codex/fix-test-usage-log-isolation
Open

fix(test): isolate usage-log fixtures from the real OpenCodex home#997
Yuxin-Qiao wants to merge 1 commit into
lidge-jun:devfrom
Yuxin-Qiao:codex/fix-test-usage-log-isolation

Conversation

@Yuxin-Qiao

@Yuxin-Qiao Yuxin-Qiao commented Aug 4, 2026

Copy link
Copy Markdown

Summary

  • tests/management-api-logs-metrics.test.ts exercises GET /api/logs display metrics through addRequestLog, which persists every row to usage.jsonl. Unlike its sibling suites that also persist (usage-failure-persistence.test.ts, api-key-attribution.test.ts), it did not redirect OPENCODEX_HOME itself and relied entirely on the bunfig.toml preload (tests/preload.ts).
  • The preload only loads when Bun discovers bunfig.toml from the current directory. Running bun test <absolute path> from outside the repo (agent/IDE pattern) skips it, and the fixture rows (ok-a/ok-b/fail, no-such-model, unpriced-model, combo/my-combo, ...) are appended to the developer's real ~/.opencodex/usage.jsonl. The GUI Usage page then renders never-used models with real token counts.
  • This change adds a per-test scratch OPENCODEX_HOME (mkdtemp, restored and cleaned in afterEach), matching the established pattern in the two sibling suites.

Reproduction before the fix

$ cd ~ && bun test /path/to/repo/tests/management-api-logs-metrics.test.ts
$ rg '"requestId":"ok-a"' ~/.opencodex/usage.jsonl   # fixture row now in the real log

Verification

  • Reproduced against a scratch OPENCODEX_HOME: 11 fixture rows were written before the fix.
  • After the fix, the same foreign-cwd invocation leaves the real usage.jsonl untouched.
  • bun run typecheck and bun run privacy:scan pass.
  • Focused suites (usage-summary, usage-log, request-log, usage-cost, management-api-logs-metrics) pass: 147 tests; this file is 9/9 in the full suite.
  • Full suite on this branch is green except 10 management-provider-validation.test.ts cases that fail only in this dev environment because chatgpt.com resolves to a non-global address (write-time DNS/SSRF gate returns 400); upstream CI for dev is green.

Summary by CodeRabbit

  • Tests
    • Improved test isolation by using temporary configuration directories for each test.
    • Added cleanup and environment restoration after tests complete.

@github-actions github-actions Bot added the bug Something isn't working label Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 2c855432-c709-433b-a892-0bae194649d5

📥 Commits

Reviewing files that changed from the base of the PR and between 6712887 and 4b6532b.

📒 Files selected for processing (1)
  • tests/management-api-logs-metrics.test.ts

📝 Walkthrough

Walkthrough

The test setup creates a temporary OPENCODEX_HOME directory before each test. Teardown restores the previous environment value, clears request logs, and removes the temporary directory.

Changes

Management API test isolation

Layer / File(s) Summary
Temporary OPENCODEX_HOME lifecycle
tests/management-api-logs-metrics.test.ts
The test imports filesystem, OS, and path utilities. Each test receives a temporary OPENCODEX_HOME; teardown restores the prior environment, clears request logs, and removes the directory.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: wibias, ingwannu, lidge-jun

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes isolating usage-log fixtures from the real OpenCodex home.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant