tests — voice

Module: tests-voice Cohesion: 0.80 Members: 0

tests — voice

This document provides an overview of the tests/voice module, which is responsible for ensuring the correct functionality of the voice-related components in the system. It covers the testing strategies, key test suites, and how external dependencies are handled through mocking.

Module Overview

The tests/voice module contains unit and integration tests for the core voice functionalities: speech recognition and wake word detection. These tests are crucial for verifying that the SpeechRecognizer and WakeWordDetector classes, located in src/voice, behave as expected across various configurations, providers, and edge cases.

The module is structured into two primary test files:

Testing Principles

The tests in this module adhere to several key principles:

Key Components and Test Suites

speech-recognition.test.ts

This file tests the SpeechRecognizer class, which is responsible for converting spoken audio into text using various speech-to-text providers.

Dependencies and Mocks

The primary external dependency for SpeechRecognizer is network communication for API calls to transcription services. This is mocked using jest.fn() for global.fetch. This allows tests to simulate API responses (success, error, different data structures) without making actual network requests.

class="hl-cmt">// Mock fetch globally
const mockFetch = jest.fn();
global.fetch = mockFetch;

Test Suites

The SpeechRecognizer tests cover the following areas:

wake-word.test.ts

This file tests the WakeWordDetector class, which is responsible for detecting specific wake words in audio streams.

Dependencies and Mocks

The WakeWordDetector relies on the @picovoice/porcupine-node library for its primary wake word detection engine. This library is extensively mocked to control its behavior during tests:

class="hl-cmt">// Mock @picovoice/porcupine-node
const mockProcess = jest.fn().mockReturnValue(-1); class="hl-cmt">// Default to no detection
const mockRelease = jest.fn();

jest.mock('@picovoice/porcupine-node', () => ({
  Porcupine: jest.fn().mockImplementation(function() { return {
    process: mockProcess,
    release: mockRelease,
    frameLength: 512,
    sampleRate: 16000,
  }; }),
  class="hl-cmt">// ... other mocks for BuiltinKeyword, getBuiltinKeywordPath
}));

This mock allows tests to simulate wake word detection (mockProcess.mockReturnValueOnce(0)) or non-detection (-1), and to verify that resources are properly released (mockRelease).

Test Suites

The WakeWordDetector tests cover the following areas:

Module Architecture Diagram

The following diagram illustrates the relationship between the test files, the core voice modules they test, and the external dependencies that are mocked.

graph TD
    subgraph tests/voice
        SRT[speech-recognition.test.ts]
        WWT[wake-word.test.ts]
    end

    subgraph src/voice
        SR(SpeechRecognizer)
        WWD(WakeWordDetector)
    end

    subgraph Mocks
        MF(Mock global.fetch)
        MP(Mock @picovoice/porcupine-node)
    end

    SRT -- tests --> SR
    SRT -- mocks --> MF
    WWT -- tests --> WWD
    WWT -- mocks --> MP

This diagram highlights that each test file (SRT, WWT) directly interacts with and tests its corresponding core module (SR, WWD), while also relying on specific mocks (MF, MP) to control external behavior and isolate the unit under test.