import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { fixFixture } from "../utils";

const FIXTURES_DIR = join(import.meta.dirname, "fixtures");

describe("LSP code actions", () => {
  describe("basic code actions", () => {
    it.each([
      ["fix/test.ts", "typescript"],
      ["suggestion/test.ts", "typescript"],
      ["js-plugin-fix/test.js", "javascript"],
      ["js-plugin-suggestion/test.js", "javascript"],
    ])("should handle %s", async (path, languageId) => {
      expect(await fixFixture(FIXTURES_DIR, path, languageId)).toMatchSnapshot();
    });
  });

  describe("code actions hidden behind `fixKind` filter", () => {
    it.each([
      ["suggestion/test.ts", "typescript"],
      ["js-plugin-suggestion/test.js", "javascript"],
    ])("should handle %s", async (path, languageId) => {
      // because these rules are only suggestions, they should be hidden when `fixKind` is set to `safe_fix`. In that case,
      // the only code action valid for this test should be "ignore for this line" and "ignore for this file".
      expect(
        await fixFixture(FIXTURES_DIR, path, languageId, {
          fixKind: "safe_fix",
        }),
      ).toMatchSnapshot();
    });
  });

  describe("with code action only context", () => {
    it.each([
      ["quickfix"],
      ["source.fixAll"],
      ["source.fixAll.oxc"],
      ["source.fixAllDangerous.oxc"],
    ])("should handle %s", async (kind) => {
      expect(
        await fixFixture(
          FIXTURES_DIR,
          "context_only/test.ts",
          "typescript",
          // fix all dangerous needs a dangerous fix kind, otherwise it will return nothing.
          kind === "source.fixAllDangerous.oxc" ? { fixKind: "dangerous_fix" } : undefined,
          {
            diagnostics: [],
            only: [kind],
          },
        ),
      ).toMatchSnapshot();
    });
  });

  describe("rulesCustomization", () => {
    it("should hide rule fix in `source.fixAll.oxc` when rulesCustomization disables this rule", async () => {
      expect(
        await fixFixture(
          FIXTURES_DIR,
          "rules_customization/test.ts",
          "typescript",
          {
            rulesCustomization: {
              "no-debugger": {
                autofix: false,
              },
            },
          },
          {
            diagnostics: [],
            only: ["source.fixAll.oxc"],
          },
        ),
      ).toMatchSnapshot();
    });
  });
});
