import { describe, expect, it } from "vitest";
import type { ConformToken, DesignSystemContext } from "@fragments-sdk/core";
import { conformSnippet } from "./engine.js";
import { scanWithFacts } from "../typestyle/check.js";

const brand: ConformToken = {
  value: "#2563eb",
  cssVar: "--brand-600",
  category: "color",
  label: "color.brand.600",
  tier: "canonical",
};

// A color token with no authored CSS variable (non-authoritative).
const accent: ConformToken = {
  value: "#10b981",
  category: "color",
  label: "color.accent",
};

function ctx(
  tokens: ConformToken[] = [brand],
  designSystemName: string | undefined = "Acme UI"
): DesignSystemContext {
  return { designSystemName, tokens };
}

describe("conformSnippet — color tokenization", () => {
  it("rewrites an exact-match color to the CSS variable (tsx)", () => {
    const result = conformSnippet({ code: '<div style={{ color: "#2563eb" }}>hi</div>' }, ctx());
    expect(result.changed).toBe(true);
    expect(result.conformed).toContain("var(--brand-600)");
    expect(result.conformed).not.toContain("#2563eb");
    expect(result.changes).toHaveLength(1);
    expect(result.changes[0]).toMatchObject({
      kind: "replace-token",
      ruleId: "styles/no-raw-color",
      before: "#2563eb",
      after: "var(--brand-600)",
      confidence: "high",
    });
  });

  it("rewrites an exact-match color in scss and css", () => {
    for (const filename of ["btn.scss", "theme.css"]) {
      const result = conformSnippet({ code: ".btn { color: #2563eb; }", filename }, ctx());
      expect(result.changed, filename).toBe(true);
      expect(result.conformed, filename).toContain("var(--brand-600)");
    }
  });

  it("matches short and uppercase hex", () => {
    const white: ConformToken = { value: "#ffffff", cssVar: "--white", category: "color" };
    expect(
      conformSnippet({ code: ".x { color: #FFF; }", filename: "x.scss" }, ctx([white])).conformed
    ).toContain("var(--white)");
    expect(
      conformSnippet({ code: ".x { color: #2563EB; }", filename: "x.scss" }, ctx()).conformed
    ).toContain("var(--brand-600)");
  });

  it("matches an 8-digit alpha hex", () => {
    const alpha: ConformToken = { value: "#2563ebff", cssVar: "--brand-a", category: "color" };
    expect(
      conformSnippet({ code: ".x { color: #2563EBFF; }", filename: "x.scss" }, ctx([alpha]))
        .conformed
    ).toContain("var(--brand-a)");
  });
});

describe("conformSnippet — apply modes", () => {
  it("apply:'none' reports the change without rewriting", () => {
    const code = '<div style={{ color: "#2563eb" }}>hi</div>';
    const result = conformSnippet({ code, apply: "none" }, ctx());
    expect(result.changed).toBe(false);
    expect(result.conformed).toBe(code);
    expect(result.changes).toHaveLength(1);
    expect(result.changes[0].after).toBe("var(--brand-600)");
  });

  it("applies by default and when apply:'deterministic' is explicit", () => {
    expect(
      conformSnippet({ code: ".x { color: #2563eb; }", filename: "x.scss" }, ctx()).changed
    ).toBe(true);
    expect(
      conformSnippet(
        { code: ".x { color: #2563eb; }", filename: "x.scss", apply: "deterministic" },
        ctx()
      ).changed
    ).toBe(true);
  });
});

describe("conformSnippet — trust boundary", () => {
  it("does NOT auto-apply a token without a CSS variable", () => {
    const result = conformSnippet(
      { code: ".x { color: #10b981; }", filename: "x.scss" },
      ctx([accent])
    );
    expect(result.changed).toBe(false);
    expect(result.conformed).toContain("#10b981");
    expect(result.changes).toHaveLength(0);
    expect(result.suggestions[0].hint).toContain("color.accent");
  });

  it("suggests, never rewrites, a color with no matching token", () => {
    const result = conformSnippet({ code: ".x { color: #abc123; }", filename: "x.scss" }, ctx());
    expect(result.changed).toBe(false);
    expect(result.suggestions[0].hint).toBe("No design token matches this color.");
  });

  it("ignores allow-listed keyword colors", () => {
    const result = conformSnippet(
      { code: ".x { color: transparent; }", filename: "x.scss" },
      ctx()
    );
    expect(result.changed).toBe(false);
    expect(result.changes).toHaveLength(0);
    expect(result.suggestions).toHaveLength(0);
  });

  it("refuses an ambiguous variable (one var, two values)", () => {
    const dupA: ConformToken = { value: "#2563eb", cssVar: "--dup", category: "color", label: "a" };
    const dupB: ConformToken = { value: "#ff0000", cssVar: "--dup", category: "color", label: "b" };
    const result = conformSnippet(
      { code: ".x { color: #2563eb; }", filename: "x.scss" },
      ctx([dupA, dupB])
    );
    expect(result.changed).toBe(false);
    expect(result.conformed).toContain("#2563eb");
  });
});

describe("conformSnippet — never emits broken code", () => {
  it("handles a value wrapped onto the next line (P0 regression)", () => {
    const code = ".btn {\n  color:\n    #2563eb;\n}\n";
    const result = conformSnippet({ code, filename: "btn.scss" }, ctx());
    expect(result.conformed).toContain("var(--brand-600)");
    expect(result.conformed).not.toContain("#2563eb");
    expect(result.conformed).not.toMatch(/:var\([^)]*\)\s*#/);
  });

  it("returns input unchanged for unparseable code without throwing", () => {
    const code = '<div style={{ color: "#2563eb"';
    const result = conformSnippet({ code }, ctx());
    expect(result.changed).toBe(false);
    expect(result.conformed).toBe(code);
    expect(result.changes).toHaveLength(0);
  });

  it("falls back gracefully when context access throws", () => {
    const exploding = {
      designSystemName: "X",
      get tokens(): ConformToken[] {
        throw new Error("boom");
      },
    } as unknown as DesignSystemContext;
    const result = conformSnippet({ code: ".x { color: #2563eb; }" }, exploding);
    expect(result.changed).toBe(false);
    expect(result.conformed).toBe(".x { color: #2563eb; }");
    expect(result.summary).toBe("Could not analyze this snippet; returned it unchanged.");
    expect(result.designSystem?.importsAdded).toEqual([]);
  });

  it("returns the input unchanged for an oversized snippet", () => {
    const code = `.x { color: #2563eb; }\n${"a".repeat(100_001)}`;
    const result = conformSnippet({ code, filename: "x.scss" }, ctx());
    expect(result.changed).toBe(false);
    expect(result.conformed).toBe(code);
    expect(result.summary).toContain("too large");
  });
});

describe("conformSnippet — result shape", () => {
  it("reports a clean summary when nothing is wrong", () => {
    const result = conformSnippet({ code: "<div>hi</div>" }, ctx());
    expect(result.changed).toBe(false);
    expect(result.summary).toBe("No design-system issues found in this snippet.");
  });

  it("grounds the design system name; falls back to a generic label", () => {
    const named = conformSnippet({ code: ".x { color: #2563eb; }", filename: "x.scss" }, ctx());
    expect(named.summary).toContain("Acme UI");

    const noName = conformSnippet(
      { code: ".x { color: #2563eb; }", filename: "x.scss" },
      { designSystemName: undefined, tokens: [brand] }
    );
    expect(noName.summary).toContain("design-system");
    expect(noName.summary).not.toContain("Acme UI");
  });

  it("pluralizes the review summary", () => {
    const result = conformSnippet(
      { code: ".x { color: #111111; background: #222222; }", filename: "x.scss" },
      ctx([])
    );
    expect(result.changes).toHaveLength(0);
    expect(result.summary).toContain("2 items to review");
  });

  it("produces contract-valid changes and suggestions", () => {
    const result = conformSnippet(
      { code: ".x { color: #2563eb; background: #abc123; }", filename: "x.scss" },
      ctx()
    );
    for (const change of result.changes) {
      expect(["replace-token"]).toContain(change.kind);
      expect(["error", "warning", "info"]).toContain(change.severity);
      expect(["high", "medium", "low"]).toContain(change.confidence);
    }
    for (const suggestion of result.suggestions) {
      expect(suggestion.kind).toBe("review-low-confidence");
      expect(["high", "medium", "low"]).toContain(suggestion.confidence);
    }
    expect(result.designSystem?.importsAdded).toEqual([]);
  });
});

const space4: ConformToken = {
  value: "16px",
  cssVar: "--space-4",
  category: "spacing",
  label: "spacing.4",
  tier: "canonical",
};

const space3: ConformToken = {
  value: "12px",
  cssVar: "--space-3",
  category: "spacing",
  label: "spacing.3",
  tier: "canonical",
};

const radiusSm: ConformToken = {
  value: "4px",
  cssVar: "--radius-sm",
  category: "radius",
  label: "radius.sm",
};

describe("conformSnippet — dimension tokenization", () => {
  it("rewrites an exact-match spacing value (scss)", () => {
    const result = conformSnippet(
      { code: ".x { padding: 16px; }", filename: "x.scss" },
      ctx([space4])
    );
    expect(result.changed).toBe(true);
    expect(result.conformed).toContain("var(--space-4)");
    expect(result.conformed).not.toContain("16px");
    expect(result.changes[0]).toMatchObject({
      kind: "replace-token",
      ruleId: "styles/no-raw-dimensions",
      before: "16px",
      after: "var(--space-4)",
      confidence: "high",
    });
  });

  it("rewrites a camelCase inline spacing prop (tsx)", () => {
    const result = conformSnippet(
      { code: '<div style={{ paddingTop: "16px" }}>hi</div>' },
      ctx([space4])
    );
    expect(result.changed).toBe(true);
    expect(result.conformed).toContain("var(--space-4)");
  });

  it("rewrites exact-match spacing shorthand parts as one safe value", () => {
    const result = conformSnippet(
      { code: '<button style={{ padding: "12px 16px" }}>Send</button>' },
      ctx([space3, space4])
    );
    expect(result.changed).toBe(true);
    expect(result.conformed).toContain('padding: "var(--space-3) var(--space-4)"');
    expect(result.conformed).not.toContain("12px 16px");
    expect(result.changes[0]).toMatchObject({
      kind: "replace-token",
      ruleId: "styles/no-raw-dimensions",
      before: "12px 16px",
      after: "var(--space-3) var(--space-4)",
      confidence: "high",
    });
  });

  it("matches rem against rem tokens (same unit)", () => {
    const remToken: ConformToken = {
      value: "1rem",
      cssVar: "--space-4",
      category: "spacing",
    };
    expect(
      conformSnippet({ code: ".x { margin: 1rem; }", filename: "x.scss" }, ctx([remToken]))
        .conformed
    ).toContain("var(--space-4)");
  });

  it("never cross-converts units (1rem must not auto-match a 16px token)", () => {
    // 1rem only equals 16px under a 16px root we cannot guarantee.
    const result = conformSnippet(
      { code: ".x { margin: 1rem; }", filename: "x.scss" },
      ctx([space4])
    );
    expect(result.changed).toBe(false);
    expect(result.conformed).toContain("1rem");
  });

  it("routes radius properties to radius tokens", () => {
    const result = conformSnippet(
      { code: ".x { border-radius: 4px; }", filename: "x.scss" },
      ctx([radiusSm, space4])
    );
    expect(result.conformed).toContain("var(--radius-sm)");
  });

  it("suggests, never rewrites, an off-scale dimension", () => {
    const result = conformSnippet(
      { code: ".x { padding: 15px; }", filename: "x.scss" },
      ctx([space4])
    );
    expect(result.changed).toBe(false);
    expect(result.conformed).toContain("15px");
    expect(result.suggestions[0].hint).toBe("No design token matches this dimension.");
  });

  it("does not touch properties outside the dimension allow-list", () => {
    const result = conformSnippet(
      { code: ".x { width: 16px; }", filename: "x.scss" },
      ctx([space4])
    );
    expect(result.changed).toBe(false);
    expect(result.conformed).toContain("16px");
    expect(result.changes).toHaveLength(0);
    expect(result.suggestions).toHaveLength(0);
  });

  it("tokenizes color and dimension together in one snippet", () => {
    const result = conformSnippet(
      { code: ".x { color: #2563eb; padding: 16px; }", filename: "x.scss" },
      ctx([brand, space4])
    );
    expect(result.changes).toHaveLength(2);
    expect(result.conformed).toContain("var(--brand-600)");
    expect(result.conformed).toContain("var(--space-4)");
  });

  it("applies a 0 fix even when the var name echoes the raw value", () => {
    // `0` reappears inside `var(--space-0)`; the safety net must not reject it.
    const space0: ConformToken = {
      value: "0",
      cssVar: "--space-0",
      category: "spacing",
      label: "spacing.0",
    };
    const result = conformSnippet(
      { code: ".x { padding: 0; }", filename: "x.scss" },
      ctx([space0])
    );
    expect(result.changed).toBe(true);
    expect(result.conformed).toContain("var(--space-0)");
  });
});

describe("conformSnippet — component mappings", () => {
  it("scanWithFacts emits component findings from canonical mappings", () => {
    const scan = scanWithFacts({
      facts: {
        governConfig: {
          severity: "warn",
          rules: {
            "components/prefer-library": {
              enabled: true,
              severity: "warn",
              options: {
                canonicalMappings: [
                  {
                    name: "Button",
                    canonical: "Button",
                    htmlEquivalent: "button",
                    importPath: "@acme/ui",
                  },
                ],
              },
            },
          },
        },
      },
      files: [
        {
          absPath: "/snippet.tsx",
          relPath: "snippet.tsx",
          content: "<button>Send</button>",
        },
      ],
    });

    expect(scan.findings).toEqual([
      expect.objectContaining({
        ruleId: "components/prefer-library",
        fix: expect.objectContaining({ kind: "replaceComponent" }),
      }),
    ]);
  });

  it("swaps a confirmed native element to the canonical component and adds its import", () => {
    const result = conformSnippet(
      {
        code: '<button className="primary" style={{ color: "#2563eb" }}>Send</button>',
        filename: "ButtonDemo.tsx",
      },
      {
        designSystemName: "Acme UI",
        tokens: [brand],
        components: [
          {
            name: "Button",
            canonical: "Button",
            htmlEquivalent: "button",
            importPath: "@acme/ui",
            propMapping: [{ rawProp: "className", canonicalProp: "variant" }],
            confidence: 0.94,
          },
        ],
      }
    );

    expect(result.changed).toBe(true);
    expect(result.conformed).toContain('import { Button } from "@acme/ui";');
    expect(result.conformed).toContain("<Button");
    expect(result.conformed).toContain("</Button>");
    expect(result.conformed).toContain('variant="primary"');
    expect(result.conformed).toContain("var(--brand-600)");
    expect(result.designSystem?.importsAdded).toEqual(['import { Button } from "@acme/ui";']);
    expect(result.changes).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ kind: "swap-component", canonical: "Button" }),
        expect.objectContaining({ kind: "rename-prop", before: "className", after: "variant" }),
        expect.objectContaining({ kind: "rewrite-import", canonical: "Button" }),
        expect.objectContaining({ kind: "replace-token", after: "var(--brand-600)" }),
      ])
    );
  });

  it("renames props for multiple same-line component swaps after shifted columns", () => {
    const result = conformSnippet(
      {
        code: 'export const Demo = () => <><div className="one" /><div className="two" /></>;',
        filename: "StackDemo.tsx",
      },
      {
        tokens: [],
        components: [
          {
            name: "Stack",
            canonical: "Stack",
            htmlEquivalent: "div",
            importPath: "@acme/ui",
            propMapping: [{ rawProp: "className", canonicalProp: "variant" }],
          },
        ],
      }
    );

    expect(result.conformed).toContain('<Stack variant="one" />');
    expect(result.conformed).toContain('<Stack variant="two" />');
    expect(result.conformed).not.toContain("className");
    expect(result.changes.filter((change) => change.kind === "rename-prop")).toHaveLength(2);
  });

  it("does not duplicate an existing canonical import", () => {
    const result = conformSnippet(
      {
        code: 'import { Button } from "@acme/ui";\n\n<button type="button">Send</button>',
        filename: "ButtonDemo.tsx",
      },
      {
        tokens: [],
        components: [
          {
            name: "Button",
            canonical: "Button",
            htmlEquivalent: "button",
            importPath: "@acme/ui",
          },
        ],
      }
    );

    expect(result.conformed.match(/import \{ Button \} from "@acme\/ui";/g)).toHaveLength(1);
    expect(result.designSystem?.importsAdded).toEqual([]);
  });
});

describe("conformSnippet — honest summary", () => {
  it("summarizes a component swap as a swap, not a token mapping", () => {
    const result = conformSnippet(
      { code: '<button type="button">Send</button>', filename: "Demo.tsx" },
      {
        designSystemName: "Acme UI",
        tokens: [],
        components: [
          { name: "Button", canonical: "Button", htmlEquivalent: "button", importPath: "@acme/ui" },
        ],
      }
    );
    expect(result.changes.some((change) => change.kind === "swap-component")).toBe(true);
    expect(result.summary).not.toContain("hardcoded value");
    expect(result.summary.toLowerCase()).toContain("swapped");
    expect(result.summary).toContain("Acme UI");
  });

  it("separately counts token mappings and component swaps in one snippet", () => {
    const result = conformSnippet(
      {
        code: '<button style={{ color: "#2563eb" }}>Send</button>',
        filename: "Demo.tsx",
      },
      {
        designSystemName: "Acme UI",
        tokens: [brand],
        components: [
          { name: "Button", canonical: "Button", htmlEquivalent: "button", importPath: "@acme/ui" },
        ],
      }
    );
    expect(result.summary).toContain("1 hardcoded value");
    expect(result.summary.toLowerCase()).toContain("swapped 1");
  });
});

describe("conformSnippet — coverage", () => {
  it("reports zero coverage and evaluated=false for an empty catalog", () => {
    const result = conformSnippet({ code: "<div>hi</div>" }, { tokens: [] });
    expect(result.coverage).toEqual({ tokens: 0, components: 0, evaluated: false });
  });

  it("reports token coverage and evaluated=true when tokens are present", () => {
    const result = conformSnippet({ code: "<div>hi</div>" }, ctx([brand]));
    expect(result.coverage).toMatchObject({ tokens: 1, evaluated: true });
  });

  it("counts usable component mappings toward coverage", () => {
    const result = conformSnippet(
      { code: "<div>hi</div>" },
      {
        tokens: [],
        components: [{ name: "Button", htmlEquivalent: "button", importPath: "@acme/ui" }],
      }
    );
    expect(result.coverage).toMatchObject({ components: 1, evaluated: true });
  });

  it("ignores component mappings that are not usable", () => {
    const result = conformSnippet(
      { code: "<div>hi</div>" },
      { tokens: [], components: [{ name: "Button" }] }
    );
    expect(result.coverage).toEqual({ tokens: 0, components: 0, evaluated: false });
  });
});

describe("conformSnippet — cross-library prop value translation", () => {
  it("translates a confirmed prop value alongside the prop rename", () => {
    const result = conformSnippet(
      { code: '<button color="primary">Send</button>', filename: "Demo.tsx" },
      {
        designSystemName: "Acme UI",
        tokens: [],
        components: [
          {
            name: "Button",
            htmlEquivalent: "button",
            importPath: "@acme/ui",
            propMapping: [
              {
                rawProp: "color",
                canonicalProp: "variant",
                valueMap: { primary: "brand", secondary: "muted" },
              },
            ],
          },
        ],
      }
    );
    expect(result.conformed).toContain('variant="brand"');
    expect(result.conformed).not.toContain('color="primary"');
    const rename = result.changes.find((change) => change.kind === "rename-prop");
    expect(rename?.message).toContain('"primary"');
    expect(rename?.message).toContain('"brand"');
  });

  it("translates a value-only mapping where the prop name is unchanged", () => {
    const result = conformSnippet(
      { code: '<button size="large">Send</button>', filename: "Demo.tsx" },
      {
        tokens: [],
        components: [
          {
            name: "Button",
            htmlEquivalent: "button",
            importPath: "@acme/ui",
            propMapping: [{ rawProp: "size", canonicalProp: "size", valueMap: { large: "lg" } }],
          },
        ],
      }
    );
    expect(result.conformed).toContain('size="lg"');
    expect(result.conformed).not.toContain('size="large"');
  });

  it("suggests, never guesses, a value with no confirmed translation", () => {
    const result = conformSnippet(
      { code: '<button color="tertiary">Send</button>', filename: "Demo.tsx" },
      {
        tokens: [],
        components: [
          {
            name: "Button",
            htmlEquivalent: "button",
            importPath: "@acme/ui",
            propMapping: [
              { rawProp: "color", canonicalProp: "variant", valueMap: { primary: "brand" } },
            ],
          },
        ],
      }
    );
    // The unknown value is preserved and the unsafe rename is withheld.
    expect(result.conformed).toContain('color="tertiary"');
    expect(result.conformed).not.toContain('variant="tertiary"');
    expect(result.suggestions).toEqual(
      expect.arrayContaining([expect.objectContaining({ kind: "map-prop-value" })])
    );
  });

  it("suggests mapping a dynamic prop value rather than rewriting it", () => {
    const result = conformSnippet(
      { code: "<button color={tone}>Send</button>", filename: "Demo.tsx" },
      {
        tokens: [],
        components: [
          {
            name: "Button",
            htmlEquivalent: "button",
            importPath: "@acme/ui",
            propMapping: [
              { rawProp: "color", canonicalProp: "variant", valueMap: { primary: "brand" } },
            ],
          },
        ],
      }
    );
    expect(result.conformed).toContain("color={tone}");
    expect(result.suggestions).toEqual(
      expect.arrayContaining([expect.objectContaining({ kind: "map-prop-value" })])
    );
  });
});

describe("conformSnippet — accessibility gaps", () => {
  const noCatalog = { tokens: [], components: [] };

  it("flags an icon-only button with no accessible name", () => {
    const result = conformSnippet(
      { code: "<button><Icon /></button>", filename: "Demo.tsx" },
      noCatalog
    );
    expect(result.suggestions).toEqual(
      expect.arrayContaining([expect.objectContaining({ kind: "flag-accessibility" })])
    );
  });

  it("does not flag a button that has a visible text label", () => {
    const result = conformSnippet(
      { code: "<button>Save</button>", filename: "Demo.tsx" },
      noCatalog
    );
    expect(result.suggestions).toEqual([]);
  });

  it("does not flag a button named by an aria-label", () => {
    const result = conformSnippet(
      { code: '<button aria-label="Close"><Icon /></button>', filename: "Demo.tsx" },
      noCatalog
    );
    expect(result.suggestions).toEqual([]);
  });

  it("flags an image with no alt attribute", () => {
    const result = conformSnippet(
      { code: '<img src="logo.png" />', filename: "Demo.tsx" },
      noCatalog
    );
    expect(result.suggestions).toEqual(
      expect.arrayContaining([expect.objectContaining({ kind: "flag-accessibility" })])
    );
  });

  it("treats an explicit empty alt as an intentional decorative image", () => {
    const result = conformSnippet(
      { code: '<img src="spacer.png" alt="" />', filename: "Demo.tsx" },
      noCatalog
    );
    expect(result.suggestions).toEqual([]);
  });

  it("does not flag an aria-hidden icon button", () => {
    const result = conformSnippet(
      { code: '<button aria-hidden="true"><Icon /></button>', filename: "Demo.tsx" },
      noCatalog
    );
    expect(result.suggestions).toEqual([]);
  });

  it("never auto-rewrites code to fix an accessibility gap", () => {
    const result = conformSnippet(
      { code: "<button><Icon /></button>", filename: "Demo.tsx" },
      noCatalog
    );
    expect(result.changed).toBe(false);
    expect(result.conformed).toBe("<button><Icon /></button>");
  });

  it("does not emit accessibility flags for a CSS snippet", () => {
    const result = conformSnippet(
      { code: ".x { color: #2563eb; }", filename: "x.scss" },
      noCatalog
    );
    expect(result.suggestions).not.toContainEqual(
      expect.objectContaining({ kind: "flag-accessibility" })
    );
  });
});

describe("conformSnippet — never silently drops a governance finding", () => {
  it("surfaces every policy finding as a change, suggestion, or unresolved", () => {
    // One tokenizable color (becomes a change) and one off-scale color (becomes
    // a suggestion). The engine must not hide either from the agent.
    const result = conformSnippet(
      { code: ".x { color: #2563eb; background: #abc123; }", filename: "x.scss" },
      ctx([brand])
    );
    const policyScan = scanWithFacts({
      facts: {
        governConfig: {
          severity: "warn",
          rules: {},
          styles: [
            {
              kind: "style.rawColors.forbid",
              except: ["transparent", "currentColor", "inherit", "initial", "unset", "none"],
              prefer: "token",
              severity: "warn",
            },
          ],
        },
      },
      files: [{ absPath: "/x.scss", relPath: "x.scss", content: ".x { color: #2563eb; background: #abc123; }" }],
    });
    const surfaced =
      result.changes.length + result.suggestions.length + result.unresolved.length;
    expect(policyScan.findings.length).toBeGreaterThan(0);
    expect(surfaced).toBeGreaterThanOrEqual(policyScan.findings.length);
  });
});
