import { describe, expect, inject, it } from "vitest";
import { createGraphQLClient, graphql } from "@/tests/utils/graphql-client";

const createCategoryIdOnly = graphql(`
  mutation CreateCategoryForEdit($code: String!, $name: String!) {
    createCategory(code: $code, name: $name) {
      id
    }
  }
`);

const updateCategory = graphql(`
  mutation UpdateCategory($id: ID!, $name: String!) {
    updateCategory(id: $id, name: $name) {
      id
    }
  }
`);

describe("product-manager--edit-category", () => {
  const gql = createGraphQLClient(inject("url"), inject("token"));

  it("Given a valid category ID and a new name, the category name is updated successfully", async () => {
    const code = `EDIT-${Date.now()}`;
    const createResult = await gql
      .mutation(createCategoryIdOnly, { code, name: "Original Name" })
      .toPromise();
    const categoryId = createResult.data!.createCategory.id;

    const result = await gql
      .mutation(updateCategory, { id: categoryId, name: "Updated Name" })
      .toPromise();

    expect(result.data?.updateCategory.id).toBe(categoryId);
  });

  it("Given a category ID that does not exist, the edit fails with a not found error", async () => {
    const fakeCategoryId = "00000000-0000-0000-0000-000000000000";

    await expect(
      gql.mutation(updateCategory, { id: fakeCategoryId, name: "No Such Category" }).toPromise(),
    ).rejects.toThrow();
  });
});
