import { describe, expect, it } from "vitest";
import { slugifyIfMissing } from "./slugify-if-missing";

describe("slugifyIfMissing", () => {
  it("derives slug from title on create when slug is empty", () => {
    const result = slugifyIfMissing({
      data: { title: "Hello World" },
      operation: "create",
    });
    expect(result?.slug).toBe("hello-world");
  });

  it("does not overwrite an existing slug", () => {
    const result = slugifyIfMissing({
      data: { title: "New Title", slug: "kept-slug" },
      operation: "create",
    });
    expect(result?.slug).toBe("kept-slug");
  });

  it("does nothing on update", () => {
    const result = slugifyIfMissing({
      data: { title: "Hello World" },
      operation: "update",
    });
    expect(result?.slug).toBeUndefined();
  });

  it("strips non-alphanumerics and trims hyphens", () => {
    const result = slugifyIfMissing({
      data: { title: "  Hello, World!! 2026  " },
      operation: "create",
    });
    expect(result?.slug).toBe("hello-world-2026");
  });

  it("returns data unchanged when title is missing", () => {
    const result = slugifyIfMissing({
      data: {},
      operation: "create",
    });
    expect(result?.slug).toBeUndefined();
  });
});
