Protocol-level checks that don't depend on the command surface.
(api: SerialStudioAPI, suite: TestSuite)
| 471 | |
| 472 | |
| 473 | def test_protocol(api: SerialStudioAPI, suite: TestSuite): |
| 474 | """Protocol-level checks that don't depend on the command surface.""" |
| 475 | print(bold(info("\n[ Protocol ]"))) |
| 476 | |
| 477 | # 1) Unknown command must return UNKNOWN_COMMAND, not crash the server. |
| 478 | t0 = time.time() |
| 479 | response = api.send_command("does.not.exist." + uuid.uuid4().hex[:8]) |
| 480 | duration = (time.time() - t0) * 1000 |
| 481 | if response is None: |
| 482 | _record( |
| 483 | suite, |
| 484 | "unknown_command -> server response", |
| 485 | TestResult.FAILED, |
| 486 | "no response received", |
| 487 | ) |
| 488 | elif response.get("success"): |
| 489 | _record( |
| 490 | suite, |
| 491 | "unknown_command -> error", |
| 492 | TestResult.FAILED, |
| 493 | "server returned success on a fake command name", |
| 494 | ) |
| 495 | else: |
| 496 | err_code = response.get("error", {}).get("code", "") |
| 497 | if err_code == ErrorCode.UNKNOWN_COMMAND: |
| 498 | _record( |
| 499 | suite, |
| 500 | "unknown_command -> UNKNOWN_COMMAND", |
| 501 | TestResult.PASSED, |
| 502 | duration_ms=duration, |
| 503 | ) |
| 504 | else: |
| 505 | _record( |
| 506 | suite, |
| 507 | "unknown_command -> UNKNOWN_COMMAND", |
| 508 | TestResult.FAILED, |
| 509 | f"got {err_code} instead", |
| 510 | ) |
| 511 | |
| 512 | # 2) Response IDs must match the request ID. |
| 513 | rid = "test-id-" + uuid.uuid4().hex[:8] |
| 514 | t0 = time.time() |
| 515 | response = api.send_command("io.getStatus", request_id=rid) |
| 516 | duration = (time.time() - t0) * 1000 |
| 517 | if response and response.get("id") == rid: |
| 518 | _record( |
| 519 | suite, |
| 520 | "response.id matches request.id", |
| 521 | TestResult.PASSED, |
| 522 | duration_ms=duration, |
| 523 | ) |
| 524 | else: |
| 525 | actual = response.get("id") if response else None |
| 526 | _record( |
| 527 | suite, |
| 528 | "response.id matches request.id", |
| 529 | TestResult.FAILED, |
| 530 | f"expected {rid!r}, got {actual!r}", |
no test coverage detected