import { expect, test } from "bun:test";
import { readKubernetesSource } from "./kubernetes";

test("Kubernetes activity source dedupes stable ids and reports absent coverage honestly", () => {
  const event = { id: "kubernetes:u:started:1", ts: "2026-08-08T10:00:00.000Z", category: "service", source: "kubernetes", actor: "system", severity: "info", title: "Pod started" };
  const result = readKubernetesSource({ path: "/events", existsSyncImpl: () => true, readFileImpl: () => `${JSON.stringify(event)}\n${JSON.stringify(event)}\nbad` });
  expect(result.events).toHaveLength(1);
  expect(result.coverage).toMatchObject({ status: "ok", totalRecords: 1, records: 1, skipped: 1 });
  expect(readKubernetesSource({ path: "/missing", existsSyncImpl: () => false }).coverage).toMatchObject({ status: "absent", error: expect.stringContaining("not been recorded") });
});

test("uses authoritative phase fields instead of resource names", () => {
  const succeeded = { id: "kubernetes:job:1", ts: "2026-08-08T10:00:00.000Z", category: "service", source: "kubernetes", actor: "system", severity: "info", title: "Job failed-migration", detail: { phase: "Succeeded" } };
  const failed = { ...succeeded, id: "kubernetes:job:2", title: "Job harmless-name", detail: { phase: "Failed" } };
  const result = readKubernetesSource({ path: "/events", existsSyncImpl: () => true, readFileImpl: () => `${JSON.stringify(succeeded)}\n${JSON.stringify(failed)}` });
  expect(result.events[0]).toMatchObject({ title: "Job failed-migration", lifecycle: "completed", result: "success" });
  expect(result.events[1]).toMatchObject({ title: "Job harmless-name", lifecycle: "failed", result: "failure" });
});
