Skip to content

test(worker): cover signup daily rate-limit branches (daily_ip_limit / daily_email_limit) - #161

Merged
nish3451 merged 8 commits into
mainfrom
fix/ptest-tsio-20260812
Aug 12, 2026
Merged

nish3451 merged 8 commits into
mainfrom
fix/ptest-tsio-20260812

Conversation

@nish3451

@nish3451 nish3451 commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

What

Closes F7 from the product-tests sweep (report-product-tests.md): both 429 branches in enforceAgentLimits — the per-IP daily cap (MAX_AGENT_RUNS_PER_IP_PER_DAY = 20) and the per-email daily soft cap (SOFT_AGENT_RUNS_PER_EMAIL_PER_DAY = 5) — had zero coverage in scripts/test-agent-worker.mjs (rg for daily_ip_limit|daily_email_limit|429 returned nothing). Either cap could silently invert — over-granting abusers or locking out legitimate customers — with the suite staying green.

Tests added (4, in the existing suite style, driving real per-bucket counters via a CountingDB fake)

  1. Per-IP cap: 20 requests from one IP succeed; the 21st (fresh email, proving the IP cap fires on its own, not masked by the email cap) returns 429 with {"ok":false,"error":"daily_ip_limit"}.
  2. Per-email cap: 5 requests for one email succeed; the 6th (fresh IP, proving the email cap fires on its own) returns 429 with {"ok":false,"error":"daily_email_limit"}.
  3. Exact boundary: every index is asserted — the Nth (at the limit) succeeds, the (N+1)th is refused; no off-by-one either way.
  4. Day rollover: both caps reset when the UTC day rolls over, driven by an injected AGENT_LIMITS_NOW clock override — deterministic, never sleeping.

Production change (smallest possible)

enforceAgentLimits now computes its bucket from new Date(env.AGENT_LIMITS_NOW || Date.now()). AGENT_LIMITS_NOW is a test-only binding that production never sets, so runtime behaviour is byte-identical when absent. No limit values, no signup behaviour, nothing else touched.

Mutation proof (both limits)

  • IP comparison disabled (0 > MAX_AGENT_RUNS_PER_IP_PER_DAY): tests 56 + 59 go RED ("the 21st request from the same IP must be refused"); restore → GREEN.
  • Email comparison disabled (0 > SOFT_AGENT_RUNS_PER_EMAIL_PER_DAY): tests 57 + 58 go RED ("the 6th request for the same email must be refused"); restore → GREEN.

Validation

  • npm test exit 0: 96 tests pass (baseline 92 + 4 new; worker suite 55 → 59).
  • sgscan exit 1 — warnings only, none in this diff (exit 2 would block; it did not).
  • crgate --agent — CodeRabbit not signed in on this machine; per packet, noted and continued.

Not merged per packet rules — Nish reviews and merges.

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of daily request limits for agent access.
    • Confirmed separate limits for IP addresses and email addresses.
    • Ensured limits reset correctly at the start of each UTC day.
    • Requests exceeding a limit now return clear JSON 429 errors identifying the applicable limit.
  • Tests

    • Added coverage for successful requests, limit enforcement, independent limits, and daily resets.

…/ daily_email_limit)

F7 from the product-tests sweep: both 429 branches in enforceAgentLimits
(per-IP cap of 20/day and per-email soft cap of 5/day) had zero coverage
in the worker suite, so either cap could silently invert without any test
going red.

Adds four tests in the existing test-agent-worker.mjs style, driving real
per-bucket counters through a CountingDB fake:
- per-IP cap: 20 succeed, the 21st from the same IP returns 429
  daily_ip_limit (fresh email proves the IP cap fires on its own)
- per-email cap: 5 succeed, the 6th for the same email returns 429
  daily_email_limit (fresh IP proves the email cap fires on its own)
- both caps reset when the UTC day rolls over, driven by an injected
  AGENT_LIMITS_NOW clock override (never sleeping)

The clock override is a test-only env binding: production never sets
AGENT_LIMITS_NOW, so runtime behaviour is unchanged when it is absent.

Mutation-proved: disabling each comparison makes its tests go red (2
failures each), restoring them goes green again.

@greptile-apps greptile-apps Bot 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.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9423b5a2-d378-48d7-9c59-1f29eb257000

📥 Commits

Reviewing files that changed from the base of the PR and between ad9cee3 and dc4c2fa.

📒 Files selected for processing (2)
  • scripts/test-agent-worker.mjs
  • src/worker.js

📝 Walkthrough

Walkthrough

The worker now accepts an injected timestamp for daily agent limits. Test infrastructure tracks per-bucket usage and verifies IP and email limits, independent operation, JSON 429 responses, and UTC day resets.

Changes

Agent daily limits

Layer / File(s) Summary
Deterministic limit timestamp
src/worker.js
The daily usage bucket uses env.AGENT_LIMITS_NOW when provided and falls back to Date.now().
Limit boundaries and UTC resets
scripts/test-agent-worker.mjs
Counting database fakes and request helpers simulate usage. Tests verify IP and email boundaries, independent limits, JSON 429 responses, and UTC day resets.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies coverage for the signup worker's daily rate-limit branches, which matches the main change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ptest-tsio-20260812

Comment @coderabbitai help to get the list of available commands.

@greptile-apps greptile-apps Bot 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.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot 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.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot 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.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot 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.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot 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.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot 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.

nish3451 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@greptile-apps greptile-apps Bot 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.

Your trial has ended. Reactivate Greptile to resume code reviews.

@nish3451
nish3451 merged commit 66aa81e into main Aug 12, 2026
3 checks passed
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.

1 participant