import { expect, test } from "bun:test";

import { compactNodeStatus, renderNodeStatus } from "./cicd-node-status";

const next = {
  status: "bun scripts/cli.ts cicd status --node NC01",
  history: "bun scripts/cli.ts platform-infra pipelines-as-code history --target NC01",
  fixManualRelease: { reference: ".agents/skills/unidesk-cicd/SKILL.md" },
};

function result(status: "ready" | "warning" | "blocked", consumers: Array<Record<string, unknown>>): Record<string, unknown> {
  return {
    ok: status === "ready",
    node: "NC01",
    target: { id: "NC01" },
    summary: {
      ready: status === "ready",
      status,
      code: status === "ready" ? "cicd-node-ready" : status === "warning" ? "cicd-node-no-consumers" : "cicd-node-blocked",
      total: consumers.length,
      readyCount: consumers.filter((consumer) => consumer.ready === true).length,
      blockedCount: consumers.filter((consumer) => consumer.ready !== true).length,
      elapsedMs: 12,
    },
    consumers,
    next,
  };
}

const readyConsumer = {
  consumer: "platform-infra-gitea-nc01",
  ready: true,
  pipelineStatus: "Succeeded",
  durationSeconds: 9,
  sourceCommit: "a".repeat(40),
  gitopsCommit: "b".repeat(40),
  argo: { sync: "Synced", health: "Healthy", revisionRelation: { relation: "exact" } },
  runtime: { readyReplicas: 1, replicas: 1, digest: `sha256:${"c".repeat(64)}` },
  reason: "ready",
  latestPipelineRun: "platform-infra-gitea-nc01-fixture",
  digest: `sha256:${"c".repeat(64)}`,
  imageStatus: "-",
  diagnostics: { hint: "GitOps-only consumer is ready", registry: { applicability: "not-configured", status: "not-configured", present: null } },
};

test("node status renderer is non-empty for zero consumers, ready, warning, and blocked projections", () => {
  const fixtures = [
    result("warning", []),
    result("ready", [readyConsumer]),
    result("warning", [{ ...readyConsumer, ready: false, reason: "diagnostics-warning" }]),
    result("blocked", [{ ...readyConsumer, ready: false, reason: "pac-registry-missing" }]),
  ];
  for (const fixture of fixtures) {
    const rendered = renderNodeStatus(fixture, true).renderedText;
    expect(rendered.trim().length).toBeGreaterThan(0);
    expect(rendered).toContain("CI/CD NODE STATUS");
    expect(rendered).toContain("NEXT");
  }
});

test("node full renderer shows registry N/A without hiding digest evidence", () => {
  const rendered = renderNodeStatus(result("ready", [readyConsumer]), true).renderedText;
  expect(rendered).toContain("N/A");
  expect(rendered).toContain("sha256:");
  expect(rendered).toContain("platform-infra-gitea-nc01-fixture");
});

test("node JSON projection keeps one-capture evidence without full diagnostics payload", () => {
  const projected = compactNodeStatus({
    ...result("ready", [readyConsumer]),
    action: "cicd-node-status",
    mutation: false,
    observation: { status: "succeeded", elapsedMs: 7000 },
    readErrors: [],
    remoteCaptureCount: 1,
  });
  expect(projected).toMatchObject({
    ok: true,
    mutation: false,
    remoteCaptureCount: 1,
    observation: { status: "succeeded" },
    readErrors: [],
    consumers: [{ consumer: "platform-infra-gitea-nc01", ready: true }],
  });
  expect(JSON.stringify(projected)).not.toContain("diagnostics");
});
