/** * Canonical structured observations (sql/022): idempotent write, conflict * preservation, tenant isolation and relation identity resolution. */ import { describe, it, expect } from "vitest"; import { CanonicalFactService } from "@/src/services/canonical-fact-service"; import { SiaxError } from "@/src/platform/errors"; const T = "t_canonical_test"; const ENTITY = { entity_type: "web_page", identity_namespace: "url", identity_value: "https://example.org" }; function fact(overrides: Record = {}) { return { entity: ENTITY, predicate: "web.title", value: "Example Domain", source_ref: "source:example", extractor_version: "0pt.motor.extraction/v1", observed_at: "2026-11-04T12:00:00Z", ...overrides, }; } describe("CanonicalFactService — write", () => { it("creates the entity and the fact, and query returns it", async () => { const svc = new CanonicalFactService(); const result = await svc.write({ tenant_id: T, facts: [fact()] }); expect(result.entities).toHaveLength(1); expect(result.entities[0].identity_value).toBe("https://example.org"); expect(result.facts).toHaveLength(1); expect(result.duplicates).toBe(0); const rows = await svc.query({ tenant_id: T, identity_value: "https://example.org" }); expect(rows).toHaveLength(1); expect(rows[0].predicate).toBe("web.title"); expect(rows[0].value_type).toBe("string"); }); it("is idempotent: an identical replay is reported as a duplicate, not stored twice", async () => { const svc = new CanonicalFactService(); await svc.write({ tenant_id: T, facts: [fact()] }); const replay = await svc.write({ tenant_id: T, facts: [fact()] }); expect(replay.duplicates).toBe(1); const rows = await svc.query({ tenant_id: T, identity_value: "https://example.org" }); expect(rows).toHaveLength(1); }); it("preserves conflicting sources as separate observations", async () => { const svc = new CanonicalFactService(); await svc.write({ tenant_id: T, facts: [fact({ source_ref: "source:a", value: "A" })] }); await svc.write({ tenant_id: T, facts: [fact({ source_ref: "source:b", value: "B" })] }); const rows = await svc.query({ tenant_id: T, identity_value: "https://example.org" }); expect(rows).toHaveLength(2); expect(rows.map((r) => r.value).sort()).toEqual(["A", "B"]); }); it("isolates tenants", async () => { const svc = new CanonicalFactService(); await svc.write({ tenant_id: "t_a", facts: [fact()] }); expect(await svc.query({ tenant_id: "t_b" })).toHaveLength(0); }); it("resolves relation subject/object identities to entity ids", async () => { const svc = new CanonicalFactService(); const result = await svc.write({ tenant_id: T, relations: [{ subject: ENTITY, predicate: "LINKS_TO", object: { entity_type: "web_page", identity_namespace: "url", identity_value: "https://example.org/next" }, source_ref: "source:example", extractor_version: "0pt.motor.extraction/v1", observed_at: "2026-11-04T12:00:00Z", }], }); expect(result.entities).toHaveLength(2); expect(result.relations[0].subject_id).toBeTruthy(); expect(result.relations[0].object_id).toBeTruthy(); expect(result.relations[0].subject_id).not.toBe(result.relations[0].object_id); }); it("rejects a fact without observed_at or with an invalid confidence", async () => { const svc = new CanonicalFactService(); await expect(svc.write({ tenant_id: T, facts: [fact({ observed_at: undefined })] })).rejects.toBeInstanceOf(SiaxError); await expect(svc.write({ tenant_id: T, facts: [fact({ confidence: 2 })] })).rejects.toBeInstanceOf(SiaxError); }); });