| 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 { gatherOrigin, GatherOriginVerb } from "../../shared/items/origin.js"; |
| 6 | import { canTradeStack } from "../../shared/items/trade.js"; |
| 7 | import { |
| 8 | inventorySlotsShortAfterTrade, |
| 9 | resolveOfferStacks |
| 10 | } from "../../shared/trade/space.js"; |
| 11 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 12 | |
| 13 | function apple (qty) |
| 14 | { |
| 15 | return { itemId: ItemId.apple, quantity: qty }; |
| 16 | } |
| 17 | |
| 18 | function stick (qty = 1) |
| 19 | { |
| 20 | return { itemId: ItemId.stick, quantity: qty }; |
| 21 | } |
| 22 | |
| 23 | function openTrade (harness, fromSession, fromChar, toSession, toEntityId) |
| 24 | { |
| 25 | harness.simulation.enqueue(fromSession, { |
| 26 | type: "trade-request", |
| 27 | characterId: fromChar.character_id, |
| 28 | targetEntityId: toEntityId |
| 29 | }); |
| 30 | const req = harness.simulation.step(); |
| 31 | const invite = req.events.find((event) => event.type === "trade-invite"); |
| 32 | assert.ok(invite, JSON.stringify(req.events)); |
| 33 | harness.simulation.enqueue(toSession, { |
| 34 | type: "trade-respond-invite", |
| 35 | characterId: invite.characterId, |
| 36 | inviteId: invite.inviteId, |
| 37 | accept: true |
| 38 | }); |
| 39 | const open = harness.simulation.step(); |
| 40 | assert.equal(open.events.filter((event) => event.type === "trade-open").length, 2, JSON.stringify(open.events)); |
| 41 | return invite; |
| 42 | } |
| 43 | |
| 44 | test("inventorySlotsShortAfterTrade accounts for freed give slots", () => |
| 45 | { |
| 46 | const slots = emptySlots(); |
| 47 | for (let i = 0; i < INVENTORY_SLOTS; i++) |
| 48 | { slots[i] = stick(1); } |
| 49 | assert.equal( |
| 50 | inventorySlotsShortAfterTrade(slots, [{ bagSlotIndex: 0, quantity: 1 }], [apple(1)]), |
| 51 | 0 |
| 52 | ); |
| 53 | assert.equal( |
| 54 | inventorySlotsShortAfterTrade(slots, [], [apple(1)]), |
| 55 | 1 |
| 56 | ); |
| 57 | }); |
| 58 | |
| 59 | test("resolveOfferStacks rejects missing quantities and slices origins", () => |
| 60 | { |
| 61 | const slots = emptySlots(); |
| 62 | slots[0] = apple(3); |
| 63 | assert.equal(resolveOfferStacks(slots, [{ bagSlotIndex: 0, quantity: 4 }]), null); |
| 64 | const resolved = resolveOfferStacks(slots, [{ bagSlotIndex: 0, quantity: 2 }]); |
| 65 | assert.equal(resolved?.length, 1); |
| 66 | assert.equal(resolved[0].itemId, ItemId.apple); |
| 67 | assert.equal(resolved[0].quantity, 2); |
| 68 | assert.equal(resolved[0].origins?.reduce((sum, entry) => sum + (entry.quantity | 0), 0), 2); |
| 69 | }); |
| 70 | |
| 71 | test("players can complete a 1-for-1 trade while adjacent", () => |
| 72 | { |
| 73 | const harness = makeSimulation({ workerCount: 1 }); |
| 74 | try |
| 75 | { |
| 76 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Alpha" }); |
| 77 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Beta" }); |
| 78 | const bagA = emptySlots(); |
| 79 | bagA[0] = apple(5); |
| 80 | const bagB = emptySlots(); |
| 81 | bagB[0] = stick(1); |
| 82 | harness.stateDb.saveBag(a.character_id, { |
| 83 | inventory: bagA, |
| 84 | equipment: emptyEquipment(), |
| 85 | vault: [], |
| 86 | vaultReserved: [] |
| 87 | }); |
| 88 | harness.stateDb.saveBag(b.character_id, { |
| 89 | inventory: bagB, |
| 90 | equipment: emptyEquipment(), |
| 91 | vault: [], |
| 92 | vaultReserved: [] |
| 93 | }); |
| 94 | const aId = harness.simulation.spawnCharacter(1, a); |
| 95 | const bId = harness.simulation.spawnCharacter(2, b); |
| 96 | harness.simulation.step(); |
| 97 | |
| 98 | openTrade(harness, 1, a, 2, bId); |
| 99 | |
| 100 | harness.simulation.enqueue(1, { |
| 101 | type: "trade-offer-set", |
| 102 | characterId: a.character_id, |
| 103 | bagSlotIndex: 0, |
| 104 | quantity: 2 |
| 105 | }); |
| 106 | harness.simulation.enqueue(2, { |
| 107 | type: "trade-offer-set", |
| 108 | characterId: b.character_id, |
| 109 | bagSlotIndex: 0, |
| 110 | quantity: 1 |
| 111 | }); |
| 112 | harness.simulation.step(); |
| 113 | |
| 114 | harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); |
| 115 | harness.simulation.step(); |
| 116 | harness.simulation.enqueue(2, { type: "trade-accept", characterId: b.character_id }); |
| 117 | const done = harness.simulation.step(); |
| 118 | assert.ok(done.events.some((event) => event.type === "trade-closed")); |
| 119 | assert.ok(done.events.some((event) => |
| 120 | event.type === "toast" && event.message === "Trade complete.")); |
| 121 | |
| 122 | const invA = harness.simulation.bagOf(aId).inventory; |
| 123 | const invB = harness.simulation.bagOf(bId).inventory; |
| 124 | assert.equal(invA.find((slot) => slot?.itemId === ItemId.stick)?.quantity, 1); |
| 125 | assert.equal(invA.find((slot) => slot?.itemId === ItemId.apple)?.quantity, 3); |
| 126 | assert.equal(invB.find((slot) => slot?.itemId === ItemId.apple)?.quantity, 2); |
| 127 | assert.equal(invB.some((slot) => slot?.itemId === ItemId.stick), false); |
| 128 | } |
| 129 | finally |
| 130 | { harness.close(); } |
| 131 | }); |
| 132 | |
| 133 | test("reject blocks the requester for one minute", () => |
| 134 | { |
| 135 | const harness = makeSimulation({ workerCount: 1, now: 5_000 }); |
| 136 | try |
| 137 | { |
| 138 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Ask" }); |
| 139 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Block" }); |
| 140 | harness.simulation.spawnCharacter(1, a); |
| 141 | const bId = harness.simulation.spawnCharacter(2, b); |
| 142 | harness.simulation.step(); |
| 143 | |
| 144 | harness.simulation.enqueue(1, { |
| 145 | type: "trade-request", |
| 146 | characterId: a.character_id, |
| 147 | targetEntityId: bId |
| 148 | }); |
| 149 | const req = harness.simulation.step(); |
| 150 | const invite = req.events.find((event) => event.type === "trade-invite"); |
| 151 | assert.ok(invite); |
| 152 | |
| 153 | harness.simulation.enqueue(2, { |
| 154 | type: "trade-respond-invite", |
| 155 | characterId: b.character_id, |
| 156 | inviteId: invite.inviteId, |
| 157 | accept: false |
| 158 | }); |
| 159 | harness.simulation.step(); |
| 160 | |
| 161 | harness.simulation.enqueue(1, { |
| 162 | type: "trade-request", |
| 163 | characterId: a.character_id, |
| 164 | targetEntityId: bId |
| 165 | }); |
| 166 | const blocked = harness.simulation.step(); |
| 167 | assert.ok(blocked.events.some((event) => |
| 168 | event.type === "command-error" |
| 169 | && /declined a recent trade request/i.test(event.message))); |
| 170 | |
| 171 | harness.setNow(5_000 + 60_000); |
| 172 | harness.simulation.enqueue(1, { |
| 173 | type: "trade-request", |
| 174 | characterId: a.character_id, |
| 175 | targetEntityId: bId |
| 176 | }); |
| 177 | const again = harness.simulation.step(); |
| 178 | assert.ok(again.events.some((event) => event.type === "trade-invite")); |
| 179 | } |
| 180 | finally |
| 181 | { harness.close(); } |
| 182 | }); |
| 183 | |
| 184 | test("full inventory without give slots fails with slot toast and resets accepts", () => |
| 185 | { |
| 186 | const harness = makeSimulation({ workerCount: 1 }); |
| 187 | try |
| 188 | { |
| 189 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Full" }); |
| 190 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Giver" }); |
| 191 | const bagA = emptySlots(); |
| 192 | for (let i = 0; i < INVENTORY_SLOTS; i++) |
| 193 | { bagA[i] = stick(1); } |
| 194 | const bagB = emptySlots(); |
| 195 | bagB[0] = apple(1); |
| 196 | harness.stateDb.saveBag(a.character_id, { |
| 197 | inventory: bagA, |
| 198 | equipment: emptyEquipment(), |
| 199 | vault: [], |
| 200 | vaultReserved: [] |
| 201 | }); |
| 202 | harness.stateDb.saveBag(b.character_id, { |
| 203 | inventory: bagB, |
| 204 | equipment: emptyEquipment(), |
| 205 | vault: [], |
| 206 | vaultReserved: [] |
| 207 | }); |
| 208 | harness.simulation.spawnCharacter(1, a); |
| 209 | const bId = harness.simulation.spawnCharacter(2, b); |
| 210 | harness.simulation.step(); |
| 211 | |
| 212 | openTrade(harness, 2, b, 1, harness.simulation.characterEntities.get(a.character_id)); |
| 213 | |
| 214 | harness.simulation.enqueue(2, { |
| 215 | type: "trade-offer-set", |
| 216 | characterId: b.character_id, |
| 217 | bagSlotIndex: 0, |
| 218 | quantity: 1 |
| 219 | }); |
| 220 | harness.simulation.step(); |
| 221 | harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); |
| 222 | harness.simulation.step(); |
| 223 | harness.simulation.enqueue(2, { type: "trade-accept", characterId: b.character_id }); |
| 224 | const result = harness.simulation.step(); |
| 225 | assert.ok(result.events.some((event) => |
| 226 | event.type === "toast" |
| 227 | && /need .* more free inventory slots/i.test(event.message))); |
| 228 | assert.ok(result.events.some((event) => event.type === "trade-state")); |
| 229 | assert.equal(result.events.some((event) => event.type === "trade-closed"), false); |
| 230 | const state = result.events.find((event) => |
| 231 | event.type === "trade-state" && event.characterId === a.character_id); |
| 232 | assert.equal(state?.youAccepted, false); |
| 233 | assert.equal(state?.theyAccepted, false); |
| 234 | void bId; |
| 235 | } |
| 236 | finally |
| 237 | { harness.close(); } |
| 238 | }); |
| 239 | |
| 240 | test("cancel closes the trade for both players", () => |
| 241 | { |
| 242 | const harness = makeSimulation({ workerCount: 1 }); |
| 243 | try |
| 244 | { |
| 245 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 246 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 247 | harness.simulation.spawnCharacter(1, a); |
| 248 | const bId = harness.simulation.spawnCharacter(2, b); |
| 249 | harness.simulation.step(); |
| 250 | openTrade(harness, 1, a, 2, bId); |
| 251 | harness.simulation.enqueue(1, { type: "trade-cancel", characterId: a.character_id }); |
| 252 | const result = harness.simulation.step(); |
| 253 | assert.equal(result.events.filter((event) => event.type === "trade-closed").length, 2); |
| 254 | } |
| 255 | finally |
| 256 | { harness.close(); } |
| 257 | }); |
| 258 | |
| 259 | test("changing an offer clears accept flags", () => |
| 260 | { |
| 261 | const harness = makeSimulation({ workerCount: 1 }); |
| 262 | try |
| 263 | { |
| 264 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 265 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 266 | const bagA = emptySlots(); |
| 267 | bagA[0] = apple(4); |
| 268 | harness.stateDb.saveBag(a.character_id, { |
| 269 | inventory: bagA, |
| 270 | equipment: emptyEquipment(), |
| 271 | vault: [], |
| 272 | vaultReserved: [] |
| 273 | }); |
| 274 | harness.simulation.spawnCharacter(1, a); |
| 275 | const bId = harness.simulation.spawnCharacter(2, b); |
| 276 | harness.simulation.step(); |
| 277 | openTrade(harness, 1, a, 2, bId); |
| 278 | |
| 279 | harness.simulation.enqueue(1, { |
| 280 | type: "trade-offer-set", |
| 281 | characterId: a.character_id, |
| 282 | bagSlotIndex: 0, |
| 283 | quantity: 1 |
| 284 | }); |
| 285 | harness.simulation.step(); |
| 286 | harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); |
| 287 | let state = harness.simulation.step().events.find((event) => |
| 288 | event.type === "trade-state" && event.characterId === a.character_id); |
| 289 | assert.equal(state?.youAccepted, true); |
| 290 | |
| 291 | harness.simulation.enqueue(1, { |
| 292 | type: "trade-offer-set", |
| 293 | characterId: a.character_id, |
| 294 | bagSlotIndex: 0, |
| 295 | quantity: 2 |
| 296 | }); |
| 297 | state = harness.simulation.step().events.find((event) => |
| 298 | event.type === "trade-state" && event.characterId === a.character_id); |
| 299 | assert.equal(state?.youAccepted, false); |
| 300 | assert.equal(state?.theyAccepted, false); |
| 301 | void bId; |
| 302 | } |
| 303 | finally |
| 304 | { harness.close(); } |
| 305 | }); |
| 306 | |
| 307 | test("received letters cannot be traded; authored letters can", () => |
| 308 | { |
| 309 | const harness = makeSimulation({ workerCount: 1 }); |
| 310 | try |
| 311 | { |
| 312 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Author" }); |
| 313 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Buddy" }); |
| 314 | const bagA = emptySlots(); |
| 315 | bagA[0] = { |
| 316 | itemId: ItemId.letter, |
| 317 | quantity: 1, |
| 318 | instance: { |
| 319 | kind: "letter", |
| 320 | letterId: "recv-1", |
| 321 | authorCharacterId: 999, |
| 322 | authorDisplayName: "Other", |
| 323 | body: "Hi", |
| 324 | opened: true, |
| 325 | writtenAt: Date.now() |
| 326 | } |
| 327 | }; |
| 328 | bagA[1] = { |
| 329 | itemId: ItemId.letter, |
| 330 | quantity: 1, |
| 331 | instance: { |
| 332 | kind: "letter", |
| 333 | letterId: "own-1", |
| 334 | authorCharacterId: a.character_id, |
| 335 | authorDisplayName: "Ann Author", |
| 336 | body: "Hello", |
| 337 | opened: true, |
| 338 | writtenAt: Date.now() |
| 339 | } |
| 340 | }; |
| 341 | harness.stateDb.saveBag(a.character_id, { |
| 342 | inventory: bagA, |
| 343 | equipment: emptyEquipment(), |
| 344 | vault: [], |
| 345 | vaultReserved: [] |
| 346 | }); |
| 347 | harness.simulation.spawnCharacter(1, a); |
| 348 | const bId = harness.simulation.spawnCharacter(2, b); |
| 349 | harness.simulation.step(); |
| 350 | openTrade(harness, 1, a, 2, bId); |
| 351 | |
| 352 | harness.simulation.enqueue(1, { |
| 353 | type: "trade-offer-set", |
| 354 | characterId: a.character_id, |
| 355 | bagSlotIndex: 0, |
| 356 | quantity: 1 |
| 357 | }); |
| 358 | const blocked = harness.simulation.step(); |
| 359 | assert.ok(blocked.events.some((event) => |
| 360 | event.type === "command-error" |
| 361 | && /only trade letters you wrote/i.test(event.message))); |
| 362 | |
| 363 | harness.simulation.enqueue(1, { |
| 364 | type: "trade-offer-set", |
| 365 | characterId: a.character_id, |
| 366 | bagSlotIndex: 1, |
| 367 | quantity: 1 |
| 368 | }); |
| 369 | const allowed = harness.simulation.step(); |
| 370 | assert.equal(allowed.events.some((event) => event.type === "command-error"), false, JSON.stringify(allowed.events)); |
| 371 | const state = allowed.events.find((event) => |
| 372 | event.type === "trade-state" && event.characterId === a.character_id); |
| 373 | assert.equal(state?.yourOffer?.[0]?.itemId, ItemId.letter); |
| 374 | assert.equal(state?.yourOffer?.[0]?.instance?.letterId, "own-1"); |
| 375 | void bId; |
| 376 | } |
| 377 | finally |
| 378 | { harness.close(); } |
| 379 | }); |
| 380 | |
| 381 | test("trade preserves item origins on partial stacks", () => |
| 382 | { |
| 383 | const harness = makeSimulation({ workerCount: 1 }); |
| 384 | try |
| 385 | { |
| 386 | const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Alpha" }); |
| 387 | const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Beta" }); |
| 388 | const mined = gatherOrigin(GatherOriginVerb.mined, "Ann Alpha"); |
| 389 | const bagA = emptySlots(); |
| 390 | bagA[0] = { |
| 391 | itemId: ItemId.apple, |
| 392 | quantity: 5, |
| 393 | origins: [{ origin: mined, quantity: 5 }] |
| 394 | }; |
| 395 | harness.stateDb.saveBag(a.character_id, { |
| 396 | inventory: bagA, |
| 397 | equipment: emptyEquipment(), |
| 398 | vault: [], |
| 399 | vaultReserved: [] |
| 400 | }); |
| 401 | const aId = harness.simulation.spawnCharacter(1, a); |
| 402 | const bId = harness.simulation.spawnCharacter(2, b); |
| 403 | harness.simulation.step(); |
| 404 | openTrade(harness, 1, a, 2, bId); |
| 405 | |
| 406 | harness.simulation.enqueue(1, { |
| 407 | type: "trade-offer-set", |
| 408 | characterId: a.character_id, |
| 409 | bagSlotIndex: 0, |
| 410 | quantity: 2 |
| 411 | }); |
| 412 | const offered = harness.simulation.step(); |
| 413 | const preview = offered.events.find((event) => |
| 414 | event.type === "trade-state" && event.characterId === b.character_id); |
| 415 | assert.equal(preview?.theirOffer?.[0]?.origins?.[0]?.quantity, 2); |
| 416 | assert.deepEqual(preview?.theirOffer?.[0]?.origins?.[0]?.origin, mined); |
| 417 | |
| 418 | harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); |
| 419 | harness.simulation.step(); |
| 420 | harness.simulation.enqueue(2, { type: "trade-accept", characterId: b.character_id }); |
| 421 | harness.simulation.step(); |
| 422 | |
| 423 | const got = harness.simulation.bagOf(bId).inventory.find((slot) => slot?.itemId === ItemId.apple); |
| 424 | assert.ok(got); |
| 425 | const received = (got.origins ?? []).find((entry) => |
| 426 | entry.origin?.kind === mined.kind |
| 427 | && entry.origin?.displayName === mined.displayName); |
| 428 | assert.ok(received, JSON.stringify(got.origins)); |
| 429 | assert.equal(received.quantity, 2); |
| 430 | const remain = harness.simulation.bagOf(aId).inventory.find((slot) => slot?.itemId === ItemId.apple); |
| 431 | assert.equal(remain?.quantity, 3); |
| 432 | assert.deepEqual(remain?.origins, [{ origin: mined, quantity: 3 }]); |
| 433 | } |
| 434 | finally |
| 435 | { harness.close(); } |
| 436 | }); |
| 437 | |
| 438 | test("canTradeStack allows normal goods and authored letters only", () => |
| 439 | { |
| 440 | assert.equal(canTradeStack({ itemId: ItemId.apple, quantity: 1 }, 1), true); |
| 441 | assert.equal(canTradeStack({ |
| 442 | itemId: ItemId.letter, |
| 443 | quantity: 1, |
| 444 | instance: { kind: "letter", authorCharacterId: 1, body: "Hi" } |
| 445 | }, 1), true); |
| 446 | assert.equal(canTradeStack({ |
| 447 | itemId: ItemId.letter, |
| 448 | quantity: 1, |
| 449 | instance: { kind: "letter", authorCharacterId: 99, body: "Hi" } |
| 450 | }, 1), false); |
| 451 | }); |