import { describe, expect, it, vi } from "vitest"; import { ALL_SEARCH_INDEXES, BULK_REINDEX_SUPPORTED_INDEXES, BULK_REINDEX_SUPPORTED_INDEX_NAMES, crmContactsIndex, kbArticlesIndex, usersIndex, } from "@hivegrid/search"; import { enqueueReindexEvents, selectIndexes, selectTenants } from "./reindex.js"; describe("selectIndexes", () => { it("returns every supported bulk-reindex entry when input is undefined or 'all'", () => { expect(selectIndexes(undefined)).toEqual(BULK_REINDEX_SUPPORTED_INDEXES); expect(selectIndexes("all")).toEqual(BULK_REINDEX_SUPPORTED_INDEXES); expect(selectIndexes("all").map((schema) => schema.name)).toEqual([...BULK_REINDEX_SUPPORTED_INDEX_NAMES]); }); it("returns the matching schema when a known name is passed", () => { expect(selectIndexes("kb-articles")).toEqual([kbArticlesIndex]); expect(selectIndexes("crm-contacts")).toEqual([crmContactsIndex]); expect(selectIndexes("users")).toEqual([usersIndex]); }); it("rejects cataloged indexes that do not have a bulk-reindex pager", () => { const unsupportedCatalogNames = ALL_SEARCH_INDEXES.map((schema) => schema.name).filter( (name) => !(BULK_REINDEX_SUPPORTED_INDEX_NAMES as readonly string[]).includes(name) ); // `chat-messages`, `files`, `tasks`, `hr-directory` and `hr-locations` are cataloged in // ALL_SEARCH_INDEXES but intentionally have no bulk-reindex pager (not in // BULK_REINDEX_SUPPORTED_INDEX_NAMES), so they must reject in selectIndexes. // Chat (P15) and HR (P17 — AK-P17a) follow the same per-event-only pattern // as `tasks`; bulk-reindex pagers remain separate follow-up work. expect(unsupportedCatalogNames).toEqual(["chat-messages", "files", "tasks", "hr-directory", "hr-locations"]); for (const name of unsupportedCatalogNames) { expect(() => selectIndexes(name)).toThrow(/Unsupported search reindex target/); } }); it("throws on unknown index names", () => { expect(() => selectIndexes("does-not-exist")).toThrow(/Unsupported search reindex target/); }); }); describe("selectTenants", () => { it("returns the full list when input is undefined or 'all'", () => { expect(selectTenants(undefined, ["a", "b"])).toEqual(["a", "b"]); expect(selectTenants("all", ["a", "b"])).toEqual(["a", "b"]); }); it("returns the single tenant when a concrete id is passed", () => { expect(selectTenants("tenant-x", ["a", "b"])).toEqual(["tenant-x"]); }); }); describe("enqueueReindexEvents", () => { it("publishes one event per index x tenant", async () => { const publish = vi.fn().mockResolvedValue(undefined); const results = await enqueueReindexEvents({ indexes: [kbArticlesIndex], tenantIds: ["t1", "t2"], publish, }); expect(results).toHaveLength(2); expect(publish).toHaveBeenCalledTimes(2); expect(publish.mock.calls.map((c) => c[0].tenantId)).toEqual(["t1", "t2"]); expect(publish.mock.calls[0][0].type).toMatch(/^search\.index\..+\.bulkreindex$/); expect(publish.mock.calls[0][0].payload).toMatchObject({ action: "bulk-reindex", indexName: kbArticlesIndex.name }); }); it("returns empty list when no tenants provided", async () => { const publish = vi.fn().mockResolvedValue(undefined); const results = await enqueueReindexEvents({ indexes: BULK_REINDEX_SUPPORTED_INDEXES, tenantIds: [], publish }); expect(results).toHaveLength(0); expect(publish).not.toHaveBeenCalled(); }); });