From ddf8bb7d8bbe40bc3bf968fd2392f6045c670c40 Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 15 Apr 2026 23:57:27 +0700 Subject: [PATCH] test(frontend/copilot): add coverage for null cursor guard and initialPageRawMessages path --- .../__tests__/useLoadMoreMessages.test.ts | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useLoadMoreMessages.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useLoadMoreMessages.test.ts index 3c2e008ad6..a196b5cfca 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useLoadMoreMessages.test.ts +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useLoadMoreMessages.test.ts @@ -360,6 +360,71 @@ describe("useLoadMoreMessages", () => { }); }); + describe("loadMore — null cursor guard", () => { + it("is a no-op when newestSequence is null (forwardPaginated=true)", async () => { + const { result } = renderHook(() => + useLoadMoreMessages({ + ...BASE_ARGS, + forwardPaginated: true, + initialNewestSequence: null, + }), + ); + + await act(async () => { + await result.current.loadMore(); + }); + + expect(mockGetV2GetSession).not.toHaveBeenCalled(); + }); + + it("is a no-op when oldestSequence is null (forwardPaginated=false)", async () => { + const { result } = renderHook(() => + useLoadMoreMessages({ + ...BASE_ARGS, + forwardPaginated: false, + initialOldestSequence: null, + }), + ); + + await act(async () => { + await result.current.loadMore(); + }); + + expect(mockGetV2GetSession).not.toHaveBeenCalled(); + }); + }); + + describe("pagedMessages — initialPageRawMessages extraToolOutputs", () => { + it("calls extractToolOutputsFromRaw when initialPageRawMessages is non-empty", async () => { + const { extractToolOutputsFromRaw } = await import( + "../helpers/convertChatSessionToUiMessages" + ); + + const rawMsg = { role: "assistant", content: "hi", sequence: 50 }; + mockGetV2GetSession.mockResolvedValueOnce( + makeSuccessResponse({ + messages: [rawMsg], + has_more_messages: false, + newest_sequence: 99, + }), + ); + + const { result } = renderHook(() => + useLoadMoreMessages({ + ...BASE_ARGS, + forwardPaginated: true, + initialPageRawMessages: [{ role: "user", content: "hello" }], + }), + ); + + await act(async () => { + await result.current.loadMore(); + }); + + expect(extractToolOutputsFromRaw).toHaveBeenCalled(); + }); + }); + describe("loadMore — epoch / stale-response guard", () => { it("discards response when epoch changes during flight (resetPaged called)", async () => { let resolveRequest!: (v: unknown) => void;