import assert from "node:assert/strict";
import { test } from "bun:test";
import { renderToStaticMarkup } from "react-dom/server";

import { MessageTracePanel } from "./MessageTracePanel";
import type { RunnerTrace } from "../../types/domain";

test("message trace panel renders assistant markdown rows through the shared renderer", () => {
  const trace: RunnerTrace = {
    traceId: "trc_markdown_row",
    status: "completed",
    events: [
      {
        seq: 1,
        label: "agentrun:assistant:message",
        type: "assistant",
        status: "completed",
        final: true,
        replyAuthority: true,
        message: "| hwpodId | node |\n|---|---|\n| `hwpod-f103` | pc-host-1 |"
      }
    ],
    eventCount: 1
  };

  const html = renderToStaticMarkup(<MessageTracePanel trace={trace} defaultOpen />);

  assert.match(html, /data-body-format="markdown"/u);
  assert.match(html, /message-trace-markdown/u);
  assert.match(html, /<table>/u);
  assert.match(html, /<code>hwpod-f103<\/code>/u);
  assert.doesNotMatch(html, /<pre class="message-trace-body message-copy-markdown" data-body-format="markdown"/u);
});

test("message trace panel exposes running status for live trace animation", () => {
  const trace: RunnerTrace = {
    traceId: "trc_running_row",
    status: "running",
    events: [
      { seq: 1, label: "agentrun:request:accepted", status: "accepted" }
    ],
    eventCount: 1
  };

  const html = renderToStaticMarkup(<MessageTracePanel trace={trace} />);

  assert.match(html, /data-trace-status="running"/u);
  assert.match(html, /open=""/u);
});

test("message trace panel collapses automatically when final response is available", () => {
  const trace: RunnerTrace = {
    traceId: "trc_completed_final",
    status: "completed",
    events: [
      { seq: 1, label: "agentrun:assistant:message", status: "completed", message: "最终回复" }
    ],
    eventCount: 1
  };

  const html = renderToStaticMarkup(<MessageTracePanel trace={trace} defaultOpen collapseWhenFinalResponse />);

  assert.match(html, /data-trace-status="completed"/u);
  assert.doesNotMatch(html, /<details[^>]* open=""/u);
});

test("message trace panel renders AgentRun terminal evidence for expired historical trace", () => {
  const trace: RunnerTrace = {
    traceId: "trc_issue932_expired",
    status: "expired",
    traceStatus: "expired",
    events: [],
    eventCount: 31,
    terminalEvidence: { available: true, source: "agentrun-command-result" },
    traceSummary: { sourceEventCount: 31, terminalStatus: "completed", source: "agentrun-command-result" },
    finalResponse: { text: "历史最终回复" }
  };

  const html = renderToStaticMarkup(<MessageTracePanel trace={trace} defaultOpen />);

  assert.match(html, /历史 trace 已过期/u);
  assert.match(html, /历史最终回复/u);
  assert.match(html, /AgentRun 终态证据/u);
  assert.match(html, /sourceEventCount=31/u);
  assert.doesNotMatch(html, /等待后端事件/u);
});

test("message trace panel does not label completed traces with pending replay as expired", () => {
  const trace: RunnerTrace = {
    traceId: "trc_issue955_completed_replay",
    status: "completed",
    traceStatus: "completed",
    events: [],
    eventCount: 106,
    traceSummary: { sourceEventCount: 106, terminalStatus: "completed", source: "code-agent-result" },
    finalResponse: { text: "这里看到了 gateway sessions。" }
  };

  const html = renderToStaticMarkup(<MessageTracePanel trace={trace} defaultOpen />);

  assert.match(html, /完整 trace 回放中/u);
  assert.match(html, /traceStatus=completed/u);
  assert.match(html, /sourceEventCount=106/u);
  assert.match(html, /这里看到了 gateway sessions/u);
  assert.doesNotMatch(html, /历史 trace 已过期/u);
});
