diff --git a/packages/parsers/src/animationRuntimeDetection.test.ts b/packages/parsers/src/animationRuntimeDetection.test.ts new file mode 100644 index 0000000000..e1f36d0902 --- /dev/null +++ b/packages/parsers/src/animationRuntimeDetection.test.ts @@ -0,0 +1,115 @@ +import { describe, expect, it } from "vitest"; +import { classifyAnimationRuntime } from "./animationRuntimeDetection.js"; + +describe("classifyAnimationRuntime", () => { + it("classifies a pure GSAP file as gsap", () => { + const html = ` + + + + +`; + + const result = classifyAnimationRuntime(html); + + expect(result.verdict).toBe("gsap"); + expect(result.blocks).toMatchObject([{ engine: "gsap", insideTemplate: false }]); + expect(html.slice(result.blocks[0]?.start, result.blocks[0]?.end)).toContain("gsap.timeline"); + }); + + it("classifies a pure anime.js file as animejs", () => { + const result = classifyAnimationRuntime(` + + + + +`); + + expect(result.verdict).toBe("animejs"); + expect(result.blocks).toMatchObject([{ engine: "animejs", insideTemplate: false }]); + }); + + it("classifies files with both engines as mixed and preserves per-block tags", () => { + const result = classifyAnimationRuntime(` + + + + + +`); + + expect(result.verdict).toBe("mixed"); + expect(result.blocks.map((block) => block.engine)).toEqual(["mixed", "gsap"]); + }); + + it("classifies helper-only anime registration without the anime global as animejs", () => { + const result = classifyAnimationRuntime(` + + + + +`); + + expect(result.verdict).toBe("animejs"); + expect(result.blocks).toMatchObject([{ engine: "animejs" }]); + }); + + it("ignores comment-only engine mentions when classifying scripts", () => { + const result = classifyAnimationRuntime(` + + + + +`); + + expect(result.verdict).toBe("animejs"); + expect(result.blocks).toMatchObject([{ engine: "animejs" }]); + }); + + it("classifies empty files and files without meaningful inline scripts as none", () => { + expect(classifyAnimationRuntime("").verdict).toBe("none"); + expect( + classifyAnimationRuntime( + '', + ).verdict, + ).toBe("none"); + expect(classifyAnimationRuntime("").verdict).toBe("none"); + }); + + it("includes template script blocks but excludes template-only engines from the file verdict", () => { + const result = classifyAnimationRuntime(` + + + + +`); + + expect(result.verdict).toBe("none"); + expect(result.blocks).toMatchObject([{ engine: "gsap", insideTemplate: true }]); + }); +}); diff --git a/packages/parsers/src/animationRuntimeDetection.ts b/packages/parsers/src/animationRuntimeDetection.ts new file mode 100644 index 0000000000..1e9a528d52 --- /dev/null +++ b/packages/parsers/src/animationRuntimeDetection.ts @@ -0,0 +1,404 @@ +/** + * Static animation-runtime classification for HyperFrames composition HTML. + * + * `classifyAnimationRuntime(html)` parses the source with linkedom and returns + * every inline script block with its content span in the original HTML string. + * A block engine may be `"mixed"` when GSAP and anime.js signals appear in the + * same script. Scripts nested under `