import { describe, expect, it } from "vitest";
import { unsafeOpaqueId } from "@awp/contracts";
import { renderApprovedLiveFactory, type LiveHierarchy } from "../../apps/web/src/approved-live.js";

function hierarchy(): LiveHierarchy {
  const projectId = unsafeOpaqueId("project:failure");
  const planRevisionId = unsafeOpaqueId("plan-revision:failure");
  const taskId = unsafeOpaqueId("task:failure");
  const factoryRunId = unsafeOpaqueId("factory:failure");
  const agentRunId = unsafeOpaqueId("agent-run:failure");
  const failedAttemptId = unsafeOpaqueId("attempt:failed");
  const retryAttemptId = unsafeOpaqueId("attempt:retry");
  return {
    project: {
      id: projectId,
      name: "Failure visibility",
      repositoryUrl: "https://example.test/repository.git",
      requiredChecks: [],
      status: "active",
    } as never,
    goals: [],
    plans: [],
    planRevisions: [],
    tasks: [
      {
        id: taskId,
        projectId,
        planRevisionId,
        title: "Recover execution",
        status: "executing",
        dependencyIds: [],
        goalIds: [],
        revision: 1,
      } as never,
    ],
    factoryRuns: [
      {
        id: factoryRunId,
        projectId,
        planRevisionId,
        taskId,
        status: "retryable",
        reason: "Retry Attempt running",
        revision: 2,
      },
    ],
    agentRuns: [
      {
        id: agentRunId,
        factoryRunId,
        taskId,
        agentPrincipalId: unsafeOpaqueId("principal:failure"),
        role: "coder",
        status: "active",
        reason: "Retry Attempt running",
        revision: 2,
      },
    ],
    attempts: [
      {
        id: failedAttemptId,
        agentRunId,
        workspaceId: unsafeOpaqueId("workspace:failure"),
        status: "terminal",
        reason: "process exited <17>",
        selection: { kind: "initial", reason: "initial dispatch" },
        revision: 2,
      },
      {
        id: retryAttemptId,
        agentRunId,
        workspaceId: unsafeOpaqueId("workspace:failure"),
        status: "running",
        selection: {
          kind: "retry",
          reason: "preserved WIP",
          previousAttemptId: failedAttemptId,
        },
        revision: 2,
      },
    ],
    workspaces: [],
    changeSets: [],
    reviews: [],
    verificationEvidence: [],
    reviewFindings: [],
    events: [],
  } as unknown as LiveHierarchy;
}

describe("approved live AgentRun failure state", () => {
  it("keeps a recorded failed Attempt explicit while its retry is active", () => {
    const html = renderApprovedLiveFactory(
      hierarchy(),
      "factory:failure",
      new URL("http://awp.test/factory-runs/factory%3Afailure?tab=agent-runs"),
    );
    expect(html).toContain('data-agent-run-id="agent-run:failure"');
    expect(html).toContain('data-run-health="failure-recorded"');
    expect(html).toContain("Failure recorded");
    expect(html).toContain("Attempt failure");
    expect(html).toContain("process exited &lt;17&gt;");
    expect(html).toContain('data-attempt-id="attempt:failed"');
    expect(html).toContain('data-attempt-id="attempt:retry"');
    expect(html).toContain("Status: running");
  });
});
