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

const FIXTURE_PATH = join(import.meta.dir, "../../../test/fixtures/activity/notif-pending.json");

describe("readSuppressedNotifications", () => {
  test("summarizes pending registry and skips malformed row counts", () => {
    const fixture = readFileSync(FIXTURE_PATH, "utf8");
    const result = readSuppressedNotifications({
      path: FIXTURE_PATH,
      readFileImpl: () => fixture,
    });

    expect(result).toEqual({
      status: "ok",
      path: FIXTURE_PATH,
      sourceCount: 2,
      attemptCount: 3,
      skipped: 1,
      windowFrom: new Date(1762483200.5 * 1000).toISOString(),
      windowTo: new Date(1762483450 * 1000).toISOString(),
      rows: [
        {
          id: "guard:runaway",
          program: "/usr/bin/guard",
          summary: "runaway-agent-pid-####",
          summarySample: "runaway-agent-pid-1234",
          bodySample: "cdx has held 4 cores for 38 minutes",
          channel: "dbus",
          count: 2,
          first: new Date(1762483200.5 * 1000).toISOString(),
          last: new Date(1762483250.25 * 1000).toISOString(),
        },
        {
          id: "daemon:ping",
          program: "/usr/bin/daemon",
          summary: "ping loop",
          summarySample: "ping loop",
          channel: "notify-send",
          count: 1,
          first: new Date(1762483400 * 1000).toISOString(),
          last: new Date(1762483450 * 1000).toISOString(),
        },
      ],
    });
  });

  test("returns absent status when pending registry is not present", () => {
    const result = readSuppressedNotifications({
      path: "/tmp/no-such-pending.json",
      existsSyncImpl: () => false,
    });

    expect(result).toEqual({
      status: "absent",
      path: "/tmp/no-such-pending.json",
      rows: [],
      sourceCount: 0,
      attemptCount: 0,
      skipped: 0,
    });
  });
});
