diff --git a/e2e/main-view-sound.spec.ts b/e2e/main-view-sound.spec.ts new file mode 100644 index 0000000..43185fb --- /dev/null +++ b/e2e/main-view-sound.spec.ts @@ -0,0 +1,189 @@ +import { expect, test, type BrowserContext, type Page } from '@playwright/test' +import { setLayerMoves } from './cubeView3DHelpers' + +// Sound on the main 3D view on the PuzlWerk viewer (`?viewer=puzlwerk`): the Sound toggle of the existing +// toolbar, the click of a finished turn and the cue of a selected wide turn. A stand-in for Web Audio +// counts the sounds the page starts (`window.__sound`). +const PUZLWERK = '.cube-card .cube-3d-puzlwerk' +const SOUND_COOKIE = 'cube-assembler-turn-sound' +const base = () => `http://127.0.0.1:${process.env.E2E_PORT ?? '4174'}` + +async function countSounds(page: Page) { + await page.addInitScript(() => { + const counts = { clicks: 0, cues: 0 } + ;(window as unknown as { __sound: typeof counts }).__sound = counts + const node = (): Record => { + const self: Record = { + connect: (next: unknown) => next, + start: () => {}, + stop: () => {}, + frequency: { value: 0 }, + Q: { value: 0 }, + gain: { + setValueAtTime: () => {}, + exponentialRampToValueAtTime: () => {}, + }, + } + return self + } + class FakeAudio { + state = 'running' + currentTime = 0 + sampleRate = 8000 + destination = node() + resume() {} + createBuffer(_c: number, length: number) { + return { getChannelData: () => new Float32Array(length) } + } + createBufferSource() { + counts.clicks += 1 + return node() + } + createBiquadFilter() { + return node() + } + createGain() { + return node() + } + createOscillator() { + counts.cues += 1 + return node() + } + } + ;(window as unknown as { AudioContext: unknown }).AudioContext = FakeAudio + }) +} + +const sounds = (page: Page) => + page.evaluate( + () => + (window as unknown as { __sound: { clicks: number; cues: number } }) + .__sound, + ) + +async function setSound(context: BrowserContext, on: boolean) { + await context.addCookies([ + { name: SOUND_COOKIE, value: on ? '1' : '0', url: base() }, + { name: 'cube-assembler-turn-ms', value: '60', url: base() }, + { name: 'cube-assembler-scramble-length', value: 'short', url: base() }, + { name: 'cube-assembler-view-help', value: '0', url: base() }, + ]) +} + +async function openMainView(page: Page) { + await page.goto('/?viewer=puzlwerk') + await page.getByRole('button', { name: 'Solved cube' }).click() + await page.getByRole('button', { name: '3D View' }).click() + await expect(page.locator(`${PUZLWERK} canvas`)).toBeVisible() + await expect(page.locator(PUZLWERK)).toHaveAttribute('data-interaction', /.+/) +} + +const soundButton = (page: Page) => + page.locator('.cube-card .cube-3d-actions').getByRole('button', { + name: 'Sound', + exact: true, + }) + +async function drag(page: Page, shift = false) { + const bounds = await page.locator(PUZLWERK).boundingBox() + if (!bounds) throw new Error('No stage') + const cx = bounds.x + bounds.width / 2 + const cy = bounds.y + bounds.height / 2 + if (shift) await page.keyboard.down('Shift') + await page.mouse.move(cx - 60, cy + 40) + await page.mouse.down() + await page.mouse.move(cx - 60, cy - 30, { steps: 6 }) + await page.mouse.up() + if (shift) await page.keyboard.up('Shift') +} + +test('the Sound toggle sits with the other toggles and stores the setting', async ({ + page, + context, +}) => { + await setSound(context, true) + await openMainView(page) + const labels = page.locator('.cube-card .cube-3d-actions button') + await expect(labels).toHaveText([ + 'Stickerless', + 'Auto-rotate', + 'Move layers', + 'Sound', + ]) + await expect(soundButton(page)).toHaveAttribute('aria-pressed', 'true') + await expect(soundButton(page)).toHaveClass(/cube-3d-btn-active/) + await soundButton(page).click() + await expect(soundButton(page)).toHaveAttribute('aria-pressed', 'false') + await expect(soundButton(page)).not.toHaveClass(/cube-3d-btn-active/) + const stored = async () => + (await context.cookies()).find((c) => c.name === SOUND_COOKIE)?.value + expect(await stored()).toBe('0') + await soundButton(page).click() + expect(await stored()).toBe('1') +}) + +test('a turn by gesture clicks with sound on and stays silent with it off', async ({ + page, + context, +}) => { + await countSounds(page) + await setLayerMoves(context) + await setSound(context, true) + await openMainView(page) + expect((await sounds(page)).clicks).toBe(0) + await drag(page) + await expect(page.locator(PUZLWERK)).toHaveAttribute('data-turn', 'end') + await expect.poll(async () => (await sounds(page)).clicks).toBe(1) + await soundButton(page).click() + await drag(page) + // The same layer again: the history merges the two quarter turns. + await expect(page.locator('.cube-card .cube-3d-move-history')).toHaveText( + /^Moves: \S+2$/, + ) + await page.waitForTimeout(500) + expect((await sounds(page)).clicks).toBe(1) +}) + +test('each step of a scramble and an undo click; a stopped-sound scramble does not', async ({ + page, + context, +}) => { + await countSounds(page) + await setSound(context, true) + await openMainView(page) + const scramble = page + .locator('.cube-card') + .getByRole('button', { name: 'Scramble', exact: true }) + await scramble.click() + await expect(scramble).toBeVisible({ timeout: 60_000 }) + // A short 3x3 scramble is ten turns. + await expect.poll(async () => (await sounds(page)).clicks).toBe(10) + await page + .locator('.cube-card') + .getByRole('button', { name: 'Undo', exact: true }) + .click() + await expect.poll(async () => (await sounds(page)).clicks).toBe(11) + await soundButton(page).click() + await scramble.click() + await expect(scramble).toBeVisible({ timeout: 60_000 }) + await page.waitForTimeout(500) + expect((await sounds(page)).clicks).toBe(11) +}) + +test('selecting a wide block plays the cue once, only with sound on', async ({ + page, + context, +}) => { + await countSounds(page) + await setLayerMoves(context) + await setSound(context, true) + await openMainView(page) + await drag(page, true) + await expect(page.locator(PUZLWERK)).toHaveAttribute('data-turn', 'end') + expect((await sounds(page)).cues).toBe(1) + await soundButton(page).click() + await drag(page, true) + await expect(page.locator(PUZLWERK)).toHaveAttribute('data-turn', 'end') + await page.waitForTimeout(300) + expect((await sounds(page)).cues).toBe(1) +}) diff --git a/src/client/view3d/cubeView3D.tsx b/src/client/view3d/cubeView3D.tsx index 5057c6e..7597b78 100644 --- a/src/client/view3d/cubeView3D.tsx +++ b/src/client/view3d/cubeView3D.tsx @@ -4,12 +4,11 @@ import { lockedHintDue } from './cubeGesture' import { LAYER_MOVES_COOKIE, STICKERLESS_COOKIE, - TURN_SOUND_COOKIE, layerMovesOn, preferenceCookie, readPreference, - turnSoundOn, } from '../preferences' +import { useTurnSound } from './turnSound' import type { FaceKey } from '../../cube/cubeAssembly' import type { CubeState } from '../../cube/cubeAssembly' import type { colorFrame as ColorFrame } from '../../core/view/AxisGizmo.gen' @@ -129,12 +128,7 @@ export function CubeView3D({ const [layerMoves, setLayerMoves] = useState(() => layerMovesOn(document.cookie), ) - const [turnSound, setTurnSound] = useState(() => turnSoundOn(document.cookie)) - const toggleTurnSound = () => { - const enabled = !turnSoundOn(document.cookie) - document.cookie = preferenceCookie(TURN_SOUND_COOKIE, enabled) - setTurnSound(enabled) - } + const [turnSound, toggleTurnSound] = useTurnSound() // Dragging a sticker while locked explains the lock, at most once per gap. const [lockedHint, setLockedHint] = useState(false) const lockedHintShownAtRef = useRef(null) diff --git a/src/client/view3d/cubeView3DPresentation.tsx b/src/client/view3d/cubeView3DPresentation.tsx index bf226af..b46bb76 100644 --- a/src/client/view3d/cubeView3DPresentation.tsx +++ b/src/client/view3d/cubeView3DPresentation.tsx @@ -14,6 +14,7 @@ import { undoDisabled, } from './cubeToolbarSections' import { useCubeHelpHint } from './useCubeHelpHint' +import { TurnSoundButton } from './turnSound' interface CubeView3DPresentationProps { isSupported: boolean @@ -294,30 +295,7 @@ export function CubeView3DPresentation({ {' '} Move layers - + diff --git a/src/client/view3d/puzlwerkCubeView.tsx b/src/client/view3d/puzlwerkCubeView.tsx index 969b2d6..9c3185c 100644 --- a/src/client/view3d/puzlwerkCubeView.tsx +++ b/src/client/view3d/puzlwerkCubeView.tsx @@ -32,6 +32,7 @@ import { applyConstruction, mountMainPlay, type MainPlay } from './puzlwerkPlay' import { arrowOrientation, keyPreset } from './puzlwerkBars' import { PuzlwerkToolbar } from './puzlwerkToolbar' import { usePuzlwerkTurns } from './usePuzlwerkTurns' +import { useTurnSound } from './turnSound' import { resetDisabled, undoDisabled } from './cubeToolbarSections' import type { CubeTurn } from './cubeView3DState' import { @@ -133,6 +134,7 @@ export function PuzlwerkCubeView({ playInputsRef.current = { palette, seamWide: hints.seamWide } turns.invalidate() } + const [turnSound, toggleTurnSound] = useTurnSound() const turnsRef = useRef(turns) turnsRef.current = turns // The display orientation the stage reported last, for the arrow keys. @@ -397,6 +399,8 @@ export function PuzlwerkCubeView({ onToggleAutoRotate={() => setAutoRotate((value) => !value)} layerMoves={layerMoves} onToggleLayerMoves={() => setLayerMoves((value) => !value)} + turnSound={turnSound} + onToggleTurnSound={toggleTurnSound} cube={{ isScrambling: turns.scrambling, onToggleScramble: turns.toggleScramble, diff --git a/src/client/view3d/puzlwerkToolbar.tsx b/src/client/view3d/puzlwerkToolbar.tsx index 53e1256..25075f1 100644 --- a/src/client/view3d/puzlwerkToolbar.tsx +++ b/src/client/view3d/puzlwerkToolbar.tsx @@ -1,6 +1,6 @@ // The controls under the stage of the main 3D view on the PuzlWerk viewer, with the classes and markup of // the existing toolbar: the FACES presets, the CUBE group (scramble, random state, undo, reset), the move -// history and the Stickerless, Auto-rotate and Move layers toggles. The CUBE group and the history are the +// history and the Stickerless, Auto-rotate, Move layers and Sound toggles. The CUBE group and the history are the // parts the existing view shares (cubeToolbarSections.tsx); their state is held by usePuzlwerkTurns.ts. import type { ViewPreset } from '@puzlwerk/viewer' import { @@ -10,6 +10,7 @@ import { } from './cubeToolbarSections' import type { CubeTurn } from './cubeView3DState' import { autoRotateLabel, FACE_BUTTONS, stickerlessLabel } from './puzlwerkBars' +import { TurnSoundButton } from './turnSound' export interface PuzlwerkToolbarProps { onPreset: (preset: ViewPreset) => void @@ -19,6 +20,8 @@ export interface PuzlwerkToolbarProps { onToggleAutoRotate: () => void layerMoves: boolean onToggleLayerMoves: () => void + turnSound: boolean + onToggleTurnSound: () => void // The CUBE group, and the moves and size of the history under it. cube: CubeActionsSectionProps moves: CubeTurn[] @@ -33,6 +36,8 @@ export function PuzlwerkToolbar({ onToggleAutoRotate, layerMoves, onToggleLayerMoves, + turnSound, + onToggleTurnSound, cube, moves, size, @@ -100,6 +105,7 @@ export function PuzlwerkToolbar({ {' '} Move layers + ) diff --git a/src/client/view3d/turnSound.tsx b/src/client/view3d/turnSound.tsx new file mode 100644 index 0000000..ee6d767 --- /dev/null +++ b/src/client/view3d/turnSound.tsx @@ -0,0 +1,75 @@ +// The turn sound setting and its toolbar button, shared by the existing 3D view and the PuzlWerk main +// view: the cookie, the gains of the click and of the wide-turn cue, and the Sound button's markup. +import { useState } from 'preact/hooks' +import { + preferenceCookie, + TURN_SOUND_COOKIE, + turnSoundOn, +} from '../preferences' +import { + MODE_CUE_GAIN, + playTurnClick, + playWideCue, + turnClickGain, +} from './turnFeel' + +export const nextTurnSound = (cookies: string): boolean => !turnSoundOn(cookies) + +// Gain of the click of a finished turn that took `durationMs`; 0 when sound is off. +export const turnClickGainFor = (durationMs: number, cookies: string): number => + turnClickGain(durationMs, turnSoundOn(cookies)) + +export const wideCueGainFor = (cookies: string): number => + turnSoundOn(cookies) ? MODE_CUE_GAIN : 0 + +// The click of a finished turn, as the existing view plays it. +export const playFinishedTurnClick = (durationMs: number): void => + playTurnClick(turnClickGainFor(durationMs, document.cookie)) + +// The cue of a selected wide turn. +export const playWideTurnCue = (): void => + playWideCue(wideCueGainFor(document.cookie)) + +// The setting as state; a toggle stores the cookie. +export function useTurnSound(): [boolean, () => void] { + const [on, setOn] = useState(() => turnSoundOn(document.cookie)) + const toggle = () => { + const enabled = nextTurnSound(document.cookie) + document.cookie = preferenceCookie(TURN_SOUND_COOKIE, enabled) + setOn(enabled) + } + return [on, toggle] +} + +export function TurnSoundButton({ + on, + onToggle, +}: { + on: boolean + onToggle: () => void +}) { + return ( + + ) +} diff --git a/src/client/view3d/useCubeGestures.ts b/src/client/view3d/useCubeGestures.ts index 5848e04..7eccc0e 100644 --- a/src/client/view3d/useCubeGestures.ts +++ b/src/client/view3d/useCubeGestures.ts @@ -36,10 +36,9 @@ import { SEAM_WIDE_COOKIE, readVibrationMs, layerMovesOn, - turnSoundOn, vibrationOn, } from '../preferences' -import { MODE_CUE_GAIN, playWideCue } from './turnFeel' +import { playWideTurnCue } from './turnSound' import type { useCubeCamera } from './useCubeCamera' import type { useCubeTurns } from './useCubeTurns' @@ -137,7 +136,7 @@ export function useCubeGestures({ const announceWideTurn = () => { setPressMode('wide') const cookies = document.cookie - playWideCue(turnSoundOn(cookies) ? MODE_CUE_GAIN : 0) + playWideTurnCue() try { if (vibrationOn(cookies)) navigator.vibrate?.(readVibrationMs(cookies)) } catch { diff --git a/src/client/view3d/useCubeTurns.ts b/src/client/view3d/useCubeTurns.ts index 937dfbf..66e2bfe 100644 --- a/src/client/view3d/useCubeTurns.ts +++ b/src/client/view3d/useCubeTurns.ts @@ -21,13 +21,9 @@ import { import type { DragTurn } from './cubeDragInteraction' import type { buildCubeMesh } from './cubeMesh' import { generateScrambleMoves, type CubeTurn } from './cubeView3DState' -import { readScrambleOptions, readTurnFeel, turnSoundOn } from '../preferences' -import { - magneticDragAngle, - playTurnClick, - scrambleDuration, - turnClickGain, -} from './turnFeel' +import { readScrambleOptions, readTurnFeel } from '../preferences' +import { magneticDragAngle, scrambleDuration } from './turnFeel' +import { playFinishedTurnClick } from './turnSound' // The layers the mesh shows turned this frame, and whether the sticker // colors must be rebuilt too. @@ -126,7 +122,7 @@ export function useCubeTurns({ anim, ) if (!turned) return - playTurnClick(turnClickGain(anim.duration, turnSoundOn(document.cookie))) + playFinishedTurnClick(anim.duration) currentCubeRef.current = turned.cube setCurrentCube(turned.cube) movesRef.current = turned.moves diff --git a/src/client/view3d/usePuzlwerkHints.ts b/src/client/view3d/usePuzlwerkHints.ts index f374be3..ffbfe6a 100644 --- a/src/client/view3d/usePuzlwerkHints.ts +++ b/src/client/view3d/usePuzlwerkHints.ts @@ -16,6 +16,7 @@ import { wideBadgeAfter, } from './puzlwerkHints' import { useCubeHelpHint } from './useCubeHelpHint' +import { playWideTurnCue } from './turnSound' export interface PuzlwerkHintsOptions { size: number @@ -75,6 +76,8 @@ export function usePuzlwerkHints({ size, locked }: PuzlwerkHintsOptions) { onPlayEvent: (event: PlayEvent) => { const next = wideBadgeAfter(wideShown.current, event, latest.current.size) if (next === wideShown.current) return + // The cue sounds when a wide block is selected, as in the existing view. + if (next) playWideTurnCue() wideShown.current = next setWideBadge(next) }, diff --git a/src/client/view3d/usePuzlwerkTurns.ts b/src/client/view3d/usePuzlwerkTurns.ts index ca6d876..ccc5f55 100644 --- a/src/client/view3d/usePuzlwerkTurns.ts +++ b/src/client/view3d/usePuzlwerkTurns.ts @@ -22,6 +22,7 @@ import { } from './puzlwerkMainState' import type { MainPlay } from './puzlwerkPlay' import { scrambleDuration } from './turnFeel' +import { playFinishedTurnClick } from './turnSound' export interface PuzlwerkTurnsOptions { size: number @@ -134,8 +135,11 @@ export function usePuzlwerkTurns({ const undo = async () => { const turn = undoTurn(stateRef.current) if (!turn || inFlightRef.current > 0 || beginRef.current) return - if (await playTurn(turn, readTurnFeel(document.cookie).turnMs)) + const durationMs = readTurnFeel(document.cookie).turnMs + if (await playTurn(turn, durationMs)) { + playFinishedTurnClick(durationMs) put(afterUndo(stateRef.current, sizeRef.current), true) + } } const reset = () => show(afterReset(sourceRef.current, sizeRef.current), true) @@ -164,6 +168,7 @@ export function usePuzlwerkTurns({ for (const turn of turns) { if (run.stopped) break if (!(await playTurn(turn, durationMs))) break + playFinishedTurnClick(durationMs) put(afterTurn(stateRef.current, sizeRef.current, turn), true) } if (runRef.current === run) { @@ -184,7 +189,10 @@ export function usePuzlwerkTurns({ setDragging(false) if (begin && event.outcome === 'committed') { const turn = committedTurn(sizeRef.current, begin, event.turns) - if (turn) put(afterTurn(stateRef.current, sizeRef.current, turn), true) + if (turn) { + playFinishedTurnClick(readTurnFeel(document.cookie).turnMs) + put(afterTurn(stateRef.current, sizeRef.current, turn), true) + } } } } diff --git a/test/puzlwerkToolbar.test.ts b/test/puzlwerkToolbar.test.ts index ef41dc1..7bc540e 100644 --- a/test/puzlwerkToolbar.test.ts +++ b/test/puzlwerkToolbar.test.ts @@ -16,6 +16,8 @@ const render = (over: Record = {}) => onToggleAutoRotate: noop, layerMoves: false, onToggleLayerMoves: noop, + turnSound: false, + onToggleTurnSound: noop, cube: { isScrambling: false, onToggleScramble: noop, @@ -112,6 +114,7 @@ describe('main view toolbar groups', () => { 'Stickerless', 'Auto-rotate', 'Move layers', + 'Sound', ]) }) }) diff --git a/test/turnSound.test.ts b/test/turnSound.test.ts new file mode 100644 index 0000000..7064b52 --- /dev/null +++ b/test/turnSound.test.ts @@ -0,0 +1,109 @@ +import { h } from 'preact' +import { describe, expect, it, vi } from 'vitest' +import { + nextTurnSound, + TurnSoundButton, + turnClickGainFor, + wideCueGainFor, +} from '../src/client/view3d/turnSound' +import { MODE_CUE_GAIN } from '../src/client/view3d/turnFeel' +import { PuzlwerkToolbar } from '../src/client/view3d/puzlwerkToolbar' +import { classOf, expand, findAll, text } from './support/vnodeTree' + +const ON = 'cube-assembler-turn-sound=1' +const OFF = 'cube-assembler-turn-sound=0' +const noop = () => {} + +describe('turn sound setting', () => { + it('toggles what the cookie says', () => { + expect(nextTurnSound(ON)).toBe(false) + expect(nextTurnSound(OFF)).toBe(true) + }) + it('is silent when off and softer for the quick turns of a scramble', () => { + expect(turnClickGainFor(160, OFF)).toBe(0) + expect(turnClickGainFor(85, OFF)).toBe(0) + const turn = turnClickGainFor(160, ON) + const scramble = turnClickGainFor(85, ON) + expect(turn).toBeGreaterThan(0) + expect(scramble).toBeGreaterThan(0) + expect(scramble).toBeLessThan(turn) + }) + it('sounds the wide cue only when on', () => { + expect(wideCueGainFor(OFF)).toBe(0) + expect(wideCueGainFor(ON)).toBe(MODE_CUE_GAIN) + }) +}) + +describe('Sound button', () => { + const render = (on: boolean, onToggle = noop) => + findAll( + expand(h(TurnSoundButton, { on, onToggle })), + (n) => n.tag === 'button', + )[0] + it('has the markup of the existing toolbar: label, pressed state, active class', () => { + const off = render(false) + expect(text(off).trim()).toBe('Sound') + expect(off.props['aria-pressed']).toBe(false) + expect(classOf(off)).toEqual(['cube-3d-btn', ''].filter(Boolean)) + const on = render(true) + expect(on.props['aria-pressed']).toBe(true) + expect(classOf(on)).toEqual(['cube-3d-btn', 'cube-3d-btn-active']) + }) + it('calls the toggle', () => { + const onToggle = vi.fn() + ;(render(false, onToggle).props.onClick as () => void)() + expect(onToggle).toHaveBeenCalledTimes(1) + }) +}) + +describe('main view toolbar sound toggle', () => { + const bar = (turnSound: boolean) => + expand( + h(PuzlwerkToolbar, { + onPreset: noop, + stickerless: true, + onToggleStickerless: noop, + autoRotate: false, + onToggleAutoRotate: noop, + layerMoves: false, + onToggleLayerMoves: noop, + turnSound, + onToggleTurnSound: noop, + cube: { + isScrambling: false, + onToggleScramble: noop, + onRandomState: noop, + onUndo: noop, + onReset: noop, + undoDisabled: false, + resetDisabled: false, + }, + moves: [], + size: 3, + }), + ) + it('ends the toggles with Sound, after Move layers', () => { + const actions = findAll(bar(true), (n) => + classOf(n).includes('cube-3d-actions'), + )[0] + const labels = findAll([actions], (n) => n.tag === 'button').map((b) => + text(b).trim(), + ) + expect(labels).toEqual([ + 'Stickerless', + 'Auto-rotate', + 'Move layers', + 'Sound', + ]) + }) + it('shows the state it is given', () => { + const pressed = (on: boolean) => + findAll( + bar(on), + (n) => + n.props['aria-pressed'] !== undefined && text(n).trim() === 'Sound', + )[0].props['aria-pressed'] + expect(pressed(true)).toBe(true) + expect(pressed(false)).toBe(false) + }) +}) diff --git a/test/usePuzlwerkTurns.test.ts b/test/usePuzlwerkTurns.test.ts index 785adc5..3104cec 100644 --- a/test/usePuzlwerkTurns.test.ts +++ b/test/usePuzlwerkTurns.test.ts @@ -10,9 +10,14 @@ import { type PuzlwerkTurnsOptions, } from '../src/client/view3d/usePuzlwerkTurns' import type { MainPlay } from '../src/client/view3d/puzlwerkPlay' +import { playFinishedTurnClick } from '../src/client/view3d/turnSound' import { renderHook } from './support/hookHarness' vi.mock('preact/hooks', () => import('./support/hookHarness')) +vi.mock('../src/client/view3d/turnSound', async (original) => ({ + ...(await original()), + playFinishedTurnClick: vi.fn(), +})) // A player whose turns end when the test says so. function controlledPlay() { @@ -58,6 +63,49 @@ function setup( beforeEach(() => { vi.stubGlobal('document', { cookie: '' }) + vi.mocked(playFinishedTurnClick).mockClear() +}) + +describe('the click of a finished turn', () => { + it('sounds once for a finished scramble step and for an undo', async () => { + const { hook, calls } = setup(3, [{ face: 'R', depth: 1, turns: 1 }]) + hook.result.current.toggleScramble() + calls[0].end(true) + await settle() + expect(playFinishedTurnClick).toHaveBeenCalledTimes(1) + // Stop: the step in progress still ends and clicks. + hook.result.current.toggleScramble() + hook.flush() + calls[1].end(true) + await settle() + expect(playFinishedTurnClick).toHaveBeenCalledTimes(2) + void hook.result.current.undo() + expect(calls).toHaveLength(3) + calls[2].end(true) + await settle() + expect(playFinishedTurnClick).toHaveBeenCalledTimes(3) + }) + + it('stays silent for a turn the player refused or that ends after Reset, Random State or unmount', async () => { + const refused = setup() + refused.hook.result.current.toggleScramble() + refused.calls[0].end(false) + await settle() + const reset = setup() + reset.hook.result.current.toggleScramble() + reset.hook.result.current.reset() + reset.calls[0].end(true) + const random = setup() + random.hook.result.current.toggleScramble() + random.hook.result.current.randomState() + random.calls[0].end(true) + const gone = setup() + gone.hook.result.current.toggleScramble() + gone.hook.unmount() + gone.calls[0].end(true) + await settle() + expect(playFinishedTurnClick).not.toHaveBeenCalled() + }) }) describe('turns the viewer finished', () => { @@ -216,6 +264,7 @@ describe('between the render of a new source and its effect', () => { expect(hook.result.current.state.cube).toBe(next) expect(hook.result.current.state.moves).toEqual([]) expect(onChange).not.toHaveBeenCalled() + expect(playFinishedTurnClick).not.toHaveBeenCalled() }) it('keeps a turn that ends after a render with the same source', async () => {