Skip to content

Add test coverage for usage recording, accumulation, saturation, and the disabled guard #98

Description

@mikewheeleer

Test the usage recording and accumulation endpoints

Description

The core metering path — POST /api/v1/usage, GET /api/v1/usage/:agent/:serviceId, and the Math.min(Number.MAX_SAFE_INTEGER, prev + requests) saturation logic in src/index.ts — is largely untested in src/health.test.ts. The 409 service_disabled guard, the accumulation across repeated calls, and the read-back of a never-seen pair (returns total: 0) all lack coverage. Since this is the ledger the billing and settlement math depend on, it deserves thorough tests.

Requirements and context

  • Repository scope: Agentpay-Org/Agentpay-backend only.
  • Cover POST /api/v1/usage: 201 happy path, validation 400s for bad agent/serviceId/requests, and the 409 service_disabled path after toggling disabled.
  • Cover repeated posts accumulating into one total, and the read endpoint returning the running total (and 0 for unknown pairs).
  • Exercise the saturation behaviour near Number.MAX_SAFE_INTEGER deterministically (seed a near-max total, then add).
  • Assert the standard 400 invalid_request shape with a requestId on every validation failure.

Suggested execution

  • Fork the repo and create a branch
  • git checkout -b test/testing-41-usage-recording
  • Implement changes
    • Write comprehensive tests in: new src/usage.test.ts using supertest against the exported app.
    • Write code in: no production change expected; if a real bug surfaces, fix it and note it in the PR.
    • Add documentation: none beyond test descriptions.
    • Add clear describe/it titles.
    • Validate security assumptions: disabled services cannot accrue usage, no negative/zero requests accepted.
  • Test and commit

Test and commit

  • Run npm run build, npm test, and npm run lint.
  • Cover edge cases: zero requests, negative requests, float requests, oversized agent/serviceId, disabled service, saturation.
  • Include the full npm test output in the PR description.

Example commit message

test: cover usage recording, accumulation, saturation, and disabled guard

Guidelines

  • Minimum 95 percent test coverage for impacted modules.
  • Clear, reviewer-focused documentation.
  • Timeframe: 96 hours.

Community & contribution rewards

  • 💬 Join the AgentPay community on Discord for questions, reviews, and faster merges: https://discord.gg/eXvRKkgcv
  • ⭐ This is a GrantFox OSS / Official Campaign task and may be rewarded. When your PR is merged you'll be prompted to rate the project — if this issue and the maintainers helped you ship, we'd be grateful for a 5-star rating. Clear questions in Discord and tidy, well-tested PRs are the fastest path to a merge and a reward.

Metadata

Metadata

Assignees

No one assigned

    Fields

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions