All files / src/helpers advancedMode.ts

100% Statements 23/23
100% Branches 5/5
100% Functions 1/1
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 481x               1x   1x 1x   1x   1x 1x 1x 1x 1x   1x 1x   1x                         1x 1x 3x 3x 3x 3x 3x 1x 1x 3x  
/**
 * Shared pieces of the Konami-unlocked advanced mode, used by both entry points
 * (the popup in `App.tsx` and the standalone options page in `OptionsApp.tsx`).
 * @category Helpers
 * @group Advanced Mode
 * @showCategories
 * @categoryDescription Advanced-mode unlock helpers.
 * @source
 */
 
import { Logger } from '@/utils/Logger';
import { getErrorMessage } from '@/helpers/exceptions';
 
const logger = new Logger('advancedMode');
 
/** Bundled chimes for entering and leaving advanced mode. */
const ADVANCED_MODE_SOUNDS = {
  on: '/static/sounds/power-up.mp3',
  off: '/static/sounds/power-down.mp3',
} as const;
 
/** Playback volume for the chimes — half, so they're noticeable but not startling. */
const VOLUME = 0.5;
 
/**
 * Plays the advanced-mode chime at half volume: a power-up on unlock, a
 * power-down on exit. Failures are swallowed — autoplay policy or a missing asset
 * must never stop the mode from toggling.
 * @param enabled - `true` when advanced mode is being turned on, `false` when off.
 * @returns Nothing; resolves once playback starts (or fails).
 * @example
 * ```ts
 * void playAdvancedModeSound(true);  // power-up
 * void playAdvancedModeSound(false); // power-down
 * ```
 * @category Helpers
 * @source
 */
export async function playAdvancedModeSound(enabled: boolean): Promise<void> {
  try {
    const audio = new Audio(enabled ? ADVANCED_MODE_SOUNDS.on : ADVANCED_MODE_SOUNDS.off);
    audio.volume = VOLUME;
    await audio.play();
  } catch (error) {
    logger.warn(`Failed to play the advanced-mode sound: ${getErrorMessage(error)}`, { error });
  }
}