-
Notifications
You must be signed in to change notification settings - Fork 4.3k
perf(studio): stabilize virtualized beat gestures #2709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
307 changes: 307 additions & 0 deletions
307
packages/studio/src/player/components/BeatStrip.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,307 @@ | ||
| // @vitest-environment happy-dom | ||
|
|
||
| import React, { act } from "react"; | ||
| import { createRoot, type Root } from "react-dom/client"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import type { TimelineElement } from "../store/playerStore"; | ||
| import { usePlayerStore } from "../store/playerStore"; | ||
| import { BeatStrip } from "./BeatStrip"; | ||
|
|
||
| (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; | ||
|
|
||
| const roots: Root[] = []; | ||
| const requestSeek = vi.fn(); | ||
| const commitBeatEdits = usePlayerStore.getState().commitBeatEdits; | ||
| const commitBeatEditsSpy = vi.fn((...args: Parameters<typeof commitBeatEdits>) => | ||
| commitBeatEdits(...args), | ||
| ); | ||
| const MUSIC_ELEMENT: TimelineElement = { | ||
| id: "background-music", | ||
| label: "Music", | ||
| tag: "audio", | ||
| src: "music.mp3", | ||
| start: 0, | ||
| duration: 10, | ||
| track: 0, | ||
| timelineRole: "music", | ||
| }; | ||
| const BEAT_ANALYSIS = { | ||
| beatTimes: [1, 3], | ||
| beatStrengths: [0.5, 0.8], | ||
| bpm: 120, | ||
| bpmConfidence: "high" as const, | ||
| channelData: null, | ||
| sampleRate: 48_000, | ||
| peak: 1, | ||
| }; | ||
|
|
||
| function pointerEvent(type: string, init: PointerEventInit): Event { | ||
| if (typeof PointerEvent === "function") return new PointerEvent(type, init); | ||
| const event = new MouseEvent(type, init); | ||
| Object.defineProperty(event, "pointerId", { value: init.pointerId ?? 0 }); | ||
| return event; | ||
| } | ||
|
|
||
| function mountBeatStrip(renderTimeRange?: { start: number; end: number }) { | ||
| const viewport = document.createElement("div"); | ||
| viewport.dataset.timelineScrollViewport = ""; | ||
| Object.defineProperties(viewport, { | ||
| clientWidth: { configurable: true, value: 500 }, | ||
| clientHeight: { configurable: true, value: 300 }, | ||
| scrollWidth: { configurable: true, value: 2_000 }, | ||
| scrollHeight: { configurable: true, value: 2_000 }, | ||
| }); | ||
| viewport.getBoundingClientRect = () => | ||
| ({ left: 0, right: 1_000, top: 0, bottom: 500, width: 1_000, height: 500 }) as DOMRect; | ||
| Object.assign(viewport, { | ||
| setPointerCapture: vi.fn(), | ||
| hasPointerCapture: vi.fn(() => true), | ||
| releasePointerCapture: vi.fn(), | ||
| }); | ||
| document.body.appendChild(viewport); | ||
| const root = createRoot(viewport); | ||
| roots.push(root); | ||
| act(() => { | ||
| root.render( | ||
| <BeatStrip | ||
| beatTimes={[1, 3]} | ||
| beatStrengths={[0.5, 0.8]} | ||
| pps={100} | ||
| renderTimeRange={renderTimeRange} | ||
| />, | ||
| ); | ||
| }); | ||
| return { root, viewport }; | ||
| } | ||
|
|
||
| function firstBeat(): HTMLDivElement { | ||
| const beat = document.querySelector<HTMLDivElement>( | ||
| '[title="Drag to move · double-click to delete"]', | ||
| ); | ||
| if (!beat) throw new Error("Expected a beat handle"); | ||
| return beat; | ||
| } | ||
|
|
||
| function startBeatDrag(clientX = 100, pointerId = 1): void { | ||
| act(() => { | ||
| firstBeat().dispatchEvent( | ||
| pointerEvent("pointerdown", { bubbles: true, button: 0, clientX, clientY: 100, pointerId }), | ||
| ); | ||
| }); | ||
| expect(usePlayerStore.getState().beatDragging).toBe(true); | ||
| } | ||
|
|
||
| function releaseBeatDrag(clientX = 140, pointerId = 1): void { | ||
| act(() => { | ||
| window.dispatchEvent( | ||
| pointerEvent("pointerup", { bubbles: true, clientX, clientY: 100, pointerId }), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| function expectCancelledBeatDrag(): void { | ||
| expect(commitBeatEditsSpy).not.toHaveBeenCalled(); | ||
| expect(usePlayerStore.getState().beatDragging).toBe(false); | ||
| } | ||
|
|
||
| function expectCommittedBeatAt(time: number): void { | ||
| expect(commitBeatEditsSpy).toHaveBeenCalledExactlyOnceWith(expect.anything(), "move beat"); | ||
| expect(usePlayerStore.getState().beatEdits?.added[0]?.time).toBe(time); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| commitBeatEditsSpy.mockClear(); | ||
| requestSeek.mockReset(); | ||
| usePlayerStore.setState({ | ||
| timelineSessionEpoch: 1, | ||
| timelineProjectId: "project-a", | ||
| beatDragging: false, | ||
| requestSeek, | ||
| elements: [MUSIC_ELEMENT], | ||
| beatAnalysis: BEAT_ANALYSIS, | ||
| beatEdits: null, | ||
| commitBeatEdits: commitBeatEditsSpy, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| act(() => window.dispatchEvent(new Event("blur"))); | ||
| for (const root of roots.splice(0)) act(() => root.unmount()); | ||
| document.body.innerHTML = ""; | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe("BeatStrip gesture ownership", () => { | ||
| it("commits once after its source row unmounts", () => { | ||
| const { root } = mountBeatStrip(); | ||
| startBeatDrag(); | ||
| act(() => root.render(null)); | ||
| expect(usePlayerStore.getState().beatDragging).toBe(true); | ||
|
|
||
| act(() => { | ||
| window.dispatchEvent( | ||
| pointerEvent("pointermove", { | ||
| bubbles: true, | ||
| clientX: 140, | ||
| clientY: 100, | ||
| pointerId: 1, | ||
| }), | ||
| ); | ||
| window.dispatchEvent( | ||
| pointerEvent("pointerup", { | ||
| bubbles: true, | ||
| clientX: 140, | ||
| clientY: 100, | ||
| pointerId: 1, | ||
| }), | ||
| ); | ||
| window.dispatchEvent( | ||
| pointerEvent("pointerup", { | ||
| bubbles: true, | ||
| clientX: 160, | ||
| clientY: 100, | ||
| pointerId: 1, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| expectCommittedBeatAt(1.4); | ||
| expect(usePlayerStore.getState().beatDragging).toBe(false); | ||
| }); | ||
|
|
||
| it("includes viewport scrolling in the final beat time", () => { | ||
| const { viewport } = mountBeatStrip(); | ||
| startBeatDrag(100); | ||
| viewport.scrollLeft = 50; | ||
|
|
||
| releaseBeatDrag(110); | ||
| expectCommittedBeatAt(1.6); | ||
| }); | ||
|
|
||
| it("cancels without mutation on pointer cancel, Escape, and project switch", () => { | ||
| mountBeatStrip(); | ||
| startBeatDrag(); | ||
| act(() => { | ||
| window.dispatchEvent(pointerEvent("pointercancel", { pointerId: 1 })); | ||
| window.dispatchEvent(pointerEvent("pointerup", { clientX: 140, clientY: 100, pointerId: 1 })); | ||
| }); | ||
|
|
||
| startBeatDrag(); | ||
| act(() => window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }))); | ||
|
|
||
| startBeatDrag(); | ||
| act(() => usePlayerStore.getState().beginTimelineSession("project-b")); | ||
| releaseBeatDrag(); | ||
| expectCancelledBeatDrag(); | ||
| }); | ||
|
|
||
| it("cancels without mutation when the captured music source disappears", () => { | ||
| mountBeatStrip(); | ||
| startBeatDrag(); | ||
| act(() => usePlayerStore.setState({ elements: [] })); | ||
| releaseBeatDrag(); | ||
| expectCancelledBeatDrag(); | ||
| }); | ||
|
|
||
| it("cancels when the captured beat disappears during the gesture", () => { | ||
| mountBeatStrip(); | ||
| startBeatDrag(); | ||
| act(() => { | ||
| usePlayerStore.setState({ | ||
| beatAnalysis: { ...BEAT_ANALYSIS, beatTimes: [3], beatStrengths: [0.8] }, | ||
| }); | ||
| }); | ||
| releaseBeatDrag(); | ||
| expectCancelledBeatDrag(); | ||
| }); | ||
|
|
||
| it("releases the actor when the owning timeline viewport unmounts", async () => { | ||
| const { viewport } = mountBeatStrip(); | ||
| startBeatDrag(); | ||
|
|
||
| await act(async () => { | ||
| viewport.remove(); | ||
| await Promise.resolve(); | ||
| }); | ||
| releaseBeatDrag(); | ||
| expectCancelledBeatDrag(); | ||
| }); | ||
|
|
||
| it("ignores unrelated pointers and keeps the active beat rendered outside the time window", () => { | ||
| const { root } = mountBeatStrip(); | ||
| startBeatDrag(); | ||
| act(() => { | ||
| window.dispatchEvent( | ||
| pointerEvent("pointermove", { | ||
| bubbles: true, | ||
| clientX: 150, | ||
| clientY: 100, | ||
| pointerId: 2, | ||
| }), | ||
| ); | ||
| window.dispatchEvent( | ||
| pointerEvent("pointerup", { | ||
| bubbles: true, | ||
| clientX: 150, | ||
| clientY: 100, | ||
| pointerId: 2, | ||
| }), | ||
| ); | ||
| root.render( | ||
| <BeatStrip | ||
| beatTimes={[1, 3]} | ||
| beatStrengths={[0.5, 0.8]} | ||
| pps={100} | ||
| renderTimeRange={{ start: 2.5, end: 3.5 }} | ||
| />, | ||
| ); | ||
| }); | ||
|
|
||
| expect( | ||
| document.querySelectorAll('[title="Drag to move · double-click to delete"]'), | ||
| ).toHaveLength(2); | ||
| expect(commitBeatEditsSpy).not.toHaveBeenCalled(); | ||
|
|
||
| act(() => { | ||
| window.dispatchEvent( | ||
| pointerEvent("pointerup", { | ||
| bubbles: true, | ||
| clientX: 150, | ||
| clientY: 100, | ||
| pointerId: 1, | ||
| }), | ||
| ); | ||
| }); | ||
| expect(commitBeatEditsSpy).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("does not autoscroll or mutate below the drag threshold", () => { | ||
| const requestAnimationFrame = vi.fn(() => 1); | ||
| vi.stubGlobal("requestAnimationFrame", requestAnimationFrame); | ||
| vi.stubGlobal("cancelAnimationFrame", vi.fn()); | ||
| mountBeatStrip(); | ||
| startBeatDrag(990); | ||
|
|
||
| act(() => { | ||
| window.dispatchEvent( | ||
| pointerEvent("pointermove", { | ||
| bubbles: true, | ||
| clientX: 991, | ||
| clientY: 100, | ||
| pointerId: 1, | ||
| }), | ||
| ); | ||
| window.dispatchEvent( | ||
| pointerEvent("pointerup", { | ||
| bubbles: true, | ||
| clientX: 991, | ||
| clientY: 100, | ||
| pointerId: 1, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| expect(requestAnimationFrame).not.toHaveBeenCalled(); | ||
| expect(commitBeatEditsSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.