|
2 | 2 | // fallow-ignore-file code-duplication |
3 | 3 |
|
4 | 4 | import { act } from "react"; |
| 5 | +import { MAX_AUDIO_GAIN } from "@hyperframes/core/audio-gain"; |
5 | 6 | import { createRoot, type Root } from "react-dom/client"; |
6 | 7 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
7 | 8 | import { usePlayerStore } from "../../player/store/playerStore"; |
8 | 9 | import type { TimelineElement } from "../../player/store/timelineElement"; |
9 | 10 | import { useAudioMetersVisible } from "../../utils/audioMeterVisibility"; |
10 | | -import { fractionToLevel, levelToFraction, SILENT_CHANNEL } from "../../utils/audioMeterMath"; |
| 11 | +import { SILENT_CHANNEL } from "../../utils/audioMeterMath"; |
11 | 12 | import { |
12 | 13 | AudioMeterStrip, |
13 | 14 | evictGoneMeterState, |
@@ -52,6 +53,22 @@ function stubTrackRect(): () => void { |
52 | 53 | }; |
53 | 54 | } |
54 | 55 |
|
| 56 | +function drag(fader: HTMLElement, clientY: number) { |
| 57 | + const restoreRect = stubTrackRect(); |
| 58 | + const original = Element.prototype.setPointerCapture; |
| 59 | + Element.prototype.setPointerCapture = vi.fn(); |
| 60 | + try { |
| 61 | + for (const type of ["pointerdown", "pointermove", "pointerup"]) { |
| 62 | + act(() => |
| 63 | + fader.dispatchEvent(new PointerEvent(type, { bubbles: true, pointerId: 1, clientY })), |
| 64 | + ); |
| 65 | + } |
| 66 | + } finally { |
| 67 | + Element.prototype.setPointerCapture = original; |
| 68 | + restoreRect(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
55 | 72 | const makeHook = (groups: Record<string, { l: number; r: number }> = {}) => ({ |
56 | 73 | start: vi.fn(), |
57 | 74 | stop: vi.fn(), |
@@ -160,102 +177,135 @@ describe("AudioMeterStrip", () => { |
160 | 177 | expect(second.read).toHaveBeenCalled(); |
161 | 178 | }); |
162 | 179 |
|
163 | | - it("drags a group fader through the live/quiet data-volume path, and the master fader through the player store", () => { |
164 | | - const restoreRect = stubTrackRect(); |
165 | | - const originalPointerCapture = Element.prototype.setPointerCapture; |
166 | | - Element.prototype.setPointerCapture = vi.fn(); |
167 | | - usePlayerStore.setState({ elements: [clip({ audioGroup: "vo", audioGroupLabel: "VO" })] }); |
| 180 | + it("keeps authored gain 2 above unity and writes exactly 1 at the midpoint", () => { |
| 181 | + usePlayerStore.setState({ elements: [clip({ audioGroup: "vo", audioGroupVolume: 2 })] }); |
168 | 182 | const { host } = mount(); |
169 | | - |
170 | | - const groupFader = host.querySelector<HTMLElement>('[aria-label="VO volume"]')!; |
171 | | - act(() => { |
172 | | - groupFader.dispatchEvent( |
173 | | - new PointerEvent("pointerdown", { bubbles: true, pointerId: 1, clientY: 25 }), |
174 | | - ); |
175 | | - }); |
176 | | - // clientY 25 on a 0..100 track (stubTrackRect) is fraction 0.75; asserting |
177 | | - // the exact computed value (not just "a string") also catches a NaN regression. |
178 | | - const expectedVolume = String(fractionToLevel(0.75)); |
179 | | - expect(onSetAudioGroupAttributeLive).toHaveBeenCalledWith("vo", "data-volume", expectedVolume); |
180 | | - expect(onSetAudioGroupAttributeQuiet).not.toHaveBeenCalled(); |
181 | | - act(() => { |
182 | | - groupFader.dispatchEvent( |
183 | | - new PointerEvent("pointerup", { bubbles: true, pointerId: 1, clientY: 25 }), |
184 | | - ); |
185 | | - }); |
186 | | - expect(onSetAudioGroupAttributeQuiet).toHaveBeenCalledWith( |
| 183 | + const fader = host.querySelector<HTMLElement>('[aria-label="vo volume"]')!; |
| 184 | + expect(Number(fader.getAttribute("aria-valuenow"))).toBeGreaterThan(0); |
| 185 | + expect(Number(fader.getAttribute("aria-valuenow"))).toBeLessThan(100); |
| 186 | + expect(fader.getAttribute("aria-valuetext")).toBe("+6.0 dB"); |
| 187 | + expect(parseFloat(fader.querySelector<HTMLElement>("div")!.style.bottom)).toBeGreaterThan(50); |
| 188 | + drag(fader, 50); |
| 189 | + expect(onSetAudioGroupAttributeLive).toHaveBeenCalledWith("vo", "data-volume", "1"); |
| 190 | + expect(onSetAudioGroupAttributeQuiet).toHaveBeenCalledExactlyOnceWith( |
187 | 191 | "vo", |
188 | 192 | "data-volume", |
189 | | - expectedVolume, |
| 193 | + "1", |
190 | 194 | "Set volume", |
191 | 195 | ); |
| 196 | + }); |
192 | 197 |
|
193 | | - const monitorFader = host.querySelector<HTMLElement>('[aria-label="Monitor volume"]')!; |
194 | | - act(() => { |
195 | | - monitorFader.dispatchEvent( |
196 | | - new PointerEvent("pointerdown", { bubbles: true, pointerId: 2, clientY: 0 }), |
197 | | - ); |
198 | | - }); |
199 | | - expect(usePlayerStore.getState().audioVolume).toBe(1); |
200 | | - act(() => { |
201 | | - monitorFader.dispatchEvent( |
202 | | - new PointerEvent("pointerdown", { bubbles: true, pointerId: 2, clientY: 100 }), |
203 | | - ); |
204 | | - }); |
205 | | - expect(usePlayerStore.getState().audioVolume).toBe(0); |
| 198 | + it("drags a group to the shared +12 dB ceiling", () => { |
| 199 | + usePlayerStore.setState({ elements: [clip({ audioGroup: "vo" })] }); |
| 200 | + const fader = mount().host.querySelector<HTMLElement>('[aria-label="vo volume"]')!; |
| 201 | + drag(fader, 0); |
| 202 | + expect(Number(onSetAudioGroupAttributeQuiet.mock.calls[0]![2])).toBeCloseTo(MAX_AUDIO_GAIN, 6); |
| 203 | + expect(onSetAudioGroupAttributeLive).toHaveBeenCalledWith("vo", "data-volume", "3.981072"); |
| 204 | + }); |
206 | 205 |
|
207 | | - Element.prototype.setPointerCapture = originalPointerCapture; |
208 | | - restoreRect(); |
| 206 | + it.each([0.5, 1, 2, 3.98])( |
| 207 | + "round-trips authored group gain %s through its displayed thumb", |
| 208 | + (gain) => { |
| 209 | + usePlayerStore.setState({ elements: [clip({ audioGroup: "vo", audioGroupVolume: gain })] }); |
| 210 | + const fader = mount().host.querySelector<HTMLElement>('[aria-label="vo volume"]')!; |
| 211 | + const thumb = fader.firstElementChild as HTMLElement; |
| 212 | + drag(fader, 100 - parseFloat(thumb.style.bottom)); |
| 213 | + expect(Number(onSetAudioGroupAttributeQuiet.mock.calls[0]![2])).toBeCloseTo(gain, 6); |
| 214 | + }, |
| 215 | + ); |
| 216 | + |
| 217 | + it("preserves low authored gain instead of rounding it to mute", () => { |
| 218 | + usePlayerStore.setState({ elements: [clip({ audioGroup: "vo" })] }); |
| 219 | + const fader = mount().host.querySelector<HTMLElement>('[aria-label="vo volume"]')!; |
| 220 | + drag(fader, 85); |
| 221 | + expect(onSetAudioGroupAttributeQuiet).toHaveBeenCalledExactlyOnceWith( |
| 222 | + "vo", |
| 223 | + "data-volume", |
| 224 | + "0.007943", |
| 225 | + "Set volume", |
| 226 | + ); |
209 | 227 | }); |
210 | 228 |
|
211 | | - it("the monitor fader writes setAudioVolume and is labeled Monitor", () => { |
212 | | - const restoreRect = stubTrackRect(); |
213 | | - const originalPointerCapture = Element.prototype.setPointerCapture; |
214 | | - Element.prototype.setPointerCapture = vi.fn(); |
215 | | - const setAudioVolume = vi.fn((volume: number) => { |
216 | | - usePlayerStore.setState({ audioVolume: volume }); |
217 | | - }); |
218 | | - usePlayerStore.setState({ elements: [clip({})], audioVolume: 1, setAudioVolume }); |
219 | | - const { host } = mount(); |
220 | | - expect(host.textContent).toContain("Monitor"); |
221 | | - const fader = host.querySelector<HTMLElement>('[aria-label="Monitor volume"]')!; |
222 | | - expect(fader.getAttribute("title")).toBe("Preview monitor volume"); |
223 | | - act(() => { |
224 | | - fader.dispatchEvent( |
225 | | - new PointerEvent("pointerdown", { bubbles: true, pointerId: 1, clientY: 100 }), |
226 | | - ); |
227 | | - }); |
228 | | - expect(setAudioVolume).toHaveBeenCalledWith(0); |
229 | | - Element.prototype.setPointerCapture = originalPointerCapture; |
230 | | - restoreRect(); |
| 229 | + it("keeps the Monitor fader within the player store range", () => { |
| 230 | + usePlayerStore.setState({ elements: [clip({})] }); |
| 231 | + const fader = mount().host.querySelector<HTMLElement>('[aria-label="Monitor volume"]')!; |
| 232 | + expect(fader.getAttribute("aria-valuemin")).toBe("-100"); |
| 233 | + expect(fader.getAttribute("aria-valuemax")).toBe("0"); |
| 234 | + drag(fader, 100); |
| 235 | + expect(usePlayerStore.getState().audioVolume).toBe(0); |
| 236 | + drag(fader, 0); |
| 237 | + expect(usePlayerStore.getState().audioVolume).toBe(1); |
| 238 | + act(() => fader.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowUp", bubbles: true }))); |
| 239 | + expect(usePlayerStore.getState().audioVolume).toBe(1); |
| 240 | + expect(fader.getAttribute("aria-valuetext")).toBe("0.0 dB"); |
| 241 | + expect(onSetAudioGroupAttributeQuiet).not.toHaveBeenCalled(); |
231 | 242 | }); |
232 | 243 |
|
233 | | - it("nudges a consistent step in the visual (dB-scale) position, and aria-valuenow tracks the thumb", () => { |
234 | | - usePlayerStore.setState({ elements: [clip({})], audioVolume: 0.5 }); |
235 | | - const { host } = mount(); |
236 | | - const masterFader = host.querySelector<HTMLElement>('[aria-label="Monitor volume"]')!; |
237 | | - const startFraction = levelToFraction(0.5); |
238 | | - expect(masterFader.getAttribute("aria-valuenow")).toBe(String(Math.round(startFraction * 100))); |
| 244 | + it.each(["pointercancel", "lostpointercapture"])( |
| 245 | + "finishes %s once and ignores subsequent hovering", |
| 246 | + (event) => { |
| 247 | + const restoreRect = stubTrackRect(); |
| 248 | + const original = Element.prototype.setPointerCapture; |
| 249 | + Element.prototype.setPointerCapture = vi.fn(); |
| 250 | + try { |
| 251 | + usePlayerStore.setState({ elements: [clip({ audioGroup: "vo" })] }); |
| 252 | + const fader = mount().host.querySelector<HTMLElement>('[aria-label="vo volume"]')!; |
| 253 | + for (const [type, clientY] of [ |
| 254 | + ["pointerdown", 50], |
| 255 | + [event, 0], |
| 256 | + ["pointermove", 0], |
| 257 | + ["pointerup", 0], |
| 258 | + ] as const) { |
| 259 | + act(() => |
| 260 | + fader.dispatchEvent(new PointerEvent(type, { bubbles: true, pointerId: 1, clientY })), |
| 261 | + ); |
| 262 | + } |
| 263 | + expect(onSetAudioGroupAttributeLive).toHaveBeenCalledExactlyOnceWith( |
| 264 | + "vo", |
| 265 | + "data-volume", |
| 266 | + "1", |
| 267 | + ); |
| 268 | + expect(onSetAudioGroupAttributeQuiet).toHaveBeenCalledExactlyOnceWith( |
| 269 | + "vo", |
| 270 | + "data-volume", |
| 271 | + "1", |
| 272 | + "Set volume", |
| 273 | + ); |
| 274 | + } finally { |
| 275 | + Element.prototype.setPointerCapture = original; |
| 276 | + restoreRect(); |
| 277 | + } |
| 278 | + }, |
| 279 | + ); |
239 | 280 |
|
240 | | - act(() => { |
241 | | - masterFader.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowUp", bubbles: true })); |
242 | | - }); |
243 | | - const afterUp = usePlayerStore.getState().audioVolume; |
244 | | - const fractionAfterUp = levelToFraction(afterUp); |
245 | | - // A flat step in raw volume (the old, buggy behaviour) would move the |
246 | | - // visual position by a very different amount depending on where it starts; |
247 | | - // stepping in fraction space keeps every step visually the same size. |
248 | | - expect(fractionAfterUp - startFraction).toBeCloseTo(0.02, 6); |
249 | | - expect(masterFader.getAttribute("aria-valuenow")).toBe( |
250 | | - String(Math.round(fractionAfterUp * 100)), |
| 281 | + it.each([ |
| 282 | + ["Home", "0"], |
| 283 | + ["End", "3.981072"], |
| 284 | + ["PageUp", "1.318257"], |
| 285 | + ["PageDown", "0.251189"], |
| 286 | + ])("supports the %s slider key", (key, gain) => { |
| 287 | + usePlayerStore.setState({ elements: [clip({ audioGroup: "vo" })] }); |
| 288 | + const fader = mount().host.querySelector<HTMLElement>('[aria-label="vo volume"]')!; |
| 289 | + act(() => fader.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }))); |
| 290 | + expect(onSetAudioGroupAttributeQuiet).toHaveBeenCalledExactlyOnceWith( |
| 291 | + "vo", |
| 292 | + "data-volume", |
| 293 | + gain, |
| 294 | + "Set volume", |
251 | 295 | ); |
| 296 | + }); |
252 | 297 |
|
253 | | - act(() => usePlayerStore.setState({ audioVolume: 0.5 })); |
254 | | - act(() => { |
255 | | - masterFader.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true })); |
256 | | - }); |
257 | | - const afterDown = usePlayerStore.getState().audioVolume; |
258 | | - expect(startFraction - levelToFraction(afterDown)).toBeCloseTo(0.02, 6); |
| 298 | + it("nudges by an equal visual step and exposes the gain readout", () => { |
| 299 | + usePlayerStore.setState({ elements: [clip({})], audioVolume: 1 }); |
| 300 | + const fader = mount().host.querySelector<HTMLElement>('[aria-label="Monitor volume"]')!; |
| 301 | + act(() => |
| 302 | + fader.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true })), |
| 303 | + ); |
| 304 | + expect(fader.getAttribute("aria-valuenow")).toBe("-2"); |
| 305 | + expect(fader.getAttribute("aria-valuetext")).toBe("-1.2 dB"); |
| 306 | + act(() => fader.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowUp", bubbles: true }))); |
| 307 | + expect(usePlayerStore.getState().audioVolume).toBeCloseTo(1, 12); |
| 308 | + expect(fader.getAttribute("aria-valuenow")).toBe("0"); |
259 | 309 | }); |
260 | 310 | }); |
261 | 311 |
|
|
0 commit comments