import { describe, it } from "vitest";
import { RuleTester } from "eslint";
import rule from "./pii-declaration-must-be-complete.js";

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

describe("pii-declaration-must-be-complete", () => {
  it("passes when custom.pii has all required fields", () => {
    tester.run("pii-declaration-must-be-complete", rule, {
      valid: [
        {
          code: `
            const field = {
              slug: "email",
              type: "email",
              custom: {
                pii: {
                  category: "contact",
                  purpose: "authentication",
                  exportable: false,
                  restrictable: true,
                },
              },
            };
          `,
        },
      ],
      invalid: [],
    });
  });

  it("fires when category is missing", () => {
    tester.run("pii-declaration-must-be-complete", rule, {
      valid: [],
      invalid: [
        {
          code: `
            const field = {
              custom: {
                pii: {
                  purpose: "authentication",
                  exportable: false,
                  restrictable: true,
                },
              },
            };
          `,
          errors: [{ messageId: "missingField", data: { field: "category" } }],
        },
      ],
    });
  });

  it("fires when purpose is missing", () => {
    tester.run("pii-declaration-must-be-complete", rule, {
      valid: [],
      invalid: [
        {
          code: `
            const field = {
              custom: {
                pii: {
                  category: "contact",
                  exportable: false,
                  restrictable: true,
                },
              },
            };
          `,
          errors: [{ messageId: "missingField", data: { field: "purpose" } }],
        },
      ],
    });
  });

  it("fires when exportable is missing", () => {
    tester.run("pii-declaration-must-be-complete", rule, {
      valid: [],
      invalid: [
        {
          code: `
            const field = {
              custom: {
                pii: {
                  category: "contact",
                  purpose: "authentication",
                  restrictable: true,
                },
              },
            };
          `,
          errors: [
            { messageId: "missingField", data: { field: "exportable" } },
          ],
        },
      ],
    });
  });

  it("fires when restrictable is missing", () => {
    tester.run("pii-declaration-must-be-complete", rule, {
      valid: [],
      invalid: [
        {
          code: `
            const field = {
              custom: {
                pii: {
                  category: "contact",
                  purpose: "authentication",
                  exportable: false,
                },
              },
            };
          `,
          errors: [
            { messageId: "missingField", data: { field: "restrictable" } },
          ],
        },
      ],
    });
  });

  it("is a no-op when custom has no pii property", () => {
    tester.run("pii-declaration-must-be-complete", rule, {
      valid: [
        {
          code: `
            const field = {
              custom: {
                someOtherProperty: "value",
              },
            };
          `,
        },
      ],
      invalid: [],
    });
  });

  it("is a no-op when custom.pii is not an object", () => {
    tester.run("pii-declaration-must-be-complete", rule, {
      valid: [
        {
          code: `
            const field = {
              custom: {
                pii: true,
              },
            };
          `,
        },
      ],
      invalid: [],
    });
  });
});
