Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/search/src/client.test.ts
T

55 lines
2.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { createSearchClient } from "./client.js";
import { toPhysicalSearchIndexUid } from "./index-name.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);
const faceted = await handle.search("", { filter: 'tenantId = "t1"', limit: 0, facets: ["tenantId"] });
expect(faceted).toMatchObject({
hits: [],
facetDistribution: { tenantId: { t1: 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(toPhysicalSearchIndexUid("kb-articles"))?.settings).toMatchObject({ searchableAttributes: ["title"] });
expect(state.indexes.has("kb-articles")).toBe(false);
});
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" });
});
});