Skip to content

refactor: split token-tracker test file for maintainability#2970

Merged
lpcox merged 2 commits into
mainfrom
copilot/refactor-token-tracker-tests
May 12, 2026
Merged

refactor: split token-tracker test file for maintainability#2970
lpcox merged 2 commits into
mainfrom
copilot/refactor-token-tracker-tests

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 12, 2026

✨ Enhancement

containers/api-proxy/token-tracker.test.js had grown to 1,324 lines and mixed four distinct concerns in one place, making failures harder to localize. This change decomposes that suite into concern-specific files without changing production code paths.

  • Test suite decomposition

    • Removed monolithic containers/api-proxy/token-tracker.test.js.
    • Split coverage into:
      • containers/api-proxy/token-tracker.parsing.test.js
      • containers/api-proxy/token-tracker.http.test.js
      • containers/api-proxy/token-tracker.websocket.test.js
      • containers/api-proxy/token-tracker.schema.test.js
  • Responsibility boundaries

    • Parsing: extractUsageFromJson, extractUsageFromSseLine, parseSseDataLines, normalizeUsage
    • HTTP tracking: isStreamingResponse, isCompressedResponse, trackTokenUsage (+ compressed response cases)
    • WebSocket tracking: parseWebSocketFrames, trackWebSocketTokenUsage
    • Schema validation: validateTokenUsageRecord, JSONL _schema field behavior, exact version pin behavior
  • Suite-level setup normalization

    • Each split suite now initializes token-log temp-dir context in a way that avoids cross-file env teardown races when tests run concurrently.
// New test file layout
containers/api-proxy/token-tracker.parsing.test.js
containers/api-proxy/token-tracker.http.test.js
containers/api-proxy/token-tracker.websocket.test.js
containers/api-proxy/token-tracker.schema.test.js

Copilot AI changed the title [WIP] Refactor token-tracker test file to improve maintainability Refactor token-tracker tests into focused parsing/http/websocket/schema suites May 12, 2026
Copilot finished work on behalf of lpcox May 12, 2026 00:56
Copilot AI requested a review from lpcox May 12, 2026 00:56
@lpcox lpcox changed the title Refactor token-tracker tests into focused parsing/http/websocket/schema suites refactor: split token-tracker test file for maintainability May 12, 2026
Copy link
Copy Markdown
Collaborator

@lpcox lpcox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean split of the 1,324-line token-tracker.test.js into 4 focused files (HTTP, parsing, schema, WebSocket). Coverage preserved. Closes #2908.

@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 12, 2026

/copilot review

@lpcox lpcox closed this May 12, 2026
@lpcox lpcox reopened this May 12, 2026
@lpcox lpcox marked this pull request as ready for review May 12, 2026 01:00
@lpcox lpcox requested a review from Mossaka as a code owner May 12, 2026 01:00
Copilot AI review requested due to automatic review settings May 12, 2026 01:00
@github-actions
Copy link
Copy Markdown
Contributor

✅ Coverage Check Passed

Overall Coverage

Metric Base PR Delta
Lines 87.89% 87.97% 📈 +0.08%
Statements 87.85% 87.92% 📈 +0.07%
Functions 83.51% 83.51% ➡️ +0.00%
Branches 79.79% 79.84% 📈 +0.05%
📁 Per-file Coverage Changes (1 files)
File Lines (Before → After) Statements (Before → After)
src/container-lifecycle.ts 87.3% → 88.4% (+1.09%) 87.7% → 88.7% (+1.06%)

Coverage comparison generated by scripts/ci/compare-coverage.ts

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Codex: FAIL
✅ Merged PRs: fix: add --ignore-scripts to security-guard Claude Code install; refactor: remove dead exports from export audit
❌ Safe Inputs GH: safeinputs-gh unavailable
✅ Playwright: GitHub title verified
❌ Tavily: no search tool exposed
✅ File/Bash: smoke-test-codex-25706649855.txt verified
✅ Build: npm ci && npm run build
Overall status: FAIL

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • registry.npmjs.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "registry.npmjs.org"

See Network Configuration for more information.

🔮 The oracle has spoken through Smoke Codex

@github-actions
Copy link
Copy Markdown
Contributor

🔥 Smoke Test Results — PR #2970

Test Result
GitHub MCP connectivity ❌ (401 Bad credentials)
GitHub.com HTTP ⚠️ (pre-step value not substituted)
File write/read ✅ (smoke-test-copilot-25706649931.txt verified)

Overall: FAIL — MCP returned 401; HTTP code template variable was not substituted.

Smoke run: workflow 25706649931

📰 BREAKING: Report filed by Smoke Copilot

@github-actions
Copy link
Copy Markdown
Contributor

🔥 Smoke Test: Copilot BYOK (Offline) Mode

Test Result
GitHub MCP connectivity ❌ (401 - MCP credentials unavailable in sandbox)
GitHub.com HTTP connectivity ✅ (smoke pre-step passed)
File write/read ✅ (smoke-test-copilot-byok-25706649918.txt verified)
BYOK inference (agent → api-proxy → api.githubcopilot.com)

Running in BYOK offline mode (COPILOT_OFFLINE=true) via api-proxy → api.githubcopilot.com

Overall: PASS

🔑 BYOK report filed by Smoke Copilot BYOK

@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Results:

❌ GitHub MCP Testing (gh CLI auth failed)
✅ Playwright Testing (page contains 'GitHub')
✅ File Writing Testing (created successfully)
✅ Bash Tool Testing (verified)

Status: FAIL (3/4 tests passed)

💥 [THE END] — Illustrated by Smoke Claude

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the containers/api-proxy token-tracker Jest suite by splitting a large monolithic test file into smaller, concern-specific suites to improve maintainability and failure localization, without changing production behavior.

Changes:

  • Removed containers/api-proxy/token-tracker.test.js (monolithic 1.3k-line suite).
  • Added focused test suites for parsing, HTTP tracking, WebSocket tracking, and schema validation.
  • Adjusted suite-level token-log temp-dir setup to reduce cross-suite environment interference.
Show a summary per file
File Description
containers/api-proxy/token-tracker.test.js Removed the monolithic token-tracker test suite in favor of split suites.
containers/api-proxy/token-tracker.parsing.test.js Adds parsing-focused tests (JSON + SSE parsing/normalization).
containers/api-proxy/token-tracker.http.test.js Adds HTTP tracking tests including compressed response cases.
containers/api-proxy/token-tracker.websocket.test.js Adds WebSocket frame parsing and WS token usage tracking tests.
containers/api-proxy/token-tracker.schema.test.js Adds schema validation + JSONL _schema behavior tests (including exact AWF_VERSION pin).

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 5/5 changed files
  • Comments generated: 4

Comment on lines +9 to +13
// Redirect token log output to a temp dir to avoid /var/log permission errors
// Keep a shared directory for the full Jest process to avoid cross-file env races.
const tokenLogDir = process.env.AWF_TOKEN_LOG_DIR || fs.mkdtempSync(path.join(os.tmpdir(), 'token-tracker-test-'));
process.env.AWF_TOKEN_LOG_DIR = tokenLogDir;

Comment on lines +9 to +13
// Redirect token log output to a temp dir to avoid /var/log permission errors
// Keep a shared directory for the full Jest process to avoid cross-file env races.
const tokenLogDir = process.env.AWF_TOKEN_LOG_DIR || fs.mkdtempSync(path.join(os.tmpdir(), 'token-tracker-test-'));
process.env.AWF_TOKEN_LOG_DIR = tokenLogDir;

Comment on lines +9 to +13
// Redirect token log output to a temp dir to avoid /var/log permission errors
// Keep a shared directory for the full Jest process to avoid cross-file env races.
const tokenLogDir = process.env.AWF_TOKEN_LOG_DIR || fs.mkdtempSync(path.join(os.tmpdir(), 'token-tracker-test-'));
process.env.AWF_TOKEN_LOG_DIR = tokenLogDir;

Comment on lines +1 to +10
/**
* Tests for token-tracker.js
*/

const {
extractUsageFromJson,
extractUsageFromSseLine,
parseSseDataLines,
normalizeUsage,
} = require('./token-tracker');
@github-actions
Copy link
Copy Markdown
Contributor

Smoke Test Results — FAIL

Check Result
Redis PING ❌ Timeout / no response
PostgreSQL pg_isready ❌ No response on port 5432
PostgreSQL SELECT 1 ❌ Not attempted (pg_isready failed)

Overall: FAILhost.docker.internal is not reachable from this runner. Service containers may not be configured or the hostname is not resolvable in this environment.

🔌 Service connectivity validated by Smoke Services

@github-actions
Copy link
Copy Markdown
Contributor

🧪 Chroot Version Comparison Results

Runtime Host Version Chroot Version Match?
Python Python 3.12.13 Python 3.12.3 ❌ NO
Node.js v24.14.1 v20.20.2 ❌ NO
Go go1.22.12 go1.22.12 ✅ YES

Overall: ❌ Not all tests passed — Python and Node.js versions differ between host and chroot environments.

Tested by Smoke Chroot

@lpcox lpcox merged commit fd40308 into main May 12, 2026
68 of 72 checks passed
@lpcox lpcox deleted the copilot/refactor-token-tracker-tests branch May 12, 2026 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactoring] Split containers/api-proxy/token-tracker.test.js — 1,324-line test file with 4 distinct concerns

3 participants