import { describe, it, expect } from "vitest";
import {
  parseTestCasesFromDoc,
  parseItDescriptionsFromTest,
  parseScenarioPatternsFromDoc,
  parseDescribeBlocksFromSpec,
} from "./parse-doc-test-cases";

describe("parseTestCasesFromDoc", () => {
  it("extracts test cases from ## Test Cases section", () => {
    const content = `# CreateProduct

## Error Scenarios

- **DUPLICATE_CODE**: A product with the same code already exists

## Test Cases

- returns error when code is empty
- creates product in DRAFT status
- passes custom fields through
`;
    const cases = parseTestCasesFromDoc(content);
    expect(cases).toEqual([
      "returns error when code is empty",
      "creates product in DRAFT status",
      "passes custom fields through",
    ]);
  });

  it("returns empty array when no Test Cases section", () => {
    const content = `# CreateProduct\n\n## Overview\n\nSome text.`;
    expect(parseTestCasesFromDoc(content)).toEqual([]);
  });

  it("stops at next ## heading", () => {
    const content = `## Test Cases

- case one
- case two

## Other Section

- not a test case
`;
    expect(parseTestCasesFromDoc(content)).toEqual(["case one", "case two"]);
  });
});

describe("parseItDescriptionsFromTest", () => {
  it("extracts it() descriptions from test file", () => {
    const content = `
describe("createProduct", () => {
  it("returns error when code is empty", async () => {});
  it("creates product in DRAFT status", async () => {});
  it('passes custom fields through', async () => {});
});`;
    const descriptions = parseItDescriptionsFromTest(content);
    expect(descriptions).toEqual([
      "returns error when code is empty",
      "creates product in DRAFT status",
      "passes custom fields through",
    ]);
  });

  it("returns empty array when no it() blocks", () => {
    expect(parseItDescriptionsFromTest("const x = 1;")).toEqual([]);
  });
});

describe("parseScenarioPatternsFromDoc", () => {
  it("extracts patterns from ## Scenario Patterns section", () => {
    const content = `# Create Item

## Scenario Patterns

- Admin creates a new item in DRAFT status
- Admin creates an item with all optional fields

## Test Cases

- not a scenario
`;
    expect(parseScenarioPatternsFromDoc(content)).toEqual([
      "Admin creates a new item in DRAFT status",
      "Admin creates an item with all optional fields",
    ]);
  });

  it("returns empty array when no Scenario Patterns section", () => {
    expect(parseScenarioPatternsFromDoc("# Foo\n\n## Test Cases\n\n- bar\n")).toEqual([]);
  });
});

describe("parseDescribeBlocksFromSpec", () => {
  it("extracts describe blocks with their test names", () => {
    const content = `
describe("admin--create-item", () => {
  test("Admin creates a new item in DRAFT status", async () => {});
  test("Admin creates an item with all optional fields", async () => {});
});

describe("admin--activate-item", () => {
  test("Admin activates a DRAFT item to ACTIVE", async () => {});
});`;
    const blocks = parseDescribeBlocksFromSpec(content);
    expect(blocks.get("admin--create-item")).toEqual([
      "Admin creates a new item in DRAFT status",
      "Admin creates an item with all optional fields",
    ]);
    expect(blocks.get("admin--activate-item")).toEqual(["Admin activates a DRAFT item to ACTIVE"]);
  });

  it("returns empty map for file with no describe blocks", () => {
    expect(parseDescribeBlocksFromSpec("const x = 1;").size).toBe(0);
  });
});
