import { describe, expect, it } from "vitest";
import { renderLiveProject } from "../../apps/web/src/live.js";

const hierarchy = {
  project: {
    id: "project-failure",
    name: "Failure visibility",
    repositoryUrl: "https://example.test/repository.git",
    status: "active",
  },
  goals: [],
  plans: [],
  tasks: [],
  factoryRuns: [],
  agentRuns: [
    {
      id: "agent-run-failure",
      taskId: "task-failure",
      status: "active",
      reason: "Retry Attempt running",
    },
  ],
  attempts: [
    {
      id: "attempt-failed",
      agentRunId: "agent-run-failure",
      status: "terminal",
      reason: "process exited <17>",
      selection: { kind: "initial", reason: "initial dispatch" },
    },
    {
      id: "attempt-retry",
      agentRunId: "agent-run-failure",
      status: "running",
      selection: { kind: "retry", reason: "preserved WIP" },
    },
  ],
  changeSets: [],
  reviews: [],
} as const;

describe("live AgentRun failure state", () => {
  it("keeps a recorded failure explicit while its retry is active", () => {
    const html = renderLiveProject(hierarchy);
    expect(html).toContain('data-run-health="failure-recorded"');
    expect(html).toContain('<span class="badge failure">Failure recorded</span>');
    expect(html).toContain("<strong>Attempt failure</strong>");
    expect(html).toContain("process exited &lt;17&gt;");
    expect(html).toContain(">active</span>");
    expect(html).toContain(">running</span>");
  });
});
