File size: 5,786 Bytes
02eac4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Integration Tests for LeRobot Arena Python Client

Comprehensive integration tests for the LeRobot Arena Python client library.

## Prerequisites

1. **Server Running**: Make sure the LeRobot Arena server is running on `http://localhost:8000`
2. **Dependencies**: Install test dependencies:
   ```bash
   pip install -e ".[dev]"
   ```

## Running Tests

### Quick Start
```bash
# Run all tests
python run_tests.py

# Or use pytest directly
pytest -v
```

### Specific Test Categories
```bash
# REST API tests only
pytest tests/test_rest_api.py -v

# Producer tests only  
pytest tests/test_producer.py -v

# Consumer tests only
pytest tests/test_consumer.py -v

# Factory function tests only
pytest tests/test_factory_functions.py -v

# End-to-end integration tests only
pytest tests/test_integration.py -v
```

### Advanced Options
```bash
# Run tests with detailed output
pytest -v -s

# Run specific test
pytest tests/test_producer.py::TestRoboticsProducer::test_send_joint_update -v

# Run tests matching pattern
pytest -k "producer" -v

# Stop on first failure
pytest -x
```

## Test Structure

### `test_rest_api.py`
- Room creation and deletion
- Listing rooms
- Getting room state and info
- Error handling for nonexistent rooms

### `test_producer.py`  
- Producer connection and disconnection
- Sending joint updates and state sync
- Emergency stop functionality
- Event callbacks
- Error conditions and validation
- Context manager support
- Multiple room handling

### `test_consumer.py`
- Consumer connection and disconnection  
- Receiving joint updates and state sync
- Event callbacks and message handling
- Multiple consumers per room
- State synchronization
- Error propagation

### `test_factory_functions.py`
- `create_client()` factory function
- `create_producer_client()` convenience function
- `create_consumer_client()` convenience function
- Parameter validation
- Auto room creation
- Error handling

### `test_integration.py`
- End-to-end producer-consumer workflows
- Multiple consumers receiving same messages
- Emergency stop propagation
- Producer reconnection scenarios
- Late-joining consumers
- High-frequency update handling
- Room state persistence

## Test Fixtures

### Core Fixtures
- `producer`: Clean RoboticsProducer instance
- `consumer`: Clean RoboticsConsumer instance  
- `test_room`: Auto-created and cleaned up room
- `connected_producer`: Producer connected to test room
- `connected_consumer`: Consumer connected to test room
- `producer_consumer_pair`: Connected producer and consumer pair

## Test Coverage

The test suite covers:

βœ… **REST API Operations**
- βœ… Room CRUD operations
- βœ… State and info retrieval
- βœ… Error handling

βœ… **WebSocket Communication**
- βœ… Connection establishment
- βœ… Message sending/receiving
- βœ… Connection cleanup

βœ… **Producer Functionality**
- βœ… Joint updates and state sync
- βœ… Emergency stop
- βœ… Heartbeat
- βœ… Event callbacks

βœ… **Consumer Functionality**  
- βœ… Message reception
- βœ… State synchronization
- βœ… Event callbacks
- βœ… Multiple consumer support

βœ… **Factory Functions**
- βœ… Client creation
- βœ… Auto room creation
- βœ… Parameter validation

βœ… **Edge Cases**
- βœ… Network disconnections
- βœ… Invalid parameters
- βœ… Nonexistent rooms
- βœ… Duplicate connections
- βœ… High-frequency updates

βœ… **Integration Scenarios**
- βœ… Full producer-consumer workflows
- βœ… Multi-consumer broadcasting
- βœ… Emergency stop propagation
- βœ… Reconnection handling
- βœ… Late consumer joining

## Running Individual Test Categories

Each test file can be run independently:

```bash
# Test basic REST operations
pytest tests/test_rest_api.py

# Test producer functionality
pytest tests/test_producer.py

# Test consumer functionality  
pytest tests/test_consumer.py

# Test factory functions
pytest tests/test_factory_functions.py

# Test end-to-end scenarios
pytest tests/test_integration.py
```

## Debugging Tests

For debugging failed tests:

```bash
# Run with full output
pytest -v -s --tb=long

# Run single test with debugging
pytest tests/test_producer.py::TestRoboticsProducer::test_send_joint_update -v -s

# Use pdb for interactive debugging
pytest --pdb
```

## Expected Test Results

When all tests pass, you should see output like:

```
πŸ§ͺ Running LeRobot Arena Python Client Integration Tests
============================================================
⚠️  Make sure the server is running on http://localhost:8000

========================= test session starts =========================
collected 45+ items

tests/test_rest_api.py::TestRestAPI::test_list_rooms_empty PASSED
tests/test_rest_api.py::TestRestAPI::test_create_room PASSED
...
tests/test_integration.py::TestIntegration::test_high_frequency_updates PASSED

========================= 45+ passed in X.XXs =========================

βœ… All tests passed!
```

## Troubleshooting

### Common Issues

1. **Server Not Running**
   ```
   ConnectionRefusedError: [Errno 61] Connection refused
   ```
   **Solution**: Start the LeRobot Arena server on `http://localhost:8000`

2. **WebSocket Connection Timeout**
   ```
   TimeoutError: WebSocket connection timeout
   ```
   **Solution**: Check server health and network connectivity

3. **Test Timing Issues**
   - Some tests may be sensitive to timing
   - Increase sleep durations in tests if needed
   - Run tests on a faster machine if possible

4. **Port Conflicts**
   - Make sure port 8000 is available
   - Update `TEST_SERVER_URL` in `conftest.py` if using different port

### Getting Help

If tests fail consistently:
1. Check server logs for errors
2. Run individual test files to isolate issues
3. Use verbose output (`-v -s`) to see detailed information
4. Check network connectivity and firewall settings