tests — sidecar

Module: tests-sidecar Cohesion: 0.80 Members: 0

tests — sidecar

This document describes the tests/sidecar/sidecar-bridge.test.ts module, which is responsible for thoroughly testing the SidecarBridge class and its associated functions.

Sidecar Bridge Tests

The sidecar-bridge.test.ts module contains the unit and integration tests for the SidecarBridge class, located in src/sidecar/sidecar-bridge.ts. The SidecarBridge is a critical component that facilitates communication between the TypeScript application and the Rust-based codebuddy-sidecar binary. This communication occurs via JSON-RPC over the standard input/output (stdin/stdout) streams of a spawned child process.

The tests ensure that the SidecarBridge correctly handles process lifecycle, JSON-RPC messaging, error conditions, and provides a robust interface for interacting with the sidecar's capabilities (e.g., Speech-to-Text, desktop automation).

Module Under Test: SidecarBridge

The SidecarBridge class is designed as a singleton to manage a single instance of the codebuddy-sidecar process. Its primary responsibilities include:

Testing Philosophy & Mocking Strategy

The majority of the tests for SidecarBridge are unit tests that heavily rely on mocking to isolate the SidecarBridge logic from the actual child_process execution and file system interactions. This allows for predictable testing of various scenarios, including process startup, communication, and error handling, without needing to compile or run the actual Rust binary for every test.

createMockProcess

A custom factory function, createMockProcess, is central to the mocking strategy. It generates an object that mimics a Node.js ChildProcess instance. This mock process:

child_process Mock

The child_process module, specifically its spawn function, is mocked globally using vi.mock('child_process', ...). This mock ensures that whenever SidecarBridge attempts to spawn a child process, it receives the currentMockProcess created by createMockProcess instead of a real system process.

fs Mock

The fs module's existsSync function is mocked to control the perceived availability of the sidecar binary. This allows tests to simulate scenarios where the binary is found or not found, influencing the isAvailable() method's behavior.

logger Mock

The internal logger utility is mocked to prevent test output from cluttering the console and to allow verification that appropriate log messages are generated during various operations.

Test Lifecycle (beforeEach, afterEach)

Key Test Suites

The tests are organized into describe blocks, each focusing on a specific aspect of the SidecarBridge.

isAvailable

This suite verifies the SidecarBridge.isAvailable() method:

singleton

This suite tests the singleton pattern implemented by getSidecarBridge() and resetSidecarBridge():

call Mechanism

This is a critical suite that tests the core JSON-RPC communication logic. It simulates the full request-response cycle:

graph TD
    A[SidecarBridge Test] -->|1. bridge.start()| B(Mocked child_process.spawn)
    B -->|2. Returns currentMockProcess| A
    A -->|3. bridge.call(method, params)| C{currentMockProcess.stdin}
    C -->|4. Writes JSON-RPC request| D[SidecarBridge Test]
    D -->|5. Simulates Sidecar Response| E{currentMockProcess.stdout}
    E -->|6. Pushes JSON-RPC response| D
    D -->|7. Awaits callPromise| A

convenience methods

This suite simply verifies the existence of the high-level API methods on the SidecarBridge instance, such as loadModel, transcribe, sttStatus (for STT), paste, typeText, keyPress, clipboardGet, clipboardSet (for desktop automation), and version. This ensures the public interface is as expected.

stop

This suite tests the SidecarBridge.stop() method:

process exit handling

This suite tests how SidecarBridge reacts to an unexpected exit of the sidecar process:

Real Binary Integration Test

A separate describe block, SidecarBridge real binary integration, provides a crucial integration test. Unlike the mocked unit tests, this suite attempts to:

This integration test provides confidence that the SidecarBridge can successfully interact with a live sidecar, bridging the gap between the mocked unit tests and real-world deployment.

Contribution & Further Development

When contributing to the SidecarBridge or adding new sidecar functionalities: