blanchon's picture
Update
1e7b565
# 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](https://bun.sh/docs/test/writing) 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
1. **Server Running**: Ensure the RobotHub TransportServer is running on `http://localhost:8000`
2. **Dependencies**: Install dependencies with `bun install`
### Run All Tests
```bash
# From the js client directory
bun test
# Or using npm script
bun run test
```
### Run Specific Test Files
```bash
# 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:
```typescript
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:
```typescript
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:
```typescript
afterEach(async () => {
if (producer.isConnected()) {
await producer.disconnect();
}
await roomManager.cleanup(producer);
});
```
### Error Testing
Comprehensive error scenario coverage:
```typescript
test("send without connection", async () => {
await expect(producer.sendJointUpdate([]))
.rejects.toThrow("Must be connected");
});
```
## Debugging Tests
### Enable Debug Logging
```bash
# Run with debug output
DEBUG=* bun test
# Or specific modules
DEBUG=robotics:* bun test
```
### Test Individual Scenarios
```bash
# Test specific functionality
bun test --grep "emergency stop"
bun test --grep "multiple consumers"
```
### Server Connectivity Issues
If tests fail due to server connectivity:
1. Ensure server is running: `curl http://localhost:8000/health`
2. Check server logs for errors
3. Verify WebSocket connections are allowed
4. Try running tests with longer timeouts
## Contributing
When adding new tests:
1. Follow the existing naming conventions
2. Add proper cleanup in `afterEach` blocks
3. Use `MessageCollector` for callback testing
4. Test both success and error scenarios
5. 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.