import { describe, it, expect } from "vitest";
import { createArticleController } from "@/interface-adapters/controllers/create-article.controller";
import { MockArticlesRepository } from "@/infrastructure/repositories/articles.repository.mock";
import { createArticleUseCase } from "@/application/use-cases/create-article.use-case";
import { InputParseError } from "@/entities/errors/common";

describe("createArticleController", () => {
  it("creates an article on valid input", async () => {
    const repo = new MockArticlesRepository();
    const useCase = createArticleUseCase(repo);
    const controller = createArticleController(useCase);

    const result = await controller({ title: "Hello", content: "body", authorId: "u1" });
    expect(result.title).toBe("Hello");
    expect(result.slug).toBe("hello");
    expect(result.status).toBe("draft");
  });

  it("throws InputParseError on missing title", async () => {
    const repo = new MockArticlesRepository();
    const useCase = createArticleUseCase(repo);
    const controller = createArticleController(useCase);

    await expect(
      controller({ content: "body", authorId: "u1" }),
    ).rejects.toBeInstanceOf(InputParseError);
  });

  it("throws InputParseError on missing authorId", async () => {
    const repo = new MockArticlesRepository();
    const useCase = createArticleUseCase(repo);
    const controller = createArticleController(useCase);

    await expect(
      controller({ title: "T" }),
    ).rejects.toBeInstanceOf(InputParseError);
  });
});
