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 error rendering", () => {
  it("keeps synthetic error markdown in a separate part when the streamed markdown is incomplete", () => {
    const { state } = createDimSdkState({
      context: { cwd: process.cwd(), env: {} } as any,
      agent: { getMode: () => "agent" } as any,
    });
    const pipeline = createMessagePipeline({
      state,
      debugLog: () => {},
    });

    pipeline.pushMessage({
      id: "assistant-1",
      role: "assistant",
      parts: [{ type: "markdown", markdown: "- **语言**: TypeScript\n- **" }],
      createdAtMs: Date.now(),
    });

    pipeline.ensureErrorVisibleInTranscript("assistant-1", new Error("boom"));

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

    const markdownParts = (message as any).parts.filter(
      (part: any) => part?.type === "markdown",
    );

    expect(markdownParts).toHaveLength(2);
    expect(markdownParts[0]?.markdown).toBe("- **语言**: TypeScript\n- **");
    expect(markdownParts[1]?.markdown).toContain("# Error");
    expect(markdownParts[1]?.markdown).toContain("boom");
  });
});
