/** * SearchService unit tests (Master-DoD §10 Search) against a faithful * in-memory OpenSearchAdapter fake (indices, aliases, bool-query subset). * The REAL adapter is exercised by * tests/integration/search-opensearch.integration.test.ts against a live * OpenSearch container — this file tests the 0pt semantics: tenant + * purpose/rights enforcement, generation switch, rebuild isolation. */ import { describe, it, expect } from "vitest"; import { SearchService, type DocumentSource } from "@/src/search/search-service"; import type { OpenSearchAdapter, OpenSearchSearchBody } from "@/src/search/opensearch-adapter"; import type { SearchDocument } from "@/src/search/search-types"; import { SiaxError } from "@/src/platform/errors"; // --------------------------------------------------------------------------- // In-memory OpenSearch fake — supports the exact query shape SearchService builds. // --------------------------------------------------------------------------- class FakeOpenSearch implements OpenSearchAdapter { indices = new Map>>(); aliases = new Map(); async createIndex(physicalIndex: string): Promise { if (this.indices.has(physicalIndex)) throw new SiaxError("CONFLICT", "opensearch", "exists"); this.indices.set(physicalIndex, new Map()); } async deleteIndex(physicalIndex: string): Promise { this.indices.delete(physicalIndex); } async indexExists(physicalIndex: string): Promise { return this.indices.has(physicalIndex); } async bulkIndex(physicalIndex: string, docs: { id: string; body: Record }[]) { const idx = this.indices.get(physicalIndex); if (!idx) return { errors: docs.map((d) => ({ doc_id: d.id, error: "index_not_found" })) }; for (const d of docs) idx.set(d.id, d.body); return { errors: [] }; } async search(target: string, body: OpenSearchSearchBody) { const physical = this.aliases.get(target) ?? target; const idx = this.indices.get(physical); if (!idx) return { total: 0, hits: [] }; const bool = (body.query as { bool: { must: unknown[]; filter: { term: Record }[] } }).bool; const rows = [...idx.entries()].filter(([, src]) => { for (const f of bool.filter) { const [field, value] = Object.entries(f.term)[0]; const actual = src[field]; if (Array.isArray(actual) ? !actual.includes(value) : actual !== value) return false; } const mm = (bool.must[0] as { multi_match?: { query: string; fields: string[] } }).multi_match; if (mm) { const q = mm.query.toLowerCase(); if (!mm.fields.some((f) => String(src[f] ?? "").toLowerCase().includes(q))) return false; } return true; }); const from = body.from ?? 0; const size = body.size ?? 10; return { total: rows.length, hits: rows.slice(from, from + size).map(([id, src]) => ({ _id: id, _score: 1, _index: physical, _source: src, })), }; } async switchAlias(alias: string, physicalIndex: string): Promise { this.aliases.set(alias, physicalIndex); } async aliasTargets(alias: string): Promise { const t = this.aliases.get(alias); return t ? [t] : []; } async listIndices(pattern: string): Promise { const re = new RegExp(`^${pattern.replace(/[-/\\^$+?.()|[\]{}]/g, "\\$&").replace(/\*/g, ".*")}$`); return [...this.indices.keys()].filter((i) => re.test(i)); } async refresh(): Promise {} async health(): Promise<{ status: string }> { return { status: "green" }; } } const DEF = { index_name: "companies", version: 1, fields: { name: "text", org_nr: "keyword", employees: "long" } as const, default_search_fields: ["name"], }; function docsSource(docs: SearchDocument[]): DocumentSource { return { name: "test:canonical", async *iterate() { yield* docs; }, }; } function doc(id: string, tenant: string, name: string, extra: Partial = {}): SearchDocument { return { doc_id: id, tenant_id: tenant, purpose_tags: ["company_enrichment"], rights_tags: ["internal"], payload: { name, org_nr: `55${id}`, employees: 10 }, ...extra, }; } async function build(docs: SearchDocument[]) { const fake = new FakeOpenSearch(); const search = new SearchService(fake); search.registerIndexDefinition({ ...DEF, fields: { ...DEF.fields } }); await search.rebuild("companies", docsSource(docs)); return { fake, search }; } describe("SearchService — definitions", () => { it("rejects non-text default_search_fields and reserved envelope fields", () => { const search = new SearchService(new FakeOpenSearch()); expect(() => search.registerIndexDefinition({ ...DEF, fields: { name: "keyword" }, default_search_fields: ["name"] }) ).toThrowError(/must be a text field/); expect(() => search.registerIndexDefinition({ ...DEF, fields: { ...DEF.fields, tenant_id: "keyword" }, }) ).toThrowError(/reserved/); }); }); describe("SearchService — tenant + purpose/rights enforcement (§10/§31)", () => { it("returns only the querying tenant's documents", async () => { const { search } = await build([doc("1", "t_a", "Volvo AB"), doc("2", "t_b", "Volvo Cars")]); const res = await search.query("companies", { tenant_id: "t_a", purpose: "company_enrichment", text: "volvo", }); expect(res.total).toBe(1); expect(res.hits[0].doc_id).toBe("1"); // Envelope fields are stripped from the returned payload. expect(res.hits[0].payload.tenant_id).toBeUndefined(); }); it("refuses queries without tenant_id or purpose", async () => { const { search } = await build([doc("1", "t_a", "X")]); await expect( search.query("companies", { tenant_id: "", purpose: "company_enrichment" }) ).rejects.toMatchObject({ code: "INVALID_REQUEST" }); await expect(search.query("companies", { tenant_id: "t_a", purpose: " " })).rejects.toMatchObject({ code: "INVALID_REQUEST", }); }); it("purpose filtering: documents not tagged for the query purpose are invisible", async () => { const { search } = await build([ doc("1", "t_a", "Tagged", { purpose_tags: ["company_enrichment"] }), doc("2", "t_a", "OtherPurpose", { purpose_tags: ["compliance"] }), ]); const res = await search.query("companies", { tenant_id: "t_a", purpose: "company_enrichment" }); expect(res.hits.map((h) => h.doc_id)).toEqual(["1"]); }); it("fails closed: rebuild rejects documents without purpose_tags (§10)", async () => { const fake = new FakeOpenSearch(); const search = new SearchService(fake); search.registerIndexDefinition({ ...DEF, fields: { ...DEF.fields } }); await expect( search.rebuild( "companies", docsSource([ doc("1", "t_a", "Ok"), doc("3", "t_a", "Untagged", { purpose_tags: [] }), // empty ⇒ INVALID_REQUEST, generation discarded ]), ), ).rejects.toThrowError(/missing purpose_tags/); }); it("rights filtering: default rights tag is 'internal'; deliverable docs need the tag", async () => { const { search } = await build([ doc("1", "t_a", "Internal", { rights_tags: ["internal"] }), doc("2", "t_a", "Deliverable", { rights_tags: ["customer_deliverable"] }), ]); const internal = await search.query("companies", { tenant_id: "t_a", purpose: "company_enrichment" }); expect(internal.hits.map((h) => h.doc_id)).toEqual(["1"]); const deliverable = await search.query("companies", { tenant_id: "t_a", purpose: "company_enrichment", rights_tag: "customer_deliverable", }); expect(deliverable.hits.map((h) => h.doc_id)).toEqual(["2"]); }); it("rejects filter fields not in the index definition", async () => { const { search } = await build([doc("1", "t_a", "X")]); await expect( search.query("companies", { tenant_id: "t_a", purpose: "company_enrichment", filters: { evil_field: "x" }, }) ).rejects.toMatchObject({ code: "INVALID_REQUEST" }); }); }); describe("SearchService — generations + rebuild (§42)", () => { it("rebuild creates g1, a second rebuild creates g2 and switches the alias atomically", async () => { const { search } = await build([doc("1", "t_a", "First")]); let state = await search.generationState("companies"); expect(state.active_generation).toBe(1); const r2 = await search.rebuild("companies", docsSource([doc("1", "t_a", "Second")])); expect(r2.new_generation).toBe(2); expect(r2.switched).toBe(true); state = await search.generationState("companies"); expect(state.active_generation).toBe(2); // Old generation kept for rollback by default. expect(state.physical_indices).toContain("opt-companies--g1"); const res = await search.query("companies", { tenant_id: "t_a", purpose: "company_enrichment" }); expect(res.hits[0].payload.name).toBe("Second"); }); it("switchGeneration rolls back to a previous generation; unknown generation is NOT_FOUND", async () => { const { search } = await build([doc("1", "t_a", "V1")]); await search.rebuild("companies", docsSource([doc("1", "t_a", "V2")])); await search.switchGeneration("companies", 1); const res = await search.query("companies", { tenant_id: "t_a", purpose: "company_enrichment" }); expect(res.hits[0].payload.name).toBe("V1"); await expect(search.switchGeneration("companies", 99)).rejects.toMatchObject({ code: "NOT_FOUND" }); }); it("a failed rebuild discards its generation and leaves the active alias untouched", async () => { const { fake, search } = await build([doc("1", "t_a", "Stable")]); // Poison the bulk path for the NEXT generation only. const origBulk = fake.bulkIndex.bind(fake); fake.bulkIndex = async (idx, docs) => idx.endsWith("--g2") ? { errors: [{ doc_id: "1", error: "mapper_parsing_exception" }] } : origBulk(idx, docs); await expect(search.rebuild("companies", docsSource([doc("1", "t_a", "Broken")]))).rejects.toMatchObject({ code: "RETRYABLE_FAILURE", }); const state = await search.generationState("companies"); expect(state.active_generation).toBe(1); expect(state.physical_indices).not.toContain("opt-companies--g2"); const res = await search.query("companies", { tenant_id: "t_a", purpose: "company_enrichment" }); expect(res.hits[0].payload.name).toBe("Stable"); }); it("indexDocuments validates the envelope and refuses unknown payload fields", async () => { const { search } = await build([doc("1", "t_a", "X")]); await expect( search.indexDocuments("companies", [ { ...doc("2", "t_a", "Y"), payload: { name: "Y", not_in_def: 1 } }, ]) ).rejects.toMatchObject({ code: "INVALID_REQUEST" }); await expect( search.indexDocuments("companies", [{ ...doc("3", "t_a", "Z"), rights_tags: [] }]) ).rejects.toMatchObject({ code: "INVALID_REQUEST" }); }); });