import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) =>
      run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseAssignment]);

    const result = await mock.withTx((trx) =>
      run(trx, {
        itemId: baseAssignment.itemId,
        taxonomyNodeId: baseAssignment.taxonomyNodeId,
      }),
    );

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.success).toBe(true);
    }
    expect(mock.deletes.length).toBeGreaterThan(0);
  });
});
