test(frontend): add missing HTTP mocks for conversation history preloading (#12549)

This commit is contained in:
sp.wack
2026-01-22 20:30:27 +04:00
committed by GitHub
parent 60e668f4a7
commit cc5d5c2335
2 changed files with 40 additions and 12 deletions

View File

@@ -40,6 +40,18 @@ import { conversationWebSocketTestSetup } from "./helpers/msw-websocket-setup";
import { useEventStore } from "#/stores/use-event-store";
import { isV1Event } from "#/types/v1/type-guards";
// Mock useUserConversation to return V1 conversation data
vi.mock("#/hooks/query/use-user-conversation", () => ({
useUserConversation: vi.fn(() => ({
data: {
conversation_version: "V1",
status: "RUNNING",
},
isLoading: false,
error: null,
})),
}));
// MSW WebSocket mock setup
const { wsLink, server: mswServer } = conversationWebSocketTestSetup();
@@ -667,6 +679,16 @@ describe("Conversation WebSocket Handler", () => {
// Set up MSW to mock both the HTTP API and WebSocket connection
mswServer.use(
// Mock events search for history preloading
http.get(
`http://localhost:3000/api/v1/conversation/${conversationId}/events/search`,
async () => {
await new Promise((resolve) => setTimeout(resolve, 10));
return HttpResponse.json({
items: mockHistoryEvents,
});
},
),
http.get(
`http://localhost:3000/api/conversations/${conversationId}/events/count`,
() => HttpResponse.json(expectedEventCount),
@@ -703,11 +725,6 @@ describe("Conversation WebSocket Handler", () => {
`http://localhost:3000/api/conversations/${conversationId}`,
);
// Initially should be loading history
expect(screen.getByTestId("is-loading-history")).toHaveTextContent(
"true",
);
// Wait for all events to be received
await waitFor(() => {
expect(screen.getByTestId("events-received")).toHaveTextContent("3");
@@ -726,6 +743,14 @@ describe("Conversation WebSocket Handler", () => {
// Set up MSW to mock both the HTTP API and WebSocket connection
mswServer.use(
// Mock empty events search
http.get(
`http://localhost:3000/api/v1/conversation/${conversationId}/events/search`,
() =>
HttpResponse.json({
items: [],
}),
),
http.get(
`http://localhost:3000/api/conversations/${conversationId}/events/count`,
() => HttpResponse.json(0),
@@ -775,6 +800,16 @@ describe("Conversation WebSocket Handler", () => {
// Set up MSW to mock both the HTTP API and WebSocket connection
mswServer.use(
// Mock events search for history preloading (50 events)
http.get(
`http://localhost:3000/api/v1/conversation/${conversationId}/events/search`,
async () => {
await new Promise((resolve) => setTimeout(resolve, 10));
return HttpResponse.json({
items: mockHistoryEvents,
});
},
),
http.get(
`http://localhost:3000/api/conversations/${conversationId}/events/count`,
() => HttpResponse.json(expectedEventCount),
@@ -810,11 +845,6 @@ describe("Conversation WebSocket Handler", () => {
`http://localhost:3000/api/conversations/${conversationId}`,
);
// Initially should be loading history
expect(screen.getByTestId("is-loading-history")).toHaveTextContent(
"true",
);
// Wait for all events to be received
await waitFor(() => {
expect(screen.getByTestId("events-received")).toHaveTextContent("50");

View File

@@ -1,5 +1,3 @@
// @vitest-environment happy-dom
import { describe, it, expect, afterEach, vi } from "vitest";
import React from "react";
import { renderHook, waitFor } from "@testing-library/react";