|
| 1 | +import { create, StoreApi } from 'zustand'; |
| 2 | +import { immer } from 'zustand/middleware/immer'; |
| 3 | +import { createAudioSlice, AudioSlice } from './audioSlice'; |
| 4 | +import type { TextGeneratorState } from './textGeneratorStore'; |
| 5 | +import { vi, describe, it, expect, beforeEach } from 'vitest'; |
| 6 | + |
| 7 | +type MinimalTextGeneratorState = Pick< |
| 8 | + TextGeneratorState, |
| 9 | + 'quizData' | 'generatedPassageLanguage' | 'passageLanguage' | 'setError' |
| 10 | +>; |
| 11 | + |
| 12 | +const minimalTextGeneratorState: MinimalTextGeneratorState = { |
| 13 | + quizData: { paragraph: 'Hello world', question: '', options: { A: '', B: '', C: '', D: '' } }, |
| 14 | + generatedPassageLanguage: 'en', |
| 15 | + passageLanguage: 'en', |
| 16 | + setError: vi.fn(), |
| 17 | +}; |
| 18 | + |
| 19 | +type AudioTestStore = MinimalTextGeneratorState & AudioSlice; |
| 20 | + |
| 21 | +type TextGeneratorSetState = StoreApi<TextGeneratorState>['setState']; |
| 22 | +type TextGeneratorGetState = StoreApi<TextGeneratorState>['getState']; |
| 23 | +type TextGeneratorStoreApi = StoreApi<TextGeneratorState>; |
| 24 | + |
| 25 | +const getTestStore = (overrides: Partial<AudioSlice> = {}) => |
| 26 | + create<AudioTestStore>()( |
| 27 | + immer((set, get, api) => ({ |
| 28 | + ...minimalTextGeneratorState, |
| 29 | + ...createAudioSlice( |
| 30 | + set as TextGeneratorSetState, |
| 31 | + get as TextGeneratorGetState, |
| 32 | + api as TextGeneratorStoreApi |
| 33 | + ), |
| 34 | + ...overrides, |
| 35 | + })) |
| 36 | + ); |
| 37 | + |
| 38 | +type SpeechSynthesisMock = { |
| 39 | + speak: ReturnType<typeof vi.fn>; |
| 40 | + cancel: ReturnType<typeof vi.fn>; |
| 41 | + pause: ReturnType<typeof vi.fn>; |
| 42 | + resume: ReturnType<typeof vi.fn>; |
| 43 | + getVoices: ReturnType<typeof vi.fn>; |
| 44 | + speaking: boolean; |
| 45 | + onvoiceschanged: null | (() => void); |
| 46 | +}; |
| 47 | + |
| 48 | +class MockSpeechSynthesisUtterance { |
| 49 | + text = ''; |
| 50 | + lang = ''; |
| 51 | + volume = 1; |
| 52 | + voice: SpeechSynthesisVoice | null = null; |
| 53 | + onboundary: ((event: any) => void) | null = null; |
| 54 | + onend: (() => void) | null = null; |
| 55 | + onerror: ((event: any) => void) | null = null; |
| 56 | + |
| 57 | + constructor(text: string) { |
| 58 | + this.text = text; |
| 59 | + } |
| 60 | +} |
| 61 | +global.SpeechSynthesisUtterance = MockSpeechSynthesisUtterance as any; |
| 62 | + |
| 63 | +describe('audioSlice', () => { |
| 64 | + let store: ReturnType<typeof getTestStore>; |
| 65 | + let speechSynthesisMock: SpeechSynthesisMock; |
| 66 | + beforeEach(() => { |
| 67 | + speechSynthesisMock = { |
| 68 | + speak: vi.fn(), |
| 69 | + cancel: vi.fn(), |
| 70 | + pause: vi.fn(), |
| 71 | + resume: vi.fn(), |
| 72 | + getVoices: vi.fn(() => [ |
| 73 | + { voiceURI: 'voice1', name: 'Voice 1', lang: 'en-US' }, |
| 74 | + { voiceURI: 'voice2', name: 'Voice 2', lang: 'en-GB' }, |
| 75 | + ]), |
| 76 | + speaking: false, |
| 77 | + onvoiceschanged: null, |
| 78 | + }; |
| 79 | + (window as any).speechSynthesis = speechSynthesisMock; |
| 80 | + store = getTestStore({ |
| 81 | + isSpeechSupported: true, |
| 82 | + passageUtteranceRef: null, |
| 83 | + availableVoices: [], |
| 84 | + selectedVoiceURI: null, |
| 85 | + translationCache: new Map(), |
| 86 | + wordsRef: [], |
| 87 | + isSpeakingPassage: false, |
| 88 | + isPaused: false, |
| 89 | + volume: 0.5, |
| 90 | + currentWordIndex: null, |
| 91 | + }); |
| 92 | + speechSynthesisMock.speak.mockClear(); |
| 93 | + speechSynthesisMock.cancel.mockClear(); |
| 94 | + speechSynthesisMock.pause.mockClear(); |
| 95 | + speechSynthesisMock.resume.mockClear(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('sets volume level', () => { |
| 99 | + store.getState().setVolumeLevel(0.8); |
| 100 | + expect(store.getState().volume).toBe(0.8); |
| 101 | + }); |
| 102 | + |
| 103 | + it('stops passage speech', () => { |
| 104 | + store.setState({ isSpeakingPassage: true, isPaused: true, currentWordIndex: 1 }); |
| 105 | + store.getState().stopPassageSpeech(); |
| 106 | + expect(store.getState().isSpeakingPassage).toBe(false); |
| 107 | + expect(store.getState().isPaused).toBe(false); |
| 108 | + expect(store.getState().currentWordIndex).toBe(null); |
| 109 | + }); |
| 110 | + |
| 111 | + it('handles play/pause toggle', () => { |
| 112 | + store.setState({ isSpeakingPassage: false, isPaused: false }); |
| 113 | + store.getState().handlePlayPause(); |
| 114 | + expect(store.getState().isSpeakingPassage).toBe(true); |
| 115 | + store.setState({ isSpeakingPassage: true, isPaused: false }); |
| 116 | + store.getState().handlePlayPause(); |
| 117 | + expect(store.getState().isPaused).toBe(true); |
| 118 | + store.setState({ isSpeakingPassage: true, isPaused: true }); |
| 119 | + store.getState().handlePlayPause(); |
| 120 | + expect(store.getState().isPaused).toBe(false); |
| 121 | + }); |
| 122 | + |
| 123 | + it('handles stop', () => { |
| 124 | + store.setState({ isSpeakingPassage: true }); |
| 125 | + store.getState().handleStop(); |
| 126 | + expect(store.getState().isSpeakingPassage).toBe(false); |
| 127 | + }); |
| 128 | + |
| 129 | + it('sets selected voice URI and restarts if speaking', () => { |
| 130 | + store.setState({ isSpeakingPassage: true }); |
| 131 | + store.getState().setSelectedVoiceURI('voice2'); |
| 132 | + expect(store.getState().selectedVoiceURI).toBe('voice2'); |
| 133 | + expect(store.getState().isSpeakingPassage).toBe(false); |
| 134 | + }); |
| 135 | + |
| 136 | + it('updates available voices and selects default if needed', () => { |
| 137 | + store.setState({ selectedVoiceURI: 'notfound' }); |
| 138 | + store.getState().updateAvailableVoices('en'); |
| 139 | + expect(store.getState().availableVoices.length).toBe(2); |
| 140 | + expect(store.getState().selectedVoiceURI).toBe('voice1'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('caches translation result', () => { |
| 144 | + store.getState().translationCache.set('en:es:hello', 'hola'); |
| 145 | + const cached = store.getState().translationCache.get('en:es:hello'); |
| 146 | + expect(cached).toBe('hola'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('returns null for empty or same language translation', async () => { |
| 150 | + const result1 = await store.getState().getTranslation('', 'en', 'es'); |
| 151 | + expect(result1).toBeNull(); |
| 152 | + const result2 = await store.getState().getTranslation('hello', 'en', 'en'); |
| 153 | + expect(result2).toBeNull(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('speakText does nothing if not supported or no text', () => { |
| 157 | + store.setState({ isSpeechSupported: false }); |
| 158 | + store.getState().speakText('hi', 'en'); |
| 159 | + expect(speechSynthesisMock.speak).not.toHaveBeenCalled(); |
| 160 | + store.setState({ isSpeechSupported: true }); |
| 161 | + store.getState().speakText(null, 'en'); |
| 162 | + expect(speechSynthesisMock.speak).not.toHaveBeenCalled(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments