// WorkloadDiff: the workload-level diff view — added/removed key lists
// and changed fields rendered as a→b rows, plus the fmtChangedValue
// formatting it shares with the table. Fabricated diff fixtures.

import { describe, expect, it } from "vitest";
import { render, screen, within } from "@testing-library/react";
import { WorkloadDiff, fmtChangedValue } from "@/components/compare/WorkloadDiff";

const workloads = {
  added: ["ns-a/Deployment/a1"],
  removed: ["ns-b/StatefulSet/b1"],
  changed: [
    {
      key: "ns-c/Deployment/c1",
      fields: [
        { field: "reqCpu", a: 8, b: 4 },
        { field: "scenario", a: "current", b: "shrink-1" },
      ],
    },
  ],
};

describe("WorkloadDiff", () => {
  it("renders added keys in emerald and removed keys in rose", () => {
    render(<WorkloadDiff workloads={workloads} />);
    expect(screen.getByText("+ ns-a/Deployment/a1")).toHaveClass("text-success");
    expect(screen.getByText("− ns-b/StatefulSet/b1")).toHaveClass("text-danger");
  });

  it("renders one changed row per field with its a→b values", () => {
    render(<WorkloadDiff workloads={workloads} />);
    const table = screen.getByRole("table");
    const rows = within(table).getAllByRole("row");
    // header + two field rows
    expect(rows.length).toBe(3);
    const cpu = within(rows[1]!).getAllByRole("cell").map((c) => c.textContent);
    expect(cpu).toEqual(["ns-c/Deployment/c1", "reqCpu", "8 → 4"]);
    const scenario = within(rows[2]!).getAllByRole("cell").map((c) => c.textContent);
    expect(scenario).toEqual(["ns-c/Deployment/c1", "scenario", "current → shrink-1"]);
  });

  it("renders an empty state when nothing changed", () => {
    render(<WorkloadDiff workloads={{ added: [], removed: [], changed: [] }} />);
    expect(screen.getByText("No workload changes between the two runs.")).toBeInTheDocument();
  });
});

describe("fmtChangedValue", () => {
  it("formats numbers with thousands separators and keeps strings as-is", () => {
    expect(fmtChangedValue(16000)).toBe("16,000");
    expect(fmtChangedValue(7.5)).toBe("7.5");
    expect(fmtChangedValue("current")).toBe("current");
  });
});
