import { describe, expect, test } from "vitest";
import {
  isCapitalized,
  isStringMatch,
  sanitizeString,
  startsWithVowel,
  toLogText,
  truncate,
  truncateText,
} from "./strings";

describe("String Utilities", () => {
  describe("truncate", () => {
    test("returns the string as is if length is less than the specified length", () => {
      expect(truncate("hello", 10)).toBe("hello");
    });

    test("truncates the string and adds ellipsis if length exceeds the specified length", () => {
      expect(truncate("hello world", 5)).toBe("hello...");
    });

    test("returns empty string if input is falsy", () => {
      expect(truncate("", 5)).toBe("");
    });

    test("handles exact length match correctly", () => {
      expect(truncate("hello", 5)).toBe("hello");
    });
  });

  describe("sanitizeString", () => {
    test("replaces special characters with delimiter", () => {
      expect(sanitizeString("hello@world")).toBe("hello_world");
    });

    test("keeps alphanumeric and allowed characters", () => {
      expect(sanitizeString("hello-world.123")).toBe("hello-world.123");
    });

    test("truncates string to specified length", () => {
      const longString = "a".repeat(300);
      expect(sanitizeString(longString)).toHaveLength(255);
    });

    test("uses custom delimiter when provided", () => {
      expect(sanitizeString("hello@world", "-")).toBe("hello-world");
    });

    test("uses custom length when provided", () => {
      expect(sanitizeString("hello world", "_", 5)).toBe("hello");
    });
  });

  describe("isCapitalized", () => {
    test("returns true for capitalized strings", () => {
      expect(isCapitalized("Hello")).toBe(true);
    });

    test("returns false for non-capitalized strings", () => {
      expect(isCapitalized("hello")).toBe(false);
    });

    test("handles single uppercase character", () => {
      expect(isCapitalized("A")).toBe(true);
    });

    test("handles single lowercase character", () => {
      expect(isCapitalized("a")).toBe(false);
    });
  });

  describe("startsWithVowel", () => {
    test("returns true for strings starting with lowercase vowels", () => {
      expect(startsWithVowel("apple")).toBe(true);
      expect(startsWithVowel("elephant")).toBe(true);
      expect(startsWithVowel("igloo")).toBe(true);
      expect(startsWithVowel("octopus")).toBe(true);
      expect(startsWithVowel("umbrella")).toBe(true);
    });

    test("returns true for strings starting with uppercase vowels", () => {
      expect(startsWithVowel("Apple")).toBe(true);
      expect(startsWithVowel("Elephant")).toBe(true);
      expect(startsWithVowel("Igloo")).toBe(true);
      expect(startsWithVowel("Octopus")).toBe(true);
      expect(startsWithVowel("Umbrella")).toBe(true);
    });

    test("returns false for strings starting with consonants", () => {
      expect(startsWithVowel("banana")).toBe(false);
      expect(startsWithVowel("Carrot")).toBe(false);
    });

    test("returns false for empty strings", () => {
      expect(startsWithVowel("")).toBe(false);
    });
  });

  describe("truncateText", () => {
    test("returns the string as is if length is less than the specified limit", () => {
      expect(truncateText("hello", 10)).toBe("hello");
    });

    test("truncates the string and adds ellipsis if length exceeds the specified limit", () => {
      expect(truncateText("hello world", 5)).toBe("hello...");
    });

    test("handles exact limit match correctly", () => {
      expect(truncateText("hello", 5)).toBe("hello");
    });
  });
});

describe("isStringMatch", () => {
  test("returns true for exact matches", () => {
    expect(isStringMatch("test", "test")).toBe(true);
  });

  test("returns true for case-insensitive matches", () => {
    expect(isStringMatch("TEST", "test")).toBe(true);
    expect(isStringMatch("test", "TEST")).toBe(true);
  });

  test("returns true for matches with spaces", () => {
    expect(isStringMatch("test case", "testcase")).toBe(true);
    expect(isStringMatch("testcase", "test case")).toBe(true);
  });

  test("returns true for matches with underscores", () => {
    expect(isStringMatch("test_case", "testcase")).toBe(true);
    expect(isStringMatch("testcase", "test_case")).toBe(true);
  });

  test("returns true for matches with dashes", () => {
    expect(isStringMatch("test-case", "testcase")).toBe(true);
    expect(isStringMatch("testcase", "test-case")).toBe(true);
  });

  test("returns true for partial matches", () => {
    expect(isStringMatch("test", "testing")).toBe(true);
  });

  test("returns false for non-matches", () => {
    expect(isStringMatch("test", "other")).toBe(false);
  });

  describe("toLogText", () => {
    test("replaces control and bidi-override characters, which could fake a log line", () => {
      expect(toLogText("a\nb\r\u0000c\u0085d\u202ee\u2066f", 100)).toBe("a?b??c?d?e?f");
      // Line and paragraph separators, which JSON.stringify leaves raw, and the bidi marks.
      expect(toLogText("a\u2028b\u2029c\u200ed\u200fe\u061cf", 100)).toBe("a?b?c?d?e?f");
    });

    test("caps the text at the limit", () => {
      expect(toLogText("x".repeat(10), 4)).toBe("xxxx...");
    });

    test("leaves out anything that is not a string", () => {
      expect(toLogText(undefined, 10)).toBeUndefined();
      expect(toLogText({ toString: () => "x" }, 10)).toBeUndefined();
    });
  });
});
