import { describe, expect, it } from "vitest";
import type { Edge, Node } from "@xyflow/react";
import type { EntropykNodeData } from "./configBuilder";
import { buildComponentInspector } from "./componentInspector";
import type { SimulationResult } from "./api";

function node(
  id: string,
  type: string,
  name: string,
): Node<EntropykNodeData> {
  return {
    id,
    type: "entropykNode",
    position: { x: 0, y: 0 },
    data: { type, name, circuit: 0, params: {} },
  };
}

describe("buildComponentInspector", () => {
  it("computes ΔP, ṁ and port table from enriched state", () => {
    const nodes = [
      node("c", "IsentropicCompressor", "comp"),
      node("d", "Condenser", "cond"),
    ];
    const edges: Edge[] = [
      {
        id: "e0",
        source: "c",
        target: "d",
        sourceHandle: "outlet",
        targetHandle: "inlet",
      },
    ];
    const result: SimulationResult = {
      input: "t",
      status: "converged",
      state: [
        {
          edge: 0,
          pressure_bar: 12.5,
          enthalpy_kj_kg: 430,
          mass_flow_kg_s: 0.05,
          source: "comp",
          target: "cond",
          source_port: "outlet",
          target_port: "inlet",
          temperature_c: 55,
          saturation_temperature_c: 48,
        },
      ],
    };
    const insp = buildComponentInspector(nodes[0], nodes, edges, result);
    expect(insp.title).toBe("comp");
    expect(insp.ports).toHaveLength(1);
    expect(insp.ports[0].port).toBe("outlet");
    expect(insp.ports[0].pressure_bar).toBe(12.5);
    expect(insp.summaryLines.some((l) => l.includes("ṁ"))).toBe(true);
  });
});
