import { describe, expect, it } from "vitest"; import { createSearchClient } from "./client.js"; import { createInMemoryMeili } from "./testing.js"; describe("createSearchClient", () => { it("rejects empty url", () => { expect(() => createSearchClient({ url: "", meiliFactory: createInMemoryMeili().factory })).toThrow(/url/); }); it("exposes a tenant-agnostic index handle that supports ensure/upsert/search/delete", async () => { const { factory, state } = createInMemoryMeili(); const client = createSearchClient({ url: "http://memory", meiliFactory: factory }); const handle = client.index<{ id: string; tenantId: string; title: string }>("kb-articles"); await handle.ensure({ primaryKey: "id", settings: { searchableAttributes: ["title"], filterableAttributes: ["tenantId"] }, }); await handle.upsert([ { id: "a", tenantId: "t1", title: "Onboarding" }, { id: "b", tenantId: "t2", title: "Onboarding" }, ]); const results = await handle.search("Onboarding", { filter: 'tenantId = "t1"', limit: 10 }); expect(results.hits.map((hit) => hit.document.id)).toEqual(["a"]); expect(results.estimatedTotalHits).toBe(1); await handle.delete(["a"]); const after = await handle.search("Onboarding", { filter: 'tenantId = "t1"', limit: 10 }); expect(after.hits).toHaveLength(0); expect(state.indexes.get("kb-articles")?.settings).toMatchObject({ searchableAttributes: ["title"] }); }); it("returns health = available from a healthy backend", async () => { const { factory } = createInMemoryMeili(); const client = createSearchClient({ url: "http://memory", meiliFactory: factory }); await expect(client.health()).resolves.toEqual({ status: "available" }); }); it("reports unavailable when backend reports another status", async () => { const { factory } = createInMemoryMeili({ healthStatus: "unavailable" }); const client = createSearchClient({ url: "http://memory", meiliFactory: factory }); await expect(client.health()).resolves.toEqual({ status: "unavailable" }); }); });