import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { AlertsTab } from "../AlertsTab";
import type { ServiceInstance } from "../../../types";

const instance: ServiceInstance = {
	id: "am-1",
	service_type: "alertmanager",
	name: "Main Alertmanager",
	config: { base_url: "https://am.example.com", timeout_seconds: 5 },
	secrets_set: {},
	enabled: true,
	created_at: 1_700_000_000,
	updated_at: 1_700_000_000,
};

vi.mock("../../../hooks/useObservability", () => ({
	useAlertmanagerAlerts: () => ({
		data: {
			total: 2,
			by_severity: { critical: 1, warning: 1 },
			alerts: [
				{
					name: "DiskFull",
					severity: "critical",
					category: "disk",
					job_name: "node",
					summary: "Disk is almost full",
					description: "Disk usage above 90%",
					active_since: "2026-06-26T10:00:00Z",
					state: "firing",
					labels: { instance: "node1" },
				},
				{
					name: "HighCpu",
					severity: "warning",
					category: "cpu",
					job_name: "node",
					summary: "High CPU usage",
					description: "",
					active_since: "2026-06-26T09:00:00Z",
					state: "firing",
					labels: {},
				},
			],
		},
		isLoading: false,
		error: null,
	}),
	useAlertmanagerStatus: () => ({
		data: { up: true, version: "0.27.0", uptime: "", name: "", peers: [] },
		isLoading: false,
		error: null,
	}),
}));

describe("AlertsTab", () => {
	it("renders the alert count and alert names", () => {
		render(<AlertsTab instance={instance} />);
		expect(screen.getByText(/Active Alerts \(2\)/)).toBeInTheDocument();
		expect(screen.getByText("DiskFull")).toBeInTheDocument();
		expect(screen.getByText("HighCpu")).toBeInTheDocument();
	});

	it("renders severity badges", () => {
		render(<AlertsTab instance={instance} />);
		expect(screen.getByText("critical")).toBeInTheDocument();
		expect(screen.getByText("warning")).toBeInTheDocument();
	});
});
