Files
hyperframes/packages/core/src/index.test.ts
T
Vance IngallsandClaude Opus 4.8 967bf9f9ed refactor(core): gate acorn GSAP writer behind cutover flag; keep recast default (WS-3F) (#1573)
* refactor(core): retire recast/babel, route all GSAP mutations to acorn (WS-E/3.F)

- Delete gsapParser.ts (2595-line recast-based parser/writer)
- Delete gsapParser.test.ts, gsapParser.stress.test.ts, gsapParser.test-helpers.ts
- Add gsapParserExports.ts: re-export umbrella for gsap-parser subpath
- Move SplitAnimationsOptions/SplitAnimationsResult to gsapSerialize.ts
- executeGsapMutation: async->sync, static acorn imports replace loadGsapParser()
- Fix 3 function name mismatches in files.ts switch cases
- generators/hyperframes.ts: imports from gsapSerialize (blocker resolved)
- gsapWriterAcorn.ts: SplitAnimationsOptions from gsapSerialize
- Parity tests: recast oracle removed; acorn-only regression (14 pass)
- Remove recast and @babel/parser from core/package.json

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(sdk): harden mutation handlers + widen variable API (code-review)

Self-contained review fixes for the SDK-hotspot stack (#1569–#1573). The
dispatch path (_dispatch → applyOp) never runs validateOp, so the new
WS-D/WS-3.C guards were advisory-only; re-enforce them in the handlers.

- addElement: null-guard the resolved parent (no more `as Element` masking a
  null → crash on unknown parent id); reject <script> and multi-root fragments
  via parseInsertableFragment instead of inserting raw markup / silently
  dropping extra roots.
- addWithKeyframes / replaceWithKeyframes: bail on empty keyframes (no
  degenerate `keyframes: {}` tween) and when the animationId resolves to
  nothing (no silent degrade-to-add leaving a duplicate tween).
- isObjectVariableValue: exclude arrays so an array override value can't be
  misclassified as a font/image object and written into the variable model.
- Composition.setVariableValue: widen the public interface signature to
  `… | FontValue | ImageValue` to match the impl + EditOp (B2 object-valued
  variables were unreachable via the typed API).
- mutate.gsap.test.ts: import addKeyframeToScript from gsap-writer-acorn —
  the gsap-parser subpath no longer re-exports write fns after recast retire,
  so the test threw at runtime (red suite).
- Dedup: export EXCLUDED_TAGS from hfIds.ts and drop the verbatim
  HF_EXCLUDED_TAGS copy in mutate.ts.

Adds guard regression tests. SDK 340/340, core hfIds 13/13, build green,
fallow --gate new-only clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(sdk): variable-model dedup + undo/scoped-parent correctness; test honesty (code-review)

Second batch of review fixes for the SDK-hotspot stack.

- Variable model (#7, #13): extract readVariableDefault/writeVariableDefault into
  a shared engine/variableModel.ts used by both mutate.ts (forward) and
  apply-patches.ts (replay), so the model shape can't diverge. Add
  clearVariableDefault and make a `variable` remove patch DELETE the decl's
  `default` key — the exact inverse of a first-set on a default-less variable.
  Previously undo of such a set no-op'd and stranded the value.
- addElement scoped parent (#8): record the caller's id verbatim
  (scoped "hf-host/hf-leaf" path or composition id) as the patch parentId
  instead of the bare data-hf-id, so redo/replay re-resolves the SAME parent via
  resolveScoped rather than the canonical top-level dup (or document.body).
- resolveTimings honesty (#5): correct the header + test that claimed a live
  "preview == render" parity — neither path consumes the resolver yet (anchor
  inputs are Pacific/backend-deferred). It's a pure-function property, not a
  current guarantee.
- GSAP writer parity (#12): the recast oracle was deleted in WS-3.F, leaving the
  WS-3.C keyframe ops comparing acorn output to itself. Pin them as golden inline
  snapshots and drop the now-dead recast scaffolding (replaceWithKfRecast,
  removeAnimRecast alias). Remaining pre-WS-3.C parity blocks noted as follow-up.

Adds regression tests (undo of default-less variable; scoped-parent redo).
SDK 342/342, core timingResolver+parity green, build + fallow --gate new-only clean.

Not changed (need design / out of scope): #9 pre-#1569 persisted-override CSS
replay (moot for unreleased data; proper fix is render-time CSS derivation),
#11 replaceWithKeyframes stale positional id (mitigated by the missing-id no-op
guard + type doc; full fix needs non-positional ids).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(sdk): replay CSS-prop derivation for legacy var overrides; stale-id selector guard (code-review)

Final review-fix batch — the two items deferred from the prior pass.

- #9 legacy variable-override CSS: applyOverrideSet now derives the `--{id}`
  CSS custom prop from any scalar `var.{id}` override on replay (and removes it
  for a null override). Sets written before the model/CSS split carried only
  `var.{id}`; without this, replaying them updated the JSON model but left
  `var(--{id})` bindings rendering the schema default. Replay-path only — the
  undo path (applyOne) is untouched, so #1569's separate-patch undo correctness
  is preserved. Object (font/image) values are never CSS, so they are skipped.
- #11 stale positional id: replaceWithKeyframes now requires the located
  animation to still target the caller's `targetSelector`. Position-derived ids
  re-point after structural edits; a stale id resolving to a DIFFERENT element's
  tween previously got silently replaced. It now bails (no-op) unless the id
  still points at the expected selector.

Adds regression tests (legacy var.{id}-only override restores CSS; object
override writes no CSS; stale-id-wrong-selector replace is a no-op).
SDK 345/345, build + fallow --gate new-only clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(core): gate acorn GSAP writer behind cutover flag; keep recast default (WS-3F)

Product decision pivot: acorn no longer replaces recast as the GSAP writer.
Recast remains the default server writer; acorn runs only when
STUDIO_SDK_CUTOVER_ENABLED=true (or =1) is set server-side — the same env
flag name as the client Vite var, so a single switch flips both sides.

Changes:
- Restore gsapParser.ts (recast writer) + test/stress/helper files deleted by 3F
- Restore @babel/parser + recast deps in packages/core/package.json
- Add isAcornGsapWriterEnabled() + loadGsapParser() to files.ts (lines 59-82)
- Split executeGsapMutation into async dispatcher + executeGsapMutationRecast
  (recast, async via loadGsapParser) + executeGsapMutationAcorn (acorn, sync)
- Dispatcher defaults to recast; acorn branch taken only when flag is on
- Restore gsapWriter.parity.test.ts, gsapWriterParity.acorn.test.ts, and
  gsapWriterParity.corpus.test.ts to true recast-vs-acorn differential suites
  (not acorn-vs-itself)
- Exempt gsapParser.ts in .fallowrc.jsonc health.ignore + ignoreExports
  (pre-existing complexity + barrel re-exports consumed outside diff scope)
- Add fallow-ignore-file code-duplication to files.ts (intentional parallel
  switch bodies for two writers)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 00:22:14 -07:00

182 lines
7.2 KiB
TypeScript

// @vitest-environment node
import { describe, it, expect } from "vitest";
import * as core from "./index.js";
describe("@hyperframes/core public API exports", () => {
describe("type-related constants and utilities", () => {
it("exports CANVAS_DIMENSIONS", () => {
expect(core.CANVAS_DIMENSIONS).toBeDefined();
expect(core.CANVAS_DIMENSIONS.landscape).toEqual({ width: 1920, height: 1080 });
expect(core.CANVAS_DIMENSIONS.portrait).toEqual({ width: 1080, height: 1920 });
expect(core.CANVAS_DIMENSIONS["landscape-4k"]).toEqual({ width: 3840, height: 2160 });
expect(core.CANVAS_DIMENSIONS["portrait-4k"]).toEqual({ width: 2160, height: 3840 });
expect(core.CANVAS_DIMENSIONS.square).toEqual({ width: 1080, height: 1080 });
expect(core.CANVAS_DIMENSIONS["square-4k"]).toEqual({ width: 2160, height: 2160 });
});
it("exports VALID_CANVAS_RESOLUTIONS derived from CANVAS_DIMENSIONS", () => {
expect(core.VALID_CANVAS_RESOLUTIONS).toEqual([
"landscape",
"portrait",
"landscape-4k",
"portrait-4k",
"square",
"square-4k",
]);
});
it("exports normalizeResolutionFlag with alias support", () => {
expect(core.normalizeResolutionFlag("4k")).toBe("landscape-4k");
expect(core.normalizeResolutionFlag("uhd")).toBe("landscape-4k");
expect(core.normalizeResolutionFlag("1080p")).toBe("landscape");
expect(core.normalizeResolutionFlag("landscape-4k")).toBe("landscape-4k");
expect(core.normalizeResolutionFlag("UHD")).toBe("landscape-4k");
expect(core.normalizeResolutionFlag("square")).toBe("square");
expect(core.normalizeResolutionFlag("square-4k")).toBe("square-4k");
expect(core.normalizeResolutionFlag("1080p-square")).toBe("square");
expect(core.normalizeResolutionFlag("4k-square")).toBe("square-4k");
expect(core.normalizeResolutionFlag("8k")).toBeUndefined();
expect(core.normalizeResolutionFlag(undefined)).toBeUndefined();
});
it("exports TIMELINE_COLORS", () => {
expect(core.TIMELINE_COLORS).toBeDefined();
expect(core.TIMELINE_COLORS.video).toBeDefined();
expect(core.TIMELINE_COLORS.image).toBeDefined();
expect(core.TIMELINE_COLORS.text).toBeDefined();
expect(core.TIMELINE_COLORS.audio).toBeDefined();
expect(core.TIMELINE_COLORS.composition).toBeDefined();
});
it("exports DEFAULT_DURATIONS", () => {
expect(core.DEFAULT_DURATIONS).toBeDefined();
expect(core.DEFAULT_DURATIONS.video).toBe(5);
expect(core.DEFAULT_DURATIONS.text).toBe(2);
});
it("exports type guard functions", () => {
expect(typeof core.isTextElement).toBe("function");
expect(typeof core.isMediaElement).toBe("function");
expect(typeof core.isCompositionElement).toBe("function");
});
it("exports getDefaultStageZoom", () => {
expect(typeof core.getDefaultStageZoom).toBe("function");
const zoom = core.getDefaultStageZoom("landscape");
expect(zoom.scale).toBe(1);
expect(zoom.focusX).toBe(960);
expect(zoom.focusY).toBe(540);
});
});
describe("template exports", () => {
it("exports generateBaseHtml", () => {
expect(typeof core.generateBaseHtml).toBe("function");
});
it("exports getStageStyles", () => {
expect(typeof core.getStageStyles).toBe("function");
});
it("exports template constants", () => {
expect(core.GSAP_CDN).toBeDefined();
expect(core.BASE_STYLES).toBeDefined();
expect(core.ELEMENT_BASE_STYLES).toBeDefined();
expect(core.MEDIA_STYLES).toBeDefined();
expect(core.TEXT_STYLES).toBeDefined();
expect(core.ZOOM_CONTAINER_STYLES).toBeDefined();
});
});
describe("parser exports", () => {
it("does NOT re-export GSAP parser functions from barrel (available via gsap-parser subpath)", () => {
// GSAP AST parser functions are not re-exported from the barrel —
// use the acorn parser (gsapParserAcorn) or writer (gsapWriterAcorn) directly.
expect(typeof (core as Record<string, unknown>).parseGsapScript).toBe("undefined");
});
it("does NOT re-export GSAP constants from barrel (available via gsap-constants subpath)", () => {
expect((core as Record<string, unknown>).SUPPORTED_PROPS).toBeUndefined();
});
it("exports HTML parser functions", () => {
expect(typeof core.parseHtml).toBe("function");
expect(typeof core.updateElementInHtml).toBe("function");
expect(typeof core.addElementToHtml).toBe("function");
expect(typeof core.removeElementFromHtml).toBe("function");
expect(typeof core.validateCompositionHtml).toBe("function");
expect(typeof core.extractCompositionMetadata).toBe("function");
});
});
describe("generator exports", () => {
it("exports hyperframes generator functions", () => {
expect(typeof core.generateHyperframesHtml).toBe("function");
expect(typeof core.generateGsapTimelineScript).toBe("function");
expect(typeof core.generateHyperframesStyles).toBe("function");
});
});
describe("compiler exports", () => {
it("exports compiler functions", () => {
expect(typeof core.compileTimingAttrs).toBe("function");
expect(typeof core.injectDurations).toBe("function");
expect(typeof core.extractResolvedMedia).toBe("function");
expect(typeof core.clampDurations).toBe("function");
expect(typeof core.shouldClampMediaDuration).toBe("function");
});
});
describe("lint exports", () => {
it("exports lintHyperframeHtml", () => {
expect(typeof core.lintHyperframeHtml).toBe("function");
});
});
describe("media exports", () => {
it("exports parseAnimatedGifMetadata", () => {
expect(typeof core.parseAnimatedGifMetadata).toBe("function");
});
});
describe("inline-script exports", () => {
it("exports hyperframe runtime artifacts", () => {
expect(core.HYPERFRAME_RUNTIME_ARTIFACTS).toBeDefined();
expect(core.HYPERFRAME_RUNTIME_CONTRACT).toBeDefined();
expect(typeof core.loadHyperframeRuntimeSource).toBe("function");
});
it("exports runtime contract constants", () => {
expect(core.HYPERFRAME_RUNTIME_GLOBALS).toBeDefined();
expect(core.HYPERFRAME_BRIDGE_SOURCES).toBeDefined();
expect(core.HYPERFRAME_CONTROL_ACTIONS).toBeDefined();
});
it("exports buildHyperframesRuntimeScript", () => {
expect(typeof core.buildHyperframesRuntimeScript).toBe("function");
});
it("exports MEDIA_VISUAL_STYLE_PROPERTIES", () => {
expect(core.MEDIA_VISUAL_STYLE_PROPERTIES).toBeDefined();
expect(Array.isArray(core.MEDIA_VISUAL_STYLE_PROPERTIES)).toBe(true);
expect(core.MEDIA_VISUAL_STYLE_PROPERTIES).toContain("width");
expect(core.MEDIA_VISUAL_STYLE_PROPERTIES).toContain("opacity");
expect(core.MEDIA_VISUAL_STYLE_PROPERTIES).toContain("transform");
});
it("exports copyMediaVisualStyles", () => {
expect(typeof core.copyMediaVisualStyles).toBe("function");
});
it("exports quantizeTimeToFrame", () => {
expect(typeof core.quantizeTimeToFrame).toBe("function");
});
});
describe("adapter exports", () => {
it("exports createGSAPFrameAdapter", () => {
expect(typeof core.createGSAPFrameAdapter).toBe("function");
});
});
});