import { TRPCError } from "@trpc/server";
import { describe, expect, it, vi } from "vitest";
import {
  findTimelineProjectOrThrow,
  timelineBudgetStatusProjectSelect,
} from "../router/timeline-project-query-support.js";

describe("timeline project query support", () => {
  it("loads a project with the requested select", async () => {
    const project = { id: "project_1", name: "Project One", budgetCents: 120000 };
    const findUnique = vi.fn().mockResolvedValue(project);

    await expect(findTimelineProjectOrThrow({
      project: { findUnique },
    } as never, {
      projectId: "project_1",
      select: timelineBudgetStatusProjectSelect,
    })).resolves.toEqual(project);

    expect(findUnique).toHaveBeenCalledWith({
      where: { id: "project_1" },
      select: timelineBudgetStatusProjectSelect,
    });
  });

  it("throws a not found error when the project does not exist", async () => {
    const findUnique = vi.fn().mockResolvedValue(null);

    await expect(findTimelineProjectOrThrow({
      project: { findUnique },
    } as never, {
      projectId: "missing_project",
      select: timelineBudgetStatusProjectSelect,
    })).rejects.toThrowError(new TRPCError({
      code: "NOT_FOUND",
      message: "Project not found",
    }));
  });
});
