168 lines
7.0 KiB
TypeScript
168 lines
7.0 KiB
TypeScript
import {
|
|
ALL_SEARCH_INDEXES,
|
|
createSearchClient,
|
|
crmContactsIndex,
|
|
filesIndex,
|
|
projectsIndex,
|
|
searchInIndex,
|
|
tasksIndex,
|
|
usersIndex,
|
|
type SearchClient,
|
|
} from "../../packages/search/src/index";
|
|
import type { DemoProOperationalGraph } from "./operations";
|
|
import type { DemoProSeedGraph } from "./types";
|
|
|
|
export type DemoProSearchCounts = Record<string, number>;
|
|
export type DemoProSearchIds = Record<string, readonly (string | number)[]>;
|
|
|
|
// Meilisearch caps search pagination at 1,000 documents by default. Counts
|
|
// remain exact through tenant facets; exact ID-set verification is performed
|
|
// for every index whose complete tenant slice fits inside that API boundary.
|
|
const MAX_EXACT_SEARCH_IDS = 1_000;
|
|
|
|
export function expectedDemoProSearchCounts(mode: "apply" | "cleanup"): DemoProSearchCounts {
|
|
const counts = Object.fromEntries(ALL_SEARCH_INDEXES.map(({ name }) => [name, 0]));
|
|
if (mode === "apply") {
|
|
counts[crmContactsIndex.name] = 976;
|
|
counts[projectsIndex.name] = 537;
|
|
counts[tasksIndex.name] = 1_684;
|
|
counts[filesIndex.name] = 917;
|
|
counts[usersIndex.name] = 50;
|
|
}
|
|
return counts;
|
|
}
|
|
|
|
export function expectedDemoProSearchIds(graph: DemoProSeedGraph, operational: DemoProOperationalGraph): DemoProSearchIds {
|
|
const ids: DemoProSearchIds = Object.fromEntries(ALL_SEARCH_INDEXES.map(({ name }) => [name, []]));
|
|
ids[crmContactsIndex.name] = [...graph.organizations, ...graph.persons, ...graph.leads].map(({ id }) => id);
|
|
ids[projectsIndex.name] = graph.projects.map(({ id }) => id);
|
|
ids[tasksIndex.name] = operational.tasks.map(({ id }) => id);
|
|
ids[filesIndex.name] = operational.files.map(({ id }) => id);
|
|
ids[usersIndex.name] = graph.employees.map(({ id }) => id);
|
|
return ids;
|
|
}
|
|
|
|
function searchId(value: string | number): string {
|
|
return `${typeof value === "number" ? "n" : "s"}:${String(value)}`;
|
|
}
|
|
|
|
function assertExpectedIds(ids: DemoProSearchIds, expectedCounts: DemoProSearchCounts): void {
|
|
for (const schema of ALL_SEARCH_INDEXES) {
|
|
const expectedIds = ids[schema.name];
|
|
if (!expectedIds) throw new Error(`demo-pro seed: expected Meilisearch IDs are missing for ${schema.name}`);
|
|
if (expectedIds.length !== expectedCounts[schema.name]) {
|
|
throw new Error(`demo-pro seed: expected Meilisearch ID count does not match ${schema.name}`);
|
|
}
|
|
if (new Set(expectedIds.map(searchId)).size !== expectedIds.length) {
|
|
throw new Error(`demo-pro seed: expected Meilisearch IDs are not unique for ${schema.name}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function readSearchState(
|
|
client: SearchClient,
|
|
tenantId: string,
|
|
expectedIdsByIndex: DemoProSearchIds
|
|
): Promise<{ counts: DemoProSearchCounts; idMismatches: string[] }> {
|
|
const entries = await Promise.all(
|
|
ALL_SEARCH_INDEXES.map(async (schema) => {
|
|
const result = await searchInIndex({
|
|
client,
|
|
schema,
|
|
tenantId,
|
|
query: "",
|
|
options: { limit: 0, facets: ["tenantId"], attributesToRetrieve: [schema.primaryKey] },
|
|
});
|
|
if (!result.facetDistribution) {
|
|
throw new Error(`demo-pro seed: Meilisearch did not return an exact facet distribution for ${schema.name}`);
|
|
}
|
|
// Meilisearch returns an empty distribution (without the requested
|
|
// facet key) when the tenant filter has no matches. The response object
|
|
// itself is still mandatory so an unsupported/ignored facets request
|
|
// cannot be mistaken for an exact zero.
|
|
const count = result.facetDistribution.tenantId?.[tenantId] ?? 0;
|
|
if (count === 0 && result.estimatedTotalHits > 0) {
|
|
throw new Error(`demo-pro seed: Meilisearch omitted the tenant facet count for ${schema.name} despite matching documents`);
|
|
}
|
|
if (!Number.isSafeInteger(count) || count < 0) {
|
|
throw new Error(`demo-pro seed: Meilisearch returned an invalid tenant facet count for ${schema.name}`);
|
|
}
|
|
const expectedIds = expectedIdsByIndex[schema.name]!;
|
|
if (count !== expectedIds.length || expectedIds.length === 0 || expectedIds.length > MAX_EXACT_SEARCH_IDS) {
|
|
return { indexName: schema.name, count, idMismatch: null };
|
|
}
|
|
const documents = await searchInIndex({
|
|
client,
|
|
schema,
|
|
tenantId,
|
|
query: "",
|
|
options: {
|
|
limit: expectedIds.length,
|
|
offset: 0,
|
|
attributesToRetrieve: [schema.primaryKey],
|
|
},
|
|
});
|
|
const actualIds = documents.hits
|
|
.map(({ document }) => document[schema.primaryKey])
|
|
.map((value) => {
|
|
if (typeof value !== "string" && typeof value !== "number") {
|
|
throw new Error(`demo-pro seed: Meilisearch returned an invalid document ID for ${schema.name}`);
|
|
}
|
|
return searchId(value);
|
|
});
|
|
const actualIdSet = new Set(actualIds);
|
|
const expectedIdSet = new Set(expectedIds.map(searchId));
|
|
const exact =
|
|
actualIds.length === expectedIds.length &&
|
|
actualIdSet.size === actualIds.length &&
|
|
[...expectedIdSet].every((documentId) => actualIdSet.has(documentId));
|
|
return {
|
|
indexName: schema.name,
|
|
count,
|
|
idMismatch: exact ? null : `${schema.name}: tenant document IDs differ from the expected set`,
|
|
};
|
|
})
|
|
);
|
|
return {
|
|
counts: Object.fromEntries(entries.map(({ indexName, count }) => [indexName, count])),
|
|
idMismatches: entries.flatMap(({ idMismatch }) => (idMismatch ? [idMismatch] : [])),
|
|
};
|
|
}
|
|
|
|
export async function waitForDemoProSearch(input: {
|
|
tenantId: string;
|
|
mode: "apply" | "cleanup";
|
|
timeoutMs: number;
|
|
client?: SearchClient;
|
|
expectedIdsByIndex?: DemoProSearchIds;
|
|
}): Promise<DemoProSearchCounts> {
|
|
if (!input.client && !process.env.MEILI_URL) throw new Error("demo-pro seed: MEILI_URL is required for final search verification");
|
|
const client =
|
|
input.client ??
|
|
createSearchClient({
|
|
url: process.env.MEILI_URL!,
|
|
apiKey: process.env.MEILI_API_KEY,
|
|
timeout: 10_000,
|
|
});
|
|
const expected = expectedDemoProSearchCounts(input.mode);
|
|
const expectedIdsByIndex = input.mode === "cleanup" ? Object.fromEntries(ALL_SEARCH_INDEXES.map(({ name }) => [name, []])) : input.expectedIdsByIndex;
|
|
if (!expectedIdsByIndex) throw new Error("demo-pro seed: exact Meilisearch IDs are required for apply verification");
|
|
assertExpectedIds(expectedIdsByIndex, expected);
|
|
const deadline = Date.now() + input.timeoutMs;
|
|
let counts: DemoProSearchCounts = {};
|
|
let idMismatches: string[] = [];
|
|
do {
|
|
({ counts, idMismatches } = await readSearchState(client, input.tenantId, expectedIdsByIndex));
|
|
if (idMismatches.length === 0 && Object.entries(expected).every(([indexName, expectedCount]) => counts[indexName] === expectedCount)) {
|
|
return counts;
|
|
}
|
|
if (input.timeoutMs <= 0) break;
|
|
await new Promise((resolve) => setTimeout(resolve, 1_000));
|
|
} while (Date.now() < deadline);
|
|
throw new Error(
|
|
`demo-pro seed: Meilisearch did not reach the expected tenant state (${JSON.stringify(counts)}${
|
|
idMismatches.length > 0 ? `; ${idMismatches.join("; ")}` : ""
|
|
})`
|
|
);
|
|
}
|