Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/scripts/demo-pro-seed/search-verification.test.ts
T

119 lines
5.1 KiB
TypeScript

import { ALL_SEARCH_INDEXES, createSearchClient, crmContactsIndex, type SearchClient } from "../../packages/search/src/index";
import { createInMemoryMeili } from "../../packages/search/src/testing";
import { describe, expect, it } from "vitest";
import { generateDemoProSeed } from "./generator";
import { generateOperationalGraph } from "./operations";
import { expectedDemoProSearchCounts, expectedDemoProSearchIds, type DemoProSearchIds, waitForDemoProSearch } from "./search-verification";
describe("demo-pro final search verification", () => {
it("requires every registered index to be empty after cleanup", () => {
expect(Object.values(expectedDemoProSearchCounts("cleanup")).every((count) => count === 0)).toBe(true);
});
it("derives the exact expected document IDs from the complete seed graphs", () => {
const graph = generateDemoProSeed();
const operational = generateOperationalGraph(graph);
const ids = expectedDemoProSearchIds(graph, operational);
const counts = expectedDemoProSearchCounts("apply");
for (const schema of ALL_SEARCH_INDEXES) {
expect(ids[schema.name]).toHaveLength(counts[schema.name]);
expect(new Set(ids[schema.name]).size).toBe(counts[schema.name]);
}
});
it("reads tenant-isolated counts from Meilisearch", async () => {
const meili = createInMemoryMeili();
const client = createSearchClient({ url: "http://memory", meiliFactory: meili.factory });
await client.index(crmContactsIndex.name).upsert([{ id: "tenant-a:contact-1", tenantId: "tenant-a", entityType: "contact" }]);
await expect(waitForDemoProSearch({ tenantId: "tenant-b", mode: "cleanup", timeoutMs: 0, client })).resolves.toMatchObject({
[crmContactsIndex.name]: 0,
});
});
it("uses exact tenant facets when estimated hits are capped", async () => {
const meili = createInMemoryMeili({ maxTotalHits: 1_000 });
const client = createSearchClient({ url: "http://memory", meiliFactory: meili.factory });
const expected = expectedDemoProSearchCounts("apply");
const expectedIds: DemoProSearchIds = {};
for (const schema of ALL_SEARCH_INDEXES) {
const count = expected[schema.name] ?? 0;
expectedIds[schema.name] = Array.from({ length: count }, (_, index) => `${schema.name}-${index}`);
await client.index(schema.name).upsert(
expectedIds[schema.name]!.map((id) => ({
id,
tenantId: "tenant-a",
}))
);
}
await expect(waitForDemoProSearch({ tenantId: "tenant-a", mode: "apply", timeoutMs: 0, client, expectedIdsByIndex: expectedIds })).resolves.toMatchObject({
tasks: 1_684,
});
});
it("fails when an index below the pagination cap has the right count but the wrong IDs", async () => {
const meili = createInMemoryMeili({ maxTotalHits: 1_000 });
const client = createSearchClient({ url: "http://memory", meiliFactory: meili.factory });
const expected = expectedDemoProSearchCounts("apply");
const expectedIds: DemoProSearchIds = {};
for (const schema of ALL_SEARCH_INDEXES) {
const count = expected[schema.name] ?? 0;
expectedIds[schema.name] = Array.from({ length: count }, (_, index) => `${schema.name}-${index}`);
await client.index(schema.name).upsert(
expectedIds[schema.name]!.map((id, index) => ({
id: schema.name === crmContactsIndex.name && index === 0 ? "unexpected-contact" : id,
tenantId: "tenant-a",
}))
);
}
await expect(waitForDemoProSearch({ tenantId: "tenant-a", mode: "apply", timeoutMs: 0, client, expectedIdsByIndex: expectedIds })).rejects.toThrow(
/document IDs differ/u
);
});
it("fails closed when Meilisearch omits tenant facets", async () => {
const client = createFacetClient(undefined);
await expect(waitForDemoProSearch({ tenantId: "tenant-a", mode: "cleanup", timeoutMs: 0, client })).rejects.toThrow(
/did not return an exact facet distribution/u
);
});
it("fails closed when a facet is empty despite matching documents", async () => {
const client = createFacetClient({}, 1);
await expect(waitForDemoProSearch({ tenantId: "tenant-a", mode: "cleanup", timeoutMs: 0, client })).rejects.toThrow(/omitted the tenant facet count/u);
});
it.each([-1, 1.5, Number.NaN])("rejects invalid exact tenant facet count %s", async (count) => {
const client = createFacetClient({ "tenant-a": count });
await expect(waitForDemoProSearch({ tenantId: "tenant-a", mode: "cleanup", timeoutMs: 0, client })).rejects.toThrow(/invalid tenant facet count/u);
});
});
function createFacetClient(tenantFacet: Record<string, number> | undefined, estimatedTotalHits = 0): SearchClient {
return {
index() {
return {
async ensure() {},
async search() {
return {
hits: [],
estimatedTotalHits,
...(tenantFacet === undefined ? {} : { facetDistribution: { tenantId: tenantFacet } }),
processingTimeMs: 0,
};
},
async upsert() {},
async delete() {},
async deleteByFilter() {},
};
},
async health() {
return { status: "available" };
},
};
}