/** * Unit tests for searchCrmContacts — the public CRM-contacts search * entry-point. These tests cover the SQL-fallback path (when MEILI_URL * is unset) so the legacy SQL contract is exercised without requiring * a live Meilisearch container. */ import { afterAll, beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("~/utils/db.server", () => ({ db: { row: { findMany: vi.fn(), }, }, })); import { db } from "~/utils/db.server"; import { searchCrmContacts } from "./crmContactSearch.server"; const dbMock = db as unknown as { row: { findMany: ReturnType; }; }; describe("searchCrmContacts (sql fallback)", () => { const originalMeili = process.env.MEILI_URL; const originalBackend = process.env.SEARCH_BACKEND; beforeEach(() => { delete process.env.MEILI_URL; delete process.env.SEARCH_BACKEND; dbMock.row.findMany.mockReset(); }); afterEachRestore(originalMeili, originalBackend); it("scopes by tenant + entity=contact and maps row values to contact-hit fields", async () => { dbMock.row.findMany.mockResolvedValueOnce([ { id: "row-1", values: [ { textValue: "Alice", property: { name: "firstName" } }, { textValue: "Anderson", property: { name: "lastName" } }, { textValue: "alice@example.com", property: { name: "email" } }, { textValue: "Acme", property: { name: "company" } }, { textValue: "CTO", property: { name: "jobTitle" } }, ], }, ]); const result = await searchCrmContacts({ tenantId: "tenant-1", query: "alice" }); expect(dbMock.row.findMany).toHaveBeenCalledTimes(1); const where = dbMock.row.findMany.mock.calls[0][0].where as Record; expect(where.tenantId).toBe("tenant-1"); expect((where.entity as { name: string }).name).toBe("contact"); expect(where.deletedAt).toBeNull(); expect(where.values).toBeDefined(); expect(result.fellBack).toBe(true); expect(result.hits).toHaveLength(1); expect(result.hits[0]).toEqual({ id: "row-1", firstName: "Alice", lastName: "Anderson", email: "alice@example.com", company: "Acme", jobTitle: "CTO", }); }); it("omits the value-text predicate when the query is empty", async () => { dbMock.row.findMany.mockResolvedValueOnce([]); await searchCrmContacts({ tenantId: "tenant-1", query: "" }); const where = dbMock.row.findMany.mock.calls[0][0].where as Record; expect(where.values).toBeUndefined(); }); it("honours the limit option", async () => { dbMock.row.findMany.mockResolvedValueOnce([]); await searchCrmContacts({ tenantId: "tenant-1", query: "x", limit: 5 }); expect(dbMock.row.findMany.mock.calls[0][0].take).toBe(5); }); it("returns total = number of fallback hits when Meilisearch is unavailable", async () => { dbMock.row.findMany.mockResolvedValueOnce([ { id: "r1", values: [] }, { id: "r2", values: [] }, { id: "r3", values: [] }, ]); const result = await searchCrmContacts({ tenantId: "tenant-1", query: "" }); expect(result.total).toBe(3); expect(result.hits).toHaveLength(3); }); }); function afterEachRestore(originalMeili: string | undefined, originalBackend: string | undefined) { // restore env vars after the file completes (vitest doesn't gather them // via beforeEach, so we centralize the cleanup here) if (typeof afterAll === "function") { afterAll(() => { if (originalMeili === undefined) delete process.env.MEILI_URL; else process.env.MEILI_URL = originalMeili; if (originalBackend === undefined) delete process.env.SEARCH_BACKEND; else process.env.SEARCH_BACKEND = originalBackend; }); } }