| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { ItemId } from "../../shared/definitions/items.js"; |
| 4 | import { emptyEquipment, emptySlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js"; |
| 5 | import { |
| 6 | inventorySlotsShortAfterTrade, |
| 7 | resolveOfferStacks |
| 8 | } from "../../shared/trade/space.js"; |
| 9 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 10 | |
| 11 | function apple (qty) |
| 12 | { |
| 13 | return { itemId: ItemId.apple, quantity: qty }; |
| 14 | } |
| 15 | |
| 16 | function stick (qty = 1) |
| 17 | { |
| 18 | return { itemId: ItemId.stick, quantity: qty }; |
| 19 | } |
| 20 | |
| 21 | function openTrade (harness, fromSession, fromChar, toSession, toEntityId) |
| 22 | { |
| 23 | harness.simulation.enqueue(fromSession, { |
| 24 | type: "trade-request", |
| 25 | characterId: fromChar.character_id, |
| 26 | targetEntityId: toEntityId |
| 27 | }); |
| 28 | const req = harness.simulation.step(); |
| 29 | const invite = req.events.find((event) => event.type === "trade-invite"); |
| 30 | assert.ok(invite, JSON.stringify(req.events)); |
| 31 | harness.simulation.enqueue(toSession, { |
| 32 | type: "trade-respond-invite", |
| 33 | characterId: invite.characterId, |
| 34 | inviteId: invite.inviteId, |
| 35 | accept: true |
| 36 | }); |
| 37 | const open = harness.simulation.step(); |
| 38 | assert.equal(open.events.filter((event) => event.type === "trade-open").length, 2, JSON.stringify(open.events)); |
| 39 | return invite; |
| 40 | } |
| 41 | |
| 42 | test("inventorySlotsShortAfterTrade accounts for freed give slots", () => |
| 43 | { |
| 44 | const slots = emptySlots(); |
| 45 | for (let i = 0; i < INVENTORY_SLOTS; i++) |
| 46 | { slots[i] = stick(1); } |
| 47 | assert.equal( |
| 48 | inventorySlotsShortAfterTrade(slots, [{ bagSlotIndex: 0, quantity: 1 }], [apple(1)]), |
| 49 | 0 |
| 50 | ); |
| 51 | assert.equal( |
| 52 | inventorySlotsShortAfterTrade(slots, [], [apple(1)]), |
| 53 | 1 |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | test("resolveOfferStacks rejects missing quantities", () => |
| 58 | { |
| 59 | const slots = emptySlots(); |
| 60 | slots[0] = apple(3); |
| 61 | assert.equal(resolveOfferStacks(slots, [{ bagSlotIndex: 0, quantity: 4 }]), null); |
| 62 | assert.deepEqual( |
| 63 | resolveOfferStacks(slots, [{ bagSlotIndex: 0, quantity: 2 }]), |
| 64 | [{ itemId: ItemId.apple, quantity: 2, instance: null, origins: undefined }] |
| 65 | ); |
| 66 | }); |
| 67 | |
| 68 | test("players can complete a 1-for-1 trade while adjacent", () => |
| 69 | { |
| 70 | const harness = makeSimulation({ workerCount: 1 }); |
| 71 | try |
| 72 | { |
| 73 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Alpha" }); |
| 74 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Beta" }); |
| 75 | const bagA = emptySlots(); |
| 76 | bagA[0] = apple(5); |
| 77 | const bagB = emptySlots(); |
| 78 | bagB[0] = stick(1); |
| 79 | harness.stateDb.saveBag(a.character_id, { |
| 80 | inventory: bagA, |
| 81 | equipment: emptyEquipment(), |
| 82 | vault: [], |
| 83 | vaultReserved: [] |
| 84 | }); |
| 85 | harness.stateDb.saveBag(b.character_id, { |
| 86 | inventory: bagB, |
| 87 | equipment: emptyEquipment(), |
| 88 | vault: [], |
| 89 | vaultReserved: [] |
| 90 | }); |
| 91 | const aId = harness.simulation.spawnCharacter(1, a); |
| 92 | const bId = harness.simulation.spawnCharacter(2, b); |
| 93 | harness.simulation.step(); |
| 94 | |
| 95 | openTrade(harness, 1, a, 2, bId); |
| 96 | |
| 97 | harness.simulation.enqueue(1, { |
| 98 | type: "trade-offer-set", |
| 99 | characterId: a.character_id, |
| 100 | bagSlotIndex: 0, |
| 101 | quantity: 2 |
| 102 | }); |
| 103 | harness.simulation.enqueue(2, { |
| 104 | type: "trade-offer-set", |
| 105 | characterId: b.character_id, |
| 106 | bagSlotIndex: 0, |
| 107 | quantity: 1 |
| 108 | }); |
| 109 | harness.simulation.step(); |
| 110 | |
| 111 | harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); |
| 112 | harness.simulation.step(); |
| 113 | harness.simulation.enqueue(2, { type: "trade-accept", characterId: b.character_id }); |
| 114 | const done = harness.simulation.step(); |
| 115 | assert.ok(done.events.some((event) => event.type === "trade-closed")); |
| 116 | assert.ok(done.events.some((event) => |
| 117 | event.type === "toast" && event.message === "Trade complete.")); |
| 118 | |
| 119 | const invA = harness.simulation.bagOf(aId).inventory; |
| 120 | const invB = harness.simulation.bagOf(bId).inventory; |
| 121 | assert.equal(invA.find((slot) => slot?.itemId === ItemId.stick)?.quantity, 1); |
| 122 | assert.equal(invA.find((slot) => slot?.itemId === ItemId.apple)?.quantity, 3); |
| 123 | assert.equal(invB.find((slot) => slot?.itemId === ItemId.apple)?.quantity, 2); |
| 124 | assert.equal(invB.some((slot) => slot?.itemId === ItemId.stick), false); |
| 125 | } |
| 126 | finally |
| 127 | { harness.close(); } |
| 128 | }); |
| 129 | |
| 130 | test("reject blocks the requester for one minute", () => |
| 131 | { |
| 132 | const harness = makeSimulation({ workerCount: 1, now: 5_000 }); |
| 133 | try |
| 134 | { |
| 135 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Ask" }); |
| 136 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Block" }); |
| 137 | harness.simulation.spawnCharacter(1, a); |
| 138 | const bId = harness.simulation.spawnCharacter(2, b); |
| 139 | harness.simulation.step(); |
| 140 | |
| 141 | harness.simulation.enqueue(1, { |
| 142 | type: "trade-request", |
| 143 | characterId: a.character_id, |
| 144 | targetEntityId: bId |
| 145 | }); |
| 146 | const req = harness.simulation.step(); |
| 147 | const invite = req.events.find((event) => event.type === "trade-invite"); |
| 148 | assert.ok(invite); |
| 149 | |
| 150 | harness.simulation.enqueue(2, { |
| 151 | type: "trade-respond-invite", |
| 152 | characterId: b.character_id, |
| 153 | inviteId: invite.inviteId, |
| 154 | accept: false |
| 155 | }); |
| 156 | harness.simulation.step(); |
| 157 | |
| 158 | harness.simulation.enqueue(1, { |
| 159 | type: "trade-request", |
| 160 | characterId: a.character_id, |
| 161 | targetEntityId: bId |
| 162 | }); |
| 163 | const blocked = harness.simulation.step(); |
| 164 | assert.ok(blocked.events.some((event) => |
| 165 | event.type === "command-error" |
| 166 | && /declined a recent trade request/i.test(event.message))); |
| 167 | |
| 168 | harness.setNow(5_000 + 60_000); |
| 169 | harness.simulation.enqueue(1, { |
| 170 | type: "trade-request", |
| 171 | characterId: a.character_id, |
| 172 | targetEntityId: bId |
| 173 | }); |
| 174 | const again = harness.simulation.step(); |
| 175 | assert.ok(again.events.some((event) => event.type === "trade-invite")); |
| 176 | } |
| 177 | finally |
| 178 | { harness.close(); } |
| 179 | }); |
| 180 | |
| 181 | test("full inventory without give slots fails with slot toast and resets accepts", () => |
| 182 | { |
| 183 | const harness = makeSimulation({ workerCount: 1 }); |
| 184 | try |
| 185 | { |
| 186 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Full" }); |
| 187 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Giver" }); |
| 188 | const bagA = emptySlots(); |
| 189 | for (let i = 0; i < INVENTORY_SLOTS; i++) |
| 190 | { bagA[i] = stick(1); } |
| 191 | const bagB = emptySlots(); |
| 192 | bagB[0] = apple(1); |
| 193 | harness.stateDb.saveBag(a.character_id, { |
| 194 | inventory: bagA, |
| 195 | equipment: emptyEquipment(), |
| 196 | vault: [], |
| 197 | vaultReserved: [] |
| 198 | }); |
| 199 | harness.stateDb.saveBag(b.character_id, { |
| 200 | inventory: bagB, |
| 201 | equipment: emptyEquipment(), |
| 202 | vault: [], |
| 203 | vaultReserved: [] |
| 204 | }); |
| 205 | harness.simulation.spawnCharacter(1, a); |
| 206 | const bId = harness.simulation.spawnCharacter(2, b); |
| 207 | harness.simulation.step(); |
| 208 | |
| 209 | openTrade(harness, 2, b, 1, harness.simulation.characterEntities.get(a.character_id)); |
| 210 | |
| 211 | harness.simulation.enqueue(2, { |
| 212 | type: "trade-offer-set", |
| 213 | characterId: b.character_id, |
| 214 | bagSlotIndex: 0, |
| 215 | quantity: 1 |
| 216 | }); |
| 217 | harness.simulation.step(); |
| 218 | harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); |
| 219 | harness.simulation.step(); |
| 220 | harness.simulation.enqueue(2, { type: "trade-accept", characterId: b.character_id }); |
| 221 | const result = harness.simulation.step(); |
| 222 | assert.ok(result.events.some((event) => |
| 223 | event.type === "toast" |
| 224 | && /need .* more free inventory slots/i.test(event.message))); |
| 225 | assert.ok(result.events.some((event) => event.type === "trade-state")); |
| 226 | assert.equal(result.events.some((event) => event.type === "trade-closed"), false); |
| 227 | const state = result.events.find((event) => |
| 228 | event.type === "trade-state" && event.characterId === a.character_id); |
| 229 | assert.equal(state?.youAccepted, false); |
| 230 | assert.equal(state?.theyAccepted, false); |
| 231 | void bId; |
| 232 | } |
| 233 | finally |
| 234 | { harness.close(); } |
| 235 | }); |
| 236 | |
| 237 | test("cancel closes the trade for both players", () => |
| 238 | { |
| 239 | const harness = makeSimulation({ workerCount: 1 }); |
| 240 | try |
| 241 | { |
| 242 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 243 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 244 | harness.simulation.spawnCharacter(1, a); |
| 245 | const bId = harness.simulation.spawnCharacter(2, b); |
| 246 | harness.simulation.step(); |
| 247 | openTrade(harness, 1, a, 2, bId); |
| 248 | harness.simulation.enqueue(1, { type: "trade-cancel", characterId: a.character_id }); |
| 249 | const result = harness.simulation.step(); |
| 250 | assert.equal(result.events.filter((event) => event.type === "trade-closed").length, 2); |
| 251 | } |
| 252 | finally |
| 253 | { harness.close(); } |
| 254 | }); |
| 255 | |
| 256 | test("changing an offer clears accept flags", () => |
| 257 | { |
| 258 | const harness = makeSimulation({ workerCount: 1 }); |
| 259 | try |
| 260 | { |
| 261 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 262 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 263 | const bagA = emptySlots(); |
| 264 | bagA[0] = apple(4); |
| 265 | harness.stateDb.saveBag(a.character_id, { |
| 266 | inventory: bagA, |
| 267 | equipment: emptyEquipment(), |
| 268 | vault: [], |
| 269 | vaultReserved: [] |
| 270 | }); |
| 271 | harness.simulation.spawnCharacter(1, a); |
| 272 | const bId = harness.simulation.spawnCharacter(2, b); |
| 273 | harness.simulation.step(); |
| 274 | openTrade(harness, 1, a, 2, bId); |
| 275 | |
| 276 | harness.simulation.enqueue(1, { |
| 277 | type: "trade-offer-set", |
| 278 | characterId: a.character_id, |
| 279 | bagSlotIndex: 0, |
| 280 | quantity: 1 |
| 281 | }); |
| 282 | harness.simulation.step(); |
| 283 | harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); |
| 284 | let state = harness.simulation.step().events.find((event) => |
| 285 | event.type === "trade-state" && event.characterId === a.character_id); |
| 286 | assert.equal(state?.youAccepted, true); |
| 287 | |
| 288 | harness.simulation.enqueue(1, { |
| 289 | type: "trade-offer-set", |
| 290 | characterId: a.character_id, |
| 291 | bagSlotIndex: 0, |
| 292 | quantity: 2 |
| 293 | }); |
| 294 | state = harness.simulation.step().events.find((event) => |
| 295 | event.type === "trade-state" && event.characterId === a.character_id); |
| 296 | assert.equal(state?.youAccepted, false); |
| 297 | assert.equal(state?.theyAccepted, false); |
| 298 | void bId; |
| 299 | } |
| 300 | finally |
| 301 | { harness.close(); } |
| 302 | }); |