import { describe, expect, it } from "vitest";
import { blockers, decide, deriveStatus, type SubjectFacts } from "./verification.rules.js";

const many = (x: number, n: number): SubjectFacts[] => Array.from({ length: n }, (_, i) => ({ id: String(i), role: "primary", status: i < x ? "V-COMP" : "V-NCOMP", replaces: null, supervisor: "approved" }));
const plan = { estimator: "wilson_one_sided", ciZ: 1.645, rule: "ci_vs_target" };
describe("decision", () => {
  it("follows V1's Wilson interval and pathway", () => {
    expect(decide(many(95, 100), 80, plan)).toMatchObject({ rate: 95, pathway: "clear_pass", low: 90.08, high: 97.55 });
    expect(decide(many(50, 100), 80, plan).pathway).toBe("clear_fail");
    expect(decide(many(80, 100), 80, plan).pathway).toBe("inconclusive");
  });
  it("the exact interval matches the published value for 8 of 10 (95 %)", () => { const d = decide(many(8, 10), 50, { ...plan, estimator: "clopper_pearson", ciZ: 1.959964 }); expect(d.low).toBeCloseTo(44.39, 1); expect(d.high).toBeCloseTo(97.48, 1); });
  it("reserves and unreachable people do not count", () => { const s = many(1, 2); s[1]!.role = "reserve"; expect(decide(s, 80, plan)).toMatchObject({ definite: 1, success: 1 }); });
});
it("blocks finalising until everyone is settled and approved", () => {
  const s = many(2, 3); s[0]!.status = "V-UNR"; s[1]!.supervisor = "pending";
  expect(blockers(s)).toEqual(["0", "1"]);
  expect(blockers([...s, { id: "9", role: "replacement", status: "V-COMP", replaces: "0", supervisor: "approved" }])).toEqual(["1"]);
});
it("derives the code from the answers", () => expect([{ q1: "no" }, { q1: "yes", q10: "yes" }, { q1: "yes", q10: "no" }, { q1: "yes", q10: "not_sure" }, { q1: "yes", q2: "not_correct", q3: "not_correct", q10: "yes" }, {}].map((a) => deriveStatus(a as Record<string, string>))).toEqual(["V-REF", "V-COMP", "V-NCOMP", "V-INC", "V-INC", undefined]));
