import { describe, expect, it } from "vitest";
import { splitAtBoundary } from "../../src/feishu/textStream.js";

describe("splitAtBoundary", () => {
  it("keeps short text in a single chunk", () => {
    expect(splitAtBoundary("short text", 20)).toEqual(["short text"]);
  });

  it("keeps text at exactly maxLength in a single chunk", () => {
    const text = "x".repeat(20);

    expect(splitAtBoundary(text, 20)).toEqual([text]);
  });

  it("splits text exceeding maxLength into chunks at or below maxLength", () => {
    const chunks = splitAtBoundary("x".repeat(45), 20);

    expect(chunks).toHaveLength(3);
    expect(chunks.every((chunk) => chunk.length <= 20)).toBe(true);
    expect(chunks.join("")).toBe("x".repeat(45));
  });

  it("moves a split before a fenced code block when the boundary lands inside it", () => {
    const prefix = "Before code starts\n";
    const codeBlock = "```ts\nx\n```\n";
    const text = `${prefix}${codeBlock}After`;
    const chunks = splitAtBoundary(text, prefix.length + 4);

    expect(chunks).toEqual([prefix, `${codeBlock}After`]);
  });

  it("prefers paragraph boundaries over newline boundaries", () => {
    const text = "para one\n\nline two\nline three";

    expect(splitAtBoundary(text, 22)).toEqual(["para one\n\n", "line two\nline three"]);
  });

  it("prefers newline boundaries over space boundaries", () => {
    const text = "alpha beta\ngamma delta epsilon";

    expect(splitAtBoundary(text, 24)).toEqual(["alpha beta\n", "gamma delta epsilon"]);
  });
});
