import { describe, expect, it } from "vitest";
import { parseResolverDoc } from "./parse-resolver-doc";

describe("parseResolverDoc", () => {
  it("parses module commands from list", () => {
    const markdown = `# createItem

## Overview

Creates a new item.

## Modules Commands Used

- \`item-management.createItem\` — Creates a new item record
- \`primitives.getUnit\` — Validates UoM reference
`;

    const result = parseResolverDoc("createItem", markdown);

    expect(result.resolverName).toBe("createItem");
    expect(result.moduleCommands).toEqual([
      { module: "item-management", command: "createItem" },
      { module: "primitives", command: "getUnit" },
    ]);
  });

  it("parses error codes from exception handling table", () => {
    const markdown = `# activateItem

## Overview

Activates an item.

## Modules Commands Used

- \`item-management.activateItem\` — Changes item status

## Exception Handling

| Error Code     | Description                       |
| -------------- | --------------------------------- |
| ITEM_NOT_FOUND | The specified item does not exist |
| INVALID_STATUS | Item is already in ACTIVE status  |
`;

    const result = parseResolverDoc("activateItem", markdown);

    expect(result.errors).toEqual([
      { code: "ITEM_NOT_FOUND", description: "The specified item does not exist" },
      { code: "INVALID_STATUS", description: "Item is already in ACTIVE status" },
    ]);
  });

  it("handles missing exception handling section", () => {
    const markdown = `# getMyProfile

## Overview

Gets user profile.

## Modules Commands Used

- \`user-management.getMyProfile\` — Fetches profile
`;

    const result = parseResolverDoc("getMyProfile", markdown);

    expect(result.resolverName).toBe("getMyProfile");
    expect(result.moduleCommands).toEqual([{ module: "user-management", command: "getMyProfile" }]);
    expect(result.errors).toEqual([]);
  });

  it("camelCases filename with leading uppercase", () => {
    const markdown = `# CreateItem

## Overview

Creates an item.

## Modules Commands Used

- \`item-management.createItem\` — Creates item
`;

    const result = parseResolverDoc("CreateItem", markdown);

    expect(result.resolverName).toBe("createItem");
  });
});
