import { DIAGNOSTIC_CODES } from "@/lib/types";
import { DIAGNOSTIC_COMPONENTS, EVENT_COPY, componentLabel, copyFor } from "./eventCopy";

test("the enum holds the fifteen codes the store defines", () => {
	expect(DIAGNOSTIC_CODES).toHaveLength(15);
	expect(new Set(DIAGNOSTIC_CODES).size).toBe(15);
});

test("every code has copy, and no copy belongs to a code that does not exist", () => {
	for (const code of DIAGNOSTIC_CODES) {
		const copy = EVENT_COPY[code];
		expect(copy, code).toBeDefined();
		expect(copy.title.length, code).toBeGreaterThan(0);
		expect(copy.impact.length, code).toBeGreaterThan(0);
		expect(copy.remediation.length, code).toBeGreaterThan(0);
	}
	expect(Object.keys(EVENT_COPY).sort()).toEqual([...DIAGNOSTIC_CODES].sort());
});

test("titles are distinct, so two open episodes never read as the same event", () => {
	const titles = DIAGNOSTIC_CODES.map((code) => EVENT_COPY[code].title);
	expect(new Set(titles).size).toBe(titles.length);
});

test("a code this build has never heard of falls back to the code itself", () => {
	// The server is the authority on the enum; a newer one can send a sixteenth.
	const copy = copyFor("nonsense.code" as (typeof DIAGNOSTIC_CODES)[number]);
	expect(copy.title).toBe("nonsense.code");
	expect(copy.remediation.length).toBeGreaterThan(0);
});

test("the component options are the code prefixes, deduplicated and in enum order", () => {
	expect(DIAGNOSTIC_COMPONENTS).toEqual([
		"disk",
		"blocklist",
		"certificate",
		"query_log",
		"upstream_history",
		"upstream",
		"client_names",
		"clients",
		"listener",
		"configuration",
	]);
});

test("component labels read as prose without inventing a name", () => {
	expect(componentLabel("query_log")).toBe("Query log");
	expect(componentLabel("disk")).toBe("Disk");
});
