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

function hierarchy(): LiveHierarchy {
  const projectId = unsafeOpaqueId("project:tool-diff");
  const planRevisionId = unsafeOpaqueId("plan-revision:tool-diff");
  const taskId = unsafeOpaqueId("task:tool-diff");
  const factoryRunId = unsafeOpaqueId("factory:tool-diff");
  const agentRunId = unsafeOpaqueId("agent:tool-diff");
  const attemptId = unsafeOpaqueId("attempt:tool-diff");
  const workspaceId = unsafeOpaqueId("workspace:tool-diff");
  const changeSetId = unsafeOpaqueId("changeset:tool-diff");
  return {
    project: {
      id: projectId,
      name: "Tool and diff evidence",
      repositoryUrl: "https://example.test/repository.git",
      requiredChecks: [],
      status: "active",
    } as never,
    goals: [],
    plans: [],
    planRevisions: [],
    tasks: [
      {
        id: taskId,
        projectId,
        planRevisionId,
        title: "Render evidence",
        status: "completed",
        dependencyIds: [],
        goalIds: [],
        revision: 1,
      } as never,
    ],
    factoryRuns: [
      { id: factoryRunId, projectId, planRevisionId, taskId, status: "completed", revision: 1 },
    ],
    agentRuns: [
      {
        id: agentRunId,
        factoryRunId,
        taskId,
        agentPrincipalId: unsafeOpaqueId("principal:tool-diff"),
        role: "coder",
        status: "completed",
        revision: 1,
      },
    ],
    attempts: [
      {
        id: attemptId,
        agentRunId,
        workspaceId,
        status: "terminal",
        selection: { kind: "initial", reason: "initial dispatch" },
        toolCalls: [{ name: "write_patch", summary: "Updated the implementation and tests" }],
        revision: 1,
      },
    ],
    workspaces: [],
    changeSets: [
      {
        id: changeSetId,
        projectId,
        taskId,
        producerAttemptId: attemptId,
        baseIdentity: "base:tool-diff",
        candidateDigest: "candidate:tool-diff",
        candidateManifest: {
          treeDigest: "tree:tool-diff",
          patchDigest: "patch:tool-diff",
          changedPaths: ["example.txt"],
          changes: [{ path: "example.txt", kind: "modify" }],
        },
        diff: [
          "diff --git a/example.txt b/example.txt",
          "--- a/example.txt",
          "+++ b/example.txt",
          "@@ -1 +1 @@",
          "-old <value>",
          "+new <value>",
        ].join("\n"),
        revision: 1,
        status: "candidate-ready",
      },
    ],
    reviews: [],
    verificationEvidence: [],
    reviewFindings: [],
    events: [],
  } as unknown as LiveHierarchy;
}

describe("approved live tool log and rendered diff", () => {
  it("renders named Attempt tool calls and semantic added/removed diff lines on canonical detail routes", () => {
    const data = hierarchy();
    const factory = renderApprovedLiveFactory(
      data,
      "factory:tool-diff",
      new URL("http://awp.test/factory-runs/factory%3Atool-diff?tab=agent-runs"),
    );
    expect(factory).toContain('data-agent-run-id="agent:tool-diff"');
    expect(factory).toContain('data-attempt-id="attempt:tool-diff"');
    expect(factory).toContain('data-tool-call="write_patch"');
    expect(factory).toContain("Updated the implementation and tests");

    const review = renderApprovedLiveReview(
      data,
      "changeset:tool-diff",
      new URL("http://awp.test/changesets/changeset%3Atool-diff?tab=files"),
    );
    expect(review).toContain('class="code-line del diff-line removed"');
    expect(review).toContain('class="code-line add diff-line added"');
    expect(review).toContain("-old &lt;value&gt;");
    expect(review).toContain("+new &lt;value&gt;");
    expect(review).not.toContain("<pre>");
  });
});
