From 17adec821c42f6262f5786349bb69945bbb96720 Mon Sep 17 00:00:00 2001 From: Sidney Swift <158200036+sidneyswift@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:00:47 -0500 Subject: [PATCH] fix: bind saved assessment readback to requested identity --- .../__tests__/CompanyAssessment.test.tsx | 34 ++++ .../useCompanyAssessmentCapture.test.tsx | 3 + .../useCompanyAssessmentIdentity.test.tsx | 162 ++++++++++++++++++ hooks/useCompanyAssessment.ts | 10 +- 4 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 hooks/__tests__/useCompanyAssessmentIdentity.test.tsx diff --git a/components/Releases/__tests__/CompanyAssessment.test.tsx b/components/Releases/__tests__/CompanyAssessment.test.tsx index 548c106da..77de819fc 100644 --- a/components/Releases/__tests__/CompanyAssessment.test.tsx +++ b/components/Releases/__tests__/CompanyAssessment.test.tsx @@ -350,3 +350,37 @@ it.each([401, 403, 404])( expect(screen.queryByText("private diagnostic")).toBeNull(); }, ); + +it("does not display a substituted saved assessment or replace its reopen ID", async () => { + const requested = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"; + const other = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb"; + vi.stubGlobal( + "fetch", + vi.fn(async () => + Response.json({ + snapshot: { id: other, state: "saved", superseded: false, brief }, + }), + ), + ); + render( + "token"} + />, + ); + fireEvent.change(screen.getByLabelText("Saved assessment ID"), { + target: { value: requested }, + }); + fireEvent.click( + screen.getByRole("button", { name: "Open saved assessment" }), + ); + await screen.findByText( + "The response did not match the requested saved assessment. Reopen using its original ID.", + ); + expect(screen.queryByText(brief.text)).toBeNull(); + expect(screen.queryByText(`Saved assessment: ${other}`)).toBeNull(); + expect( + (screen.getByLabelText("Saved assessment ID") as HTMLInputElement).value, + ).toBe(requested); +}); diff --git a/hooks/__tests__/useCompanyAssessmentCapture.test.tsx b/hooks/__tests__/useCompanyAssessmentCapture.test.tsx index 867cf59a6..8dd867bd4 100644 --- a/hooks/__tests__/useCompanyAssessmentCapture.test.tsx +++ b/hooks/__tests__/useCompanyAssessmentCapture.test.tsx @@ -27,6 +27,7 @@ it("invalidates snapshot synchronously before awaiting a new request and never r : Promise.resolve("token"), ), ); + act(() => result.current.setOpenId(snapshot.id)); await act(async () => result.current.run("read_brief")); const old = result.current.snapshot!; expect(result.current.isCurrentSnapshot(old)).toBe(true); @@ -58,8 +59,10 @@ it("keeps a previous snapshot ineligible after an unsuccessful read", async () = const { result } = renderHook(() => useCompanyAssessment("request", "org", async () => "token"), ); + act(() => result.current.setOpenId(snapshot.id)); await act(async () => result.current.run("read_brief")); const old = result.current.snapshot!; + act(() => result.current.setOpenId(snapshot.id)); await act(async () => result.current.run("read_brief")); expect(result.current.snapshot).toBeNull(); expect(result.current.isCurrentSnapshot(old)).toBe(false); diff --git a/hooks/__tests__/useCompanyAssessmentIdentity.test.tsx b/hooks/__tests__/useCompanyAssessmentIdentity.test.tsx new file mode 100644 index 000000000..31e20cff4 --- /dev/null +++ b/hooks/__tests__/useCompanyAssessmentIdentity.test.tsx @@ -0,0 +1,162 @@ +// @vitest-environment jsdom +import { act, renderHook } from "@testing-library/react"; +import { afterEach, expect, it, vi } from "vitest"; +import { useCompanyAssessment } from "../useCompanyAssessment"; +const requestedId = "a1111111-1111-4111-8111-111111111111"; +const unrelatedId = "22222222-2222-4222-8222-222222222222"; +const brief = { + purpose: "company_onboarding", + text: "Unrelated private artifact", + readiness: "partial", + missingTopics: [], + guidance: "Unverified assertions.", +}; +afterEach(() => vi.unstubAllGlobals()); +it("withholds a saved artifact returned for a different requested ID", async () => { + const fetcher = vi.fn(async () => + Response.json({ + snapshot: { + id: unrelatedId, + state: "saved", + superseded: false, + brief, + }, + }), + ); + vi.stubGlobal("fetch", fetcher); + const { result } = renderHook(() => + useCompanyAssessment("request", "org", async () => "synthetic"), + ); + act(() => result.current.setOpenId(requestedId)); + await act(async () => result.current.run("read_brief")); + expect(JSON.parse(fetcher.mock.calls[0][1]!.body as string).brief_id).toBe( + requestedId, + ); + expect(result.current.snapshot).toBeNull(); + expect(result.current.brief).toBeNull(); + expect(result.current.savedId).toBe(""); + expect(result.current.openId).toBe(requestedId); + expect(result.current.retrySave).toBe(false); +}); + +it("reopens the same saved snapshot after the first hook is lost", async () => { + const saved = { + id: requestedId, + state: "saved", + superseded: false, + brief: { + ...brief, + text: "Exact saved version", + input_manifest: { + documents: [ + { + documentId: unrelatedId, + version: 3, + sourceVersionIds: [requestedId], + }, + ], + }, + }, + }; + const requests: Record[] = []; + vi.stubGlobal( + "fetch", + vi.fn(async (_url, init) => { + const body = JSON.parse(init!.body as string); + requests.push(body); + if ( + body.action === "save_brief" || + (body.action === "read_brief" && + body.brief_id.toLowerCase() === requestedId) + ) + return Response.json({ snapshot: saved }); + return new Response(null, { status: 404 }); + }), + ); + const first = renderHook(() => + useCompanyAssessment("request", "org", async () => "synthetic"), + ); + await act(async () => first.result.current.run("save_brief")); + const savedId = first.result.current.savedId; + expect(savedId).toBe(requestedId); + first.unmount(); + const second = renderHook(() => + useCompanyAssessment("different-request", "org", async () => "synthetic"), + ); + act(() => second.result.current.setOpenId(` ${savedId.toUpperCase()} `)); + await act(async () => second.result.current.run("read_brief")); + expect(second.result.current.snapshot).toEqual(saved); + expect(requests.map((body) => body.action)).toEqual([ + "save_brief", + "read_brief", + ]); + expect(requests[1]).toEqual({ + action: "read_brief", + brief_id: savedId.toUpperCase(), + organization_id: "org", + }); +}); +it("binds delayed readback to the submitted ID rather than an edited input", async () => { + let finish!: (response: Response) => void; + vi.stubGlobal( + "fetch", + vi.fn( + () => + new Promise((resolve) => { + finish = resolve; + }), + ), + ); + const { result } = renderHook(() => + useCompanyAssessment("request", "org", async () => "synthetic"), + ); + act(() => result.current.setOpenId(requestedId)); + let pending!: Promise; + await act(async () => { + pending = result.current.run("read_brief"); + }); + act(() => result.current.setOpenId(unrelatedId)); + await act(async () => { + finish( + Response.json({ + snapshot: { id: unrelatedId, state: "saved", superseded: false, brief }, + }), + ); + await pending; + }); + expect(result.current.snapshot).toBeNull(); + expect(result.current.notice).toContain("did not match"); +}); +it.each([403, 404])( + "withholds prior saved text when reopen returns %s without another source lookup", + async (status) => { + const fetcher = vi + .fn() + .mockResolvedValueOnce( + Response.json({ + snapshot: { + id: requestedId, + state: "saved", + superseded: false, + brief, + }, + }), + ) + .mockResolvedValueOnce(new Response(null, { status })); + vi.stubGlobal("fetch", fetcher); + const { result } = renderHook(() => + useCompanyAssessment("request", "org", async () => "synthetic"), + ); + act(() => result.current.setOpenId(requestedId)); + await act(async () => result.current.run("read_brief")); + const previous = result.current.snapshot!; + await act(async () => result.current.run("read_brief")); + expect(result.current.snapshot).toBeNull(); + expect(result.current.brief).toBeNull(); + expect(result.current.isCurrentSnapshot(previous)).toBe(false); + expect(fetcher).toHaveBeenCalledTimes(2); + expect(fetcher.mock.calls.every(([url]) => url === "/api/context")).toBe( + true, + ); + }, +); diff --git a/hooks/useCompanyAssessment.ts b/hooks/useCompanyAssessment.ts index 122569c83..be8975c0f 100644 --- a/hooks/useCompanyAssessment.ts +++ b/hooks/useCompanyAssessment.ts @@ -40,6 +40,7 @@ export function useCompanyAssessment( ); const run = async (action: "brief" | "save_brief" | "read_brief") => { if (inFlight.current) return; + const requestedBriefId = openId.trim(); inFlight.current = true; const revision = ++generation.current; acceptedSnapshot.current = null; @@ -76,8 +77,15 @@ export function useCompanyAssessment( }, revision, ) - : await request({ action, brief_id: openId.trim() }, revision); + : await request({ action, brief_id: requestedBriefId }, revision); if (revision !== generation.current) return; + if ( + action === "read_brief" && + result.snapshot.id.toLowerCase() !== requestedBriefId.toLowerCase() + ) + throw new ReleaseCaseRequestError( + "The response did not match the requested saved assessment. Reopen using its original ID.", + ); setRetrySave(false); if ( result.snapshot.brief &&