diff --git a/apps/app/src/components/thread/timeline/useThreadTimelineController.test.tsx b/apps/app/src/components/thread/timeline/useThreadTimelineController.test.tsx index ead1581963..61350879de 100644 --- a/apps/app/src/components/thread/timeline/useThreadTimelineController.test.tsx +++ b/apps/app/src/components/thread/timeline/useThreadTimelineController.test.tsx @@ -968,6 +968,114 @@ describe("useThreadTimelineController", () => { expect(toastSpy).toHaveBeenCalledTimes(1); }); + it("reveals a message newer than the cached timeline after refreshing", async () => { + const toastSpy = vi.spyOn(appToast, "message").mockReturnValue("toast-1"); + onTestFinished(() => toastSpy.mockRestore()); + const refresh = createDeferredPromise(); + vi.mocked(sdk.threads.timeline).mockReturnValueOnce(refresh.promise); + const { queryClient, wrapper: queryWrapper } = createQueryClientTestHarness( + { queries: { staleTime: 0 } }, + ); + queryClient.setQueryData( + TIMELINE_QUERY_KEY, + makeTimelineResponse({ rows: [newestLoadedRow], maxSeq: 1 }), + ); + const wrapper = ({ children }: { children: ReactNode }) => + queryWrapper({ + children: ( + + + {children} + + + ), + }); + const scrollIntoView = vi.fn(); + const renderedTarget = document.createElement("div"); + renderedTarget.setAttribute("data-timeline-row-id", realtimeRow.id); + renderedTarget.scrollIntoView = scrollIntoView; + onTestFinished(() => renderedTarget.remove()); + const { result } = renderHook( + () => { + const timeline = useThreadTimelineController({ threadId: "thread-1" }); + const pending = useScrollToSearchedMessage( + timeline.timelineRows, + "thread-1", + { reportsMissingTarget: true }, + ); + return { pending, rows: rowIds(timeline) }; + }, + { wrapper }, + ); + + await waitFor(() => expect(sdk.threads.timeline).toHaveBeenCalledOnce()); + document.body.appendChild(renderedTarget); + await act(async () => { + refresh.resolve( + makeTimelineResponse({ + rows: [newestLoadedRow, realtimeRow], + maxSeq: 2, + }), + ); + }); + await waitFor(() => expect(result.current.rows).toContain(realtimeRow.id)); + await waitFor(() => expect(result.current.pending).toBe(false)); + expect(scrollIntoView).toHaveBeenCalledWith({ + block: "start", + inline: "nearest", + }); + expect(toastSpy).not.toHaveBeenCalled(); + }); + + it("keeps a missing message link settled when later rows contain its target", async () => { + const toastSpy = vi.spyOn(appToast, "message").mockReturnValue("toast-1"); + onTestFinished(() => toastSpy.mockRestore()); + const { queryClient, wrapper: queryWrapper } = createQueryClientTestHarness( + { queries: { staleTime: Infinity } }, + ); + queryClient.setQueryData( + TIMELINE_QUERY_KEY, + makeTimelineResponse({ rows: [newestLoadedRow], maxSeq: 1 }), + ); + const wrapper = ({ children }: { children: ReactNode }) => + queryWrapper({ + children: ( + + + {children} + + + ), + }); + const { result } = renderHook( + () => { + const timeline = useThreadTimelineController({ threadId: "thread-1" }); + const pending = useScrollToSearchedMessage( + timeline.timelineRows, + "thread-1", + { reportsMissingTarget: true }, + ); + return { pending, rows: rowIds(timeline) }; + }, + { wrapper }, + ); + + await waitFor(() => expect(toastSpy).toHaveBeenCalledOnce()); + expect(result.current.pending).toBe(false); + act(() => { + queryClient.setQueryData( + TIMELINE_QUERY_KEY, + makeTimelineResponse({ + rows: [newestLoadedRow, realtimeRow], + maxSeq: 2, + }), + ); + }); + await waitFor(() => expect(result.current.rows).toContain(realtimeRow.id)); + expect(result.current.pending).toBe(false); + expect(toastSpy).toHaveBeenCalledOnce(); + }); + it("reveals an accepted steer from a link to its request seq", async () => { const toastSpy = vi.spyOn(appToast, "message").mockReturnValue("toast-1"); onTestFinished(() => toastSpy.mockRestore());