Skip to content

Write HTML. Render video. Built for agents.

9.9 KB
Raw Blame
import { describe, expect, it } from "vitest";
import {
  resolveEditingAffordances,
  resolveEditingSections,
  type EditableElementFacts,
} from "./affordances";
import { HF_AUDIO_GROUP_TAG } from "../audioGroups";

function baseFacts(over: Partial<EditableElementFacts> = {}): EditableElementFacts {
  return {
    hasStableTarget: true,
    tag: "div",
    inlineStyles: {},
    computedStyles: {},
    isCompositionHost: false,
    isCompositionRoot: false,
    isInsideLockedComposition: false,
    isMasterView: false,
    existsInSource: true,
    hasEditableText: false,
    hasTimingStart: false,
    animationCount: 0,
    ...over,
  };
}

describe("resolveEditingAffordances — capabilities", () => {
  it("locked composition: nothing editable, not selectable, reason set", () => {
    const a = resolveEditingAffordances(baseFacts({ isInsideLockedComposition: true }));
    expect(a.capabilities).toMatchObject({
      canSelect: false,
      canEditStyles: false,
      canMove: false,
    });
    expect(a.capabilities.reasonIfDisabled).toContain("locked composition");
  });

  it("no stable target: not editable but selectable", () => {
    const a = resolveEditingAffordances(baseFacts({ hasStableTarget: false }));
    expect(a.capabilities.canSelect).toBe(true);
    expect(a.capabilities.canEditStyles).toBe(false);
  });

  it("not in source: script-generated, select-only", () => {
    const a = resolveEditingAffordances(baseFacts({ existsInSource: false }));
    expect(a.capabilities.canSelect).toBe(true);
    expect(a.capabilities.canEditStyles).toBe(false);
    expect(a.capabilities.reasonIfDisabled).toContain("generated by a script");
  });

  it("composition root: edit styles only, no move/resize, and NOT croppable (it is the canvas)", () => {
    const a = resolveEditingAffordances(baseFacts({ isCompositionRoot: true }));
    expect(a.capabilities).toMatchObject({ canEditStyles: true, canMove: false, canResize: false });
    expect(a.capabilities.canCrop).toBe(false);
  });

  it("sub-composition host in master view: styles locked but STILL croppable (viewport clip)", () => {
    const a = resolveEditingAffordances(baseFacts({ isCompositionHost: true, isMasterView: true }));
    expect(a.capabilities.canEditStyles).toBe(false); // internals are edited by drilling in
    expect(a.capabilities.canCrop).toBe(true); // crop is a viewport clip on the host in the parent source
  });

  it("script-generated / locked elements are not croppable", () => {
    expect(
      resolveEditingAffordances(baseFacts({ existsInSource: false })).capabilities.canCrop,
    ).toBe(false);
    expect(
      resolveEditingAffordances(baseFacts({ isInsideLockedComposition: true })).capabilities
        .canCrop,
    ).toBe(false);
  });

  it("absolute + left/top + identity transform: canMove", () => {
    const a = resolveEditingAffordances(
      baseFacts({
        computedStyles: { position: "absolute", left: "10px", top: "20px", transform: "none" },
      }),
    );
    expect(a.capabilities.canMove).toBe(true);
  });

  it("transform-driven geometry blocks canMove", () => {
    const a = resolveEditingAffordances(
      baseFacts({
        computedStyles: {
          position: "absolute",
          left: "10px",
          top: "20px",
          transform: "matrix(1,0,0,1,5,5)",
        },
      }),
    );
    expect(a.capabilities.canMove).toBe(false);
  });

  it("canResize requires canMove plus a width/height", () => {
    const a = resolveEditingAffordances(
      baseFacts({
        computedStyles: {
          position: "absolute",
          left: "0px",
          top: "0px",
          width: "100px",
          transform: "none",
        },
      }),
    );
    expect(a.capabilities.canResize).toBe(true);
  });

  it("inline left/top override missing computed", () => {
    const a = resolveEditingAffordances(
      baseFacts({
        inlineStyles: { left: "5px", top: "5px" },
        computedStyles: { position: "fixed", transform: "none" },
      }),
    );
    expect(a.capabilities.canMove).toBe(true);
  });

  it("computedStyles absent: canMove/canResize default false", () => {
    const a = resolveEditingAffordances(
      baseFacts({ computedStyles: undefined, inlineStyles: { left: "5px", top: "5px" } }),
    );
    expect(a.capabilities.canMove).toBe(false);
    expect(a.capabilities.canResize).toBe(false);
  });

  it("composition host + master view: no edit styles, geometry blocked", () => {
    const a = resolveEditingAffordances(baseFacts({ isCompositionHost: true, isMasterView: true }));
    expect(a.capabilities.canEditStyles).toBe(false);
    expect(a.capabilities.canApplyManualOffset).toBe(false);
    expect(a.capabilities.reasonIfDisabled).toContain("internal layer");
  });
});

describe("resolveEditingAffordances — sections", () => {
  it("video: media + colorGrading", () => {
    const s = resolveEditingAffordances(baseFacts({ tag: "video" })).sections;
    expect(s).toMatchObject({ media: true, colorGrading: true });
  });

  it("audible video: audioFx; silent video: no audioFx", () => {
    expect(
      resolveEditingAffordances(baseFacts({ tag: "video", hasAudio: true })).sections.audioFx,
    ).toBe(true);
    expect(
      resolveEditingAffordances(baseFacts({ tag: "video", hasAudio: false })).sections.audioFx,
    ).toBe(false);
    expect(resolveEditingAffordances(baseFacts({ tag: "video" })).sections.audioFx).toBe(false);
  });

  it("audio: media but not colorGrading", () => {
    const s = resolveEditingAffordances(baseFacts({ tag: "audio" })).sections;
    expect(s).toMatchObject({ media: true, colorGrading: false });
  });

  it("audio: no layout or style — an audio element has no rendered box", () => {
    const s = resolveEditingAffordances(baseFacts({ tag: "audio" })).sections;
    expect(s).toMatchObject({ layout: false, style: false });
  });

  it("div/img/video: layout + style apply", () => {
    for (const tag of ["div", "img", "video"]) {
      const s = resolveEditingAffordances(baseFacts({ tag })).sections;
      expect(s).toMatchObject({ layout: true, style: true });
    }
  });

  it("img: media + colorGrading", () => {
    const s = resolveEditingAffordances(baseFacts({ tag: "img" })).sections;
    expect(s).toMatchObject({ media: true, colorGrading: true });
  });

  it("canvas: colorGrading", () => {
    const s = resolveEditingAffordances(baseFacts({ tag: "canvas" })).sections;
    expect(s).toMatchObject({ media: false, colorGrading: true });
  });

  it("editable text on a plain element: text section", () => {
    const s = resolveEditingAffordances(baseFacts({ hasEditableText: true })).sections;
    expect(s.text).toBe(true);
  });

  it("text section suppressed on host / locked", () => {
    expect(
      resolveEditingAffordances(baseFacts({ hasEditableText: true, isCompositionHost: true }))
        .sections.text,
    ).toBe(false);
    expect(
      resolveEditingAffordances(
        baseFacts({ hasEditableText: true, isInsideLockedComposition: true }),
      ).sections.text,
    ).toBe(false);
  });

  it("data-start drives timing; animations drive timing + animation", () => {
    expect(resolveEditingAffordances(baseFacts({ hasTimingStart: true })).sections.timing).toBe(
      true,
    );
    const anim = resolveEditingAffordances(baseFacts({ animationCount: 2 })).sections;
    expect(anim).toMatchObject({ timing: true, animation: true });
  });
});

describe("resolveEditingSections (sections-only export)", () => {
  it("matches resolveEditingAffordances().sections for the same facts", () => {
    const facts = baseFacts({ tag: "video", hasEditableText: true, animationCount: 1 });
    expect(resolveEditingSections(facts)).toEqual(resolveEditingAffordances(facts).sections);
  });

  it("animationCount > 0 turns on timing + animation even without data-start", () => {
    const s = resolveEditingSections(baseFacts({ hasTimingStart: false, animationCount: 3 }));
    expect(s).toMatchObject({ timing: true, animation: true });
  });
});

describe("audioFx section", () => {
  it("applies to an audio element", () => {
    expect(resolveEditingSections(baseFacts({ tag: "audio" })).audioFx).toBe(true);
  });

  it("does not apply to video, whose sound lives on a separate audio element", () => {
    expect(resolveEditingSections(baseFacts({ tag: "video" })).audioFx).toBe(false);
    expect(resolveEditingSections(baseFacts({ tag: "div" })).audioFx).toBe(false);
  });
});

describe("audio has timing but no animation; a bus has neither", () => {
  // Nothing on an `<audio>` element or an `<hf-audio-group>` bus has a
  // transform, an opacity or a box, so a tween on one moves nothing. The panel
  // showed its GSAP editor for both anyway, because it gated that on the
  // handlers being wired rather than on the element being animatable.
  it("withholds animation from an audio clip that HAS tweens", () => {
    const s = resolveEditingSections(baseFacts({ tag: "audio", animationCount: 3 }));
    expect(s.animation).toBe(false);
  });

  it("withholds it from a bus too", () => {
    const s = resolveEditingSections(baseFacts({ tag: HF_AUDIO_GROUP_TAG, animationCount: 3 }));
    expect(s.animation).toBe(false);
  });

  it("still grants it to a div with tweens, so the gate is the tag", () => {
    expect(resolveEditingSections(baseFacts({ tag: "div", animationCount: 1 })).animation).toBe(
      true,
    );
  });

  // An audio clip is placed on the timeline like any other, so Start/Duration
  // stay editable — that is the half of the old Motion section worth keeping.
  it("keeps timing on an audio clip with an authored start", () => {
    const s = resolveEditingSections(baseFacts({ tag: "audio", hasTimingStart: true }));
    expect(s.timing).toBe(true);
  });

  // A bus has no `data-start` and no duration; its automation clock is
  // composition time. Start/Duration/End would be editing nothing.
  it("withholds timing from a bus even if something claims a start", () => {
    const s = resolveEditingSections(
      baseFacts({ tag: HF_AUDIO_GROUP_TAG, hasTimingStart: true, animationCount: 2 }),
    );
    expect(s.timing).toBe(false);
  });
});