import { describe, expect, test } from "bun:test";
import { assembleLiveReport } from "./assemble";

const INDEX = `
| Priority | Status | Plan | Scope | Current receipt |
|---:|---|---|---|---|
| 1 | ACTIVE | [Alpha](alpha.md) | scope-a | index receipt alpha |
| 2 | BLOCKED | [Beta](beta.md) | scope-b | index receipt beta |
`;

const ALPHA = `
status: ACTIVE
worker: seat-alpha

## Current receipt
plan receipt alpha
`;

const BETA = `
status: BLOCKED
worker: none

## Current receipt
plan receipt beta
`;

describe("assembleLiveReport", () => {
  test("merges index rows with plan receipts and active sessions", () => {
    const files = new Map<string, string>([
      ["docs/plans/INDEX.md", INDEX],
      ["docs/plans/alpha.md", ALPHA],
      ["docs/plans/beta.md", BETA],
    ]);

    const report = assembleLiveReport({
      repoRoot: "/repo",
      generatedAt: "2026-08-10T06:00:00.000Z",
      sourceRevision: "abc123",
      existsSyncImpl: (path) => files.has(String(path).replace("/repo/", "")),
      readFileImpl: (path) => {
        const rel = String(path).replace("/repo/", "");
        const contents = files.get(rel);
        if (!contents) throw new Error(`missing fixture: ${rel}`);
        return contents;
      },
      readActiveSessionsImpl: () => [
        {
          ledgerId: "20260810T010000Z",
          runtime: "cursor-agent",
          host: "debian2",
          startedAt: "2026-08-10T01:00:00.000Z",
        },
      ],
    });

    expect(report.activeWorkers).toEqual([
      {
        planTitle: "Alpha",
        worker: "seat-alpha",
        href: "alpha.md",
        receipt: "plan receipt alpha",
      },
    ]);
    expect(report.blockers).toEqual([
      {
        planTitle: "Beta",
        href: "beta.md",
        receipt: "plan receipt beta",
      },
    ]);
    expect(report.activeSessions).toHaveLength(1);
    expect(report.warnings).toEqual([]);
  });
});
