|
| 1 | +"""Tests for src/lua/endpoints/add.lua""" |
| 2 | + |
| 3 | +import socket |
| 4 | + |
| 5 | +from tests.lua.conftest import api, assert_error_response, load_fixture |
| 6 | + |
| 7 | + |
| 8 | +class TestAddEndpoint: |
| 9 | + """Test basic add endpoint functionality.""" |
| 10 | + |
| 11 | + def test_add_joker(self, client: socket.socket) -> None: |
| 12 | + """Test adding a joker with valid key.""" |
| 13 | + gamestate = load_fixture( |
| 14 | + client, |
| 15 | + "add", |
| 16 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 17 | + ) |
| 18 | + assert gamestate["state"] == "SELECTING_HAND" |
| 19 | + assert gamestate["jokers"]["count"] == 0 |
| 20 | + response = api(client, "add", {"key": "j_joker"}) |
| 21 | + assert response["jokers"]["count"] == 1 |
| 22 | + assert response["jokers"]["cards"][0]["key"] == "j_joker" |
| 23 | + |
| 24 | + def test_add_consumable_tarot(self, client: socket.socket) -> None: |
| 25 | + """Test adding a tarot consumable with valid key.""" |
| 26 | + gamestate = load_fixture( |
| 27 | + client, |
| 28 | + "add", |
| 29 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 30 | + ) |
| 31 | + assert gamestate["state"] == "SELECTING_HAND" |
| 32 | + assert gamestate["consumables"]["count"] == 0 |
| 33 | + response = api(client, "add", {"key": "c_fool"}) |
| 34 | + assert response["consumables"]["count"] == 1 |
| 35 | + assert response["consumables"]["cards"][0]["key"] == "c_fool" |
| 36 | + |
| 37 | + def test_add_consumable_planet(self, client: socket.socket) -> None: |
| 38 | + """Test adding a planet consumable with valid key.""" |
| 39 | + gamestate = load_fixture( |
| 40 | + client, |
| 41 | + "add", |
| 42 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 43 | + ) |
| 44 | + assert gamestate["state"] == "SELECTING_HAND" |
| 45 | + assert gamestate["consumables"]["count"] == 0 |
| 46 | + response = api(client, "add", {"key": "c_mercury"}) |
| 47 | + assert response["consumables"]["count"] == 1 |
| 48 | + assert response["consumables"]["cards"][0]["key"] == "c_mercury" |
| 49 | + |
| 50 | + def test_add_consumable_spectral(self, client: socket.socket) -> None: |
| 51 | + """Test adding a spectral consumable with valid key.""" |
| 52 | + gamestate = load_fixture( |
| 53 | + client, |
| 54 | + "add", |
| 55 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 56 | + ) |
| 57 | + assert gamestate["state"] == "SELECTING_HAND" |
| 58 | + assert gamestate["consumables"]["count"] == 0 |
| 59 | + response = api(client, "add", {"key": "c_familiar"}) |
| 60 | + assert response["consumables"]["count"] == 1 |
| 61 | + assert response["consumables"]["cards"][0]["key"] == "c_familiar" |
| 62 | + |
| 63 | + def test_add_voucher(self, client: socket.socket) -> None: |
| 64 | + """Test adding a voucher with valid key in SHOP state.""" |
| 65 | + gamestate = load_fixture( |
| 66 | + client, |
| 67 | + "add", |
| 68 | + "state-SHOP--jokers.count-0--consumables.count-0--vouchers.count-0", |
| 69 | + ) |
| 70 | + assert gamestate["state"] == "SHOP" |
| 71 | + assert gamestate["vouchers"]["count"] == 0 |
| 72 | + response = api(client, "add", {"key": "v_overstock_norm"}) |
| 73 | + assert response["vouchers"]["count"] == 1 |
| 74 | + assert response["vouchers"]["cards"][0]["key"] == "v_overstock_norm" |
| 75 | + |
| 76 | + def test_add_playing_card(self, client: socket.socket) -> None: |
| 77 | + """Test adding a playing card with valid key.""" |
| 78 | + gamestate = load_fixture( |
| 79 | + client, |
| 80 | + "add", |
| 81 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 82 | + ) |
| 83 | + assert gamestate["state"] == "SELECTING_HAND" |
| 84 | + assert gamestate["hand"]["count"] == 8 |
| 85 | + response = api(client, "add", {"key": "H_A"}) |
| 86 | + assert response["hand"]["count"] == 9 |
| 87 | + assert response["hand"]["cards"][8]["key"] == "H_A" |
| 88 | + |
| 89 | + def test_add_no_key_provided(self, client: socket.socket) -> None: |
| 90 | + """Test add endpoint with no key parameter.""" |
| 91 | + gamestate = load_fixture( |
| 92 | + client, |
| 93 | + "add", |
| 94 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 95 | + ) |
| 96 | + assert gamestate["state"] == "SELECTING_HAND" |
| 97 | + assert_error_response( |
| 98 | + api(client, "add", {}), |
| 99 | + "SCHEMA_MISSING_REQUIRED", |
| 100 | + "Missing required field 'key'", |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +class TestAddEndpointValidation: |
| 105 | + """Test add endpoint parameter validation.""" |
| 106 | + |
| 107 | + def test_invalid_key_type_number(self, client: socket.socket) -> None: |
| 108 | + """Test that add fails when key parameter is a number.""" |
| 109 | + gamestate = load_fixture( |
| 110 | + client, |
| 111 | + "add", |
| 112 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 113 | + ) |
| 114 | + assert gamestate["state"] == "SELECTING_HAND" |
| 115 | + assert_error_response( |
| 116 | + api(client, "add", {"key": 123}), |
| 117 | + "SCHEMA_INVALID_TYPE", |
| 118 | + "Field 'key' must be of type string", |
| 119 | + ) |
| 120 | + |
| 121 | + def test_invalid_key_unknown_format(self, client: socket.socket) -> None: |
| 122 | + """Test that add fails when key has unknown prefix format.""" |
| 123 | + gamestate = load_fixture( |
| 124 | + client, |
| 125 | + "add", |
| 126 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 127 | + ) |
| 128 | + assert gamestate["state"] == "SELECTING_HAND" |
| 129 | + assert_error_response( |
| 130 | + api(client, "add", {"key": "x_unknown"}), |
| 131 | + "SCHEMA_INVALID_VALUE", |
| 132 | + "Invalid card key format. Expected: joker (j_*), consumable (c_*), voucher (v_*), or playing card (SUIT_RANK)", |
| 133 | + ) |
| 134 | + |
| 135 | + def test_invalid_key_known_format(self, client: socket.socket) -> None: |
| 136 | + """Test that add fails when key has known format.""" |
| 137 | + gamestate = load_fixture( |
| 138 | + client, |
| 139 | + "add", |
| 140 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 141 | + ) |
| 142 | + assert gamestate["state"] == "SELECTING_HAND" |
| 143 | + assert_error_response( |
| 144 | + api(client, "add", {"key": "j_NON_EXTING_JOKER"}), |
| 145 | + "SCHEMA_INVALID_VALUE", |
| 146 | + "Failed to add card: j_NON_EXTING_JOKER", |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +class TestAddEndpointStateRequirements: |
| 151 | + """Test add endpoint state requirements.""" |
| 152 | + |
| 153 | + def test_add_from_BLIND_SELECT(self, client: socket.socket) -> None: |
| 154 | + """Test that add fails from BLIND_SELECT state.""" |
| 155 | + gamestate = load_fixture(client, "add", "state-BLIND_SELECT") |
| 156 | + assert gamestate["state"] == "BLIND_SELECT" |
| 157 | + assert_error_response( |
| 158 | + api(client, "add", {"key": "j_joker"}), |
| 159 | + "STATE_INVALID_STATE", |
| 160 | + "Endpoint 'add' requires one of these states: 1, 5, 8", |
| 161 | + ) |
| 162 | + |
| 163 | + def test_add_playing_card_from_SHOP(self, client: socket.socket) -> None: |
| 164 | + """Test that add playing card fails from SHOP state.""" |
| 165 | + gamestate = load_fixture( |
| 166 | + client, |
| 167 | + "add", |
| 168 | + "state-SHOP--jokers.count-0--consumables.count-0--vouchers.count-0", |
| 169 | + ) |
| 170 | + assert gamestate["state"] == "SHOP" |
| 171 | + assert_error_response( |
| 172 | + api(client, "add", {"key": "H_A"}), |
| 173 | + "STATE_INVALID_STATE", |
| 174 | + "Playing cards can only be added in SELECTING_HAND state", |
| 175 | + ) |
| 176 | + |
| 177 | + def test_add_voucher_card_from_SELECTING_HAND(self, client: socket.socket) -> None: |
| 178 | + """Test that add voucher card fails from SELECTING_HAND state.""" |
| 179 | + gamestate = load_fixture( |
| 180 | + client, |
| 181 | + "add", |
| 182 | + "state-SELECTING_HAND--jokers.count-0--consumables.count-0--hand.count-8", |
| 183 | + ) |
| 184 | + assert gamestate["state"] == "SELECTING_HAND" |
| 185 | + assert_error_response( |
| 186 | + api(client, "add", {"key": "v_overstock"}), |
| 187 | + "STATE_INVALID_STATE", |
| 188 | + "Vouchers can only be added in SHOP state", |
| 189 | + ) |
0 commit comments