import { describe, expect, it } from "vitest";
import { PythonSyntaxProvider } from "./PythonSyntaxProvider";

const provider = new PythonSyntaxProvider();

describe("Python lexical syntax provider", () => {
  it("finds ordinary and Python-named captures without counting lookarounds", async () => {
    const result = await provider.parsePattern({
      flavour: "python",
      pattern: String.raw`(?P<word>\w+)(?:\s+)(?=(\d+))`,
      flags: [],
      options: {},
    });

    expect(result.accepted).toBe(true);
    expect(result.coverage.status).toBe("partial");
    expect(result.captures).toEqual([
      expect.objectContaining({ number: 1, name: "word" }),
      expect.objectContaining({ number: 2 }),
    ]);
  });

  it("ignores verbose comments, classes and escaped parentheses", async () => {
    const result = await provider.parsePattern({
      flavour: "python",
      pattern: String.raw`[(] \( # (ignored)
      (kept)`,
      flags: ["x"],
      options: {},
    });

    expect(result.captures).toHaveLength(1);
  });

  it("tokenizes Python capture replacement forms and engine escapes", async () => {
    const result = await provider.parseReplacement({
      flavour: "python",
      replacement: String.raw`\g<word>-\1-\n`,
      captureMetadata: [
        {
          number: 1,
          name: "word",
          range: { startUtf16: 0, endUtf16: 10 },
          repeated: false,
        },
      ],
    });

    expect(result.accepted).toBe(true);
    expect(result.tokens.map((token) => token.kind)).toEqual([
      "named-capture",
      "literal",
      "numbered-capture",
      "literal",
      "engine-special",
    ]);
  });
});
