import assert from "node:assert/strict";
import { describe, it } from "vitest";

// Pure unit tests for headless-uok-status formatting helpers.
// Extracted inline since importing from src/headless-uok-status.ts
// requires a full TS transform — these test the pure logic directly.

function passRate(entry) {
	if (entry.total === 0) return "—";
	return `${Math.round((entry.pass / entry.total) * 100)}%`;
}

function cbIcon(state) {
	if (state === "open") return "🔴";
	if (state === "half-open") return "🟡";
	return "🟢";
}

function formatTable(gates) {
	if (gates.length === 0) {
		return "No gate run data found in the last 24h.\n";
	}
	const header =
		"| Gate | Scope | Pass% | Pass | Fail | Retry | CB | Streak | Last Evaluated |\n" +
		"|------|-------|-------|------|------|-------|----|--------|----------------|\n";
	const rows = gates
		.map((g) => {
			const last = g.lastEvaluatedAt
				? new Date(g.lastEvaluatedAt)
						.toISOString()
						.replace("T", " ")
						.slice(0, 19)
				: "never";
			return `| ${g.id} | ${g.scope} | ${passRate(g)} | ${g.pass} | ${g.fail} | ${g.retry} | ${cbIcon(g.circuitBreaker)} ${g.circuitBreaker} | ${g.failureStreak} | ${last} |`;
		})
		.join("\n");
	return `${header}${rows}\n`;
}

function makeGate(overrides = {}) {
	return {
		id: "verification",
		scope: "task",
		total: 10,
		pass: 8,
		fail: 2,
		retry: 0,
		lastEvaluatedAt: "2026-05-11T12:00:00.000Z",
		circuitBreaker: "closed",
		failureStreak: 0,
		...overrides,
	};
}

describe("passRate", () => {
	it("returns_em_dash_when_total_is_zero", () => {
		assert.equal(passRate(makeGate({ total: 0 })), "—");
	});

	it("returns_100_when_all_pass", () => {
		assert.equal(passRate(makeGate({ total: 5, pass: 5 })), "100%");
	});

	it("returns_80_when_8_of_10_pass", () => {
		assert.equal(passRate(makeGate({ total: 10, pass: 8 })), "80%");
	});

	it("rounds_to_nearest_integer", () => {
		assert.equal(passRate(makeGate({ total: 3, pass: 1 })), "33%");
	});
});

describe("cbIcon", () => {
	it("returns_red_for_open", () => {
		assert.equal(cbIcon("open"), "🔴");
	});

	it("returns_yellow_for_half_open", () => {
		assert.equal(cbIcon("half-open"), "🟡");
	});

	it("returns_green_for_closed", () => {
		assert.equal(cbIcon("closed"), "🟢");
	});

	it("returns_green_for_unknown_state", () => {
		assert.equal(cbIcon("unknown"), "🟢");
	});
});

describe("formatTable", () => {
	it("returns_no_data_message_when_empty", () => {
		const result = formatTable([]);
		assert.ok(result.includes("No gate run data found"));
	});

	it("includes_header_row", () => {
		const result = formatTable([makeGate()]);
		assert.ok(result.includes("| Gate | Scope | Pass%"));
	});

	it("includes_separator_row", () => {
		const result = formatTable([makeGate()]);
		assert.ok(result.includes("|------|"));
	});

	it("includes_gate_id_in_row", () => {
		const result = formatTable([makeGate({ id: "cost-guard" })]);
		assert.ok(result.includes("cost-guard"));
	});

	it("includes_scope_in_row", () => {
		const result = formatTable([makeGate({ id: "Q5", scope: "task" })]);
		assert.ok(result.includes("task"));
	});

	it("includes_last_evaluated_header", () => {
		const result = formatTable([makeGate()]);
		assert.ok(result.includes("Last Evaluated"));
	});

	it("shows_never_when_lastEvaluatedAt_is_null", () => {
		const result = formatTable([makeGate({ lastEvaluatedAt: null })]);
		assert.ok(result.includes("never"));
	});

	it("shows_circuit_breaker_open_icon", () => {
		const result = formatTable([makeGate({ circuitBreaker: "open" })]);
		assert.ok(result.includes("🔴"));
		assert.ok(result.includes("open"));
	});

	it("shows_circuit_breaker_half_open_icon", () => {
		const result = formatTable([makeGate({ circuitBreaker: "half-open" })]);
		assert.ok(result.includes("🟡"));
	});

	it("shows_pass_rate_percentage", () => {
		const result = formatTable([makeGate({ total: 10, pass: 8 })]);
		assert.ok(result.includes("80%"));
	});

	it("formats_multiple_gates", () => {
		const gates = [
			makeGate({ id: "verification" }),
			makeGate({ id: "cost-guard", scope: "slice" }),
		];
		const result = formatTable(gates);
		assert.ok(result.includes("verification"));
		assert.ok(result.includes("cost-guard"));
	});

	it("formats_failure_streak", () => {
		const result = formatTable([makeGate({ failureStreak: 3 })]);
		assert.ok(result.includes("| 3 |"));
	});

	it("formats_timestamp_as_utc_date_string", () => {
		const result = formatTable([
			makeGate({ lastEvaluatedAt: "2026-05-11T12:00:00.000Z" }),
		]);
		assert.ok(result.includes("2026-05-11 12:00:00"));
	});
});
