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

import type { ChatMessage } from "../src/types/index.ts";
import { traceLifecycleExpanded } from "../src/components/workbench/trace-lifecycle.ts";

test("Trace lifecycle respects the workbench timeline policy", () => {
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "running", runnerTrace: { status: "running" } })), null);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "running", runnerTrace: { status: "pending" } })), null);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "completed", runnerTrace: { status: "running" } })), null);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "running", runnerTrace: { status: "completed" } })), null);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "running", runnerTrace: { status: "running", finalResponse: "ok" } })), null);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "failed", runnerTrace: { status: "failed" } })), null);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "completed", traceAutoLifecycle: null, runnerTrace: { status: "completed", finalResponse: "ok" } })), null);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "completed", traceAutoLifecycle: "terminal", runnerTrace: { status: "completed", finalResponse: "ok" } })), null);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "completed", traceAutoLifecycle: "terminal", runnerTrace: { status: "completed", finalResponse: "ok" } }), { autoCollapseTerminal: true }), false);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "running", traceAutoLifecycle: "running", runnerTrace: { status: "completed", finalResponse: "ok" } }), { autoExpandRunning: true }), true);
  assert.equal(traceLifecycleExpanded(agentMessage({ status: "running", runnerTrace: { status: "running" } }), { autoExpandRunning: true }), true);
  assert.equal(traceLifecycleExpanded({ ...agentMessage({ status: "running" }), role: "user", title: "用户" }), null);
});

function agentMessage(patch: Partial<ChatMessage>): ChatMessage {
  return { id: "msg_trace_lifecycle", role: "agent", title: "Code Agent", text: "", status: "running", createdAt: "2026-06-15T00:00:00.000Z", ...patch };
}
