import assert from "node:assert/strict";
import { test } from "vitest";
import { assessUokDiagnosticsDispatchGate } from "../auto/phases.js";

test("assessUokDiagnosticsDispatchGate_when_clear_allows_dispatch", () => {
	const result = assessUokDiagnosticsDispatchGate({
		verdict: "clear",
		classification: "healthy",
		issues: [],
		reportPath: "/repo/.sf/runtime/uok-diagnostics.json",
	});

	assert.deepEqual(result, { proceed: true });
});

test("assessUokDiagnosticsDispatchGate_when_attention_warning_allows_dispatch", () => {
	const result = assessUokDiagnosticsDispatchGate({
		verdict: "attention",
		classification: "degraded",
		issues: [{ code: "uok-parity-degraded", severity: "warning" }],
		reportPath: "/repo/.sf/runtime/uok-diagnostics.json",
	});

	assert.deepEqual(result, { proceed: true });
});

test("assessUokDiagnosticsDispatchGate_when_projection_mismatch_survives_repair_blocks_dispatch", () => {
	const result = assessUokDiagnosticsDispatchGate({
		verdict: "attention",
		classification: "degraded",
		issues: [{ code: "db-projection-unit-mismatch", severity: "warning" }],
		reportPath: "/repo/.sf/runtime/uok-diagnostics.json",
	});

	assert.equal(result.proceed, false);
	assert.equal(result.issueCode, "db-projection-unit-mismatch");
	assert.match(result.reason, /db-projection-unit-mismatch/);
});

test("assessUokDiagnosticsDispatchGate_when_degraded_blocks_with_evidence", () => {
	const result = assessUokDiagnosticsDispatchGate({
		verdict: "degraded",
		classification: "needs-repair",
		issues: [{ code: "stale-runtime-projection", severity: "error" }],
		reportPath: "/repo/.sf/runtime/uok-diagnostics.json",
	});

	assert.equal(result.proceed, false);
	assert.equal(result.issueCode, "stale-runtime-projection");
	assert.equal(result.reportPath, "/repo/.sf/runtime/uok-diagnostics.json");
	assert.match(result.reason, /UOK diagnostics blocked dispatch/);
	assert.match(result.reason, /stale-runtime-projection/);
	assert.match(result.reason, /uok-diagnostics\.json/);
});
