Files
0pt/tests/p0/decision-er-shadow.test.ts
Claude Code dd4731aa3c
CI (SIAX Cloud) / integration (push) Has been skipped
masterplan-lock / check (pull_request) Successful in 8s
CI (SIAX Cloud) / security (push) Successful in 24s
CI (SIAX Cloud) / security (pull_request) Successful in 40s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 1m0s
Security Scan (SIAX Cloud) / security (push) Successful in 1m34s
CI (SIAX Cloud) / integration (pull_request) Successful in 1m30s
masterplan-lock / check (push) Successful in 10s
CI (SIAX Cloud) / quality (pull_request) Failing after 24s
CI (SIAX Cloud) / quality (push) Failing after 33s
feat(decision): Jev ER auto-match shadow (0PT_DECISION_ER_SHADOW)
Shadow-eval av D0 auto-match: D0-tröskeln förblir auktoritet,
kvaliteten mäts fire-and-forget mot siax-decision (:4600,
decisionId 0pt.er.auto_match). Gate 0PT_DECISION_ER_SHADOW=1
(OFF by default). Fail-closed: gate-off/fel/ABSTAINED påverkar
aldrig det deterministiska svaret; decision-planen loggar inbyggt
(AUD0).

- packages/decision: erAutoMatchShadow + erAutoMatchShadowEnabled
- src/er/comparePair: void erAutoMatchShadow(...).catch(() => null)
- tests/p0/decision-er-shadow.test.ts: gate-off/on, POST-body,
  DECIDED/ABSTAINED, fetch-fel, fire-and-forget-wire (16 nya tester)
2026-09-23 16:53:55 +02:00

120 lines
4.5 KiB
TypeScript

/**
* Jev shadow-eval av ER auto-match (0PT_DECISION_ER_SHADOW):
* gate-off → ingen fetch; gate-on → rätt POST-body + DECIDED parsad;
* ABSTAINED → decided:false; fetch-fel → null (fail-closed);
* wirat anrop är fire-and-forget — hängande fetch bryter aldrig
* det deterministiska D0-svaret.
*/
import { describe, it, expect, vi, afterEach } from "vitest";
import { erAutoMatchShadow, type ErAutoMatchShadowInput } from "@siax/0pt-decision";
import { EntityResolutionService, type ErRecord } from "@/src/er/entity-resolution-service";
const GATE = "0PT_DECISION_ER_SHADOW";
const INPUT: ErAutoMatchShadowInput = {
tenant_id: "t_a",
target: { record_id: "a1", fields: { name: "Volvo AB" } },
candidates: [{ record_id: "a2", fields: { name: "Volvo Aktiebolag" } }],
score: 0.95,
};
function fetchMock(payload: unknown, ok = true) {
return vi.fn().mockResolvedValue({ ok, json: async () => payload });
}
afterEach(() => {
vi.unstubAllEnvs();
vi.restoreAllMocks();
});
describe("erAutoMatchShadow — gate", () => {
it("gate OFF → ingen fetch, null (default: shadow är av)", async () => {
vi.stubEnv(GATE, "");
const f = fetchMock({});
vi.stubGlobal("fetch", f);
expect(await erAutoMatchShadow(INPUT)).toBeNull();
expect(f).not.toHaveBeenCalled();
});
});
describe("erAutoMatchShadow — gate ON", () => {
it("DECIDED → rätt POST-body + svar parsat", async () => {
vi.stubEnv(GATE, "1");
const f = fetchMock({
data: { status: "DECIDED", choice: "0", confidence: 0.91, model: "jev", latencyMs: 120 },
});
vi.stubGlobal("fetch", f);
const out = await erAutoMatchShadow(INPUT);
expect(out).toEqual({ decided: true, choice: "0", confidence: 0.91, model: "jev", latencyMs: 120 });
expect(f).toHaveBeenCalledTimes(1);
const [url, init] = f.mock.calls[0] as [string, RequestInit];
expect(url).toMatch(/\/decision\/match$/);
const body = JSON.parse(String(init.body));
expect(body.decision).toBe("0pt.er.auto_match");
expect(body.left).toEqual(INPUT.target);
expect(body.right).toEqual(INPUT.candidates);
expect(body.instructions).toBe("Which candidate is the same entity as the target?");
expect(init.method).toBe("POST");
});
it("ABSTAINED → decided:false (aldrig fabrikerat beslut)", async () => {
vi.stubEnv(GATE, "1");
vi.stubGlobal("fetch", fetchMock({ data: { status: "ABSTAINED", confidence: 0.3 } }));
const out = await erAutoMatchShadow(INPUT);
expect(out).toEqual({ decided: false, confidence: 0.3 });
});
it("HTTP-fel (res.ok false) → null", async () => {
vi.stubEnv(GATE, "1");
vi.stubGlobal("fetch", fetchMock({}, false));
expect(await erAutoMatchShadow(INPUT)).toBeNull();
});
it("fetch kastar (decision-nere/timeout) → null, kastar aldrig", async () => {
vi.stubEnv(GATE, "1");
vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new Error("ECONNREFUSED")));
expect(await erAutoMatchShadow(INPUT)).toBeNull();
});
});
describe("EntityResolutionService — shadow-wire (fire-and-forget)", () => {
const RECORDS: ErRecord[] = [
{ record_id: "a1", fields: { name: "Volvo Personvagnar AB", city: "goteborg", vin: "YV1" } },
{ record_id: "a2", fields: { name: "Volvo Personvagnar Aktiebolag", city: "goteborg", vin: "YV1" } },
];
function wireRequest() {
return {
tenant_id: "t_a",
project_id: "p_1",
records: RECORDS,
blocking_fields: ["city"],
compare_fields: [{ field: "name", weight: 2 }, { field: "city" }],
// Tvinga auto-match-bandet (score hamnar annars i review-bandet med defaults).
auto_match_threshold: 0.5,
review_threshold: 0.3,
};
}
it("gate ON + hängande fetch (aldrig resolving) → auto-match-status oförändrad", async () => {
vi.stubEnv(GATE, "1");
vi.stubGlobal("fetch", vi.fn().mockReturnValue(new Promise(() => {})));
const er = new EntityResolutionService();
const res = await er.generateCandidates(wireRequest());
expect(res.pairs).toHaveLength(1);
expect(res.pairs[0].status).toBe("match");
expect(res.pairs[0].decided_by).toBe("score");
expect(globalThis.fetch).toHaveBeenCalledTimes(1);
});
it("gate OFF → ingen fetch vid auto-match", async () => {
vi.stubEnv(GATE, "");
const f = fetchMock({});
vi.stubGlobal("fetch", f);
const er = new EntityResolutionService();
const res = await er.generateCandidates(wireRequest());
expect(res.pairs[0].status).toBe("match");
expect(f).not.toHaveBeenCalled();
});
});