|
| 1 | +"""Tests for src/lua/endpoints/screenshot.lua""" |
| 2 | + |
| 3 | +import socket |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from tests.lua.conftest import ( |
| 9 | + api, |
| 10 | + assert_error_response, |
| 11 | + assert_gamestate_response, |
| 12 | + assert_path_response, |
| 13 | + load_fixture, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class TestScreenshotEndpoint: |
| 18 | + """Test basic screenshot endpoint functionality.""" |
| 19 | + |
| 20 | + def test_screenshot_from_MENU(self, client: socket.socket, tmp_path: Path) -> None: |
| 21 | + """Test that screenshot succeeds from MENU state.""" |
| 22 | + gamestate = api(client, "menu", {}) |
| 23 | + assert_gamestate_response(gamestate, state="MENU") |
| 24 | + temp_file = tmp_path / "screenshot.png" |
| 25 | + response = api(client, "screenshot", {"path": str(temp_file)}) |
| 26 | + assert_path_response(response) |
| 27 | + assert response["result"]["path"] == str(temp_file) |
| 28 | + assert temp_file.exists() |
| 29 | + assert temp_file.stat().st_size > 0 |
| 30 | + assert temp_file.read_bytes()[:8] == b"\x89PNG\r\n\x1a\n" |
| 31 | + |
| 32 | + def test_screenshot_from_BLIND_SELECT( |
| 33 | + self, client: socket.socket, tmp_path: Path |
| 34 | + ) -> None: |
| 35 | + """Test that screenshot succeeds from BLIND_SELECT state.""" |
| 36 | + gamestate = load_fixture(client, "screenshot", "state-BLIND_SELECT") |
| 37 | + assert gamestate["state"] == "BLIND_SELECT" |
| 38 | + temp_file = tmp_path / "screenshot.png" |
| 39 | + response = api(client, "screenshot", {"path": str(temp_file)}) |
| 40 | + assert_path_response(response) |
| 41 | + assert response["result"]["path"] == str(temp_file) |
| 42 | + assert temp_file.exists() |
| 43 | + assert temp_file.stat().st_size > 0 |
| 44 | + assert temp_file.read_bytes()[:8] == b"\x89PNG\r\n\x1a\n" |
| 45 | + |
| 46 | + |
| 47 | +class TestScreenshotValidation: |
| 48 | + """Test screenshot endpoint parameter validation.""" |
| 49 | + |
| 50 | + def test_missing_path_parameter(self, client: socket.socket) -> None: |
| 51 | + """Test that screenshot fails when path parameter is missing.""" |
| 52 | + response = api(client, "screenshot", {}) |
| 53 | + assert_error_response( |
| 54 | + response, |
| 55 | + "BAD_REQUEST", |
| 56 | + "Missing required field 'path'", |
| 57 | + ) |
| 58 | + |
| 59 | + def test_invalid_path_type(self, client: socket.socket) -> None: |
| 60 | + """Test that screenshot fails when path is not a string.""" |
| 61 | + response = api(client, "save", {"path": 123}) |
| 62 | + assert_error_response( |
| 63 | + response, |
| 64 | + "BAD_REQUEST", |
| 65 | + "Field 'path' must be of type string", |
| 66 | + ) |
0 commit comments