import { describe, expect, it } from "vitest";
import { makeSlug, parseTags, estimateReadMinutes, isValidCategory, statusOf } from "./blog";

describe("blog helpers", () => {
  describe("makeSlug", () => {
    it("converts vietnamese title to ASCII hyphenated", () => {
      expect(makeSlug("Tử Vi Đẩu Số là gì?")).toBe("tu-vi-dau-so-la-gi");
    });

    it("strips special chars", () => {
      expect(makeSlug("Hello, World! 2026")).toBe("hello-world-2026");
    });

    it("caps at 100 chars", () => {
      const long = "a".repeat(200);
      expect(makeSlug(long).length).toBeLessThanOrEqual(100);
    });

    it("handles empty/whitespace input", () => {
      expect(makeSlug("   ")).toBe("");
    });
  });

  describe("parseTags", () => {
    it("splits comma-separated", () => {
      expect(parseTags("tu vi, phong thuy, menh hoc")).toEqual(["tu vi", "phong thuy", "menh hoc"]);
    });

    it("dedupes and trims", () => {
      expect(parseTags("a, a , b,  c , b")).toEqual(["a", "b", "c"]);
    });

    it("handles newline separator", () => {
      expect(parseTags("a\nb,c")).toEqual(["a", "b", "c"]);
    });

    it("caps at 20", () => {
      const many = Array.from({ length: 30 }, (_, i) => `tag${i}`).join(",");
      expect(parseTags(many)).toHaveLength(20);
    });
  });

  describe("estimateReadMinutes", () => {
    it("returns 1 for short content", () => {
      expect(estimateReadMinutes("Just a few words.")).toBe(1);
    });

    it("scales with word count (~200 wpm)", () => {
      const text = Array.from({ length: 600 }, () => "word").join(" ");
      expect(estimateReadMinutes(text)).toBe(3);
    });

    it("never returns 0", () => {
      expect(estimateReadMinutes("")).toBe(1);
    });
  });

  describe("isValidCategory", () => {
    it("accepts known keys", () => {
      expect(isValidCategory("tu-vi")).toBe(true);
      expect(isValidCategory("phong-thuy")).toBe(true);
    });

    it("rejects unknown", () => {
      expect(isValidCategory("xyz")).toBe(false);
      expect(isValidCategory(null)).toBe(false);
    });
  });

  describe("statusOf", () => {
    it("draft when publishedAt null", () => {
      expect(statusOf(null)).toBe("draft");
      expect(statusOf(undefined)).toBe("draft");
    });
    it("published when date set", () => {
      expect(statusOf(new Date())).toBe("published");
    });
  });
});
