import { describe, it, expect } from "vitest";
import { ZodError } from "zod";
import { getPageBySlugUseCase } from "@/application/use-cases/get-page-by-slug.use-case";
import { MockPagesRepository } from "@/infrastructure/repositories/pages.repository.mock";

describe("getPageBySlugUseCase", () => {
  it("returns the page when found", async () => {
    const repo = new MockPagesRepository();
    const useCase = getPageBySlugUseCase(repo);
    const result = await useCase({ slug: "about" });
    expect(result?.slug).toBe("about");
  });

  it("returns undefined when not found", async () => {
    const repo = new MockPagesRepository();
    const useCase = getPageBySlugUseCase(repo);
    const result = await useCase({ slug: "missing-page" });
    expect(result).toBeUndefined();
  });

  it("(R25) throws ZodError when repository returns malformed page data", async () => {
    const repo = new MockPagesRepository([
      {
        id: "p-bad",
        title: "",
        slug: "bad-page",
        hero: { heading: "" },
        layout: [],
        status: "published",
        publishedAt: null,
        seo: { title: "" },
        createdAt: new Date(),
        updatedAt: new Date(),
      } as never,
    ]);
    const useCase = getPageBySlugUseCase(repo);
    await expect(useCase({ slug: "bad-page" })).rejects.toBeInstanceOf(ZodError);
  });
});
