import { describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { readFleetHardenSource } from "./fleetHarden";

const FIXTURE_PATH = join(import.meta.dir, "../../../test/fixtures/activity/fleetharden.jsonl");

describe("readFleetHardenSource", () => {
  test("maps hardened host events from jsonl and skips malformed lines", () => {
    const fixture = readFileSync(FIXTURE_PATH, "utf8");
    const { coverage, events } = readFleetHardenSource({
      path: FIXTURE_PATH,
      readFileImpl: () => fixture,
    });

    expect(coverage).toEqual({
      id: "fleet-harden",
      label: "fleet hardening",
      category: "service",
      path: FIXTURE_PATH,
      status: "ok",
      records: 3,
      totalRecords: 3,
      skipped: 2,
      earliest: "2026-08-01T00:00:00.000Z",
      latest: "2026-08-01T01:00:00.000Z",
    });
    expect(events).toHaveLength(3);
    expect(events).toEqual([
      {
        id: "fleet-harden:1",
        ts: "2026-08-01T00:00:00.000Z",
        category: "service",
        source: "fleet-harden",
        actor: "system",
        severity: "info",
        title: "rotate on alpha",
        session: "change-01",
        dedupeKey: "rotate",
        host: "alpha",
        detail: {
          ts: "2026-08-01T00:00:00.000Z",
          changeId: "change-01",
          host: "alpha",
          event: "rotate",
        },
      },
      {
        id: "fleet-harden:2",
        ts: "2026-08-01T01:00:00.000Z",
        category: "service",
        source: "fleet-harden",
        actor: "system",
        severity: "info",
        title: "rebuild",
        session: "change-02",
        dedupeKey: "rebuild",
        detail: {
          ts: "2026-08-01T01:00:00.000Z",
          changeId: "change-02",
          event: "rebuild",
        },
      },
      {
        id: "fleet-harden:5",
        ts: "2026-08-01T00:30:00.000Z",
        category: "service",
        source: "fleet-harden",
        actor: "system",
        severity: "info",
        title: "notify",
        session: "change-04",
        dedupeKey: "notify",
        detail: {
          ts: "2026-08-01T00:30:00.000Z",
          changeId: "change-04",
          host: "",
          event: "notify",
        },
      },
    ]);
    expect(events[2]).not.toHaveProperty("host");
  });

  test("reports absent when harden ledger does not exist", () => {
    const { coverage, events } = readFleetHardenSource({
      path: "/tmp/no-such-fleet-harden.jsonl",
      existsSyncImpl: () => false,
    });

    expect(events).toEqual([]);
    expect(coverage).toEqual({
      id: "fleet-harden",
      label: "fleet hardening",
      category: "service",
      path: "/tmp/no-such-fleet-harden.jsonl",
      status: "absent",
      records: 0,
      totalRecords: 0,
      skipped: 0,
    });
  });

  test("honors range filters while coverage stays whole-file", () => {
    const fixture = readFileSync(FIXTURE_PATH, "utf8");
    const { coverage, events } = readFleetHardenSource({
      path: FIXTURE_PATH,
      readFileImpl: () => fixture,
      fromMs: Date.parse("2026-08-01T00:20:00.000Z"),
      toMs: Date.parse("2026-08-01T00:40:00.000Z"),
    });

    expect(coverage.totalRecords).toBe(3);
    expect(coverage.earliest).toBe("2026-08-01T00:00:00.000Z");
    expect(coverage.latest).toBe("2026-08-01T01:00:00.000Z");
    expect(events).toHaveLength(1);
    expect(events[0]).toEqual({
      id: "fleet-harden:5",
      ts: "2026-08-01T00:30:00.000Z",
      category: "service",
      source: "fleet-harden",
      actor: "system",
      severity: "info",
      title: "notify",
      session: "change-04",
      dedupeKey: "notify",
      detail: {
        ts: "2026-08-01T00:30:00.000Z",
        changeId: "change-04",
        host: "",
        event: "notify",
      },
    });
  });
});
