test(contracts): guard source-slice windows against silently widening - #1985
Conversation
The suite has ~25 source-text contract windows written as source.slice(source.indexOf(start), source.indexOf(end)) whose failure mode is a silent pass, not a broken test. When the end marker stops matching, indexOf returns -1 and slice(n, -1) does not throw — it returns the rest of the file bar one character. The window quietly stops being scoped, and every positive toContain in it keeps passing for the wrong reason. The mirror case, a missing start, gives slice(-1, n): an empty window in which every negative assertion passes vacuously. Neither surfaces as a failure, which is why this is worth fixing rather than tolerating. Adds tests/helpers/source-contract.ts with sourceSegment/sourceFrom, which throw on a missing start, a missing end, and an ambiguous start — a window anchored on a string that occurs twice silently covers only the first hit, so a negative assertion there proves nothing about the rest of the file. The helper's own test pins the hazard by demonstrating the raw idiom returning out-of-window content, so the reason for the guard cannot be lost. Migrates the three worst offenders: search-route-ownership.test.ts (three windows, one ending on an indentation depth and one anchored on a comment string), document-detail-performance.test.ts (end marker was the next literal "useEffect", of which DocumentViewer has several), and therapy-compass-responsive-contract.test.ts. Deliberately not migrated here: audit-navigation-auth-regressions.test.ts is the densest case but PR #1983 edits the same file, so it waits for one late sync rather than a merge fight; the two specs that slice ui-smoke.spec.ts between Playwright test titles; and rag-retrieval-parallelism.test.ts, which wants a session that flags the RAG surface first. Queued with a real coverage hole the survey turned up: the window anchored on "{showUniversalAlsoMatches &&" covers only the first of two occurrences in ClinicalDashboard.tsx, so its negative assertion does not enforce what it names. The new helper rejects that anchor outright, which is how it was found. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XYphQZmsBBeqnidpSnAtjE
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XYphQZmsBBeqnidpSnAtjE
This pull request has been ignored for the connected project Preview Branches by Supabase. |
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in:3 minutes Your organization has reached its usage spending cap. Adjust your spending cap in the billing tab. 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: CHILL Plan: Pro Run ID: 📒 Files selected for processing (8)
Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit:f4931d758e
ℹ️ 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".
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
BigSimmo
commented
Aug 15, 2026
Codex review-and-fix summary
Codex did not merge or modify auto-merge. The existing merge workflow completed the PR. |
Summary
This started as "some contract tests are fragile" and turned out to be a silent-pass class, which is a different and worse problem — a broken test is loud and gets fixed; a test that keeps passing while enforcing nothing does not.
Around 25 source-text contract windows in the suite are written as:
When the end marker stops matching,
indexOfreturns-1, andslice(n, -1)does not throw — it returns the rest of the file bar one character. The window quietly stops being scoped, and every positivetoContainin it keeps passing for the wrong reason. The mirror case, a missing start, givesslice(-1, n): a one-character window in which everynot.toContainpasses vacuously. Neither surfaces as a failure.tests/helpers/source-contract.tsexportingsourceSegment/sourceFrom, which throw on a missing start, a missing end, and an ambiguous start — a window anchored on a string that occurs twice silently covers only the first hit, so a negative assertion there proves nothing about the rest of the file.search-route-ownership.test.ts(three windows — one ending on an indentation depth, one anchored on a comment string, one running to EOF with no end marker at all),document-detail-performance.test.ts(end marker was the next literaluseEffect, of whichDocumentViewerhas several), andtherapy-compass-responsive-contract.test.ts.A real coverage hole surfaced and is not fixed here.
audit-navigation-auth-regressions.test.tsanchors a window on{showUniversalAlsoMatches &&, which occurs twice inClinicalDashboard.tsx— the second occurrence sits outside the window, so itsnot.toContain('activeModeResultKind === "favourites"')does not enforce the contract it names. The new helper rejects that anchor outright, which is exactly how it was found. It is queued rather than fixed because PR #1983 edits that same file, and the anti-churn rule prefers one late sync to a merge fight.Also deliberately left for later: the two specs that slice
ui-smoke.spec.tsbetween Playwright test titles (renaming or reordering an unrelated spec silently rescopes them), andrag-retrieval-parallelism.test.ts, which wants a session that flags the RAG surface first perAGENTS.md.Explicitly not loosened — strictness that is the point, not fragility:
favourites-demo-boundary.test.ts's...(demoMode ? prototypeFavouriteItems : [])pins with their paired negatives. That exact conditional-spread form is the live-vs-demo privacy contract; the failure mode being prevented is someone rewriting it to an unconditional merge.$$;, andheader-scroll-hide-contract.test.tsanchoring on the matching</PhoneHeaderCollapsePortal>closing tag. Those are true structural terminators and are the model the rest should move toward.Verification
npm run verify:pr-localMigrated files re-run individually green:
search-route-ownership17/17,document-detail-performance+therapy-compass-responsive-contract23/23, new helper test 6/6.UI verification not run and not applicable: this PR touches only test infrastructure and adds no rendered UI. (
check:playwright-browser-revisionalso fails closed in this container —chromium-1194against a pinned1234— so no browser gate is claimed anywhere in this stack.)Risk and rollout
Notes
RAG impact: no retrieval behaviour change — this PR adds a test helper and migrates three test files. No retrieval, ranking, selection, chunking, scoring, or eval-fixture file is touched;rag-retrieval-parallelism.test.tswas deliberately left alone precisely so this PR stays clear of that surface.eval:rag:offline` was selected by the PR-local router and passed.Generated by Claude Code