import { beforeEach, describe, expect, it, vi } from "vitest"; import { CHAT_PERMISSIONS } from "@hivegrid/chat"; import { buildChatMessageDiscoveryVM } from "@hivegrid/view-models"; const mocks = vi.hoisted(() => ({ getTranslations: vi.fn(), loadContext: vi.fn(), loadDiscovery: vi.fn(), })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: (...args: unknown[]) => mocks.getTranslations(...args) })); vi.mock("./chatAccess.server", () => ({ loadChatContext: (...args: unknown[]) => mocks.loadContext(...args) })); vi.mock("~/modules/chat/services/ChatMessageDiscoveryService.server", () => ({ loadChatMessageDiscovery: (...args: unknown[]) => mocks.loadDiscovery(...args), })); import { loadChatMessageDiscoveryRoute } from "./chatMessageDiscoveryLoader.server"; const context = { tenantId: "tenant_1", userId: "viewer_1", tenantUserId: "tenant_user_1", permissions: [CHAT_PERMISSIONS.readOwnChannels], hasModule: true, } as const; const hrefs = { chat: "/demo/chat", search: "/demo/chat/search", mentions: "/demo/chat/mentions" } as const; describe("chat message discovery route loader", () => { beforeEach(() => { vi.clearAllMocks(); mocks.getTranslations.mockResolvedValue({ t: (key: string) => `t:${key}` }); mocks.loadContext.mockResolvedValue(context); mocks.loadDiscovery.mockResolvedValue(buildChatMessageDiscoveryVM({ mode: "search", query: "heat", rows: [], hrefs })); }); it("normalizes search query and forwards the opaque cursor", async () => { const request = new Request("http://app.localhost:3000/demo/chat/search?q=%20heat%20&cursor=cursor_1"); const result = await loadChatMessageDiscoveryRoute({ request, params: { tenant: "demo" }, context: {} } as never, "search"); expect(mocks.loadDiscovery).toHaveBeenCalledWith({ context, tenantSlug: "demo", mode: "search", query: "heat", beforeCursor: "cursor_1", limit: 30, labels: { directMessage: "t:chat.channel.fallback.directMessage", unnamedGroup: "t:chat.channel.fallback.group", unnamedProject: "t:chat.channel.fallback.project", unknownUser: "t:chat.participant.unknown", }, }); expect(result.pageCursor).toBe("cursor_1"); expect(result.title).toContain("t:chat.messageDiscovery.search.title"); }); it("keeps mentions query-free even when the URL contains q", async () => { mocks.loadDiscovery.mockResolvedValue(buildChatMessageDiscoveryVM({ mode: "mentions", rows: [], hrefs })); const request = new Request("http://app.localhost:3000/demo/chat/mentions?q=ignored"); await loadChatMessageDiscoveryRoute({ request, params: { tenant: "demo" }, context: {} } as never, "mentions"); expect(mocks.loadDiscovery).toHaveBeenCalledWith(expect.not.objectContaining({ query: expect.anything() })); }); });