import { describe, expect, it, vi } from "vitest";
import { deleteDemandRequirement } from "../index.js";

describe("deleteDemandRequirement", () => {
  it("deletes an explicit demand and unlinks assignments", async () => {
    const db = {
      demandRequirement: {
        findUnique: vi.fn().mockResolvedValue({
          id: "demand_1",
          projectId: "project_1",
        }),
        delete: vi.fn().mockResolvedValue({}),
      },
      assignment: {
        updateMany: vi.fn().mockResolvedValue({ count: 2 }),
      },
    };

    const result = await deleteDemandRequirement(db as never, "demand_1");

    expect(result).toEqual({
      deletedId: "demand_1",
      projectId: "project_1",
    });
    expect(db.assignment.updateMany).toHaveBeenCalledWith({
      where: { demandRequirementId: "demand_1" },
      data: { demandRequirementId: null },
    });
    expect(db.demandRequirement.delete).toHaveBeenCalledWith({
      where: { id: "demand_1" },
    });
  });
});
