diff --git a/apps/client/src/features/ai-chat/components/message-list.test.tsx b/apps/client/src/features/ai-chat/components/message-list.test.tsx new file mode 100644 index 00000000..b19470a0 --- /dev/null +++ b/apps/client/src/features/ai-chat/components/message-list.test.tsx @@ -0,0 +1,119 @@ +import { describe, expect, it, vi } from "vitest"; +import { render } from "@testing-library/react"; +import { MantineProvider } from "@mantine/core"; +import type { UIMessage } from "@ai-sdk/react"; + +// Stub react-i18next (MessageList and TypingIndicator read `useTranslation`). +// Mirrors the t-mock pattern used by the other component tests in this folder +// (reasoning-block.test.tsx, message-item-memo.test.tsx). +vi.mock("react-i18next", () => ({ + useTranslation: () => ({ t: (key: string) => key }), +})); + +// Spy on `renderChatMarkdown` exactly as message-item-memo.test.tsx does: keep +// every OTHER named export of markdown.ts intact via `importActual`, and override +// only `renderChatMarkdown` with a `vi.fn()` that returns simple HTML. This makes +// assertions synchronous (no async marked + DOMPurify pass) and lets us count +// parses by argument. `vi.hoisted` so the spy exists when the hoisted `vi.mock` +// factory runs. +const { renderChatMarkdownSpy } = vi.hoisted(() => ({ + renderChatMarkdownSpy: vi.fn((text: string) => `
${text}
`), +})); +vi.mock("@/features/ai-chat/utils/markdown.ts", async () => { + const actual = await vi.importActual< + typeof import("@/features/ai-chat/utils/markdown.ts") + >("@/features/ai-chat/utils/markdown.ts"); + return { ...actual, renderChatMarkdown: renderChatMarkdownSpy }; +}); + +// IMPORTANT: do NOT mock MessageItem and do NOT mock messageSignature — exercising +// the REAL MessageList -> real MessageItem -> real messageSignature wiring is the +// whole point of this file (it closes the parent-side coverage gap left by the +// memo tests, which simulate the parent by hardcoding `signature={...}` in their +// harness). Use the relative import for the component under test, mirroring how +// message-list.tsx itself imports `MessageItem from "./message-item"`. +import MessageList from "./message-list"; + +// matchMedia / localStorage / sessionStorage (read by MantineProvider and app +// code) are stubbed globally in vitest.setup.ts — do NOT re-stub those here. +// +// MessageList renders Mantine's ScrollArea, which constructs a `ResizeObserver`. +// jsdom does not implement it, so install a minimal no-op stub BEFORE rendering. +vi.stubGlobal( + "ResizeObserver", + class { + observe() {} + unobserve() {} + disconnect() {} + }, +); + +// One assistant message wrapping the given `parts`. Reused across renders in the +// regression test to model how the AI SDK hands back the SAME message object. +const msg = (parts: UIMessage["parts"]): UIMessage => + ({ id: "m1", role: "assistant", parts }) as UIMessage; + +describe("MessageList", () => { + it("wires the real MessageItem and supplies a valid signature end-to-end", () => { + renderChatMarkdownSpy.mockClear(); + const { queryByText } = render( +