Files
heygen-com__hyperframes/packages/parsers/src/springEase.test.ts
Miguel Ángel cdf9c817e1 refactor: extract @hyperframes/parsers from core (#1755)
## Summary

Extracts the GSAP parser/writer suite, HTML parser, hf-ids, spring-ease, and the shared composition data types out of `@hyperframes/core/src/parsers/` into a new, independently-publishable **`@hyperframes/parsers`** package.

This is the foundation of the [#1749](https://github.com/heygen-com/hyperframes/issues/1749) effort: make HyperFrames' parsing/linting/validation usable as plain libraries in a Node app, without shelling out to the CLI. Parsers is the standalone base every other extracted package builds on.

**Part 1 of 3** — splits #1754 into independently-reviewable pieces. Parts 2 (lint) and 3 (studio-server) stack on this branch.

## What moves

| | |
|---|---|
| Source moved out of core | **~9,900 LOC** (`src/parsers/` → `packages/parsers/src/`) |
| Total lines removed from core (incl. tests + goldens) | ~19,600 |
| Files relocated | 39 |
| Tests carried over | **660 passing** (5 skipped, 3 todo) |

The big movers: `gsapParser` / `gsapParserAcorn` (the recast + acorn dual parsers), `gsapWriterAcorn`, `gsapSerialize`, `gsapUnroll`, `htmlParser`, `hfIds`, `springEase`, `stableIds`, plus the `__goldens__` corpus.

## Bundle footprint of the new package

| Artifact | Size |
|---|---|
| `dist/` (unpacked) | 1.7 MB |
| npm tarball (packed) | 409 KB |
| `dist/index.js` | 90 KB (**~21 KB gzipped**) |
| Heaviest entries | `gsapWriterAcorn.js` 93 KB · `gsapParser.js` 91 KB |

Most of the weight is the GSAP AST machinery (recast/babel/acorn). It's tree-shakeable via subpath entries (`@hyperframes/parsers/hf-ids`, `/gsap-constants`, etc.) so a consumer that only needs `hf-ids` (2 KB) doesn't pull the parsers.

## How `@hyperframes/core` changes

The interesting part: **core sheds its entire AST toolchain.**

| core `dependencies` | before | after |
|---|---|---|
| count | 9 | 6 |
| removed | — | `@babel/parser`, `acorn`, `acorn-walk`, `magic-string`, `recast` |
| added | — | `@hyperframes/parsers`, `linkedom` |

Before this PR, importing `@hyperframes/core` at all dragged in babel + recast + acorn just to construct types. Now those live behind `@hyperframes/parsers`, and a consumer that only wants core's runtime/compiler types never resolves the parser stack. Core keeps thin `@deprecated` re-export stubs at the old subpaths (`@hyperframes/core/gsap-parser`, `/gsap-constants`, …) so nothing downstream breaks.

## Design notes

- **`"bun"` export condition before `"node"`** in every package export. Bun resolves the TypeScript source directly (no pre-built `dist/`), while Node/tsx/Docker contexts fall through to `"node"` → `dist/`. This keeps the dev loop zero-build while published artifacts stay Node-consumable.
- `@hyperframes/parsers` is **standalone** — zero `@hyperframes/*` dependencies — so it can be the base of the stack.

## Test plan

- [x] `bun run --filter @hyperframes/parsers test` — 660 tests pass
- [x] `bun run --filter @hyperframes/sdk test` — 382 tests pass
- [x] `bun run build` — full monorepo build succeeds
- [x] Fallow audit passes on CI
2026-06-27 00:46:26 -04:00

90 lines
3.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { generateSpringEaseData, SPRING_PRESETS } from "./springEase";
/** Parse an SVG-path CustomEase string into {x, y} pairs. */
function parsePairs(data: string): { x: number; y: number }[] {
// Strip "M0,0 L" prefix, then split on whitespace between coordinate pairs
const body = data.replace(/^M0,0\s+L/, "");
const tokens = body.split(/\s+/);
return [
{ x: 0, y: 0 }, // from M0,0
...tokens.map((tok) => {
const [xStr, yStr] = tok.split(",");
return { x: Number(xStr), y: Number(yStr) };
}),
];
}
describe("generateSpringEaseData", () => {
it("generates a valid SVG-path CustomEase data string", () => {
const data = generateSpringEaseData(1, 180, 12);
expect(typeof data).toBe("string");
// Must start with M0,0 (SVG moveTo)
expect(data.startsWith("M0,0")).toBe(true);
// Must contain L (lineTo) segments
expect(data).toContain(" L");
const pairs = parsePairs(data);
expect(pairs.length).toBeGreaterThan(10);
// First point at origin, last at (1,1)
expect(pairs[0]).toEqual({ x: 0, y: 0 });
expect(pairs[pairs.length - 1]).toEqual({ x: 1, y: 1 });
});
it("underdamped spring produces overshoot", () => {
const data = generateSpringEaseData(1, 180, 8); // low damping = bouncy
const pairs = parsePairs(data);
const hasOvershoot = pairs.some((p) => p.y > 1.01);
expect(hasOvershoot).toBe(true);
});
it("critically damped spring has no overshoot", () => {
const mass = 1;
const stiffness = 100;
const criticalDamping = 2 * Math.sqrt(stiffness * mass); // zeta = 1
const data = generateSpringEaseData(mass, stiffness, criticalDamping);
const pairs = parsePairs(data);
const maxY = Math.max(...pairs.map((p) => p.y));
expect(maxY).toBeLessThanOrEqual(1.005);
});
it("overdamped spring has no overshoot and monotonically increases", () => {
// zeta > 1 — heavy damping
const data = generateSpringEaseData(1, 100, 30);
const pairs = parsePairs(data);
const maxY = Math.max(...pairs.map((p) => p.y));
expect(maxY).toBeLessThanOrEqual(1.005);
// Monotonically non-decreasing (within floating point tolerance)
for (let i = 1; i < pairs.length; i++) {
expect(pairs[i].y).toBeGreaterThanOrEqual(pairs[i - 1].y - 0.001);
}
});
it("all presets generate valid data", () => {
for (const preset of SPRING_PRESETS) {
const data = generateSpringEaseData(preset.mass, preset.stiffness, preset.damping);
expect(data.length).toBeGreaterThan(0);
expect(data.startsWith("M0,0")).toBe(true);
const pairs = parsePairs(data);
expect(pairs.length).toBeGreaterThan(50);
}
});
it("output x values span [0,1] monotonically", () => {
const data = generateSpringEaseData(1, 180, 12);
const pairs = parsePairs(data);
expect(pairs[0].x).toBe(0);
expect(pairs[pairs.length - 1].x).toBe(1);
for (let i = 1; i < pairs.length; i++) {
expect(pairs[i].x).toBeGreaterThan(pairs[i - 1].x - 0.0001);
expect(pairs[i].x).toBeLessThanOrEqual(1);
}
});
it("respects custom step count", () => {
const data = generateSpringEaseData(1, 100, 15, 60);
const pairs = parsePairs(data);
// 60 steps + the M0,0 origin = 61 points
expect(pairs.length).toBe(61);
});
});