import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseAttributeAssignment } from "../testing/fixtures";
import { run } from "./listAttributeAssignmentsByAttribute";

describe("listAttributeAssignmentsByAttribute", () => {
  it("returns assignments when found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseAttributeAssignment]);

    const result = await run(mock.db, { attributeId: "attr-1" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.assignments).toEqual([baseAttributeAssignment]);
    }

    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].sql).toMatch(/"attributeId"/);
    expect(mock.selects[0].parameters).toContain("attr-1");
  });

  it("returns empty array when no assignments", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { attributeId: "attr-3" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.assignments).toEqual([]);
    }
  });
});
