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

import {
  buildManuscriptExcerpt,
  DEFAULT_EXCERPT_MAX_CHARS,
} from "./manuscriptExcerpt";

describe("buildManuscriptExcerpt", () => {
  it("短文（未超界）trim 后原样返回，无省略标记", () => {
    const text = "  第一章 少年初入江湖。  ";
    const excerpt = buildManuscriptExcerpt(text, 1800);
    expect(excerpt).toBe("第一章 少年初入江湖。");
    expect(excerpt).not.toContain("中略");
  });

  it("空串与纯空白 → 返回 \"\"", () => {
    expect(buildManuscriptExcerpt("", 1800)).toBe("");
    expect(buildManuscriptExcerpt("   \n\t  ", 1800)).toBe("");
  });

  it("正好等于上界 → 原样返回，不截断", () => {
    const text = "字".repeat(1800);
    const excerpt = buildManuscriptExcerpt(text, 1800);
    expect(excerpt).toBe(text);
    expect(excerpt).not.toContain("中略");
  });

  it("超长 → 取首尾拼接、含省略标记，且首尾片段都保留", () => {
    const head = "甲".repeat(1000);
    const tail = "乙".repeat(1000);
    const text = head + tail; // 2000 > 1800
    const excerpt = buildManuscriptExcerpt(text, 1800);

    expect(excerpt).toContain("中略");
    // 首段来自开头、尾段来自结尾。
    expect(excerpt.startsWith("甲")).toBe(true);
    expect(excerpt.endsWith("乙")).toBe(true);
  });

  it("超长摘录长度 ≤ 上界 + 少许标记冗余（正文部分不超界）", () => {
    const text = "文".repeat(5000);
    const maxChars = 1800;
    const excerpt = buildManuscriptExcerpt(text, maxChars);

    // 省略标记之外的正文字符数不得超过上界。
    const bodyLen = excerpt.replace(/……（中略）……/g, "").replace(/\s+/g, "")
      .length;
    expect(bodyLen).toBeLessThanOrEqual(maxChars);
    // 整体长度仅比上界多出标记长度这点冗余。
    expect(excerpt.length).toBeLessThanOrEqual(maxChars + 20);
  });

  it("默认上界常量生效（不传 maxChars）", () => {
    const text = "字".repeat(DEFAULT_EXCERPT_MAX_CHARS + 500);
    const excerpt = buildManuscriptExcerpt(text);
    expect(excerpt).toContain("中略");
    const bodyLen = excerpt.replace(/……（中略）……/g, "").replace(/\s+/g, "")
      .length;
    expect(bodyLen).toBeLessThanOrEqual(DEFAULT_EXCERPT_MAX_CHARS);
  });
});
