Skip to content

test: expand coverage to src/services/ (snapshot-builder, token-optimizer, ai-client)Β #33

@hoainho

Description

@hoainho

🌟 Before claiming this issue

Two quick steps before you open a PR:

  1. ⭐ Star the repository β€” low-friction signal that you'll follow through
  2. πŸ’¬ Comment I'll take this (or similar) below β€” prevents two contributors racing on the same issue

Full policy: CONTRIBUTING.md β†’ How to claim. If a claim is older than 7 days with no PR, you can reclaim it politely.


What

vitest.config.ts line 13 restricts coverage to src/panel/**. The most important pure-logic modules in the codebase are excluded from coverage and have zero tests:

File Public API Test coverage
src/services/snapshot-builder.ts buildSnapshot, hashSnapshot, snapshotToPromptText 0
src/services/token-optimizer.ts TokenOptimizer class (5 methods) 0
src/services/ai-client.ts analyzeSnapshot, response parsing 0
src/utils/messaging.ts 6 utility functions covered by 1 test (messaging.test.ts)
src/utils/sanitize.ts 2 exported functions covered

Why it matters

  • These services are pure logic β€” easy to test, no Chrome mocking needed
  • hashSnapshot uses crypto.subtle β€” a regression could break deduplication silently
  • checkRateLimit is a sliding window β€” off-by-one would let users exceed quotas
  • AI client error paths are completely untested

What to do

This is a help wanted issue because it's M-sized (~6h). Suitable for someone who:

  • Wants a bigger contribution than a 1-line fix
  • Likes writing tests
  • Has time to learn the codebase's testing conventions (src/__tests__/setup.ts)

Suggested deliverables

  1. src/__tests__/snapshot-builder.test.ts with β‰₯6 tests
  2. src/__tests__/token-optimizer.test.ts with β‰₯6 tests
  3. (Optional) src/__tests__/ai-client.test.ts with happy-path + error tests using fetch mocks
  4. Update vitest.config.ts to include src/services/** and src/utils/** in coverage

Test ideas β€” snapshot-builder

  • buildSnapshot with empty TabState β†’ all arrays empty, memory: null
  • buildSnapshot with 51 issues β†’ top 50 retained (MAX_ISSUES enforcement)
  • snapshotToPromptText with full snapshot β†’ contains section headers
  • hashSnapshot deterministic: same input β†’ same hash
  • hashSnapshot different snapshots β†’ different hashes

Test ideas β€” token-optimizer

  • checkRateLimit with 0 calls β†’ allowed: true, remainingCalls: 3
  • After 3 recordCall() β†’ allowed: false
  • getCachedResult with expired entry β†’ returns null
  • setCachedResult at MAX_CACHE_ENTRIES β†’ evicts oldest
  • validateSubscriptionKey with valid/invalid format

Acceptance criteria

  • At least 2 new test files committed
  • All new tests pass
  • vitest.config.ts coverage include widened to services + utils
  • npm run test:coverage shows β‰₯60% coverage of src/services/
  • No changes to production code (tests should not require refactoring)

Scope: M (3 new test files + config) | Effort: ~4-6h | Risk: Lane Tiny | Label: help wanted

Metadata

Metadata

Assignees

No one assigned

    Labels

    dxDeveloper experience improvementsenhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions