Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(voice): switch narration off in the extension when the microphone is refused, as the desktop does

Luis Ortizb50fa7ac00ec
diff --git a/apps/extension/src/entrypoints/background/__tests__/refused-microphone.test.ts b/apps/extension/src/entrypoints/background/__tests__/refused-microphone.test.ts
new file mode 100644
index 0000000..3429750
--- /dev/null
+++ b/apps/extension/src/entrypoints/background/__tests__/refused-microphone.test.ts
@@ -0,0 +1,70 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const queryMicPermission = vi.fn();
+const startVoiceCapture = vi.fn();
+const openMicPermissionPage = vi.fn();
+const storageSet = vi.fn();
+
+vi.mock('@/lib/offscreen/supports-voice', () => ({ supportsVoice: () => true }));
+vi.mock('@/lib/offscreen/ensure-voice-host', () => ({ ensureVoiceHost: vi.fn().mockResolvedValue(true) }));
+vi.mock('@/lib/offscreen/query-mic-permission', () => ({
+ queryMicPermission: (...args: unknown[]) => queryMicPermission(...args),
+}));
+vi.mock('@/lib/offscreen/start-voice-capture', () => ({
+ startVoiceCapture: (...args: unknown[]) => startVoiceCapture(...args),
+}));
+vi.mock('@/lib/offscreen/open-mic-permission-page', () => ({
+ openMicPermissionPage: (...args: unknown[]) => openMicPermissionPage(...args),
+}));
+vi.mock('@/lib/offscreen/stop-voice-capture', () => ({ stopVoiceCapture: vi.fn() }));
+vi.mock('@/lib/offscreen/has-voice-host', () => ({ hasVoiceHost: vi.fn().mockResolvedValue(false) }));
+vi.mock('@/lib/offscreen/close-voice-host-if-idle', () => ({ closeVoiceHostIfIdle: vi.fn() }));
+vi.mock('@/lib/offscreen/close-voice-host', () => ({ closeVoiceHost: vi.fn() }));
+vi.mock('@/lib/offscreen/abort-voice-capture', () => ({ abortVoiceCapture: vi.fn() }));
+vi.mock('@/lib/offscreen/flush-voice-capture', () => ({ flushVoiceCapture: vi.fn() }));
+vi.mock('@/lib/offscreen/register-voice-panel-relay', () => ({ registerVoicePanelRelay: vi.fn() }));
+vi.mock('@/lib/browser-api/on-message', () => ({ onMessage: vi.fn() }));
+vi.mock('@/lib/port/broadcast-voice-to-panel', () => ({ broadcastVoiceToPanel: vi.fn() }));
+vi.mock('@/core/env', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('@/core/env')>()),
+ localStorage: {
+ get: vi.fn().mockResolvedValue({ voiceEnabled: true, voiceProvider: 'openai', voiceApiKey: 'sk-test' }),
+ set: (...args: unknown[]) => storageSet(...args),
+ },
+}));
+
+import { startVoiceNarration } from '../voice';
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ startVoiceCapture.mockResolvedValue({ started: true });
+});
+
+describe('a refused microphone', () => {
+ it('switches narration off when the browser has the microphone blocked', async () => {
+ queryMicPermission.mockResolvedValue({ state: 'denied' });
+
+ await startVoiceNarration(1);
+
+ expect(storageSet).toHaveBeenCalledWith({ voiceEnabled: false });
+ expect(openMicPermissionPage).not.toHaveBeenCalled();
+ });
+
+ it('switches narration off when the microphone is refused as recording starts', async () => {
+ queryMicPermission.mockResolvedValue({ state: 'granted' });
+ startVoiceCapture.mockResolvedValue({ started: false, reason: 'permission-denied', error: 'refused' });
+
+ await startVoiceNarration(1);
+
+ expect(storageSet).toHaveBeenCalledWith({ voiceEnabled: false });
+ });
+
+ it('asks for the microphone the first time instead of switching narration off', async () => {
+ queryMicPermission.mockResolvedValue({ state: 'prompt' });
+
+ await startVoiceNarration(1);
+
+ expect(openMicPermissionPage).toHaveBeenCalled();
+ expect(storageSet).not.toHaveBeenCalledWith({ voiceEnabled: false });
+ });
+});
diff --git a/apps/extension/src/entrypoints/background/voice.ts b/apps/extension/src/entrypoints/background/voice.ts
index d466b9e..1a34310 100644
--- a/apps/extension/src/entrypoints/background/voice.ts
+++ b/apps/extension/src/entrypoints/background/voice.ts
@@ -7,6 +7,7 @@ import { NARRATION_SETTLE_MS } from '@/core/capture/voice/narration-settle-ms';
import { readTranscriptionSettings } from '@/core/capture/voice/read-transcription-settings';
import { readVoiceSettings } from '@/core/capture/voice/read-voice-settings';
import type { VoicePhase } from '@/core/capture/voice/voice-update';
+import { localStorage } from '@/core/env';
import { getStepsForGuide } from '@/core/guides/service';
import { onMessage as onRuntimeMessage } from '@/lib/browser-api/on-message';
import { abortVoiceCapture } from '@/lib/offscreen/abort-voice-capture';
@@ -125,6 +126,10 @@ function releaseTranscription(guideId: string): boolean {
return true;
}
+async function switchNarrationOff(): Promise<void> {
+ await localStorage.set({ voiceEnabled: false });
+}
+
function requestMicPermission(tabId?: number): void {
void openMicPermissionPage(tabId).catch((error) => logger.error('voice: mic permission page failed to open', error));
}
@@ -139,7 +144,8 @@ async function beginVoiceCapture(microphoneId: string | undefined, tabId: number
if (permission.state === 'denied' || permission.state === 'prompt') {
logger.info('voice: microphone permission not granted yet', permission.state);
report({ phase: 'error', reason: 'permission-denied', error: 'Microphone access has not been granted' });
- requestMicPermission(tabId);
+ if (permission.state === 'denied') await switchNarrationOff();
+ else requestMicPermission(tabId);
await closeVoiceHostIfIdle();
return;
}
@@ -153,7 +159,7 @@ async function beginVoiceCapture(microphoneId: string | undefined, tabId: number
logger.warn('voice: narration unavailable, recording without it', started);
report({ phase: 'error', reason: started.reason, error: started.error });
- if (started.reason === 'permission-denied') requestMicPermission(tabId);
+ if (started.reason === 'permission-denied') await switchNarrationOff();
await closeVoiceHost();
}
@@ -370,6 +376,7 @@ function handlePermissionResult(event: VoicePermissionResultEvent): void {
if (outcome === 'ignore') return;
if (outcome === 'report-denied') {
report({ phase: 'error', reason: 'permission-denied', error: 'Microphone access was refused' });
+ void switchNarrationOff();
return;
}
if (phase.phase === 'error') report({ phase: 'idle' });