import assert from "node:assert/strict";
import { test } from "vitest";
import {
	assessAssertionCoverage,
	fulfilledAssertionIdsFromHandoff,
	requiredAssertionIdsFromContract,
} from "../uok/assertion-coverage.js";

test("requiredAssertionIdsFromContract_when_droid_and_sf_fields_present_returns_unique_required_ids", () => {
	assert.deepEqual(
		requiredAssertionIdsFromContract({
			assertionIds: ["VAL-001", "VAL-002"],
			fulfills: ["VAL-002", "VAL-003"],
			validationContract: {
				assertions: ["VAL-004"],
				fulfills: ["VAL-003"],
			},
			unitContract: {
				assertionIds: ["VAL-005"],
			},
		}),
		["VAL-001", "VAL-002", "VAL-003", "VAL-004", "VAL-005"],
	);
});

test("fulfilledAssertionIdsFromHandoff_when_closeout_uses_alias_fields_returns_unique_fulfilled_ids", () => {
	assert.deepEqual(
		fulfilledAssertionIdsFromHandoff({
			fulfilledAssertions: ["VAL-001", "VAL-002"],
			assertionsFulfilled: ["VAL-002", "VAL-003"],
			fulfills: ["VAL-004"],
		}),
		["VAL-001", "VAL-002", "VAL-003", "VAL-004"],
	);
});

test("assessAssertionCoverage_when_handoff_covers_all_required_assertions_returns_ok", () => {
	const result = assessAssertionCoverage(
		{
			fulfills: ["VAL-LOG-001", "VAL-LOG-002"],
		},
		{
			fulfilledAssertions: ["VAL-LOG-002", "VAL-LOG-001"],
		},
	);

	assert.deepEqual(result, {
		ok: true,
		required: ["VAL-LOG-001", "VAL-LOG-002"],
		fulfilled: ["VAL-LOG-002", "VAL-LOG-001"],
		missing: [],
		extra: [],
	});
});

test("assessAssertionCoverage_when_handoff_omits_required_assertion_reports_missing_and_extra", () => {
	const result = assessAssertionCoverage(
		{
			validationContract: {
				assertionIds: ["VAL-A", "VAL-B"],
			},
		},
		{
			fulfilledAssertions: ["VAL-A", "VAL-C"],
		},
	);

	assert.equal(result.ok, false);
	assert.deepEqual(result.required, ["VAL-A", "VAL-B"]);
	assert.deepEqual(result.fulfilled, ["VAL-A", "VAL-C"]);
	assert.deepEqual(result.missing, ["VAL-B"]);
	assert.deepEqual(result.extra, ["VAL-C"]);
});
