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

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

describe("readNotificationsSource", () => {
  test("maps notification attempts and normalizes keys, severities, and titles", () => {
    const fixture = readFileSync(FIXTURE_PATH, "utf8");
    const { coverage, events } = readNotificationsSource({
      path: FIXTURE_PATH,
      readFileImpl: () => fixture,
    });

    expect(coverage).toEqual({
      id: "notifications",
      label: "notification attempts",
      category: "notification",
      path: FIXTURE_PATH,
      status: "ok",
      records: 3,
      totalRecords: 3,
      skipped: 3,
      earliest: new Date(1762483200 * 1000).toISOString(),
      latest: new Date(1762667000 * 1000).toISOString(),
    });
    expect(events).toHaveLength(3);
    expect(events[0]).toMatchObject({
      id: "notifications:1",
      category: "notification",
      source: "notifications",
      actor: "system",
      title: "runaway-agent-pid-1234 — alpha beta gamma ",
      severity: "error",
      dedupeKey: "guard:runaway-agent-pid-#",
    });
    expect((events[0] as { runtime?: string }).runtime).toBeUndefined();
    expect((events[0] as { host?: string }).host).toBeUndefined();
    expect((events[0] as { project?: string }).project).toBeUndefined();
    expect((events[0] as { session?: string }).session).toBeUndefined();

    expect(events[1]).toMatchObject({
      id: "notifications:2",
      severity: "notice",
      dedupeKey: "guard:runaway-agent-pid-#",
      title: `runaway-agent-pid-5678 — ${"B".repeat(200)}`,
    });

    expect(events[2]).toMatchObject({
      id: "notifications:6",
      severity: "info",
      title: "skip-no-summary",
      dedupeKey: "guard:skip-no-summary",
    });
  });

  test("keeps coverage full-file while applying from/to windows", () => {
    const fixture = readFileSync(FIXTURE_PATH, "utf8");
    const { coverage, events } = readNotificationsSource({
      path: FIXTURE_PATH,
      readFileImpl: () => fixture,
      fromMs: new Date(1762573100 * 1000).getTime(),
      toMs: new Date(1762573300 * 1000).getTime(),
    });

    expect(coverage.totalRecords).toBe(3);
    expect(coverage.earliest).toBe(new Date(1762483200 * 1000).toISOString());
    expect(coverage.latest).toBe(new Date(1762667000 * 1000).toISOString());
    expect(events).toHaveLength(1);
    expect(events[0]).toMatchObject({
      id: "notifications:2",
      ts: new Date(1762573200.5 * 1000).toISOString(),
      title: `runaway-agent-pid-5678 — ${"B".repeat(200)}`,
    });
  });

  test("reports absent when log file does not exist", () => {
    const { coverage, events } = readNotificationsSource({
      path: "/tmp/no-such-notif-log.jsonl",
      existsSyncImpl: () => false,
    });

    expect(events).toEqual([]);
    expect(coverage).toEqual({
      id: "notifications",
      label: "notification attempts",
      category: "notification",
      path: "/tmp/no-such-notif-log.jsonl",
      status: "absent",
      records: 0,
      totalRecords: 0,
      skipped: 0,
    });
  });
});
