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.
Test the usage recording and accumulation endpoints
Description
The core metering path —
POST /api/v1/usage,GET /api/v1/usage/:agent/:serviceId, and theMath.min(Number.MAX_SAFE_INTEGER, prev + requests)saturation logic insrc/index.ts— is largely untested insrc/health.test.ts. The409 service_disabledguard, the accumulation across repeated calls, and the read-back of a never-seen pair (returnstotal: 0) all lack coverage. Since this is the ledger the billing and settlement math depend on, it deserves thorough tests.Requirements and context
POST /api/v1/usage: 201 happy path, validation400s for badagent/serviceId/requests, and the409 service_disabledpath after togglingdisabled.total, and the read endpoint returning the running total (and0for unknown pairs).Number.MAX_SAFE_INTEGERdeterministically (seed a near-max total, then add).400 invalid_requestshape with arequestIdon every validation failure.Suggested execution
git checkout -b test/testing-41-usage-recordingsrc/usage.test.tsusingsupertestagainst the exportedapp.describe/ittitles.Test and commit
npm run build,npm test, andnpm run lint.npm testoutput in the PR description.Example commit message
test: cover usage recording, accumulation, saturation, and disabled guardGuidelines
Community & contribution rewards