import { describe, it, expect } from "vitest";
import { getArticlesController } from "@/interface-adapters/controllers/get-articles.controller";
import { MockArticlesRepository } from "@/infrastructure/repositories/articles.repository.mock";
import { getArticlesUseCase } from "@/application/use-cases/get-articles.use-case";
import { InputParseError } from "@/entities/errors/common";
import { articleFactory } from "@/__factories__/article.factory";

describe("getArticlesController", () => {
  it("returns array on valid input", async () => {
    const repo = new MockArticlesRepository();
    const useCase = getArticlesUseCase(repo);
    const controller = getArticlesController(useCase);

    const result = await controller({});
    expect(result).toEqual([]);
  });

  it("filters by status", async () => {
    const repo = new MockArticlesRepository();
    articleFactory.reset();
    await repo.createArticle(articleFactory.build({ id: "1", slug: "a", status: "draft" }));
    await repo.createArticle(articleFactory.build({ id: "2", slug: "b", status: "published" }));

    const useCase = getArticlesUseCase(repo);
    const controller = getArticlesController(useCase);

    const result = await controller({ status: "published" });
    expect(result).toHaveLength(1);
    expect(result[0]?.id).toBe("2");
  });

  it("throws InputParseError on invalid input shape", async () => {
    const repo = new MockArticlesRepository();
    const useCase = getArticlesUseCase(repo);
    const controller = getArticlesController(useCase);

    await expect(
      controller({ limit: "not a number" }),
    ).rejects.toBeInstanceOf(InputParseError);
  });
});
