import { describe, it, expect } from "vitest";
import { mapEventToCategory, mapEventToSeverity } from "./toast-rules";
import type { InstanceEventPayload } from "../types/events";

function makeEvent(
	event: string,
	overrides?: Partial<InstanceEventPayload>,
): InstanceEventPayload {
	return {
		event,
		instance_id: "i-1",
		status: undefined,
		message: undefined,
		metadata: {},
		timestamp: "2026-05-29T10:00:00Z",
		correlation_id: "c1",
		...overrides,
	};
}

describe("mapEventToCategory", () => {
	it('returns "instance" for instance.* events', () => {
		expect(mapEventToCategory(makeEvent("instance.started"))).toBe("instance");
		expect(mapEventToCategory(makeEvent("instance.error"))).toBe("instance");
	});

	it('returns "health" for health.* events', () => {
		expect(mapEventToCategory(makeEvent("health.error"))).toBe("health");
	});

	it('returns "system" for unknown events', () => {
		expect(mapEventToCategory(makeEvent("system.announcement"))).toBe("system");
	});
});

describe("mapEventToSeverity", () => {
	it("returns error for instance.error and health.error", () => {
		expect(mapEventToSeverity(makeEvent("instance.error"))).toBe("error");
		expect(mapEventToSeverity(makeEvent("health.error"))).toBe("error");
	});

	it("returns warning for unhealthy health changes", () => {
		expect(
			mapEventToSeverity(
				makeEvent("instance.health_changed", { status: "unhealthy" }),
			),
		).toBe("warning");
	});

	it("returns success for recovery to running", () => {
		expect(
			mapEventToSeverity(
				makeEvent("instance.health_changed", { status: "running" }),
			),
		).toBe("success");
	});

	it("returns info for lifecycle events", () => {
		expect(mapEventToSeverity(makeEvent("instance.created"))).toBe("info");
		expect(mapEventToSeverity(makeEvent("instance.started"))).toBe("info");
		expect(mapEventToSeverity(makeEvent("instance.stopped"))).toBe("info");
		expect(mapEventToSeverity(makeEvent("instance.restarted"))).toBe("info");
		expect(mapEventToSeverity(makeEvent("instance.deleted"))).toBe("info");
	});

	it("returns info for unmapped events", () => {
		expect(mapEventToSeverity(makeEvent("unknown.event"))).toBe("info");
	});
});
