import { describe, it, expect } from "bun:test";
import { truncateText, formatNumberToShortForm } from "./text";

describe("truncateText", () => {
  it("should truncate text longer than the specified length", () => {
    const input = "This is a long text that needs to be truncated.";
    const result = truncateText(input, 10);
    expect(result).toBe("This is a ...");
  });

  it("should not truncate text shorter than the specified length", () => {
    const input = "Short text";
    const result = truncateText(input, 20);
    expect(result).toBe(input);
  });

  it("should use the default length of 1000 if no length is specified", () => {
    const input = "A".repeat(1001);
    const result = truncateText(input);
    expect(result).toBe("A".repeat(1000) + "...");
  });
});

describe("formatNumberToShortForm", () => {
  it("should format numbers in billions with 'b' suffix", () => {
    const result = formatNumberToShortForm(1_500_000_000);
    expect(result).toMatch(/1\.5.*b/);
  });

  it("should format numbers in millions with 'M' suffix", () => {
    const result = formatNumberToShortForm(2_300_000);
    expect(result).toMatch(/2\.3.*M/);
  });

  it("should format numbers in thousands with 'k' suffix", () => {
    const result = formatNumberToShortForm(12_000);
    expect(result).toMatch(/12.*k/);
  });

  it("should format numbers below 1000 without a suffix", () => {
    const result = formatNumberToShortForm(999);
    expect(result).toBe("999");
  });

  it("should handle negative numbers correctly", () => {
    const result = formatNumberToShortForm(-1_200_000);
    expect(result).toMatch(/-1\.2.*M/);
  });

  it("should respect the specified locale", () => {
    const result = formatNumberToShortForm(1_234_567, "de-DE");
    expect(result).toMatch(/1,23.*M/);
  });
});
