From a04101a33482e1542add5e96e029f180132ecb74 Mon Sep 17 00:00:00 2001 From: Vishal Sharma Date: Tue, 24 Nov 2026 17:35:28 +0530 Subject: [PATCH] feat(cli): support visibleDuring assertion in motion spec Signed-off-by: Vishal Sharma --- docs/packages/cli.mdx | 1 + packages/cli/src/utils/motionAudit.test.ts | 64 ++++++++++++++++++- packages/cli/src/utils/motionAudit.ts | 33 ++++++++++ packages/cli/src/utils/motionSpec.test.ts | 39 +++++++++++ packages/cli/src/utils/motionSpec.ts | 19 +++++- skills-manifest.json | 2 +- .../references/lint-validate-inspect.md | 13 ++-- 7 files changed, 162 insertions(+), 9 deletions(-) diff --git a/docs/packages/cli.mdx b/docs/packages/cli.mdx index 02ccb68071e..31df20a9ee1 100644 --- a/docs/packages/cli.mdx +++ b/docs/packages/cli.mdx @@ -1667,6 +1667,7 @@ HTML. Without a sidecar, nothing changes. | `before(a, b)` | Broken stagger order — `a` must first appear strictly before `b` (`motion_out_of_order`) | | `staysInFrame(selector)` | Off-frame drift — once visible, the box never leaves the canvas (`motion_off_frame`) | | `keepsMoving(withinSelector?)` | A frozen shot — no fully static window longer than `maxStaticSec`, default 2s (`motion_frozen`); the film's last 3 s never count | +| `visibleDuring(selector, fromSec, toSec)` | Disappearing in window — element must remain visible (opacity ≥ 0.5) across `[fromSec, toSec]` (`motion_hidden_in_window`) | `keepsMoving` uses the same motion classifier as the frozen-sweep guard: box geometry, opacity, text and generated content, form-control state, painted CSS diff --git a/packages/cli/src/utils/motionAudit.test.ts b/packages/cli/src/utils/motionAudit.test.ts index 895e4df1f6e..29c0019f54a 100644 --- a/packages/cli/src/utils/motionAudit.test.ts +++ b/packages/cli/src/utils/motionAudit.test.ts @@ -184,6 +184,67 @@ describe("keepsMoving", () => { }); }); +describe("visibleDuring", () => { + const assertion: MotionAssertion = { + kind: "visibleDuring", + selector: "#limit", + fromSec: 2, + toSec: 4, + }; + + it("passes when the element stays visible throughout the window", () => { + const f = frames([1, 2, 3, 4, 5], () => ({ + data: { "#limit": visible() }, + })); + expect(evaluateMotion(f, [assertion], CANVAS)).toEqual([]); + }); + + it("flags an element that disappears mid-window", () => { + const f = frames([1, 2, 3, 4], (t) => ({ + data: { "#limit": t === 3 ? hidden : visible() }, + })); + const issue = expectOne(evaluateMotion(f, [assertion], CANVAS)); + expect(issue.code).toBe("motion_hidden_in_window"); + expect(issue.time).toBe(3); + expect(issue.message).toContain("3s"); + expect(issue.message).toContain("2s to 4s"); + }); + + it("flags an element whose opacity drops below appear threshold mid-window", () => { + const f = frames([2, 3, 4], (t) => ({ + data: { + "#limit": t === 3 ? { rect: rect(10, 10, 50, 50), opacity: 0.3, visible: true } : visible(), + }, + })); + const issue = expectOne(evaluateMotion(f, [assertion], CANVAS)); + expect(issue.code).toBe("motion_hidden_in_window"); + expect(issue.time).toBe(3); + expect(issue.message).toContain("opacity 0.3"); + }); + + it("flags an element that has not appeared by the window start", () => { + const f = frames([1, 2, 3, 4], (t) => ({ + data: { "#limit": t >= 3 ? visible() : hidden }, + })); + const issue = expectOne(evaluateMotion(f, [assertion], CANVAS)); + expect(issue.code).toBe("motion_hidden_in_window"); + expect(issue.time).toBe(2); + }); + + it("ignores invisibility outside the window", () => { + const f = frames([0.5, 1, 2, 3, 4, 5, 6], (t) => ({ + data: { "#limit": t >= 2 && t <= 4 ? visible() : hidden }, + })); + expect(evaluateMotion(f, [assertion], CANVAS)).toEqual([]); + }); + + it("flags a selector that matched no element in any frame", () => { + const f = frames([1, 2, 3, 4], () => ({ data: { "#limit": null } })); + const issue = expectOne(evaluateMotion(f, [assertion], CANVAS)); + expect(issue.code).toBe("motion_selector_missing"); + }); +}); + describe("evaluateMotion edge cases", () => { it("returns nothing for an empty frame set", () => { expect(evaluateMotion([], [{ kind: "appearsBy", selector: "#h", bySec: 1 }], CANVAS)).toEqual( @@ -198,10 +259,11 @@ describe("collectSamplingTargets", () => { { kind: "appearsBy", selector: "#h", bySec: 0.5 }, { kind: "before", a: "#h", b: "#cta" }, { kind: "staysInFrame", selector: ".card" }, + { kind: "visibleDuring", selector: "#limit", fromSec: 2, toSec: 4 }, { kind: "keepsMoving", withinSelector: ".scene" }, { kind: "keepsMoving" }, ]); - expect(targets.selectors.sort()).toEqual(["#cta", "#h", ".card"]); + expect(targets.selectors.sort()).toEqual(["#cta", "#h", "#limit", ".card"]); expect(targets.livenessScopes.sort()).toEqual(["*", ".scene"]); }); }); diff --git a/packages/cli/src/utils/motionAudit.ts b/packages/cli/src/utils/motionAudit.ts index 0790c2dd4a6..181774afa68 100644 --- a/packages/cli/src/utils/motionAudit.ts +++ b/packages/cli/src/utils/motionAudit.ts @@ -162,6 +162,36 @@ function staysInFrame(frames: MotionFrame[], selector: string, canvas: Canvas): return []; } +function visibleDuring( + frames: MotionFrame[], + selector: string, + fromSec: number, + toSec: number, +): LayoutIssue[] { + if (!everMatched(frames, selector)) return [missingIssue(selector, 0)]; + + for (const frame of frames) { + if (frame.time < fromSec || frame.time > toSec) continue; + const sample = frame.data[selector]; + if (sample && sample.visible && sample.opacity >= APPEAR_OPACITY) continue; + + return [ + { + code: "motion_hidden_in_window", + severity: "error", + time: frame.time, + selector, + message: sample + ? `${selector} is hidden at ${round(frame.time)}s (opacity ${round(sample.opacity)}) but must remain visible from ${round(fromSec)}s to ${round(toSec)}s` + : `${selector} disappears at ${round(frame.time)}s but must remain visible from ${round(fromSec)}s to ${round(toSec)}s`, + rect: sample ? sample.rect : ZERO_RECT, + fixHint: "Ensure the element stays on-screen with opacity >= 0.5 across the entire window.", + }, + ]; + } + return []; +} + export function frozenSpans( frames: MotionFrame[], scope: string, @@ -245,6 +275,8 @@ export function evaluateMotion( assertion.maxStaticSec ?? DEFAULT_MAX_STATIC_SEC, endCardStart, ); + case "visibleDuring": + return visibleDuring(frames, assertion.selector, assertion.fromSec, assertion.toSec); } }); } @@ -256,6 +288,7 @@ export function assertionTargets(assertion: MotionAssertion): { switch (assertion.kind) { case "appearsBy": case "staysInFrame": + case "visibleDuring": return { selectors: [assertion.selector], scopes: [] }; case "before": return { selectors: [assertion.a, assertion.b], scopes: [] }; diff --git a/packages/cli/src/utils/motionSpec.test.ts b/packages/cli/src/utils/motionSpec.test.ts index c66dd1026d3..27f4eab2142 100644 --- a/packages/cli/src/utils/motionSpec.test.ts +++ b/packages/cli/src/utils/motionSpec.test.ts @@ -102,6 +102,45 @@ describe("parseMotionSpec", () => { expect(result.ok).toBe(false); if (!result.ok) expect(result.errors[0]).toContain('"*"'); }); + + it("parses a valid visibleDuring assertion", () => { + const spec = expectOk( + parseMotionSpec({ + assertions: [{ kind: "visibleDuring", selector: "#limit", fromSec: 2, toSec: 4.5 }], + }), + ); + expect(spec.assertions[0]).toEqual({ + kind: "visibleDuring", + selector: "#limit", + fromSec: 2, + toSec: 4.5, + }); + }); + + it("rejects visibleDuring when toSec <= fromSec", () => { + const result = parseMotionSpec({ + assertions: [{ kind: "visibleDuring", selector: "#limit", fromSec: 3, toSec: 2 }], + }); + expect(result.ok).toBe(false); + if (!result.ok) expect(result.errors[0]).toContain("greater than"); + }); + + it("rejects visibleDuring when fromSec or toSec is negative or invalid", () => { + const result = parseMotionSpec({ + assertions: [ + { kind: "visibleDuring", selector: "#limit", fromSec: -1, toSec: 3 }, + { kind: "visibleDuring", selector: "#limit", fromSec: 1, toSec: -2 }, + { kind: "visibleDuring", selector: "" }, + ], + }); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.errors).toHaveLength(3); + expect(result.errors[0]).toContain("fromSec"); + expect(result.errors[1]).toContain("toSec"); + expect(result.errors[2]).toContain("selector"); + } + }); }); describe("findMotionSpec", () => { diff --git a/packages/cli/src/utils/motionSpec.ts b/packages/cli/src/utils/motionSpec.ts index e4e62c95e0d..6305701d7e0 100644 --- a/packages/cli/src/utils/motionSpec.ts +++ b/packages/cli/src/utils/motionSpec.ts @@ -11,7 +11,8 @@ export type MotionAssertion = | { kind: "appearsBy"; selector: string; bySec: number } | { kind: "before"; a: string; b: string } | { kind: "staysInFrame"; selector: string } - | { kind: "keepsMoving"; withinSelector?: string; maxStaticSec?: number }; + | { kind: "keepsMoving"; withinSelector?: string; maxStaticSec?: number } + | { kind: "visibleDuring"; selector: string; fromSec: number; toSec: number }; export interface MotionSpec { version?: number; @@ -65,6 +66,22 @@ const VALIDATORS: Record = { if (isPositive(raw.maxStaticSec)) assertion.maxStaticSec = raw.maxStaticSec; return assertion; }, + visibleDuring: (raw, at) => { + if (!isSelector(raw.selector)) + return `${at} (visibleDuring): "selector" must be a non-empty string`; + if (typeof raw.fromSec !== "number" || !Number.isFinite(raw.fromSec) || raw.fromSec < 0) + return `${at} (visibleDuring): "fromSec" must be a number >= 0`; + if (typeof raw.toSec !== "number" || !Number.isFinite(raw.toSec) || raw.toSec < 0) + return `${at} (visibleDuring): "toSec" must be a number >= 0`; + if (raw.toSec <= raw.fromSec) + return `${at} (visibleDuring): "toSec" must be greater than "fromSec"`; + return { + kind: "visibleDuring", + selector: raw.selector, + fromSec: raw.fromSec, + toSec: raw.toSec, + }; + }, }; function validateAssertion(raw: unknown, index: number): MotionAssertion | string { diff --git a/skills-manifest.json b/skills-manifest.json index c1649be3bb8..58e7c8835f4 100644 --- a/skills-manifest.json +++ b/skills-manifest.json @@ -10,7 +10,7 @@ "files": 87 }, "hyperframes-cli": { - "hash": "7d502c80e8be04c4", + "hash": "abad7afec0ee7ed8", "files": 14 }, "hyperframes-registry": { diff --git a/skills/hyperframes-cli/references/lint-validate-inspect.md b/skills/hyperframes-cli/references/lint-validate-inspect.md index 8b0eeed56a7..f0f8be47ca4 100644 --- a/skills/hyperframes-cli/references/lint-validate-inspect.md +++ b/skills/hyperframes-cli/references/lint-validate-inspect.md @@ -107,12 +107,13 @@ Drop a `*.motion.json` sidecar next to the composition (matching the html basena } ``` -| Assertion | Fails (code) when | -| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------- | -| `appearsBy(selector, bySec)` | not visible (opacity ≥ 0.5) by `bySec` — `motion_appears_late` | -| `before(a, b)` | `a` does not first appear strictly before `b` — `motion_out_of_order` | -| `staysInFrame(selector)` | once visible, its box leaves the canvas — `motion_off_frame` | -| `keepsMoving(withinSelector?)` | a fully-static window exceeds `maxStaticSec` (default 2s); the film's final 3 s (an end card) never count — `motion_frozen` | +| Assertion | Fails (code) when | +| ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | +| `appearsBy(selector, bySec)` | not visible (opacity ≥ 0.5) by `bySec` — `motion_appears_late` | +| `before(a, b)` | `a` does not first appear strictly before `b` — `motion_out_of_order` | +| `staysInFrame(selector)` | once visible, its box leaves the canvas — `motion_off_frame` | +| `keepsMoving(withinSelector?)` | a fully-static window exceeds `maxStaticSec` (default 2s); the film's final 3 s (an end card) never count — `motion_frozen` | +| `visibleDuring(selector, fromSec, toSec)` | element disappears or drops below appear threshold in window — `motion_hidden_in_window` | `duration`, `withinSelector`, and `maxStaticSec` are optional. Findings are **errors by default** and appear in the same human and `--json` output as layout findings. A selector that matches nothing is reported as `motion_selector_missing` rather than silently passing — a typo'd selector fails loudly. Use this in the feedback loop instead of eyeballing the render: assert what the motion is supposed to do, and let `check` tell you when the seek diverges from intent.