Spaces:
Sleeping
Sleeping
File size: 6,606 Bytes
1e7b565 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# 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. |