import { describe, expect, test } from "bun:test";
import { parseKafkaReplayOptions, renderKafkaReplay } from "./platform-infra-kafka";
import { renderMachine } from "./agentrun/render";

describe("platform-infra Kafka replay", () => {
  test("accepts the bounded no-publish application replay contract", () => {
    expect(parseKafkaReplayOptions([
      "agentrun", "--node", "NC01", "--lane", "nc01-v02",
      "--session-id", "ses_example", "--trace-id", "trc_example",
      "--no-publish", "-o", "yaml",
    ])).toMatchObject({
      targetId: "NC01",
      lane: "nc01-v02",
      sessionId: "ses_example",
      traceId: "trc_example",
      output: "yaml",
    });
  });

  test("rejects replay without the explicit no-publish boundary", () => {
    expect(() => parseKafkaReplayOptions(["agentrun", "--session-id", "ses_example"])).toThrow("requires --no-publish");
  });

  test("renders matched replay counts and mutation disclosure", () => {
    const rendered = renderKafkaReplay({
      ok: true,
      diagnosis: {
        networkPlane: "k3s-application-pod",
        application: "agentrun",
        namespace: "agentrun-v02",
        workload: "deployment/agentrun-mgr",
        topic: "codex-stdio.raw.v1",
        scanned: 30,
        accepted: 12,
        produced: 8,
        rejected: { classification: "matched" },
        mutation: false,
        topicAppended: false,
        valuesPrinted: false,
        firstMismatchIdentity: null,
      },
    });
    expect(rendered.renderedText).toContain("k3s-application-pod");
    expect(rendered.renderedText).toContain("30");
    expect(rendered.renderedText).toContain("matched");
    expect(rendered.renderedText).toContain("no              no");
  });

  test("keeps JSON and YAML machine projections isomorphic", () => {
    const payload = {
      ok: true,
      diagnosis: {
        networkPlane: "k3s-application-pod",
        scanned: 30,
        accepted: 12,
        produced: 8,
        rejected: { classification: "matched", byReason: {} },
        mutation: false,
        topicAppended: false,
        valuesPrinted: false,
      },
    };
    const json = JSON.parse(renderMachine("replay", payload, "json").renderedText);
    const yaml = Bun.YAML.parse(renderMachine("replay", payload, "yaml").renderedText);
    expect(yaml).toEqual(json);
  });
});
