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

import type { ToolInputFieldView } from "@/lib/api/types";
import type { PreviewModel } from "./toolbox";
import { fieldSummary, renderPreviewText } from "./aiSummary";

const fields: ToolInputFieldView[] = [
  { name: "brief", label: "脑洞", type: "textarea", required: true, help: null },
  { name: "count", label: "数量", type: "number", required: false, help: null },
];

describe("fieldSummary", () => {
  it("拼出「label：value」摘要，跳过空值", () => {
    const summary = fieldSummary(fields, { brief: "废柴逆袭", count: "" });
    expect(summary).toBe("脑洞：废柴逆袭");
  });

  it("多字段用 · 连接", () => {
    const summary = fieldSummary(fields, { brief: "扮猪吃虎", count: "3" });
    expect(summary).toBe("脑洞：扮猪吃虎 · 数量：3");
  });

  it("字段为空 / 无值 → 空串", () => {
    expect(fieldSummary([], {})).toBe("");
    expect(fieldSummary(fields, {})).toBe("");
  });
});

describe("renderPreviewText", () => {
  it("把预览项渲染成人读文本（heading + 字段 + 正文）", () => {
    const preview: PreviewModel = {
      kind: "IdeaListResult",
      items: [
        {
          heading: "废柴逆袭",
          fields: [{ label: "钩子", value: "扮猪吃虎" }],
          body: null,
        },
        { heading: null, fields: [], body: "一段开篇正文。" },
      ],
    };
    const text = renderPreviewText(preview);
    expect(text).toContain("废柴逆袭");
    expect(text).toContain("钩子：扮猪吃虎");
    expect(text).toContain("一段开篇正文。");
  });

  it("空预览 → 空串", () => {
    expect(renderPreviewText({ kind: "x", items: [] })).toBe("");
  });
});
