Repository navigation
test(daemon): extract shared DaemonAPIHandler mock scaffolding - #3434
Conversation
Problem: the _MockServer/_MockHeaders/_make_handler trio was hand-duplicated near-identically across test_daemon_http_security.py, test_daemon_events_endpoint.py, and test_provider_usage_endpoint.py (CodeRabbit finding on PR #2559, filed as polylogue-myhg). Drift already happened once: the host= parameter was added to two of the three copies during a fast-follow, not all three. Solution: new tests/infra/daemon_http_harness.py holds MockDaemonServer, MockHeaders, make_daemon_handler, capture_json_response, and capture_responses. These mock only the HTTP transport boundary (the listening socket/ThreadingHTTPServer a real DaemonAPIHandler normally sits on, and the parsed header block a socket read would produce) — do_GET/do_POST dispatch, _check_auth, cross-origin checks, route handlers, and JSON/SSE serialization all still run as the real production DaemonAPIHandler built via __new__. The three test files now import the shared harness instead of defining their own copies; each keeps a thin file-local _make_handler wrapper only where it needs a different default (http_security.py defaults its server to a configured auth token, since that file's whole point is exercising the auth-required path). While touching test_daemon_events_endpoint.py, three ad-hoc type()-built _Srv server stand-ins were also converted to MockDaemonServer for the same reason CodeRabbit flagged the original trio — same duplication pattern, same drift risk. No boundary that was previously mocked is now hollowed out: the production DaemonAPIHandler class is still constructed and its real methods still execute against these fakes in every migrated test. Verification: ruff check + mypy (strict) clean on all four touched files; devtools test tests/unit/daemon/test_daemon_http_security.py tests/unit/daemon/test_daemon_events_endpoint.py tests/unit/daemon/test_provider_usage_endpoint.py -> 584 passed. Ref polylogue-myhg Co-Authored-By: Claude <[email protected]>
Problem: three CodeRabbit findings from PR #3425's review were triaged and filed as polylogue-2vor rather than blocking that merge: 1. typed_refs_from_session_refs() coerced a session_refs row with a valid url/repo but no ref_number to "PR #0" (observed for Codex Cloud's chatgpt_codex_sidecar._pull_request_ref(), which stores external_pull_request_id in url and leaves repo/number unset). Because typed refs are authoritative over the regex fallback, this could suppress a correctly-parsed regex result with a bogus PR #0. 2. Disagreement detection in _resolve_refs() compared PR/issue identity by bare number only, so acme/product#42 and other/repo#42 compared equal -- a real cross-repo disagreement was never surfaced. 3. Foreign-trailer classification in detect_session_commits() set foreign_trailer=True whenever a commit carried any Claude-Session trailer, even when the current session has no bridge_session_ids of its own (own_trailer_tokens empty) -- i.e. even when there is no typed identity to compare against, producing a disagreement out of thin air. Solution: 1. typed_refs_from_session_refs() now tries to recover a real number by parsing a genuine github.com PR/issue URL out of `url` when `number` is absent; if that also fails, the row is skipped rather than defaulting to 0. 2. New _refs_match() compares the full (owner, repo, number) identity whenever both refs are repo-qualified, falling back to number-only equality when either side lacks repo identity (e.g. a bare "#42" regex mention, where number is the only available signal). 3. foreign_trailer now additionally requires own_trailer_tokens to be non-empty. Each fix has a regression test in tests/unit/insights/test_session_commit.py naming the exact bead/finding it covers. Verification: ruff check + mypy (strict) clean; devtools test tests/unit/insights/test_session_commit.py -> 46 passed; also ran tests/unit/cli/test_correlate_view.py tests/unit/storage/test_archive_tiers_write.py (other consumers of this module) -> 80 passed; devtools render all --check clean. Ref polylogue-2vor Co-Authored-By: Claude <[email protected]>
Problem: devtools verify --quick (mypy, whole-repo) caught what the narrower per-file devtools test runs in the prior commit could not: tests/unit/daemon/test_web_auth.py and test_route_contracts.py import _MockServer/_capture_responses FROM test_daemon_http_security.py rather than defining their own copies, so removing those names broke both files (attr-defined errors under mypy --strict; would also have been an ImportError at collection time). Solution: both files now import MockDaemonServer/capture_responses directly from tests/infra/daemon_http_harness.py (still importing _make_handler from test_daemon_http_security.py, since that file's thin wrapper deliberately defaults the mock server's auth token to "secret" -- test_web_auth.py's whole point is exercising the web-credential/cookie auth path, which only activates once a token is configured; _check_auth() returns True unconditionally when no token is set, so an open-by-default server would silently skip the very paths these tests assert on). Verification: ruff check + mypy (strict, whole repo: 2397 source files) clean; devtools test tests/unit/daemon/test_web_auth.py tests/unit/daemon/test_route_contracts.py tests/unit/daemon/test_daemon_http_security.py tests/unit/daemon/test_daemon_events_endpoint.py tests/unit/daemon/test_provider_usage_endpoint.py -> 709 passed; devtools verify --quick clean end to end. Ref polylogue-myhg Co-Authored-By: Claude <[email protected]>
|
Warning Review limit reached
Next review available in: 22 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (8)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: acdb805448
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Both closed after PR #3434 merged: shared DaemonAPIHandler mock scaffolding extraction (tests/infra/daemon_http_harness.py) and the three session_commit.py typed-evidence gaps from PR #3425's CodeRabbit review. Co-Authored-By: Claude <[email protected]>
|
Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.
Only the rows listed above are dispositioned. Held findings remain open. |
Summary
_MockServer/_MockHeaders/_make_handlertrio from three daemon test files into a sharedtests/infra/daemon_http_harness.py(polylogue-myhg).polylogue/insights/session_commit.py, with regression tests).Problem
polylogue-myhg: CodeRabbit flagged (PR #2559) that
_MockServer/_MockHeaders/_make_handlerwere duplicated near-identically acrosstest_daemon_http_security.py,test_daemon_events_endpoint.py, andtest_provider_usage_endpoint.py. Drift already happened once: thehost=parameter was added to two of the three copies during a fast-follow, not all three. A fresh grep tonight confirmed the duplication was still live.polylogue-2vor: three P2 findings in
session_commit.pywere left unaddressed at PR #3425's merge time:typed_refs_from_session_refs()coerced asession_refsrow with a validurl/repobut noref_numberto "PR #0" (observed for Codex Cloud'schatgpt_codex_sidecar._pull_request_ref(), which storesexternal_pull_request_idinurland leavesrepo/numberunset). Since typed refs are authoritative over the regex fallback, this could suppress a correctly-parsed regex result with a bogus PR #0.acme/product#42andother/repo#42compared equal — a real cross-repo disagreement was never surfaced.bridge_session_idsof its own — i.e. even with no typed identity to compare against.Solution
Mock harness extraction (
tests/infra/daemon_http_harness.py):MockDaemonServer,MockHeaders,make_daemon_handler,capture_json_response,capture_responses— these mock only the HTTP transport boundary (the listening socket/ThreadingHTTPServera realDaemonAPIHandlernormally sits on, and the parsed header block a socket read would produce).do_GET/do_POSTdispatch,_check_auth, cross-origin checks, route handlers, and JSON/SSE serialization all still run as the real productionDaemonAPIHandlerbuilt via__new__— no boundary that was previously mocked is hollowed out.test_daemon_http_security.pykeeps a thin file-local_make_handlerwrapper because that file's whole point is exercising the auth-required path, so its default server needs a configured token ("secret") unlike the harness's own open-by-defaultMockDaemonServer.test_daemon_events_endpoint.py, three ad-hoctype()-built_Srvserver stand-ins were also converted toMockDaemonServer— same duplication pattern CodeRabbit flagged in the original trio.test_web_auth.py,test_route_contracts.py) that imported_MockServer/_capture_responsesfromtest_daemon_http_security.pyrather than defining their own copies — a case a narrow per-filedevtools testrun doesn't catch but whole-repomypy --strictdoes.session_commit.py fixes (regression test per finding, see
tests/unit/insights/test_session_commit.py):typed_refs_from_session_refs()now tries to recover a real number by parsing a genuinegithub.comPR/issue URL out ofurlwhennumberis absent; if that also fails, the row is skipped rather than defaulting to 0._refs_match()compares the full(owner, repo, number)identity whenever both refs are repo-qualified, falling back to number-only equality when either side lacks repo identity (e.g. a bare#42regex mention).foreign_trailernow additionally requiresown_trailer_tokensto be non-empty.Verification
ruff check+mypy(strict, whole repo: 2397 source files) clean.devtools test tests/unit/daemon/test_daemon_http_security.py tests/unit/daemon/test_daemon_events_endpoint.py tests/unit/daemon/test_provider_usage_endpoint.py tests/unit/daemon/test_web_auth.py tests/unit/daemon/test_route_contracts.py→ 709 passed.devtools test tests/unit/insights/test_session_commit.py→ 46 passed; also rantests/unit/cli/test_correlate_view.py tests/unit/storage/test_archive_tiers_write.py(other consumers ofsession_commit.py) → 80 passed.devtools verify --quick→ clean end to end.Ref polylogue-myhg
Ref polylogue-2vor