import { describe, expect, it } from "vitest";
import { articleSchema, articleStatusSchema, type Article } from "./article";

describe("articleSchema", () => {
  it("accepts a minimal valid article with default status", () => {
    const result = articleSchema.parse({
      id: "abc",
      title: "Hello",
      slug: "hello",
      content: { type: "doc", children: [] },
      authorId: "u1",
      createdAt: new Date(),
      updatedAt: new Date(),
    });
    expect(result.status).toBe("draft");
  });

  it("accepts unknown rich-text content", () => {
    const result = articleSchema.parse({
      id: "abc",
      title: "Hello",
      slug: "hello",
      content: "any string is also fine",
      authorId: "u1",
      createdAt: new Date(),
      updatedAt: new Date(),
    });
    expect(result.content).toBe("any string is also fine");
  });

  it("rejects empty title", () => {
    expect(() =>
      articleSchema.parse({
        id: "a",
        title: "",
        slug: "s",
        content: null,
        authorId: "u",
        createdAt: new Date(),
        updatedAt: new Date(),
      }),
    ).toThrow();
  });

  it("rejects title over 255 chars", () => {
    expect(() =>
      articleSchema.parse({
        id: "a",
        title: "x".repeat(256),
        slug: "s",
        content: null,
        authorId: "u",
        createdAt: new Date(),
        updatedAt: new Date(),
      }),
    ).toThrow();
  });
});

describe("articleStatusSchema", () => {
  it("accepts 'draft' and 'published'", () => {
    expect(articleStatusSchema.parse("draft")).toBe("draft");
    expect(articleStatusSchema.parse("published")).toBe("published");
  });

  it("rejects unknown status", () => {
    expect(() => articleStatusSchema.parse("archived")).toThrow();
  });
});

describe("Article type", () => {
  it("widens content to unknown", () => {
    const _example: Article = {
      id: "x",
      title: "t",
      slug: "s",
      content: { whatever: true },
      status: "draft",
      authorId: "u",
      createdAt: new Date(),
      updatedAt: new Date(),
    };
    expect(_example).toBeDefined();
  });
});
