// CostOverviewTable: rows render as the engine delivers them — nodegroupUnits
// is already merged per (name, instanceType) by report-model (cross-AZ
// entries folded), so the table must not re-fold or re-order, and the
// totals row sums the units' counts.

import { describe, expect, it } from "vitest";
import { render, screen, within } from "@testing-library/react";
import { CostOverviewTable } from "@/components/regions/CostOverviewTable";
import type { CostOverview } from "@/engine/report-model";

const cost: CostOverview = {
  nodegroupCount: 2,
  nodeCount: 4,
  nodeCost: 400,
  nodegroupUnits: [
    { name: "workers", instanceType: "m5.large", unitPrice: 100, count: 3, cost: 300 },
    { name: "workers", instanceType: "m5.large", unitPrice: 100, count: 1, cost: 100 },
  ],
  pvcCount: 0,
  pvcTotalGib: 0,
  pvcCost: 0,
  pvcRate: 0,
  total: 400,
};

function rowText(row: HTMLElement): string[] {
  return within(row).getAllByRole("cell").map((c) => c.textContent ?? "");
}

describe("CostOverviewTable", () => {
  it("renders one row per delivered unit and sums the node count", () => {
    render(<CostOverviewTable cost={cost} />);
    const rows = screen.getAllByRole("row");
    // header + two units + nodes total + pvcs + grand total
    expect(rows).toHaveLength(6);
    expect(rowText(rows[1]!)).toEqual(["workers", "m5.large", "3", "$100.00", "$300.00"]);
    expect(rowText(rows[2]!)).toEqual(["workers", "m5.large", "1", "$100.00", "$100.00"]);
    expect(rowText(rows[3]!)).toEqual(["Nodes total", "—", "4", "—", "$400.00"]);
  });
});
