Test receive_file method
(self)
| 349 | |
| 350 | @pytest.mark.asyncio |
| 351 | async def test_receive_file(self): |
| 352 | """Test receive_file method""" |
| 353 | import json |
| 354 | |
| 355 | mock_transport = MagicMock(spec=WebSocketTransport) |
| 356 | |
| 357 | # Prepare file transfer messages |
| 358 | start_msg = { |
| 359 | "type": "file_transfer_start", |
| 360 | "filename": "test.bin", |
| 361 | "size": 2048, |
| 362 | "chunk_size": 1024, |
| 363 | "total_chunks": 2, |
| 364 | } |
| 365 | |
| 366 | chunk1_meta = {"type": "binary_data", "chunk_num": 0, "size": 1024} |
| 367 | chunk2_meta = {"type": "binary_data", "chunk_num": 1, "size": 1024} |
| 368 | |
| 369 | complete_msg = { |
| 370 | "type": "file_transfer_complete", |
| 371 | "filename": "test.bin", |
| 372 | "total_chunks": 2, |
| 373 | "checksum": "abc123", |
| 374 | } |
| 375 | |
| 376 | # Mock transport to return messages in sequence |
| 377 | mock_transport.receive = AsyncMock( |
| 378 | side_effect=[ |
| 379 | json.dumps(start_msg).encode("utf-8"), |
| 380 | json.dumps(chunk1_meta).encode("utf-8"), |
| 381 | json.dumps(chunk2_meta).encode("utf-8"), |
| 382 | json.dumps(complete_msg).encode("utf-8"), |
| 383 | ] |
| 384 | ) |
| 385 | |
| 386 | mock_transport.receive_binary = AsyncMock( |
| 387 | side_effect=[ |
| 388 | b"A" * 1024, # Chunk 1 |
| 389 | b"B" * 1024, # Chunk 2 |
| 390 | ] |
| 391 | ) |
| 392 | |
| 393 | protocol = AIPProtocol(mock_transport) |
| 394 | |
| 395 | # Receive file |
| 396 | with tempfile.NamedTemporaryFile(delete=False, suffix=".bin") as temp_file: |
| 397 | output_path = temp_file.name |
| 398 | |
| 399 | try: |
| 400 | metadata = await protocol.receive_file(output_path, validate_checksum=False) |
| 401 | |
| 402 | assert metadata["filename"] == "test.bin" |
| 403 | assert metadata["size"] == 2048 |
| 404 | |
| 405 | # Verify file was written |
| 406 | with open(output_path, "rb") as f: |
| 407 | content = f.read() |
| 408 | assert len(content) == 2048 |
nothing calls this directly
no test coverage detected