Skip to content

test: add unit coverage for cost-tracker, compact, knowledge, tool-utils#71

Open
stealthwhizz wants to merge 1 commit into
open-gitagent:mainfrom
stealthwhizz:test/core-unit-coverage
Open

test: add unit coverage for cost-tracker, compact, knowledge, tool-utils#71
stealthwhizz wants to merge 1 commit into
open-gitagent:mainfrom
stealthwhizz:test/core-unit-coverage

Conversation

@stealthwhizz

Copy link
Copy Markdown
Collaborator

Summary

Several core modules shipped with no unit tests. This PR adds deterministic, offline node:test suites for the pure logic in four of them — +26 test cases, all placed under test/ so npm test actually runs them.

Coverage before / after

Existing tests covered sdk (tool helper, schema builder, programmatic hooks, query error paths) and telemetry. These modules had none:

Module New cases What's covered
cost-tracker.ts 6 Session + per-model token/cost aggregation, derived totalTokens fallback, defensive get() copy, reset()
compact.ts 10 estimateTokens heuristic, per-message-type accounting (incl. tool_use overhead), 75% compaction threshold, tool-result head/tail truncation, transcript rendering (drops deltas/system noise), compact-prompt building
knowledge.ts 6 Index loading: always_load preload + trim, available listing, missing-file skip, malformed index → empty; prompt formatting (inline docs + read hint)
tool-utils.ts 4 GCToolDefinition → AgentTool mapping, string vs {text, details} handler results, abort-signal forwarding

Notes

  • All suites are pure/offline — no API keys, no network, temp dirs cleaned up.
  • Full suite: 53 passing (27 existing + 26 new).

Follow-up worth flagging (not in this PR)

The npm test script globs only test/*.test.ts, so the existing suites under src/**/__tests__/ (memory, sandbox-memory, skill-learner, task-tracker) and src/__tests__/telemetry.test.ts are never run by CI. Wiring those into the test script (or moving them under test/) would be a good separate cleanup.

These core modules had no unit tests. Added deterministic, offline suites
(node:test) covering their pure logic:

- cost-tracker: session/per-model aggregation, derived totalTokens, defensive
  get() copy, reset
- compact: token estimation, per-message-type accounting, compaction threshold,
  tool-result truncation, transcript rendering + compact prompt building
- knowledge: index loading (always_load preload, available listing, missing
  files, malformed index) and prompt formatting
- tool-utils: GCToolDefinition → AgentTool mapping, string vs object handler
  results, abort-signal forwarding

All 26 new cases live under test/ so `npm test` runs them. Full suite: 53 passing.
Copilot AI review requested due to automatic review settings July 6, 2026 17:25

@shreyas-lyzr shreyas-lyzr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

26 new test cases across four previously-uncovered modules. All look correct.

A few things I checked explicitly:

  • estimateTokens arithmetic (ceil(5/4)=2), estimateMessageTokens summation (1+2+51+1=55), and the 75% compaction threshold (800/1000=0.8) all hold.
  • totalTokens fallback (input+output when field absent) is tested correctly.
  • The defensive-copy test for CostTracker.get() properly verifies internal state survives external mutation of the snapshot.
  • knowledge tests use mkdtemp + rm recursive cleanup — no leaked temp dirs.
  • before() blocks are correctly placed at module scope, not inside describe(), which is required for node:test.
  • All suites are offline — no network, no API keys, no side effects.

The follow-up note in the PR description about test/tests not being wired into the npm test glob is a real gap worth tracking separately.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds deterministic node:test unit suites for several core “pure logic” modules so their behavior is exercised offline and without external dependencies.

Changes:

  • Added new unit tests for CostTracker session + per-model aggregation behavior.
  • Added new unit tests for compaction/token estimation utilities, truncation logic, and transcript/prompt building.
  • Added new unit tests for knowledge index loading/formatting and tool-definition conversion utilities.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
test/tool-utils.test.ts Tests toAgentTool mapping behavior, handler result normalization, and abort-signal forwarding.
test/knowledge.test.ts Tests knowledge index loading (including malformed/missing cases) and prompt formatting output.
test/cost-tracker.test.ts Tests CostTracker accumulation, per-model aggregation, defensive snapshotting, and reset behavior.
test/compact.test.ts Tests token estimation, compaction thresholding, tool-result truncation, transcript rendering, and compact-prompt building.

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants