fix(lint): rigid-size guard in both dims + direct tests for rotation_pivot_drift

Review fixes for the size-stability false positive and Fallow CRAP score.

- isRotationSizeStable now bounds the bbox ratio in BOTH width and height. The
  width-only guard let a top-anchored height-scaling element (fixed width,
  growing height) through: its AABB center moves on its own, so it emitted
  rotation_pivot_drift and recommended a destructive pivot rewrite.
- Factor the guard chain into named single-decision predicates
  (hasEnoughRotationSamples / isActuallySpinning / isRotationSizeStable /
  isSizableRotation) so detectRotationPivotDrift reads as a candidate filter +
  drift test. Drops the CRAP score (complexity + coverage).
- Export detectRotationPivotDrift and add direct positive/negative coverage for
  all five filter stages, including the height-scaling regression.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
xuanru
2026-07-23 07:17:23 +00:00
co-authored by Claude Opus 4.8
parent 341a5f3ec1
commit 6aff5734a1
2 changed files with 169 additions and 10 deletions
@@ -0,0 +1,115 @@
import { describe, expect, it } from "vitest";
import { detectRotationPivotDrift } from "./checkPipeline.js";
import type { RotationSample } from "./checkTypes.js";
const CANVAS = { width: 1000, height: 1000 };
/** One rotation sample; defaults describe a large, size-stable element. */
function sample(overrides: Partial<RotationSample> = {}): RotationSample {
return { time: 0, selector: "#spokes", cx: 250, cy: 250, w: 200, h: 200, angle: 0, ...overrides };
}
/** A group that SHOULD fire: spins (0→90→180), size-stable, sizable, and its
* bbox center travels 50px — the wrong-pivot signature. threshold here is
* max(0.1*200, 0.02*1000) = 20px, so 50px drift clears it. */
function driftingSpinner(): RotationSample[] {
return [
sample({ time: 0, angle: 0, cx: 250, cy: 250 }),
sample({ time: 1, angle: 90, cx: 250, cy: 280 }),
sample({ time: 2, angle: 180, cx: 250, cy: 300 }),
];
}
describe("detectRotationPivotDrift", () => {
it("fires on a spinning, size-stable element whose bbox center drifts past threshold", () => {
const findings = detectRotationPivotDrift(driftingSpinner(), CANVAS);
expect(findings).toHaveLength(1);
const [f] = findings;
expect(f?.code).toBe("rotation_pivot_drift");
expect(f?.severity).toBe("warning");
expect(f?.selector).toBe("#spokes");
expect(f?.message).toContain("50px");
expect(f?.fixHint).toContain("transformOrigin");
});
// Stage 1 — sample count.
it("does not fire with fewer than the minimum samples", () => {
const group = driftingSpinner().slice(0, 2);
expect(detectRotationPivotDrift(group, CANVAS)).toHaveLength(0);
});
// Stage 2 — real spin. A translating-but-not-spinning element is not our bug.
it("does not fire when the element barely rotates (fixed tilt, not spinning)", () => {
const group = [
sample({ time: 0, angle: 0, cx: 250, cy: 250 }),
sample({ time: 1, angle: 2, cx: 250, cy: 280 }),
sample({ time: 2, angle: 4, cx: 250, cy: 300 }),
];
expect(detectRotationPivotDrift(group, CANVAS)).toHaveLength(0);
});
// Stage 3 — size stability, WIDTH axis (scale/entrance, not pivot drift).
it("does not fire when width scales across samples", () => {
const group = [
sample({ time: 0, angle: 0, w: 100, cx: 250, cy: 250 }),
sample({ time: 1, angle: 90, w: 200, cx: 250, cy: 280 }),
sample({ time: 2, angle: 180, w: 300, cx: 250, cy: 300 }),
];
expect(detectRotationPivotDrift(group, CANVAS)).toHaveLength(0);
});
// Stage 3 — size stability, HEIGHT axis. Regression for the width-only guard:
// fixed width, top-anchored height growth (top=100 → cy = 100 + h/2) drifts
// the AABB center 50px on its own. Must NOT be reported as pivot drift.
it("does not fire when height scales (top-anchored) even though the AABB center moves", () => {
const group = [
sample({ time: 0, angle: 0, w: 100, h: 50, cx: 250, cy: 125 }),
sample({ time: 1, angle: 90, w: 100, h: 100, cx: 250, cy: 150 }),
sample({ time: 2, angle: 180, w: 100, h: 150, cx: 250, cy: 175 }),
];
expect(detectRotationPivotDrift(group, CANVAS)).toHaveLength(0);
});
it("does not fire when a sample has a degenerate zero dimension", () => {
const group = driftingSpinner().map((s, i) => (i === 0 ? { ...s, w: 0 } : s));
expect(detectRotationPivotDrift(group, CANVAS)).toHaveLength(0);
});
// Stage 4 — sizable. Tiny decorative spinners are ignored (area < 2500px²).
it("does not fire on a tiny element below the median-area floor", () => {
const group = driftingSpinner().map((s) => ({ ...s, w: 40, h: 40 }));
expect(detectRotationPivotDrift(group, CANVAS)).toHaveLength(0);
});
// Stage 5 — center drift. A correctly-centered spinner holds its bbox center.
it("does not fire on a spinner whose bbox center stays put", () => {
const group = [
sample({ time: 0, angle: 0 }),
sample({ time: 1, angle: 120 }),
sample({ time: 2, angle: 240 }),
];
expect(detectRotationPivotDrift(group, CANVAS)).toHaveLength(0);
});
it("uses the viewport floor when the element is small relative to a large canvas", () => {
// medianSize=200 → sizeFloor=20; viewportFloor on a 3000px canvas = 60.
// A 40px drift is below 60 → clean; the same group fired on CANVAS above.
const group = [
sample({ time: 0, angle: 0, cx: 250, cy: 250 }),
sample({ time: 1, angle: 90, cx: 250, cy: 270 }),
sample({ time: 2, angle: 180, cx: 250, cy: 290 }),
];
expect(detectRotationPivotDrift(group, { width: 3000, height: 3000 })).toHaveLength(0);
});
it("reports each drifting selector independently and leaves clean ones alone", () => {
const clean = driftingSpinner().map((s) => ({ ...s, selector: "#hub", cx: 500, cy: 500 }));
const findings = detectRotationPivotDrift([...driftingSpinner(), ...clean], CANVAS);
expect(findings.map((f) => f.selector)).toEqual(["#spokes"]);
});
it("returns nothing for an empty sample set", () => {
expect(detectRotationPivotDrift([], CANVAS)).toHaveLength(0);
});
});
+54 -10
View File
@@ -562,25 +562,69 @@ function rotationDriftFinding(
* (excludes scale/entrance animations), a sizable element, and honors
* `[data-layout-allow-orbit]` opt-outs (applied browser-side).
*/
function detectRotationPivotDrift(
function groupRotationSamplesBySelector(
samples: RotationSample[],
canvas: Canvas,
): AnchoredLayoutIssue[] {
): Map<string, RotationSample[]> {
const bySelector = new Map<string, RotationSample[]>();
for (const sample of samples) {
const group = bySelector.get(sample.selector);
if (group) group.push(sample);
else bySelector.set(sample.selector, [sample]);
}
return bySelector;
}
/** Enough samples to establish a spin trajectory (one frame can't). */
function hasEnoughRotationSamples(group: RotationSample[]): boolean {
return group.length >= ROTATION_MIN_SAMPLES;
}
/** Real spin, not a fixed tilt: the rotation angle actually varies across the grid. */
function isActuallySpinning(group: RotationSample[]): boolean {
return maxAngleSpread(group.map((s) => s.angle)) > ROTATION_MIN_ANGLE_SPREAD_DEG;
}
/** Rigid bbox size in BOTH dimensions. A scale/entrance animation is not pivot
* drift; in particular top-anchored height scaling (fixed width, growing height)
* moves the AABB center on its own — the earlier width-only guard let that through
* as a false positive. */
function isRotationSizeStable(group: RotationSample[]): boolean {
const widths = group.map((s) => s.w);
const heights = group.map((s) => s.h);
const minWidth = Math.min(...widths);
const minHeight = Math.min(...heights);
if (minWidth <= 0 || minHeight <= 0) return false;
return (
Math.max(...widths) / minWidth <= ROTATION_MAX_SIZE_RATIO &&
Math.max(...heights) / minHeight <= ROTATION_MAX_SIZE_RATIO
);
}
/** Skip tiny decorative spinners; only sizable rotating figures matter. */
function isSizableRotation(group: RotationSample[]): boolean {
return median(group.map((s) => s.w * s.h)) >= ROTATION_MIN_MEDIAN_AREA_PX;
}
/** The size/motion gates a selector group must clear before the (viewport-
* dependent) center-drift test. Each is a strict FP guard, deliberately so:
* a false positive feeds destructive downstream auto-fixes. */
function isRotationDriftCandidate(group: RotationSample[]): boolean {
return (
hasEnoughRotationSamples(group) &&
isActuallySpinning(group) &&
isRotationSizeStable(group) &&
isSizableRotation(group)
);
}
export function detectRotationPivotDrift(
samples: RotationSample[],
canvas: Canvas,
): AnchoredLayoutIssue[] {
const findings: AnchoredLayoutIssue[] = [];
const viewportFloor = ROTATION_DRIFT_VIEWPORT_FRACTION * Math.min(canvas.width, canvas.height);
for (const group of bySelector.values()) {
if (group.length < ROTATION_MIN_SAMPLES) continue;
if (maxAngleSpread(group.map((s) => s.angle)) <= ROTATION_MIN_ANGLE_SPREAD_DEG) continue;
const widths = group.map((s) => s.w);
const minWidth = Math.min(...widths);
if (minWidth <= 0 || Math.max(...widths) / minWidth > ROTATION_MAX_SIZE_RATIO) continue;
if (median(group.map((s) => s.w * s.h)) < ROTATION_MIN_MEDIAN_AREA_PX) continue;
for (const group of groupRotationSamplesBySelector(samples).values()) {
if (!isRotationDriftCandidate(group)) continue;
const medianSize = median(group.map((s) => Math.max(s.w, s.h)));
const threshold = Math.max(ROTATION_DRIFT_SIZE_FRACTION * medianSize, viewportFloor);
const drift = maxCenterDrift(group);