Skip to content

test(contracts): guard source-slice windows against silently widening - #1985

Merged
BigSimmo merged 9 commits into
mainfrom
claude/test-source-contract-hardening
Aug 15, 2026
Merged

test(contracts): guard source-slice windows against silently widening#1985
BigSimmo merged 9 commits into
mainfrom
claude/test-source-contract-hardening

Conversation

@BigSimmo

Copy link
Copy Markdown
Owner

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:

source.slice(source.indexOf(start),source.indexOf(end))

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): a one-character window in which every not.toContain passes vacuously. Neither surfaces as a failure.

  • Adds tests/helpers/source-contract.ts exporting 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 can't be lost to a future cleanup.
  • Migrates the three worst offenders: 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 literal useEffect, of which DocumentViewer has several), and therapy-compass-responsive-contract.test.ts.

A real coverage hole surfaced and is not fixed here.audit-navigation-auth-regressions.test.ts anchors a window on {showUniversalAlsoMatches &&, which occurs twice in ClinicalDashboard.tsx — the second occurrence sits outside the window, so its not.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.ts between Playwright test titles (renaming or reordering an unrelated spec silently rescopes them), and rag-retrieval-parallelism.test.ts, which wants a session that flags the RAG surface first per AGENTS.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.
  • The SQL windows terminating on $$;, and header-scroll-hide-contract.test.ts anchoring 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-local
 Test Files 609 passed
Tests 6608 passed | 4 skipped (6612)
PR-local verification summary:
- completed: check:runtime, check:installed-lock-parity, format:changed, lint, typecheck, test, eval:rag:offline, check:medication-interactions, check:medication-lexicon-report
- failed: (none)
- not reached: (none)

Migrated files re-run individually green: search-route-ownership 17/17, document-detail-performance + therapy-compass-responsive-contract 23/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-revision also fails closed in this container — chromium-1194 against a pinned 1234 — so no browser gate is claimed anywhere in this stack.)

Risk and rollout

  • Risk: low, and it is confined to test infrastructure. The realistic failure mode is that a migrated window now throws where it previously passed silently — which is the intended behaviour, but it means a future unrelated edit gets a loud failure it would previously have skated past. That is the trade being made deliberately. No product code changes.
  • Rollback: two commits, revertable together. The helper is additive; reverting restores the previous inline slices.
  • Provider or production effects: None.

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

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
@supabase

supabaseBot commented Aug 15, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project sjrfecxgysukkwxsowpy because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@coderabbitai

coderabbitaiBot commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 47e052a4-9d96-440b-a785-49dda24bfc40

📥 Commits

Reviewing files that changed from the base of the PR and between e8c035b and 85d4aaf.

📒 Files selected for processing (8)
  • docs/branch-review-records/642a0ca61b346fbb0b4dd8bda07b9e763f3acdf9d7d26bff9e0017e1cdd58c60.record.md
  • docs/branch-review-records/cd495affb93b35a4c9c27292318bff9bf09cbe3e2cf670a084930b870cd6fed6.record.md
  • docs/outstanding-issues-inbox/6e354901-7694-40e0-b03d-8c398cec7b05.json
  • tests/document-detail-performance.test.ts
  • tests/helpers/source-contract.ts
  • tests/search-route-ownership.test.ts
  • tests/source-contract-helper.test.ts
  • tests/therapy-compass-responsive-contract.test.ts

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

@chatgpt-codex-connectorchatgpt-codex-connectorBot 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.

💡 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".

Comment threadtests/helpers/source-contract.ts Outdated
@BigSimmo
BigSimmo enabled auto-merge (squash) August 15, 2026 12:55
@BigSimmo
BigSimmo merged commit 2e3ac49 into mainAug 15, 2026
26 checks passed
@BigSimmo
BigSimmo deleted the claude/test-source-contract-hardening branch August 15, 2026 13:49
@BigSimmoChatGPT Codex Connector

Copy link
Copy Markdown
OwnerAuthor

Codex review-and-fix summary

  • Final state: merged externally by the PR's pre-existing auto-merge workflow.
  • Final reviewed head:85d4aaf4aa0bbd18946943584b9e4b77a34f531e
  • Base at merge:e8c035bb5d014dabfc84123883bba35d08997033
  • Merge commit:2e3ac494b8b75a5e8f9e9ca53e0b623980602b61
  • Branch update / merge tree: latest main was merged into the PR branch twice as the base advanced; the final head contains the reviewed fixes, is current with the final base, and has a clean merge tree byte-identical to the head tree.
  • Issues fixed: the new ambiguity guard now detects self-overlapping repeated start markers by searching from the next character in both sourceSegment and sourceFrom; empty start/end markers now fail closed instead of returning misleading windows.
  • Adversarial review: the independent GitHub Codex review identified the overlapping-marker P2 and it was reproduced exactly. A distinct manual adversarial pass found the empty-marker fail-open case. The final diff was re-read against all migrated callers; no additional PR-introduced P0/P1/P2 issue was found.
  • Threads: the sole actionable review thread was replied to with the fixed head and resolved; no actionable thread remains.
  • Decisive offline checks: focused Vitest 49/49 across the helper and three migrated contracts; source typecheck; changed-file ESLint; Prettier; git diff --check; branch-review-ledger and outstanding-issues guards. The focused suite and affected guards were rerun after the final base update.
  • Exact-head required CI:CI passed, including Static PR checks, Unit coverage, Safety/config, Production UI critical, Production UI 1/2/3, and aggregate PR required.
  • Exact-head advisory CI:SAST and Secret Scan passed. Migration replay, Advisory UI, Ingestion SAST, container images, build, Lighthouse budget, visual baselines, and release-browser-matrix were skipped by CI scope.
  • Blockers: none.
  • Residual risk: the intentionally queued unguarded source-slice windows described in the PR remain follow-up debt; no provider-backed or local live-service gate was run.

Codex did not merge or modify auto-merge. The existing merge workflow completed the PR.

Sign up for freeto 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.

2 participants

@BigSimmo@claude