import { describe, it, expect } from "vitest";
import { truncateMiddle } from "./truncateMiddle";

describe("truncateMiddle", () => {
  it("returns short strings unchanged", () => {
    expect(truncateMiddle("short", 32)).toBe("short");
    expect(truncateMiddle("exactly-len", 11)).toBe("exactly-len");
  });

  it("abbreviates in the middle keeping head and tail", () => {
    const result = truncateMiddle("long_media_name_like_this", 16);
    expect(result).toContain("…");
    expect(result.length).toBe(16);
    expect(result.startsWith("long")).toBe(true);
    expect(result.endsWith("this")).toBe(true);
  });

  it("never exceeds maxLength", () => {
    for (const len of [1, 2, 3, 5, 10, 25]) {
      expect(truncateMiddle("a".repeat(100), len).length).toBeLessThanOrEqual(len);
    }
  });

  it("handles null and undefined", () => {
    expect(truncateMiddle(null)).toBe("");
    expect(truncateMiddle(undefined)).toBe("");
  });

  it("falls back to head-truncation when there is no room for content", () => {
    expect(truncateMiddle("abcdef", 1)).toBe("a");
    expect(truncateMiddle("abcdef", 0)).toBe("");
  });

  it("supports a custom ellipsis", () => {
    const result = truncateMiddle("long_media_name_like_this", 16, "...");
    expect(result).toContain("...");
    expect(result.length).toBe(16);
  });
});
