import assert from "node:assert/strict";
import test from "node:test";

import { listNativeCases, listNativeRuns, readNativeAggregate, readNativeEvents, readNativeRun, resetNativeFixtures, startNativeRun } from "./caserun-native-fixtures";

test("native CaseRun fixtures expose six deterministic API-owned states", () => {
  resetNativeFixtures();
  const cases = listNativeCases();
  assert.equal(cases.mode, "native-test");
  assert.deepEqual(cases.cases.map((item) => item.caseId), ["native-queued", "native-running", "native-completed", "native-failed", "native-blocked", "native-canceled"]);

  const started = startNativeRun("native-completed");
  assert.equal(started?.status, "queued");
  assert.equal(started?.terminal, false);
  const running = readNativeRun(started!.runId);
  assert.equal(running?.status, "running");
  assert.equal(running?.terminal, false);
  for (let index = 0; index < 3; index += 1) readNativeRun(started!.runId);
  const completed = readNativeRun(started!.runId);
  assert.equal(completed?.status, "completed");
  assert.equal(completed?.terminal, true);
  assert.equal(completed?.workflowId, `harnessrl-caserun-${started!.runId}`);
  assert.equal(completed?.result.summary.artifactCount, 1);
  assert.equal(completed?.result.summary.artifacts[0].path, "native-result.json");
  assert.match(completed?.references.aggregate.sha256 ?? "", /^[a-f0-9]{64}$/);

  const aggregate = readNativeAggregate(started!.runId);
  assert.equal(aggregate?.mode, "native-test");
  assert.equal(aggregate?.runId, started!.runId);
  assert.equal(aggregate?.status, "completed");
  assert.equal(aggregate?.sha256, completed?.references.aggregate.sha256);

  const events = readNativeEvents(started!.runId);
  assert.deepEqual(events?.events.map((event) => event.stage), ["queued", "prepare", "prepared", "build-completed", "collect-completed", "completed"]);
  assert.equal(events?.events[0]?.createdAt, events?.events[0]?.at);
  assert.equal(events?.mode, "native-test");
});

test("native run history exposes a completed CLI-shaped result", () => {
  resetNativeFixtures();
  const history = listNativeRuns();
  assert.equal(history.mode, "native-test");
  assert.equal(history.runs[0]?.runId, "cli-software-smoke-passed");
  assert.equal(history.runs[0]?.caseId, "software-smoke");
  assert.equal(history.runs[0]?.status, "completed");
  assert.equal(history.runs[0]?.artifactCount, 1);
  assert.equal(readNativeAggregate("cli-software-smoke-passed")?.terminal, true);
});

test("native blockers and cancellation come from the API fixture", () => {
  resetNativeFixtures();
  const blocked = startNativeRun("native-blocked")!;
  const blockedTerminal = readNativeRun(blocked.runId)!;
  assert.equal(blockedTerminal.terminal, true);
  assert.equal(blockedTerminal.blocker?.code, "native-capability-blocked");

  const canceled = startNativeRun("native-canceled")!;
  readNativeRun(canceled.runId);
  const canceledTerminal = readNativeRun(canceled.runId)!;
  assert.equal(canceledTerminal.status, "canceled");
  assert.equal(canceledTerminal.terminal, true);
});
