import { describe, it, expect } from "vitest";
import { getPageBySlugController } from "@/interface-adapters/controllers/get-page-by-slug.controller";
import { getPageBySlugUseCase } from "@/application/use-cases/get-page-by-slug.use-case";
import { MockPagesRepository } from "@/infrastructure/repositories/pages.repository.mock";
import { InputParseError } from "@/entities/errors/common";

describe("getPageBySlugController", () => {
  it("returns the page when found", async () => {
    const repo = new MockPagesRepository();
    const useCase = getPageBySlugUseCase(repo);
    const controller = getPageBySlugController(useCase);

    const result = await controller({ slug: "about" });
    expect(result?.slug).toBe("about");
  });

  it("throws InputParseError on missing slug", async () => {
    const repo = new MockPagesRepository();
    const useCase = getPageBySlugUseCase(repo);
    const controller = getPageBySlugController(useCase);

    await expect(
      controller({}),
    ).rejects.toBeInstanceOf(InputParseError);
  });

  it("returns undefined when slug not found", async () => {
    const repo = new MockPagesRepository();
    const useCase = getPageBySlugUseCase(repo);
    const controller = getPageBySlugController(useCase);

    const result = await controller({ slug: "nonexistent" });
    expect(result).toBeUndefined();
  });
});
