import { describe, expect, it, vi } from "vitest"; import { createSearchClient, crmContactsIndex, kbArticlesIndex, rowsIndex, searchInIndex, usersIndex } from "@toolctrl/search"; import { createInMemoryMeili } from "@toolctrl/search/testing"; import { runSearchIndexerJob, SearchIndexerJobError, parseSearchIndexJob } from "./search-indexer.js"; import { createSearchIndexerBulkPagers, registeredSearchIndexerBulkPagerNames } from "./search-indexer-bulk-pagers.js"; function makeClient() { const meili = createInMemoryMeili(); const client = createSearchClient({ url: "http://memory", meiliFactory: meili.factory }); return { meili, client }; } describe("parseSearchIndexJob", () => { it("rejects unknown actions", () => { expect(() => parseSearchIndexJob({ payload: { action: "rename", indexName: "kb-articles", tenantId: "t" } })).toThrow(SearchIndexerJobError); }); it("rejects missing tenantId in payload", () => { expect(() => parseSearchIndexJob({ payload: { action: "upsert", indexName: "kb-articles" } })).toThrow(/tenantId/); }); it("accepts a well-formed upsert envelope", () => { const parsed = parseSearchIndexJob({ outboxEventId: "evt_1", tenantId: "t1", eventType: "search.index.kb.upsert", payload: { action: "upsert", indexName: "kb-articles", tenantId: "t1", document: { id: "a", tenantId: "t1", title: "X" } }, }); expect(parsed.payload.action).toBe("upsert"); expect(parsed.outboxEventId).toBe("evt_1"); }); it("rejects envelope and payload tenant mismatches", () => { for (const payload of [ { action: "upsert", indexName: "kb-articles", tenantId: "t2", document: { id: "a", tenantId: "t2", title: "X" } }, { action: "delete", indexName: "kb-articles", tenantId: "t2", documentId: "a" }, { action: "bulk-reindex", indexName: "kb-articles", tenantId: "t2" }, ]) { expect(() => parseSearchIndexJob({ outboxEventId: "evt_1", tenantId: "t1", eventType: "search.index.kb.upsert", payload, }) ).toThrow(/tenantId mismatch/); } }); }); describe("runSearchIndexerJob", () => { it("upserts a document and isolates it to the tenant", async () => { const { client } = makeClient(); const data = { eventType: "search.index.kb.upsert", tenantId: "t1", payload: { action: "upsert", indexName: kbArticlesIndex.name, tenantId: "t1", document: { id: "a", tenantId: "t1", title: "Hello" }, }, }; const result = await runSearchIndexerJob(data, { searchClient: client }); expect(result).toMatchObject({ action: "upsert", documentsAffected: 1, tenantId: "t1" }); const hits = await searchInIndex({ client, schema: kbArticlesIndex, tenantId: "t1", query: "Hello" }); expect(hits.hits.map((h) => h.document.id)).toEqual(["a"]); const other = await searchInIndex({ client, schema: kbArticlesIndex, tenantId: "t2", query: "Hello" }); expect(other.hits).toHaveLength(0); }); it("deletes a document", async () => { const { client } = makeClient(); await runSearchIndexerJob( { eventType: "search.index.kb.upsert", tenantId: "t1", payload: { action: "upsert", indexName: kbArticlesIndex.name, tenantId: "t1", document: { id: "a", tenantId: "t1", title: "Hi" } }, }, { searchClient: client } ); const result = await runSearchIndexerJob( { eventType: "search.index.kb.delete", tenantId: "t1", payload: { action: "delete", indexName: kbArticlesIndex.name, tenantId: "t1", documentId: "a" }, }, { searchClient: client } ); expect(result.action).toBe("delete"); const hits = await searchInIndex({ client, schema: kbArticlesIndex, tenantId: "t1", query: "Hi" }); expect(hits.hits).toHaveLength(0); }); it("runs bulk-reindex via the supplied pager", async () => { const { client } = makeClient(); const pager = vi .fn() .mockResolvedValueOnce({ documents: [ { id: "1", tenantId: "t1", title: "Doc 1" }, { id: "2", tenantId: "t1", title: "Doc 2" }, ], nextCursor: "cursor-1", }) .mockResolvedValueOnce({ documents: [{ id: "3", tenantId: "t1", title: "Doc 3" }], nextCursor: null }); const result = await runSearchIndexerJob( { eventType: "search.index.kb.bulk-reindex", tenantId: "t1", payload: { action: "bulk-reindex", indexName: kbArticlesIndex.name, tenantId: "t1" }, }, { searchClient: client, bulkPagers: { [kbArticlesIndex.name]: pager }, bulkPageSize: 2, } ); expect(result).toMatchObject({ action: "bulk-reindex", documentsAffected: 3, tenantId: "t1" }); expect(pager).toHaveBeenCalledTimes(2); }); it("fails bulk-reindex when the pager cursor does not advance", async () => { const { client } = makeClient(); const pager = vi .fn() .mockResolvedValueOnce({ documents: [{ id: "1", tenantId: "t1", title: "Doc 1" }], nextCursor: "cursor-1", }) .mockResolvedValueOnce({ documents: [{ id: "2", tenantId: "t1", title: "Doc 2" }], nextCursor: "cursor-1", }); await expect( runSearchIndexerJob( { eventType: "search.index.kb.bulk-reindex", tenantId: "t1", payload: { action: "bulk-reindex", indexName: kbArticlesIndex.name, tenantId: "t1" }, }, { searchClient: client, bulkPagers: { [kbArticlesIndex.name]: pager }, bulkPageSize: 1, } ) ).rejects.toMatchObject({ code: "BULK_PAGER_STALLED" }); expect(pager).toHaveBeenCalledTimes(2); }); it("fails bulk-reindex when maxBulkPages is reached before completion", async () => { const { client } = makeClient(); const pager = vi.fn().mockResolvedValueOnce({ documents: [{ id: "1", tenantId: "t1", title: "Doc 1" }], nextCursor: "cursor-1", }); await expect( runSearchIndexerJob( { eventType: "search.index.kb.bulk-reindex", tenantId: "t1", payload: { action: "bulk-reindex", indexName: kbArticlesIndex.name, tenantId: "t1" }, }, { searchClient: client, bulkPagers: { [kbArticlesIndex.name]: pager }, bulkPageSize: 1, maxBulkPages: 1, } ) ).rejects.toMatchObject({ code: "BULK_REINDEX_INCOMPLETE" }); expect(pager).toHaveBeenCalledTimes(1); }); it("replaces the tenant slice during bulk-reindex", async () => { const { client } = makeClient(); await runSearchIndexerJob( { eventType: "search.index.kb.upsert", tenantId: "t1", payload: { action: "upsert", indexName: kbArticlesIndex.name, tenantId: "t1", document: { id: "stale", tenantId: "t1", title: "Stale Doc" } }, }, { searchClient: client } ); await runSearchIndexerJob( { eventType: "search.index.kb.upsert", tenantId: "t2", payload: { action: "upsert", indexName: kbArticlesIndex.name, tenantId: "t2", document: { id: "stale", tenantId: "t2", title: "Other Tenant" } }, }, { searchClient: client } ); const result = await runSearchIndexerJob( { eventType: "search.index.kb.bulk-reindex", tenantId: "t1", payload: { action: "bulk-reindex", indexName: kbArticlesIndex.name, tenantId: "t1" }, }, { searchClient: client, bulkPagers: { [kbArticlesIndex.name]: vi.fn().mockResolvedValueOnce({ documents: [{ id: "fresh", tenantId: "t1", title: "Fresh Doc" }], nextCursor: null, }), }, } ); expect(result).toMatchObject({ action: "bulk-reindex", documentsAffected: 1, tenantId: "t1" }); await expect(searchInIndex({ client, schema: kbArticlesIndex, tenantId: "t1", query: "Stale" })).resolves.toMatchObject({ hits: [] }); await expect(searchInIndex({ client, schema: kbArticlesIndex, tenantId: "t1", query: "Fresh" })).resolves.toMatchObject({ hits: [{ document: expect.objectContaining({ id: "fresh" }) }] }); await expect(searchInIndex({ client, schema: kbArticlesIndex, tenantId: "t2", query: "Other" })).resolves.toMatchObject({ hits: [{ document: expect.objectContaining({ id: "stale" }) }] }); }); it("throws when bulk-reindex has no pager registered for the index", async () => { const { client } = makeClient(); await expect( runSearchIndexerJob( { eventType: "search.index.kb.bulk-reindex", tenantId: "t1", payload: { action: "bulk-reindex", indexName: kbArticlesIndex.name, tenantId: "t1" }, }, { searchClient: client } ) ).rejects.toThrow(SearchIndexerJobError); }); it("registers a kb-articles bulk pager that maps tenant-scoped documents", async () => { const findMany = vi.fn(async () => [ { id: "article_1", knowledgeBaseId: "kb_1", slug: "hello", title: "Hello", description: "Summary", contentPublishedAsText: "Published text", language: "en", publishedAt: new Date("2026-01-02T03:04:05.000Z"), createdAt: new Date("2026-01-01T00:00:00.000Z"), updatedAt: null, knowledgeBase: { tenantId: "tenant_1" }, }, ]); const pagers = createSearchIndexerBulkPagers({ prisma: fakeSearchPagerPrisma({ knowledgeBaseArticle: { findMany } }), }); const result = await pagers[kbArticlesIndex.name]?.({ indexName: kbArticlesIndex.name, tenantId: "tenant_1", cursor: "article_0", pageSize: 1, }); expect(registeredSearchIndexerBulkPagerNames).toContain(kbArticlesIndex.name); expect(findMany).toHaveBeenCalledWith( expect.objectContaining({ where: { id: { gt: "article_0" }, knowledgeBase: { tenantId: "tenant_1" } }, take: 1, }) ); expect(result?.documents).toEqual([ expect.objectContaining({ id: "article_1", tenantId: "tenant_1", knowledgeBaseId: "kb_1", summary: "Summary", content: "Published text", isPublished: true, }), ]); expect(result?.nextCursor).toBe("article_1"); }); it("defensively filters kb-article bulk pager rows by tenant and terminates short pages", async () => { const findMany = vi.fn(async () => [ { id: "article_1", knowledgeBaseId: "kb_1", slug: "hello", title: "Hello", description: "Summary", contentPublishedAsText: "Published text", language: "de", publishedAt: "2026-01-02T03:04:05.000Z", createdAt: new Date("2026-01-01T00:00:00.000Z"), updatedAt: new Date("2026-01-03T00:00:00.000Z"), knowledgeBase: { tenantId: "tenant_1" }, }, { id: "article_2", knowledgeBaseId: "kb_2", slug: "other", title: "Other", description: "Other summary", contentPublishedAsText: "Other text", language: "en", publishedAt: null, createdAt: new Date("2026-01-01T00:00:00.000Z"), updatedAt: null, knowledgeBase: { tenantId: "tenant_2" }, }, ]); const pagers = createSearchIndexerBulkPagers({ prisma: fakeSearchPagerPrisma({ knowledgeBaseArticle: { findMany } }), }); const result = await pagers[kbArticlesIndex.name]?.({ indexName: kbArticlesIndex.name, tenantId: "tenant_1", cursor: null, pageSize: 3, }); expect(findMany).toHaveBeenCalledWith( expect.objectContaining({ where: { knowledgeBase: { tenantId: "tenant_1" } }, orderBy: { id: "asc" }, take: 3, }) ); expect(result?.documents).toEqual([ expect.objectContaining({ id: "article_1", tenantId: "tenant_1", language: "de", createdAt: "2026-01-01T00:00:00.000Z", updatedAt: "2026-01-03T00:00:00.000Z", publishedAt: "2026-01-02T03:04:05.000Z", }), ]); expect(result?.nextCursor).toBeNull(); }); it("registers a rows bulk pager that maps searchable tenant rows", async () => { const findRows = vi.fn(async () => [ makeSearchRow({ id: "row_1", tenantId: "tenant_1", entityName: "account", values: { name: "Acme", status: "active", hiddenNumber: 42, }, }), ]); const pagers = createSearchIndexerBulkPagers({ prisma: fakeSearchPagerPrisma({ row: { findMany: findRows } }), }); const result = await pagers[rowsIndex.name]?.({ indexName: rowsIndex.name, tenantId: "tenant_1", cursor: "row_0", pageSize: 1, }); expect(registeredSearchIndexerBulkPagerNames).toContain(rowsIndex.name); expect(findRows).toHaveBeenCalledWith( expect.objectContaining({ where: { id: { gt: "row_0" }, tenantId: "tenant_1", deletedAt: null }, take: 1, }) ); expect(result?.documents).toEqual([ expect.objectContaining({ id: "row_1", tenantId: "tenant_1", entityId: "entity_1", entityName: "account", folio: 7, searchable: "Acme \n active", createdById: "user_1", }), ]); expect(result?.nextCursor).toBe("row_1"); }); it("registers a crm-contacts bulk pager from contact rows only", async () => { const findRows = vi.fn(async () => [ makeSearchRow({ id: "contact_1", tenantId: "tenant_1", entityName: "contact", values: { firstName: "Ada", lastName: "Lovelace", email: "ada@example.test", company: "Analytical Engines", jobTitle: "Founder", status: "customer", }, }), makeSearchRow({ id: "contact_2", tenantId: "tenant_2", entityName: "contact", values: { firstName: "Other" }, }), ]); const pagers = createSearchIndexerBulkPagers({ prisma: fakeSearchPagerPrisma({ row: { findMany: findRows } }), }); const result = await pagers[crmContactsIndex.name]?.({ indexName: crmContactsIndex.name, tenantId: "tenant_1", cursor: null, pageSize: 3, }); expect(registeredSearchIndexerBulkPagerNames).toContain(crmContactsIndex.name); expect(findRows).toHaveBeenCalledWith( expect.objectContaining({ where: { tenantId: "tenant_1", deletedAt: null, entity: { name: "contact" } }, orderBy: { id: "asc" }, take: 3, }) ); expect(result?.documents).toEqual([ expect.objectContaining({ id: "contact_1", tenantId: "tenant_1", firstName: "Ada", lastName: "Lovelace", email: "ada@example.test", company: "Analytical Engines", jobTitle: "Founder", status: "customer", ownerId: "user_1", }), ]); expect(result?.nextCursor).toBeNull(); }); it("registers a users bulk pager from tenant memberships", async () => { const findTenantUsers = vi.fn(async () => [ { id: "tenant_user_1", tenantId: "tenant_1", userId: "user_1", type: 1, status: 2, user: { id: "user_1", createdAt: new Date("2026-01-01T00:00:00.000Z"), email: "ada@example.test", firstName: "Ada", lastName: "Lovelace", active: true, admin: { userId: "user_1" }, logs: [{ createdAt: new Date("2026-01-03T00:00:00.000Z") }], }, }, { id: "tenant_user_2", tenantId: "tenant_2", userId: "user_2", type: 2, status: 2, user: { id: "user_2", createdAt: new Date("2026-01-01T00:00:00.000Z"), email: "other@example.test", firstName: "Other", lastName: "Tenant", active: true, admin: null, logs: [], }, }, ]); const pagers = createSearchIndexerBulkPagers({ prisma: fakeSearchPagerPrisma({ tenantUser: { findMany: findTenantUsers } }), }); const result = await pagers[usersIndex.name]?.({ indexName: usersIndex.name, tenantId: "tenant_1", cursor: "tenant_user_0", pageSize: 2, }); expect(registeredSearchIndexerBulkPagerNames).toContain(usersIndex.name); expect(findTenantUsers).toHaveBeenCalledWith( expect.objectContaining({ where: { id: { gt: "tenant_user_0" }, tenantId: "tenant_1" }, orderBy: { id: "asc" }, take: 2, }) ); expect(result?.documents).toEqual([ expect.objectContaining({ id: "user_1", tenantId: "tenant_1", firstName: "Ada", lastName: "Lovelace", email: "ada@example.test", type: 1, admin: true, active: true, createdAt: "2026-01-01T00:00:00.000Z", lastLoginAt: "2026-01-03T00:00:00.000Z", }), ]); expect(result?.nextCursor).toBe("tenant_user_2"); }); it("rejects payload pointing at an unknown index", async () => { const { client } = makeClient(); await expect( runSearchIndexerJob( { eventType: "search.index.unknown.upsert", tenantId: "t1", payload: { action: "upsert", indexName: "does-not-exist", tenantId: "t1", document: { id: "a", tenantId: "t1" } }, }, { searchClient: client } ) ).rejects.toThrow(/unknown search index/); }); }); function fakeSearchPagerPrisma(overrides: Partial[0]["prisma"]>) { return { knowledgeBaseArticle: { findMany: vi.fn(async () => []) }, row: { findMany: vi.fn(async () => []) }, tenantUser: { findMany: vi.fn(async () => []) }, ...overrides, } as Parameters[0]["prisma"]; } function makeSearchRow(input: { id: string; tenantId: string; entityName: string; values: Record; }) { const properties = Object.keys(input.values).map((name, index) => ({ id: `property_${name}`, name, type: typeof input.values[name] === "string" ? (name === "hiddenNumber" ? 0 : 1) : 0, order: index, })); return { id: input.id, tenantId: input.tenantId, entityId: "entity_1", folio: 7, createdByUserId: "user_1", createdAt: new Date("2026-01-01T00:00:00.000Z"), updatedAt: new Date("2026-01-02T00:00:00.000Z"), deletedAt: null, entity: { name: input.entityName, properties, }, values: properties.map((property) => { const value = input.values[property.name]; return { propertyId: property.id, textValue: typeof value === "string" ? value : null, numberValue: typeof value === "number" ? value : null, dateValue: null, booleanValue: typeof value === "boolean" ? value : null, property, }; }), }; }