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

/**
 * Tests for the custom value sanitization logic used in the action handler.
 * This logic is inline in app._index.tsx — we extract and test the pattern here.
 */
function sanitizeCustomValue(input: string): string {
  return input
    .split(",")
    .map((v) => v.trim())
    .filter((v) => v.length > 0)
    .join(", ");
}

describe("sanitizeCustomValue", () => {
  it("trims whitespace around commas", () => {
    expect(sanitizeCustomValue("  foo ,  bar  , baz  ")).toBe("foo, bar, baz");
  });

  it("handles trailing comma", () => {
    expect(sanitizeCustomValue("foo, bar,")).toBe("foo, bar");
  });

  it("handles leading comma", () => {
    expect(sanitizeCustomValue(",foo, bar")).toBe("foo, bar");
  });

  it("handles multiple trailing commas", () => {
    expect(sanitizeCustomValue("foo,,,")).toBe("foo");
  });

  it("handles empty string", () => {
    expect(sanitizeCustomValue("")).toBe("");
  });

  it("handles single value", () => {
    expect(sanitizeCustomValue("foo")).toBe("foo");
  });

  it("handles value with spaces only", () => {
    expect(sanitizeCustomValue("  ,  ,  ")).toBe("");
  });

  it("preserves hyphens in handles", () => {
    expect(sanitizeCustomValue("mentions-legales, politique-de-confidentialite")).toBe(
      "mentions-legales, politique-de-confidentialite",
    );
  });

  it("preserves slashes in paths", () => {
    expect(sanitizeCustomValue("/pages/contact, /blogs/news")).toBe(
      "/pages/contact, /blogs/news",
    );
  });

  it("preserves paths with trailing slash", () => {
    expect(sanitizeCustomValue("/blogs/news/, /pages/about/")).toBe(
      "/blogs/news/, /pages/about/",
    );
  });
});

describe("MAX_CUSTOM_VALUE_LENGTH", () => {
  const MAX = 2000;

  it("rejects values over 2000 chars", () => {
    const longValue = "a".repeat(MAX + 1);
    expect(longValue.length).toBeGreaterThan(MAX);
  });

  it("accepts values at exactly 2000 chars", () => {
    const exactValue = "a".repeat(MAX);
    expect(exactValue.length).toBe(MAX);
  });
});
