From 9ef490bec0cb1252a68c13f8e898894edb309467 Mon Sep 17 00:00:00 2001 From: Werner Stein Date: Thu, 19 Nov 2026 10:58:07 +0000 Subject: [PATCH 1/4] feat(view3d): Sound toggle on the PuzlWerk main view The turn sound setting, its gains and the Sound button move into turnSound.tsx and are used by the existing view and by the PuzlWerk toolbar, so both show the same button and store the same cookie. Co-Authored-By: Claude Sonnet 5.5 --- src/client/view3d/cubeView3D.tsx | 10 +- src/client/view3d/cubeView3DPresentation.tsx | 26 +---- src/client/view3d/puzlwerkCubeView.tsx | 4 + src/client/view3d/puzlwerkToolbar.tsx | 8 +- src/client/view3d/turnSound.tsx | 75 +++++++++++++ src/client/view3d/useCubeGestures.ts | 5 +- src/client/view3d/useCubeTurns.ts | 12 +- test/puzlwerkToolbar.test.ts | 3 + test/turnSound.test.ts | 109 +++++++++++++++++++ 9 files changed, 208 insertions(+), 44 deletions(-) create mode 100644 src/client/view3d/turnSound.tsx create mode 100644 test/turnSound.test.ts diff --git a/src/client/view3d/cubeView3D.tsx b/src/client/view3d/cubeView3D.tsx index 5057c6e9..7597b78c 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 bf226afa..b46bb76d 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 969b2d65..9c3185c7 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 53e12561..25075f1a 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 00000000..ee6d7676 --- /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 5848e045..7eccc0e2 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 937dfbf8..66e2bfe4 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/test/puzlwerkToolbar.test.ts b/test/puzlwerkToolbar.test.ts index ef41dc18..7bc540e5 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 00000000..7064b521 --- /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) + }) +}) From dc426d5038379745669a7622feb937461f01ea65 Mon Sep 17 00:00:00 2001 From: Werner Stein Date: Thu, 19 Nov 2026 10:58:07 +0000 Subject: [PATCH 2/4] feat(view3d): turn click and wide cue on the PuzlWerk main view A finished turn (gesture, undo, scramble step) clicks and a selected wide block plays the cue, with the gains of the existing view and only while the Sound toggle is on. The e2e spec counts the sounds a Web Audio stand-in receives. Co-Authored-By: Claude Sonnet 5.5 --- e2e/main-view-sound.spec.ts | 189 ++++++++++++++++++++++++++ src/client/view3d/usePuzlwerkHints.ts | 3 + src/client/view3d/usePuzlwerkTurns.ts | 12 +- 3 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 e2e/main-view-sound.spec.ts diff --git a/e2e/main-view-sound.spec.ts b/e2e/main-view-sound.spec.ts new file mode 100644 index 00000000..43185fba --- /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/usePuzlwerkHints.ts b/src/client/view3d/usePuzlwerkHints.ts index f374be3e..ffbfe6a9 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 ca6d8765..ccc5f559 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) + } } } } From 52941a4ea0025e9c8909b708bcf59ffd87c2c53b Mon Sep 17 00:00:00 2001 From: Werner Stein Date: Thu, 19 Nov 2026 11:46:28 +0000 Subject: [PATCH 3/4] test(view3d): the turn click follows the held state The click sounds once for a finished scramble step and an undo, and not for a turn the player refused or one that ends after Reset, Random State or unmount. Co-Authored-By: Claude Sonnet 5.5 --- test/usePuzlwerkTurns.test.ts | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/usePuzlwerkTurns.test.ts b/test/usePuzlwerkTurns.test.ts index 785adc55..fc2601f3 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', () => { From d00c2cbd5f9f8b568401280f05e8c7b0b55a15ed Mon Sep 17 00:00:00 2001 From: Werner Stein Date: Thu, 19 Nov 2026 13:01:39 +0000 Subject: [PATCH 4/4] test(view3d): no click for a turn that ends before a new source's effect Co-Authored-By: Claude Sonnet 5.5 --- test/usePuzlwerkTurns.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/usePuzlwerkTurns.test.ts b/test/usePuzlwerkTurns.test.ts index fc2601f3..3104cec8 100644 --- a/test/usePuzlwerkTurns.test.ts +++ b/test/usePuzlwerkTurns.test.ts @@ -264,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 () => {