import assert from "node:assert/strict";

import {
  createRunSummary,
  summarizeRunResult,
} from "../../../runtime/ui/ini-panel/run-summary.js";

const program = {
  source: "snapshot",
  opfsPath: "linuxcnc/gcode/ui-session.ngc",
  wasmPath: "/work/ui-session.ngc",
};
const snapshot = {
  sessionId: "ui-machine-session",
  metadata: {
    workflow: "save-session-snapshot",
    snapshotLabel: "ui-machine-session/ui-machine-session.json",
  },
};

assert.deepEqual(
  createRunSummary(program, {
    runStatus: "ok",
    iniWasmPath: "/work/session-machine.ini",
    canonicalEventCount: 2,
    motionSnapshotCount: 2,
  }, snapshot),
  {
    runStatus: "ok",
    programSource: "snapshot",
    snapshotLabel: "ui-machine-session/ui-machine-session.json",
    workflow: "save-session-snapshot",
    programOpfsPath: "linuxcnc/gcode/ui-session.ngc",
    programWasmPath: "/work/ui-session.ngc",
    iniWasmPath: "/work/session-machine.ini",
    canonicalEventCount: 2,
    motionSnapshotCount: 2,
  },
);

assert.deepEqual(
  createRunSummary({
    source: "default",
    opfsPath: "linuxcnc/gcode/ui-session.ngc",
    wasmPath: "/work/ui-session.ngc",
  }),
  {
    runStatus: "ok",
    programSource: "default",
    snapshotLabel: null,
    workflow: null,
    programOpfsPath: "linuxcnc/gcode/ui-session.ngc",
    programWasmPath: "/work/ui-session.ngc",
    iniWasmPath: null,
    canonicalEventCount: 0,
    motionSnapshotCount: 0,
  },
);

assert.deepEqual(
  createRunSummary(program, {
    snapshotLabel: "override-session/override.json",
  }, snapshot),
  {
    runStatus: "ok",
    programSource: "snapshot",
    snapshotLabel: "override-session/override.json",
    workflow: "save-session-snapshot",
    programOpfsPath: "linuxcnc/gcode/ui-session.ngc",
    programWasmPath: "/work/ui-session.ngc",
    iniWasmPath: null,
    canonicalEventCount: 0,
    motionSnapshotCount: 0,
  },
);

assert.deepEqual(
  summarizeRunResult([
    "canon_event=STRAIGHT_TRAVERSE line=1 x=1 y=2",
    "run_step phase=execute line=1",
    "canon_event=STRAIGHT_FEED line=2 x=3 y=4",
    "message=canon_event=not-a-canonical-prefix",
    "",
  ].join("\n"), [{}, {}]),
  {
    canonicalEventCount: 2,
    motionSnapshotCount: 2,
  },
);

assert.deepEqual(
  summarizeRunResult("", []),
  {
    canonicalEventCount: 0,
    motionSnapshotCount: 0,
  },
);

console.log("ini_panel_run_summary_node_smoke=ok");
