import { describe, it } from "vitest";
import { RuleTester } from "eslint";
import rule from "./atomic-tier-import-direction.js";

const tester = new RuleTester({
  languageOptions: {
    parser: await import("@typescript-eslint/parser"),
    ecmaVersion: "latest",
    sourceType: "module",
    parserOptions: { ecmaFeatures: { jsx: true } },
  },
});

describe("atomic-tier-import-direction", () => {
  it("passes when an organism imports from atoms", () => {
    tester.run("atomic-tier-import-direction", rule, {
      valid: [{
        filename: "/repo/packages/core-ui/src/organisms/Card/Card.tsx",
        code: `import { Button } from "../../atoms/Button/Button";`,
      }],
      invalid: [],
    });
  });

  it("fires when an atom imports from organisms", () => {
    tester.run("atomic-tier-import-direction", rule, {
      valid: [],
      invalid: [{
        filename: "/repo/packages/core-ui/src/atoms/Button/Button.tsx",
        code: `import { Card } from "../../organisms/Card/Card";`,
        errors: [{ messageId: "wrongDirection", data: { fromTier: "atoms", toTier: "organisms", importPath: "../../organisms/Card/Card" } }],
      }],
    });
  });

  it("is a no-op for files outside any tier folder", () => {
    tester.run("atomic-tier-import-direction", rule, {
      valid: [{
        filename: "/repo/packages/auth/src/application/use-cases/sign-in.use-case.ts",
        code: `export const x = 1;`,
      }],
      invalid: [],
    });
  });

  it("passes for same-tier imports", () => {
    tester.run("atomic-tier-import-direction", rule, {
      valid: [{
        filename: "/repo/packages/core-ui/src/molecules/SearchBar/SearchBar.tsx",
        code: `import { FormField } from "../FormField/FormField";`,
      }],
      invalid: [],
    });
  });
});
