Skip to content

Implement Mock Testing Harness for Video Server with Undici Interceptors#818

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-d4f0c9f3-73e6-4887-9cc1-068182eca333
Draft

Implement Mock Testing Harness for Video Server with Undici Interceptors#818
Copilot wants to merge 4 commits intomainfrom
copilot/fix-d4f0c9f3-73e6-4887-9cc1-068182eca333

Conversation

Copy link
Contributor

Copilot AI commented Aug 9, 2025

This PR implements a comprehensive mock testing harness for intercepting undici requests to the MediaMTX video server, addressing the need for reliable testing of video lease functionality without requiring a real video server.

Problem

The existing video-lease.srv.test.ts test file was making real HTTP requests to a video server, which:

  • Required external dependencies for testing
  • Made tests unreliable and environment-dependent
  • Prevented comprehensive testing of different video server scenarios
  • Limited ability to test error conditions and edge cases

Solution

Created a MockVideoServer class that uses undici's MockAgent to intercept HTTP requests and provide realistic mock responses for all MediaMTX API endpoints used by CloudTAK:

Key Features

Comprehensive Endpoint Coverage:

  • /v3/config/global/get - Global video server configuration
  • /v3/paths/list - List active video paths
  • /v3/config/paths/add/{path} - Add new video path
  • /v3/paths/get/{path} - Get path information
  • /v3/config/paths/patch/{path} - Update path configuration
  • /v3/config/paths/delete/{path} - Delete video path
  • /v3/recordings/get/{path} - Get recording information

Stateful Simulation:

  • Maintains internal state for video paths and configurations
  • Supports dynamic response generation based on current state
  • Provides helper methods for manipulating mock state during tests

Testing Utilities:

// Add paths with custom properties
mockVideoServer.addPath('test-stream', {
    ready: true,
    tracks: ['video', 'audio'],
    readers: [{ type: 'hls', id: 'viewer-1' }]
});

// Simulate different scenarios
mockVideoServer.setPathReady('test-stream', false);
mockVideoServer.simulateError('/v3/config/global/get', 500);

Implementation Details

The mock server uses undici's MockAgent with persistent interceptors to handle requests:

import { MockAgent, setGlobalDispatcher } from 'undici';

// Intercept requests to video server
this.mockAgent = new MockAgent();
this.mockAgent.disableNetConnect();
setGlobalDispatcher(this.mockAgent);

// Setup persistent interceptors for all endpoints
pool.intercept({
    path: '/v3/paths/list',
    method: 'GET'
}).reply(() => ({
    statusCode: 200,
    data: JSON.stringify({
        itemCount: this.paths.size,
        items: Array.from(this.paths.values())
    })
})).persist();

Test Coverage

  • Unit Tests: 32 comprehensive tests covering all MockVideoServer functionality
  • Integration Tests: Demonstrates successful interception of video service requests
  • Error Handling: Tests for non-existent paths, server errors, and edge cases
  • State Management: Verifies proper cleanup and reset functionality

Files Added/Modified

  • api/test/mock-video-server.ts - Core MockVideoServer implementation
  • api/test/mock-video-server.test.ts - Comprehensive test suite (32 tests)
  • api/test/video-lease.srv.test.ts - Updated to demonstrate mock usage
  • api/test/README-MockVideoServer.md - Complete documentation and usage guide
  • api/lib/control/video-service.ts - Fixed return type for runOnDemand method

Usage Example

import MockVideoServer from './mock-video-server.js';

const mockVideoServer = new MockVideoServer({
    url: 'http://media:8080',
    configured: true
});

// Video service requests are now intercepted and mocked
const videoControl = new VideoServiceControl(config);
const configuration = await videoControl.configuration(); // Uses mock responses

mockVideoServer.close(); // Cleanup

This implementation provides a robust foundation for testing video-related functionality in CloudTAK without external dependencies, enabling more reliable and comprehensive test coverage.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits August 9, 2025 18:51
…ents

Co-authored-by: ingalls <1297009+ingalls@users.noreply.github.com>
…tests

Co-authored-by: ingalls <1297009+ingalls@users.noreply.github.com>
…tration tests

Co-authored-by: ingalls <1297009+ingalls@users.noreply.github.com>
Copilot AI changed the title [WIP] Generate a Mock Testing harness for intercepting undici requests to the Video Server and update the test/video-lease.test.ts file to use the new harness Implement Mock Testing Harness for Video Server with Undici Interceptors Aug 9, 2025
Copilot AI requested a review from ingalls August 9, 2025 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants