import { CHAT_EVENT_NAMES } from "@hivegrid/realtime"; import { fetchOutboundHttpWithRedirects } from "@hivegrid/foundation/outbound-http"; import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ client: { $transaction: vi.fn() }, tx: { $executeRaw: vi.fn(), $queryRaw: vi.fn(), tenant: { findFirst: vi.fn() }, teamChatMessage: { findFirst: vi.fn(), update: vi.fn() }, teamChatEvent: { create: vi.fn() }, outboxEvent: { create: vi.fn(), findUnique: vi.fn() }, }, runWithRlsContext: vi.fn(), moduleEnabled: vi.fn(), publishWakeup: vi.fn(), })); vi.mock("@hivegrid/db/client.server", () => ({ getWriteDbClient: () => mocks.client })); vi.mock("@hivegrid/db/rls-context.server", () => ({ runWithRlsContext: (context: unknown, operation: () => unknown) => mocks.runWithRlsContext(context, operation), })); vi.mock("./module-access.server.js", () => ({ tenantHasChatModuleForSystemTx: (...args: unknown[]) => mocks.moduleEnabled(...args), })); vi.mock("./realtime-wakeup.server.js", () => ({ publishChatPersistentRealtimeWakeup: (...args: unknown[]) => mocks.publishWakeup(...args), })); import { CHAT_LINK_PREVIEW_HTML_LIMIT_BYTES, CHAT_LINK_PREVIEW_REQUESTED_OUTBOX_EVENT_TYPE, CHAT_LINK_PREVIEW_USER_AGENT, createPrismaChatLinkPreviewStore, extractChatLinkPreviewPlaceholders, fetchChatLinkPreview, hashChatLinkPreviewBody, parseChatLinkPreviewJob, publishChatLinkPreviewRequest, runChatLinkPreviewJob, type ChatLinkPreviewJob, } from "./link-preview.server.js"; const BODY = "Siehe https://docs.example.test/article"; const NOW = new Date("2026-07-16T10:00:00.000Z"); beforeEach(() => { vi.clearAllMocks(); }); function job(overrides: Partial = {}): ChatLinkPreviewJob { return { outboxEventId: "outbox_1", tenantId: "tenant_1", eventType: CHAT_LINK_PREVIEW_REQUESTED_OUTBOX_EVENT_TYPE, payload: { chatId: "chat_1", messageId: "message_1", bodyHash: hashChatLinkPreviewBody(BODY) }, ...overrides, }; } describe("chat link preview URL intake", () => { it("extracts at most three stable public cards and removes punctuation", () => { expect( extractChatLinkPreviewPlaceholders("https://one.example/a, https://two.example/b https://one.example/a https://three.example/c https://four.example/d") ).toEqual([ { url: "https://one.example/a", title: null, description: null, imageUrl: null, siteName: "one.example" }, { url: "https://two.example/b", title: null, description: null, imageUrl: null, siteName: "two.example" }, { url: "https://three.example/c", title: null, description: null, imageUrl: null, siteName: "three.example" }, ]); }); it.each([ "http://localhost/admin", "http://127.0.0.1/metadata", "http://10.0.0.1/internal", "http://169.254.169.254/latest", "http://93.184.216.34/public", "https://user:secret@example.test/", "https://example.test:8443/", "https://service.internal/", ])("rejects a blocked or credential-bearing target: %s", (url) => { expect(extractChatLinkPreviewPlaceholders(url)).toEqual([]); }); it("publishes only identifiers and a body hash, with no URL in the dispatch key", async () => { mocks.tx.outboxEvent.create.mockImplementation(async ({ data }) => ({ id: "outbox_1", createdAt: NOW, processedAt: null, retries: 0, lastError: null, scheduledAt: NOW, priority: 0, lockedUntil: null, status: "PENDING", ...data, })); await publishChatLinkPreviewRequest(mocks.tx as never, { tenantId: "tenant_1", chatId: "chat_1", messageId: "message_1", requestId: "request_1", body: "Read https://docs.example.test/private?token=do-not-log", }); const data = mocks.tx.outboxEvent.create.mock.calls[0]?.[0].data; expect(data).toMatchObject({ tenantId: "tenant_1", eventType: CHAT_LINK_PREVIEW_REQUESTED_OUTBOX_EVENT_TYPE, payload: { chatId: "chat_1", messageId: "message_1", bodyHash: expect.stringMatching(/^[a-f0-9]{64}$/u) }, dispatchKey: expect.stringMatching(/^chat-link-preview:[a-f0-9]{64}$/u), }); expect(JSON.stringify(data)).not.toContain("do-not-log"); expect(JSON.stringify(data)).not.toContain("docs.example.test"); }); it("does not enqueue a job when the message has no eligible URL", async () => { await expect( publishChatLinkPreviewRequest(mocks.tx as never, { tenantId: "tenant_1", chatId: "chat_1", messageId: "message_1", requestId: "request_1", body: "No external link", }) ).resolves.toBeNull(); expect(mocks.tx.outboxEvent.create).not.toHaveBeenCalled(); }); }); describe("chat link preview metadata fetch", () => { it("extracts bounded OG and HTML metadata with a clear, secret-free request", async () => { const html = ` Fallback title `; const outboundFetch = vi.fn(async (_url: string, init: RequestInit) => { expect(new Headers(init.headers).get("user-agent")).toBe(CHAT_LINK_PREVIEW_USER_AGENT); expect(new Headers(init.headers).has("authorization")).toBe(false); expect(new Headers(init.headers).has("cookie")).toBe(false); return new Response(html, { status: 200, headers: { "content-type": "text/html; charset=utf-8" } }); }); const result = await fetchChatLinkPreview("https://docs.example.test/article", { outboundFetch }); expect(result).toEqual({ status: "ready", preview: { url: "https://docs.example.test/article", title: "Safe & useful", description: "A short description", imageUrl: "https://docs.example.test/preview.png", siteName: "Example Docs", }, }); expect(outboundFetch).toHaveBeenCalledWith( "https://docs.example.test/article", expect.objectContaining({ method: "GET" }), expect.objectContaining({ timeoutMs: 5_000, redirectLimit: 3 }) ); }); it("removes Unicode format and bidi controls from remote metadata", async () => { const outboundFetch = vi.fn( async () => new Response('', { status: 200, headers: { "content-type": "text/html" }, }) ); await expect(fetchChatLinkPreview("https://docs.example.test/article", { outboundFetch })).resolves.toMatchObject({ status: "ready", preview: { title: "Invoice gpj.exe", description: "Safe hidden text", }, }); }); it("discovers an SSRF-guarded oEmbed endpoint and prefers its metadata", async () => { const outboundFetch = vi .fn() .mockResolvedValueOnce( new Response( '', { status: 200, headers: { "content-type": "text/html" } } ) ) .mockResolvedValueOnce( new Response(JSON.stringify({ title: "oEmbed title", author_name: "Ada", provider_name: "Video EU" }), { status: 200, headers: { "content-type": "application/json+oembed" }, }) ); const result = await fetchChatLinkPreview("https://video.example.test/watch/1", { outboundFetch }); expect(result).toMatchObject({ status: "ready", preview: { title: "oEmbed title", description: "Ada", siteName: "Video EU" }, }); expect(outboundFetch.mock.calls.map((call) => call[0])).toEqual(["https://video.example.test/watch/1", "https://embed.example.test/oembed?id=1"]); }); it("keeps a safe URL card for terminal content and bounds response size", async () => { const outboundFetch = vi.fn( async () => new Response("binary", { status: 200, headers: { "content-type": "application/octet-stream", "content-length": String(CHAT_LINK_PREVIEW_HTML_LIMIT_BYTES + 1) }, }) ); await expect(fetchChatLinkPreview("https://files.example.test/archive", { outboundFetch })).resolves.toMatchObject({ status: "terminal", reason: "response_too_large", preview: { url: "https://files.example.test/archive", title: null }, }); }); it("classifies server failures as transient for bounded BullMQ retry", async () => { const outboundFetch = vi.fn(async () => new Response("down", { status: 503, headers: { "content-type": "text/html" } })); await expect(fetchChatLinkPreview("https://docs.example.test/article", { outboundFetch })).resolves.toMatchObject({ status: "transient", reason: "http_503", }); }); it("rejects a redirect whose final response URL is an IP literal", async () => { const response = new Response("Internal", { status: 200, headers: { "content-type": "text/html" } }); Object.defineProperty(response, "url", { value: "http://93.184.216.34/metadata" }); const outboundFetch = vi.fn(async () => response); await expect(fetchChatLinkPreview("https://docs.example.test/article", { outboundFetch })).resolves.toMatchObject({ status: "terminal", reason: "blocked_redirect_url", preview: { url: "https://docs.example.test/article", title: null }, }); }); it("applies the same final-URL policy to oEmbed redirects", async () => { const oEmbedResponse = new Response(JSON.stringify({ title: "Untrusted" }), { status: 200, headers: { "content-type": "application/json+oembed" }, }); Object.defineProperty(oEmbedResponse, "url", { value: "http://93.184.216.34/oembed" }); const outboundFetch = vi .fn() .mockResolvedValueOnce( new Response('', { status: 200, headers: { "content-type": "text/html" }, }) ) .mockResolvedValueOnce(oEmbedResponse); await expect(fetchChatLinkPreview("https://video.example.test/watch/1", { outboundFetch })).resolves.toMatchObject({ status: "terminal", reason: "blocked_oembed_redirect_url", preview: { url: "https://video.example.test/watch/1", title: null }, }); }); it("rejects a non-default port on a redirect before the second request", async () => { const fetchImpl = vi.fn(async () => new Response(null, { status: 302, headers: { location: "https://redirect.example.test:8443/private" } })); await expect( fetchChatLinkPreview("https://docs.example.test/article", { outboundFetch: fetchOutboundHttpWithRedirects, fetchOptions: { fetchImpl, addressResolver: async () => ["93.184.216.34"], // A caller cannot replace the link-specific per-hop policy. urlValidator: (value) => value, }, }) ).resolves.toMatchObject({ status: "terminal", reason: "blocked_url" }); expect(fetchImpl).toHaveBeenCalledTimes(1); }); }); describe("chat link preview job and RLS store", () => { beforeEach(() => { mocks.runWithRlsContext.mockImplementation((_context, operation: () => unknown) => operation()); mocks.client.$transaction.mockImplementation((operation: (tx: typeof mocks.tx) => unknown) => operation(mocks.tx)); mocks.moduleEnabled.mockResolvedValue(true); mocks.tx.tenant.findFirst.mockResolvedValue({ id: "tenant_1" }); mocks.tx.teamChatMessage.findFirst.mockResolvedValue({ body: BODY, linkPreviews: extractChatLinkPreviewPlaceholders(BODY), chat: { eventStreamId: "stream_1" }, }); mocks.tx.teamChatMessage.update.mockResolvedValue({ id: "message_1" }); mocks.tx.teamChatEvent.create.mockResolvedValue({ sequence: 7n, occurredAt: NOW, tenantId: "tenant_1", chatId: "chat_1", requestId: "link-preview-event", data: { messageId: "message_1", linkPreviews: [{ url: "https://docs.example.test/article", title: "Docs", description: null, imageUrl: null, siteName: "Example" }], }, }); mocks.publishWakeup.mockResolvedValue(undefined); mocks.tx.outboxEvent.create.mockImplementation(async ({ data }) => ({ id: "outbox_retry", createdAt: NOW, processedAt: null, retries: 0, lastError: null, scheduledAt: data.scheduledAt ?? NOW, priority: 0, lockedUntil: null, status: "PENDING", ...data, })); }); it("parses only the exact tenant-scoped outbox contract", () => { expect(parseChatLinkPreviewJob(job())).toEqual(job()); expect(() => parseChatLinkPreviewJob({ ...job(), eventType: "chat.realtime.persistent" })).toThrow("event type"); expect(() => parseChatLinkPreviewJob({ ...job(), payload: { ...job().payload, bodyHash: "bad" } })).toThrow("bodyHash"); expect(() => parseChatLinkPreviewJob({ ...job(), payload: { ...job().payload, attempt: 1 } })).toThrow("urlIndex"); }); it("loads only messages whose channel is still active", async () => { const store = createPrismaChatLinkPreviewStore(); await expect(store.loadSource(job())).resolves.toEqual({ body: BODY }); expect(mocks.tx.teamChatMessage.findFirst).toHaveBeenCalledWith( expect.objectContaining({ where: expect.objectContaining({ chat: { archivedAt: null } }) }) ); }); it("persists metadata plus one durable ready event and realtime wakeup under tenant RLS", async () => { const store = createPrismaChatLinkPreviewStore(); const result = await store.persist({ ...job(), updates: [ { urlIndex: 0, preview: { url: "https://docs.example.test/article", title: "Docs", description: null, imageUrl: null, siteName: "Example" }, }, ], retries: [], }); expect(result).toBe("updated"); expect(mocks.runWithRlsContext).toHaveBeenCalledWith({ tenantId: "tenant_1" }, expect.any(Function)); expect(mocks.tx.$executeRaw).toHaveBeenCalledOnce(); expect(mocks.tx.teamChatMessage.findFirst).toHaveBeenCalledWith( expect.objectContaining({ where: expect.objectContaining({ tenantId: "tenant_1", id: "message_1", chatId: "chat_1", deletedAt: null, chat: { archivedAt: null } }), }) ); expect(mocks.tx.teamChatEvent.create).toHaveBeenCalledWith( expect.objectContaining({ data: expect.objectContaining({ tenantId: "tenant_1", chatId: "chat_1", type: "link_preview_ready", actorUserId: null, entityId: "message_1", }), }) ); expect(mocks.publishWakeup).toHaveBeenCalledWith(mocks.tx, { event: expect.objectContaining({ event: CHAT_EVENT_NAMES.linkPreviewReady, cursor: "stream_1:7" }), }); }); it("rechecks the body hash after fetching and skips a stale edit", async () => { const store = createPrismaChatLinkPreviewStore(); mocks.tx.teamChatMessage.findFirst.mockResolvedValue({ body: "Edited https://new.example.test/", linkPreviews: [], chat: { eventStreamId: "stream_1" }, }); await expect( store.persist({ ...job(), updates: [{ urlIndex: 0, preview: extractChatLinkPreviewPlaceholders(BODY)[0]! }], retries: [], }) ).resolves.toBe("skipped"); expect(mocks.tx.teamChatMessage.update).not.toHaveBeenCalled(); expect(mocks.tx.teamChatEvent.create).not.toHaveBeenCalled(); }); it("persists the fallback card and schedules only the transient URL with Retry-After", async () => { const store = { loadSource: vi.fn(async () => ({ body: BODY })), persist: vi.fn(async () => "updated" as const), }; const outboundFetch = vi.fn(async () => new Response("down", { status: 503, headers: { "content-type": "text/html", "retry-after": "120" } })); await expect(runChatLinkPreviewJob(job(), { store, outboundFetch, now: () => NOW })).resolves.toEqual({ status: "updated", messageId: "message_1", transientFailures: 1, scheduledRetries: 1, deferredByBudget: 0, }); expect(store.persist).toHaveBeenCalledWith( expect.objectContaining({ tenantId: "tenant_1", updates: [{ urlIndex: 0, preview: extractChatLinkPreviewPlaceholders(BODY)[0] }], retries: [ expect.objectContaining({ urlIndex: 0, attempt: 1, budgetDeferrals: 0, reason: "transient", scheduledAt: new Date("2026-07-16T10:02:00.000Z"), }), ], }) ); }); it("does not refetch successful URLs when a targeted retry runs", async () => { const body = "One https://one.example.test/a two https://two.example.test/b"; const targetedJob = job({ payload: { chatId: "chat_1", messageId: "message_1", bodyHash: hashChatLinkPreviewBody(body), urlIndex: 1, attempt: 1, budgetDeferrals: 0, }, }); const store = { loadSource: vi.fn(async () => ({ body })), persist: vi.fn(async () => "updated" as const), }; const outboundFetch = vi.fn(async () => new Response("Second ready", { status: 200, headers: { "content-type": "text/html" } })); await expect(runChatLinkPreviewJob(targetedJob, { store, outboundFetch })).resolves.toMatchObject({ status: "updated", transientFailures: 0, scheduledRetries: 0, }); expect(outboundFetch).toHaveBeenCalledTimes(1); expect(outboundFetch).toHaveBeenCalledWith("https://two.example.test/b", expect.anything(), expect.anything()); expect(store.persist).toHaveBeenCalledWith( expect.objectContaining({ updates: [expect.objectContaining({ urlIndex: 1, preview: expect.objectContaining({ title: "Second ready" }) })], retries: [], }) ); }); it("defers only over-budget URLs and does not start their network request", async () => { const body = "One https://one.example.test/a two https://two.example.test/b"; const initialJob = job({ payload: { chatId: "chat_1", messageId: "message_1", bodyHash: hashChatLinkPreviewBody(body) }, }); const store = { loadSource: vi.fn(async () => ({ body })), persist: vi.fn(async () => "updated" as const), }; const tenantBudget = { consume: vi.fn().mockResolvedValueOnce({ allowed: true, retryAfterMs: 0 }).mockResolvedValueOnce({ allowed: false, retryAfterMs: 30_000 }), }; const outboundFetch = vi.fn(async () => new Response("First ready", { status: 200, headers: { "content-type": "text/html" } })); await expect(runChatLinkPreviewJob(initialJob, { store, outboundFetch, tenantBudget, now: () => NOW })).resolves.toEqual({ status: "updated", messageId: "message_1", transientFailures: 0, scheduledRetries: 1, deferredByBudget: 1, }); expect(tenantBudget.consume).toHaveBeenCalledTimes(2); expect(outboundFetch).toHaveBeenCalledTimes(1); expect(store.persist).toHaveBeenCalledWith( expect.objectContaining({ updates: [expect.objectContaining({ urlIndex: 0 })], retries: [ expect.objectContaining({ urlIndex: 1, attempt: 0, budgetDeferrals: 1, reason: "tenant_budget", scheduledAt: new Date("2026-07-16T10:00:30.000Z"), }), ], }) ); }); it("persists a URL-free targeted retry through the transactional outbox", async () => { const store = createPrismaChatLinkPreviewStore(); const result = await store.persist({ ...job(), updates: [], retries: [ { urlIndex: 0, attempt: 1, budgetDeferrals: 0, scheduledAt: new Date("2026-07-16T10:02:00.000Z"), reason: "transient", }, ], }); expect(result).toBe("unchanged"); const retryData = mocks.tx.outboxEvent.create.mock.calls[0]?.[0].data; expect(retryData).toMatchObject({ tenantId: "tenant_1", eventType: CHAT_LINK_PREVIEW_REQUESTED_OUTBOX_EVENT_TYPE, payload: { chatId: "chat_1", messageId: "message_1", bodyHash: hashChatLinkPreviewBody(BODY), urlIndex: 0, attempt: 1, budgetDeferrals: 0, }, scheduledAt: new Date("2026-07-16T10:02:00.000Z"), dispatchKey: expect.stringMatching(/^chat-link-preview-retry:[a-f0-9]{64}$/u), }); expect(JSON.stringify(retryData)).not.toContain("docs.example.test"); expect(mocks.tx.teamChatEvent.create).not.toHaveBeenCalled(); }); it("does not perform any network request for a stale or deleted source", async () => { const outboundFetch = vi.fn(); const store = { loadSource: vi.fn(async () => null), persist: vi.fn() }; await expect(runChatLinkPreviewJob(job(), { store, outboundFetch })).resolves.toEqual({ status: "skipped", messageId: "message_1", transientFailures: 0, scheduledRetries: 0, deferredByBudget: 0, }); expect(outboundFetch).not.toHaveBeenCalled(); expect(store.persist).not.toHaveBeenCalled(); }); });