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.