From 7453970ac97c0a01a31383f38d137ed82e1e46bc Mon Sep 17 00:00:00 2001 From: BigSimmo <87357024+BigSimmo@users.noreply.github.com> Date: Sun, 16 Aug 2026 19:52:29 +0800 Subject: [PATCH] refactor(sources): migrate source-window contract tests to shared source segment helper --- .../audit-navigation-auth-regressions.test.ts | 41 +++++++++++++------ tests/document-section-nav-contract.test.ts | 10 +++-- tests/in-page-nav-playwright-contract.test.ts | 18 +++++--- tests/tools-search-directions-mockups.test.ts | 12 +++--- 4 files changed, 54 insertions(+), 27 deletions(-) diff --git a/tests/audit-navigation-auth-regressions.test.ts b/tests/audit-navigation-auth-regressions.test.ts index 8c4b23a106..34e84d65cc 100644 --- a/tests/audit-navigation-auth-regressions.test.ts +++ b/tests/audit-navigation-auth-regressions.test.ts @@ -7,20 +7,12 @@ import { describe, expect, it } from "vitest"; import { GET as redirectApplications, HEAD as headApplications } from "@/app/applications/route"; import { resolveDifferentialCompareHandoff } from "@/lib/differentials"; import { legacyHomeRedirectUrl } from "@/lib/legacy-home-redirect"; +import { sourceSegment } from "./helpers/source-contract"; function source(relativePath: string) { return readFileSync(resolve(process.cwd(), relativePath), "utf8"); } -function sourceSegment(contents: string, startMarker: string, endMarker: string) { - const start = contents.indexOf(startMarker); - const end = contents.indexOf(endMarker, start + startMarker.length); - if (start < 0 || end < 0) { - throw new Error(`Could not locate source segment from ${startMarker} to ${endMarker}.`); - } - return contents.slice(start, end); -} - const clinicalDashboardSource = source("src/components/ClinicalDashboard.tsx"); const masterSearchHeaderSource = source("src/components/clinical-dashboard/master-search-header.tsx"); const universalAlsoMatchesSource = source("src/components/clinical-dashboard/universal-search-also-matches.tsx"); @@ -103,6 +95,7 @@ describe("audit navigation and auth regressions", () => { masterSearchHeaderSource, "ref={modeMenuRef}", 'className={cn("relative z-[60]', + { label: "master mode-menu focus boundary" }, ); expect(focusLeaveContract).toContain("onBlur={(event) => {"); @@ -135,16 +128,19 @@ describe("audit navigation and auth regressions", () => { masterSearchHeaderSource, "function renderModeMenuOption(", "function renderModeMenuOptions()", + { label: "mode-menu option prefetch" }, ); const openModeMenuWithFocus = sourceSegment( masterSearchHeaderSource, "function openModeMenuWithFocus(", "function toggleModeMenu(", + { label: "mode-menu focus-open prefetch" }, ); const toggleModeMenu = sourceSegment( masterSearchHeaderSource, "function toggleModeMenu(", "function handleModeTriggerKeyDown(", + { label: "mode-menu toggle prefetch" }, ); expect(masterSearchHeaderSource).toContain("function prefetchModeSelection(modeId: AppModeId)"); @@ -181,6 +177,7 @@ describe("audit navigation and auth regressions", () => { clinicalDashboardSource, "const showUniversalAlsoMatches =", "const showDesktopHomeComposer =", + { label: "also-matches visibility gate" }, ); expect(alsoMatchesGate).toContain('activeModeResultKind === "tools"'); expect(alsoMatchesGate).toContain('activeModeResultKind === "favourites"'); @@ -195,6 +192,7 @@ describe("audit navigation and auth regressions", () => { clinicalDashboardSource, "const uploadReadOnlyMode =", "const canUsePrivateApis =", + { label: "upload read-only capability" }, ); // Uploads stay writable in local no-auth; only explicit demo / auth-unavailable lock them. expect(uploadReadOnlyContract).toContain("const uploadReadOnlyMode = resolveUploadReadOnlyMode({"); @@ -209,6 +207,7 @@ describe("audit navigation and auth regressions", () => { clinicalDashboardSource, "const canUsePrivateApis =", "const canRunSearch =", + { label: "private API capability" }, ); expect(privateCapabilityContract).toContain("const canUsePrivateApis ="); expect(privateCapabilityContract).toContain( @@ -219,6 +218,7 @@ describe("audit navigation and auth regressions", () => { clinicalDashboardSource, "if (!nextDemoMode && !canUsePrivateApis) {", "const shouldRefreshWorkState =", + { label: "private polling capability" }, ); expect(pollingContract).toContain("if (!nextDemoMode && !canUsePrivateApis) {"); expect(pollingContract).toContain("setDocuments([]);"); @@ -228,6 +228,7 @@ describe("audit navigation and auth regressions", () => { clinicalDashboardSource, "const mutateDocumentLabel =", "const handleDocumentDeleted =", + { label: "private label mutation" }, ); expect(labelMutationContract).toContain("if (!canUsePrivateApis) return false;"); @@ -235,6 +236,7 @@ describe("audit navigation and auth regressions", () => { clinicalDashboardSource, "function openUploadDrawer()", "function openEvidenceDrawer()", + { label: "private upload mutation" }, ); expect(uploadMutationContract).toContain("if (!canUseAdministrativeApis) {"); }); @@ -266,6 +268,7 @@ describe("audit navigation and auth regressions", () => { uploadDesktopHookSource, "export function useUploadDesktopLayout(", "}", + { label: "upload desktop layout hook" }, ); expect(useUploadDesktopLayoutBody).toMatch( /return\s+useSyncExternalStore\(\s*subscribeToUploadDesktopLayout,\s*getUploadDesktopLayoutSnapshot,\s*\(\)\s*=>\s*false\s*\)/, @@ -282,15 +285,27 @@ describe("audit navigation and auth regressions", () => { }); it("leaves favourites universal matches to the favourites hub", () => { - const universalMatchesContract = sourceSegment( + const nonAnswerUniversalMatchesContract = sourceSegment( clinicalDashboardSource, - "{showUniversalAlsoMatches &&", + "{showUniversalAlsoMatches &&\n (activeModeResultKind", // The shared home now opens the mode-content chain, ahead of differentials. "{showSharedHome ?", + { label: "non-answer also-matches render branch" }, + ); + const answerUniversalMatchesContract = sourceSegment( + clinicalDashboardSource, + '{showUniversalAlsoMatches && activeModeResultKind === "answer" ? (', + "", + { label: "answer also-matches render branch" }, ); - expect(universalMatchesContract).toContain(" { }); it("keeps document-viewer ui-smoke on the current phone section chrome", () => { - const documentViewerSmoke = uiSmokeSource.slice( - uiSmokeSource.indexOf('test("document viewer puts the PDF preview first'), - uiSmokeSource.indexOf('test("answer glass header overlays main'), + const documentViewerSmoke = sourceSegment( + uiSmokeSource, + 'test("document viewer puts the PDF preview first', + 'test("answer glass header overlays main', + { label: "document viewer phone section smoke" }, ); expect(documentViewerSmoke).toContain('getByTestId("document-section-trigger")'); diff --git a/tests/in-page-nav-playwright-contract.test.ts b/tests/in-page-nav-playwright-contract.test.ts index d2c0e06193..15722a8c89 100644 --- a/tests/in-page-nav-playwright-contract.test.ts +++ b/tests/in-page-nav-playwright-contract.test.ts @@ -1,6 +1,8 @@ import { readFileSync } from "node:fs"; import { describe, expect, it } from "vitest"; +import { sourceSegment } from "./helpers/source-contract"; + /** * Guards the Production UI failures that blocked PR #1781: * @@ -20,9 +22,11 @@ const IN_PAGE_NAV_HEADER = "src/components/in-page-nav/in-page-nav-header.tsx"; describe("in-page-nav Playwright contract", () => { it("prescribing smoke asserts the aria-label back name on both desktop and phone", () => { const source = readFileSync(UI_SMOKE, "utf8"); - const prescribingBlock = source.slice( - source.indexOf('test("prescribing workflow uses in-app medication routes'), - source.indexOf('test("tablet document chrome keeps one new-chat action'), + const prescribingBlock = sourceSegment( + source, + 'test("prescribing workflow uses in-app medication routes', + 'test("tablet document chrome keeps one new-chat action', + { label: "prescribing smoke" }, ); expect(prescribingBlock).toContain('name: "Back to medications"'); @@ -36,9 +40,11 @@ describe("in-page-nav Playwright contract", () => { // Medications exact inside medication-page-*; the back control lives in // MedicationNavHeader with aria-label "Back to medications". const source = readFileSync(UI_SMOKE, "utf8"); - const quickLinksBlock = source.slice( - source.indexOf('test("answer results surface cross-mode quick links"'), - source.indexOf('test("answer mode keeps prior turns visible for follow-up questions"'), + const quickLinksBlock = sourceSegment( + source, + 'test("answer results surface cross-mode quick links"', + 'test("answer mode keeps prior turns visible for follow-up questions"', + { label: "answer cross-mode quick-links smoke" }, ); expect(quickLinksBlock).toContain('name: "Back to medications"'); diff --git a/tests/tools-search-directions-mockups.test.ts b/tests/tools-search-directions-mockups.test.ts index 5f09bfffa7..d0068cf764 100644 --- a/tests/tools-search-directions-mockups.test.ts +++ b/tests/tools-search-directions-mockups.test.ts @@ -5,6 +5,7 @@ import { describe, expect, it } from "vitest"; import { submittedToolIdsForMockup } from "../src/components/tools-search-directions-mockups"; import { normalizeSearchText } from "../src/lib/catalog-search"; import { toolCatalogRecords, toolSearchText } from "../src/lib/tools-catalog"; +import { sourceSegment } from "./helpers/source-contract"; const source = readFileSync(new URL("../src/components/tools-search-directions-mockups.tsx", import.meta.url), "utf8"); @@ -23,7 +24,9 @@ describe("tools search direction mockup evidence", () => { }); it("shows both representative queries at both widths for every direction", () => { - const showcase = source.slice(source.indexOf("function DirectionShowcase"), source.indexOf("const currentDefects")); + const showcase = sourceSegment(source, "function DirectionShowcase", "const currentDefects", { + label: "direction showcase frames", + }); expect(showcase).toContain(''); expect(showcase).toContain(''); expect(showcase).toContain(''); @@ -35,10 +38,9 @@ describe("tools search direction mockup evidence", () => { }); it("keeps the ranked filter only for multi-match frames at both widths", () => { - const brief = source.slice( - source.indexOf("function BriefDirection"), - source.indexOf("function SegmentedCountBand"), - ); + const brief = sourceSegment(source, "function BriefDirection", "function SegmentedCountBand", { + label: "brief direction ranked filter", + }); expect(brief).toContain('device === "desktop" && matches.length > 1'); expect(brief).toContain('device === "phone" && matches.length > 1'); expect(brief).not.toContain('device === "desktop" ?