import { describe, expect, it } from "vitest";

import {
  formatChangedSection,
  getVisibleMarkdownBlocks,
  parseMarkdownBlocks,
} from "../optimized-preview";

describe("OptimizedPreview markdown helpers", () => {
  it("parses markdown headings, paragraphs, and bullets without exposing markers", () => {
    const blocks = parseMarkdownBlocks(
      "## 服务特点\n\n- **定制化方案**：拒绝模板化。\n- 数据驱动：提升运营效率。\n\n伟思德鲁提供全域增长服务。",
    );

    expect(blocks).toEqual([
      { type: "heading", text: "服务特点", level: 2 },
      {
        type: "list",
        items: ["**定制化方案**：拒绝模板化。", "数据驱动：提升运营效率。"],
      },
      { type: "paragraph", text: "伟思德鲁提供全域增长服务。" },
    ]);
    expect(blocks.map((block) => JSON.stringify(block)).join("\n")).not.toContain(
      "##",
    );
  });

  it("reveals markdown blocks progressively while streaming", () => {
    const blocks = parseMarkdownBlocks(
      "## 第一段\n\n正文一。\n\n## 第二段\n\n正文二。",
    );

    expect(getVisibleMarkdownBlocks(blocks, true, 2)).toEqual(blocks.slice(0, 2));
    expect(getVisibleMarkdownBlocks(blocks, false, 1)).toEqual(blocks);
  });

  it("formats changed section keys as customer-readable Chinese labels", () => {
    expect(formatChangedSection("title")).toBe("标题优化");
    expect(formatChangedSection("body_markdown")).toBe("正文结构与表达");
    expect(formatChangedSection("client_cases")).toBe("客户案例");
    expect(formatChangedSection("unknown_internal_key")).toBe("其他内容");
  });
});
