import { describe, expect, test } from "bun:test";
import { join } from "node:path";
import { readAgentSessionsSource } from "./agentSessions";

const FIXTURE_ROOT = join(import.meta.dir, "../../../test/fixtures/activity/agent-sessions");

describe("readAgentSessionsSource", () => {
  test("maps start/finish events and enforces optional-field honesty", () => {
    const { coverage, events } = readAgentSessionsSource({
      root: FIXTURE_ROOT,
    });

    expect(coverage).toEqual({
      id: "agent-sessions",
      label: "agent sessions",
      category: "agent",
      path: join(FIXTURE_ROOT, "sessions"),
      status: "ok",
      records: 3,
      totalRecords: 2,
      skipped: 1,
      earliest: "2026-08-07T10:00:00.000Z",
      latest: "2026-08-07T10:30:00.000Z",
    });

    expect(events).toHaveLength(3);
    expect(events).toEqual([
      expect.objectContaining({
        id: "agent-sessions:codex-20260807T100000Z-000001:start",
        ts: "2026-08-07T10:00:00.000Z",
        category: "agent",
        source: "agent-sessions",
        actor: "agent",
        severity: "info",
        runtime: "codex",
        title: "codex session started by factory",
        lifecycle: "started",
        result: "unknown",
        host: "alpha",
        session: "codex-20260807T100000Z-000001",
        project: "overdeck",
        account: "zync2",
        dedupeKey: "codex:started",
      }),
      expect.objectContaining({
        id: "agent-sessions:codex-20260807T100000Z-000001:end",
        ts: "2026-08-07T10:01:00.000Z",
        title: "codex session finished — normal exit",
        lifecycle: "completed",
        result: "unknown",
        durationMs: 60_000,
        account: "zync2",
        dedupeKey: "codex:finished",
      }),
      expect.objectContaining({
        id: "agent-sessions:claude-20260807T103000Z-000002:start",
        ts: "2026-08-07T10:30:00.000Z",
        title: "claude session started",
        lifecycle: "started",
        result: "unknown",
        session: "claude-20260807T103000Z-000002",
      }),
    ]);

    const openEntry = events.at(2);
    expect(openEntry).not.toHaveProperty("project");
    expect(openEntry).not.toHaveProperty("durationMs");
    expect(openEntry).not.toHaveProperty("account");
  });

  test("honors range filters while coverage stays whole-file", () => {
    const { coverage, events } = readAgentSessionsSource({
      root: FIXTURE_ROOT,
      fromMs: Date.parse("2026-08-07T10:15:00.000Z"),
      toMs: Date.parse("2026-08-07T10:45:00.000Z"),
    });

    expect(coverage.totalRecords).toBe(2);
    expect(coverage.earliest).toBe("2026-08-07T10:00:00.000Z");
    expect(coverage.latest).toBe("2026-08-07T10:30:00.000Z");
    expect(events).toHaveLength(1);
    expect(events.at(0)?.id).toBe("agent-sessions:claude-20260807T103000Z-000002:start");
  });

  test("reports absent when sessions directory does not exist", () => {
    const { coverage, events } = readAgentSessionsSource({
      root: "/tmp/no-such-agent-sessions-dir",
    });

    expect(events).toEqual([]);
    expect(coverage).toEqual({
      id: "agent-sessions",
      label: "agent sessions",
      category: "agent",
      path: "/tmp/no-such-agent-sessions-dir/sessions",
      status: "absent",
      records: 0,
      totalRecords: 0,
      skipped: 0,
    });
  });
});
