Spaces:
Sleeping
RobotHub TransportServer JavaScript Client Tests
Overview
This directory contains comprehensive tests for the RobotHub TransportServer JavaScript/TypeScript client library, mirroring the Python test structure. The tests are built using Bun's test framework and provide full coverage of both robotics and video functionality.
Test Structure
The test suite is organized to match the Python client tests:
tests/
βββ setup.ts # Test utilities and helpers (equivalent to conftest.py)
βββ producer.test.ts # RoboticsProducer tests
βββ consumer.test.ts # RoboticsConsumer tests
βββ factory.test.ts # Factory function tests
βββ integration.test.ts # Integration tests
βββ rest-api.test.ts # REST API tests
βββ video-client.test.ts # Video client tests
βββ README.md # This file
Running Tests
Prerequisites
- Server Running: Ensure the RobotHub TransportServer is running on
http://localhost:8000
- Dependencies: Install dependencies with
bun install
Run All Tests
# From the js client directory
bun test
# Or using npm script
bun run test
Run Specific Test Files
# Run only producer tests
bun test tests/producer.test.ts
# Run only integration tests
bun test tests/integration.test.ts
# Run with verbose output
bun test --verbose
Test Categories
1. Robotics Producer Tests (producer.test.ts
)
- β Basic connection and disconnection
- β Connection info validation
- β Joint updates and state synchronization
- β Emergency stop functionality
- β Event callbacks (connected, disconnected, error)
- β Error handling for disconnected operations
- β Multiple room connections
- β Custom participant IDs
- β Large data handling
- β High-frequency updates
2. Robotics Consumer Tests (consumer.test.ts
)
- β Basic connection and disconnection
- β Connection info validation
- β State synchronization retrieval
- β Event callbacks setup
- β Multiple consumers in same room
- β Receiving state sync and joint updates
- β Emergency stop reception
- β Custom participant IDs
- β Reconnection scenarios
- β State persistence after producer updates
3. Factory Function Tests (factory.test.ts
)
- β Client creation by role
- β Invalid role handling
- β Auto room creation for producers
- β Specific room connection
- β Producer-consumer pair creation
- β Default URL handling
- β Multiple producer management
- β Nonexistent room error handling
4. Integration Tests (integration.test.ts
)
- β Full producer-consumer workflow
- β Multiple consumers receiving same data
- β Emergency stop propagation
- β Producer reconnection scenarios
- β Late-joining consumers
- β Room state persistence
- β High-frequency update handling
5. REST API Tests (rest-api.test.ts
)
- β Room listing (empty and populated)
- β Room creation (auto and custom IDs)
- β Room information retrieval
- β Room state retrieval
- β Room deletion
- β Error handling for nonexistent rooms
6. Video Client Tests (video-client.test.ts
)
- β Type definitions validation
- β Producer and consumer creation
- β Room creation and listing (when server available)
- β Connection validation
- β Factory function existence
- β Mock frame source functionality
- β Configuration type validation
Test Results Summary
β
69 tests passed
β 0 tests failed
π 187 expect() calls
β±οΈ Runtime: ~4 seconds
API Correspondence
The JavaScript tests mirror the Python test structure, ensuring API parity:
Python Test | JavaScript Test | Coverage |
---|---|---|
test_producer.py |
producer.test.ts |
β Complete |
test_consumer.py |
consumer.test.ts |
β Complete |
test_factory_functions.py |
factory.test.ts |
β Complete |
test_integration.py |
integration.test.ts |
β Complete |
test_rest_api.py |
rest-api.test.ts |
β Complete |
test_video_client.py |
video-client.test.ts |
β Complete |
conftest.py |
setup.ts |
β Complete |
Test Features
Async/Await Support
All tests use modern async/await patterns with proper cleanup:
test("producer connection", async () => {
const { workspaceId, roomId } = await producer.createRoom();
await producer.connect(workspaceId, roomId);
expect(producer.isConnected()).toBe(true);
await producer.disconnect();
});
Message Collection
Tests use a MessageCollector
utility for testing callbacks:
const updateCollector = new MessageCollector(1);
consumer.onJointUpdate(updateCollector.collect);
await producer.sendJointUpdate(joints);
const updates = await updateCollector.waitForMessages(2000);
expect(updates.length).toBeGreaterThanOrEqual(1);
Resource Management
Automatic cleanup prevents test interference:
afterEach(async () => {
if (producer.isConnected()) {
await producer.disconnect();
}
await roomManager.cleanup(producer);
});
Error Testing
Comprehensive error scenario coverage:
test("send without connection", async () => {
await expect(producer.sendJointUpdate([]))
.rejects.toThrow("Must be connected");
});
Debugging Tests
Enable Debug Logging
# Run with debug output
DEBUG=* bun test
# Or specific modules
DEBUG=robotics:* bun test
Test Individual Scenarios
# Test specific functionality
bun test --grep "emergency stop"
bun test --grep "multiple consumers"
Server Connectivity Issues
If tests fail due to server connectivity:
- Ensure server is running:
curl http://localhost:8000/health
- Check server logs for errors
- Verify WebSocket connections are allowed
- Try running tests with longer timeouts
Contributing
When adding new tests:
- Follow the existing naming conventions
- Add proper cleanup in
afterEach
blocks - Use
MessageCollector
for callback testing - Test both success and error scenarios
- Update this README with new test descriptions
Performance Notes
- Tests run in parallel by default
- Average test suite runtime: ~4 seconds
- Individual test timeouts: 10 seconds
- Message collection timeouts: 2-5 seconds
The JavaScript test suite provides comprehensive validation that the TypeScript/JavaScript client maintains full API compatibility with the Python client while leveraging modern JavaScript testing practices.