import { describe, expect, it } from "vitest";
import { createMessagePipeline } from "../src/dimsdk/core/store/message-pipeline.ts";
import { createDimSdkState } from "../src/dimsdk/core/store/state.ts";

describe("dimsdk message pipeline subagent tool parts", () => {
  it("inserts structured subagent tool parts under the matching subagent header", () => {
    const { state } = createDimSdkState({
      context: { cwd: process.cwd(), env: {} } as any,
      agent: { getMode: () => "agent" } as any,
    });
    const pipeline = createMessagePipeline({
      state,
      debugLog: () => {},
    });

    pipeline.pushMessage({
      id: "assistant-subagent-1",
      role: "assistant",
      parts: [{ type: "status", text: "" }],
      createdAtMs: Date.now(),
    });

    pipeline.upsertSubagentInfo(
      "assistant-subagent-1",
      "thread_child",
      "header",
      "Analyze every file in the workspace",
    );
    pipeline.upsertToolCallDelta(
      "assistant-subagent-1",
      "child_call_read",
      "read-file-range",
      '{"path":"apps/cli/src/best-agent-native-bridge.ts","startLine":1,"endLine":40}',
      undefined,
      { subagentId: "thread_child" },
    );
    pipeline.upsertToolCallEndWithHighlights(
      "assistant-subagent-1",
      "child_call_read",
      "read-file-range",
      { path: "apps/cli/src/best-agent-native-bridge.ts", startLine: 1, endLine: 40 },
      undefined,
      { subagentId: "thread_child" },
    );
    pipeline.insertApprovalPart("assistant-subagent-1", {
      id: "approval_child_read",
      permission: "pass",
      reason: "Approval required to continue reading the file",
      riskLevel: "medium",
      source: "goatchain",
      status: "pending",
      subagentId: "thread_child",
      tool: "read-file-range" as any,
      toolCallId: "child_call_read",
    });
    pipeline.upsertToolOutputStart(
      "assistant-subagent-1",
      "child_call_read",
      "read-file-range",
      { subagentId: "thread_child" },
    );
    pipeline.appendToolOutputDelta(
      "assistant-subagent-1",
      "child_call_read",
      "read-file-range",
      "File: apps/cli/src/best-agent-native-bridge.ts",
      { subagentId: "thread_child" },
    );
    pipeline.attachToolResultPart(
      "assistant-subagent-1",
      {
        output: "File: apps/cli/src/best-agent-native-bridge.ts",
        status: "success",
        toolCallId: "child_call_read",
      },
      { subagentId: "thread_child" },
    );
    pipeline.upsertSubagentInfo(
      "assistant-subagent-1",
      "thread_child",
      "finished",
      "done",
    );

    const message = state.messages[0];
    expect(message?.role).toBe("assistant");

    const parts = (message as any).parts;
    expect(parts.map((part: any) => part.type)).toEqual([
      "status",
      "info",
      "tool_call",
      "approve",
      "tool_output",
      "tool_result",
      "info",
    ]);
    expect(parts[1]).toMatchObject({
      _subagent: "header",
      _subagentId: "thread_child",
      text: "Analyze every file in the workspace",
      type: "info",
    });
    expect(parts[2]).toMatchObject({
      _subagentId: "thread_child",
      call: {
        id: "child_call_read",
        name: "read-file-range",
        subagentId: "thread_child",
      },
      type: "tool_call",
    });
    expect(parts[3]).toMatchObject({
      request: {
        id: "approval_child_read",
        subagentId: "thread_child",
        toolCallId: "child_call_read",
      },
      type: "approve",
    });
    expect(parts[4]).toMatchObject({
      _subagentId: "thread_child",
      output: "File: apps/cli/src/best-agent-native-bridge.ts",
      subagentId: "thread_child",
      toolCallId: "child_call_read",
      type: "tool_output",
    });
    expect(parts[5]).toMatchObject({
      _subagentId: "thread_child",
      result: {
        output: "File: apps/cli/src/best-agent-native-bridge.ts",
        status: "success",
        subagentId: "thread_child",
        toolCallId: "child_call_read",
      },
      type: "tool_result",
    });
    expect(parts[6]).toMatchObject({
      _subagent: "finished",
      _subagentId: "thread_child",
      text: "done",
      type: "info",
    });
  });
});
