import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import { Transaction } from "../generated/kysely-tailordb";
import { AssignmentNotFoundError } from "../lib/errors.generated";
import { baseAssignment } from "../testing/fixtures";
import { run } from "./removeItemFromTaxonomy";

describe("removeItemFromTaxonomy", () => {
  it("returns error when assignment does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(db, { itemId: "item-99", taxonomyNodeId: "node-99" });
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(AssignmentNotFoundError);
    }
  });

  it("removes assignment successfully", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseAssignment);
    spies.delete.mockReturnValue(undefined);

    const result = await run(db, {
      itemId: baseAssignment.itemId,
      taxonomyNodeId: baseAssignment.taxonomyNodeId,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.success).toBe(true);
    }
    expect(spies.delete).toHaveBeenCalled();
  });
});
