import test from "node:test"; import assert from "node:assert/strict"; import { ItemId } from "../../shared/definitions/items.js"; import { emptyEquipment, emptySlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js"; import { inventorySlotsShortAfterTrade, resolveOfferStacks } from "../../shared/trade/space.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; function apple (qty) { return { itemId: ItemId.apple, quantity: qty }; } function stick (qty = 1) { return { itemId: ItemId.stick, quantity: qty }; } function openTrade (harness, fromSession, fromChar, toSession, toEntityId) { harness.simulation.enqueue(fromSession, { type: "trade-request", characterId: fromChar.character_id, targetEntityId: toEntityId }); const req = harness.simulation.step(); const invite = req.events.find((event) => event.type === "trade-invite"); assert.ok(invite, JSON.stringify(req.events)); harness.simulation.enqueue(toSession, { type: "trade-respond-invite", characterId: invite.characterId, inviteId: invite.inviteId, accept: true }); const open = harness.simulation.step(); assert.equal(open.events.filter((event) => event.type === "trade-open").length, 2, JSON.stringify(open.events)); return invite; } test("inventorySlotsShortAfterTrade accounts for freed give slots", () => { const slots = emptySlots(); for (let i = 0; i < INVENTORY_SLOTS; i++) { slots[i] = stick(1); } assert.equal( inventorySlotsShortAfterTrade(slots, [{ bagSlotIndex: 0, quantity: 1 }], [apple(1)]), 0 ); assert.equal( inventorySlotsShortAfterTrade(slots, [], [apple(1)]), 1 ); }); test("resolveOfferStacks rejects missing quantities", () => { const slots = emptySlots(); slots[0] = apple(3); assert.equal(resolveOfferStacks(slots, [{ bagSlotIndex: 0, quantity: 4 }]), null); assert.deepEqual( resolveOfferStacks(slots, [{ bagSlotIndex: 0, quantity: 2 }]), [{ itemId: ItemId.apple, quantity: 2, instance: null, origins: undefined }] ); }); test("players can complete a 1-for-1 trade while adjacent", () => { const harness = makeSimulation({ workerCount: 1 }); try { const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Alpha" }); const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Beta" }); const bagA = emptySlots(); bagA[0] = apple(5); const bagB = emptySlots(); bagB[0] = stick(1); harness.stateDb.saveBag(a.character_id, { inventory: bagA, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); harness.stateDb.saveBag(b.character_id, { inventory: bagB, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const aId = harness.simulation.spawnCharacter(1, a); const bId = harness.simulation.spawnCharacter(2, b); harness.simulation.step(); openTrade(harness, 1, a, 2, bId); harness.simulation.enqueue(1, { type: "trade-offer-set", characterId: a.character_id, bagSlotIndex: 0, quantity: 2 }); harness.simulation.enqueue(2, { type: "trade-offer-set", characterId: b.character_id, bagSlotIndex: 0, quantity: 1 }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); harness.simulation.step(); harness.simulation.enqueue(2, { type: "trade-accept", characterId: b.character_id }); const done = harness.simulation.step(); assert.ok(done.events.some((event) => event.type === "trade-closed")); assert.ok(done.events.some((event) => event.type === "toast" && event.message === "Trade complete.")); const invA = harness.simulation.bagOf(aId).inventory; const invB = harness.simulation.bagOf(bId).inventory; assert.equal(invA.find((slot) => slot?.itemId === ItemId.stick)?.quantity, 1); assert.equal(invA.find((slot) => slot?.itemId === ItemId.apple)?.quantity, 3); assert.equal(invB.find((slot) => slot?.itemId === ItemId.apple)?.quantity, 2); assert.equal(invB.some((slot) => slot?.itemId === ItemId.stick), false); } finally { harness.close(); } }); test("reject blocks the requester for one minute", () => { const harness = makeSimulation({ workerCount: 1, now: 5_000 }); try { const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Ask" }); const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Block" }); harness.simulation.spawnCharacter(1, a); const bId = harness.simulation.spawnCharacter(2, b); harness.simulation.step(); harness.simulation.enqueue(1, { type: "trade-request", characterId: a.character_id, targetEntityId: bId }); const req = harness.simulation.step(); const invite = req.events.find((event) => event.type === "trade-invite"); assert.ok(invite); harness.simulation.enqueue(2, { type: "trade-respond-invite", characterId: b.character_id, inviteId: invite.inviteId, accept: false }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "trade-request", characterId: a.character_id, targetEntityId: bId }); const blocked = harness.simulation.step(); assert.ok(blocked.events.some((event) => event.type === "command-error" && /declined a recent trade request/i.test(event.message))); harness.setNow(5_000 + 60_000); harness.simulation.enqueue(1, { type: "trade-request", characterId: a.character_id, targetEntityId: bId }); const again = harness.simulation.step(); assert.ok(again.events.some((event) => event.type === "trade-invite")); } finally { harness.close(); } }); test("full inventory without give slots fails with slot toast and resets accepts", () => { const harness = makeSimulation({ workerCount: 1 }); try { const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Full" }); const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Giver" }); const bagA = emptySlots(); for (let i = 0; i < INVENTORY_SLOTS; i++) { bagA[i] = stick(1); } const bagB = emptySlots(); bagB[0] = apple(1); harness.stateDb.saveBag(a.character_id, { inventory: bagA, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); harness.stateDb.saveBag(b.character_id, { inventory: bagB, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); harness.simulation.spawnCharacter(1, a); const bId = harness.simulation.spawnCharacter(2, b); harness.simulation.step(); openTrade(harness, 2, b, 1, harness.simulation.characterEntities.get(a.character_id)); harness.simulation.enqueue(2, { type: "trade-offer-set", characterId: b.character_id, bagSlotIndex: 0, quantity: 1 }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); harness.simulation.step(); harness.simulation.enqueue(2, { type: "trade-accept", characterId: b.character_id }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "toast" && /need .* more free inventory slots/i.test(event.message))); assert.ok(result.events.some((event) => event.type === "trade-state")); assert.equal(result.events.some((event) => event.type === "trade-closed"), false); const state = result.events.find((event) => event.type === "trade-state" && event.characterId === a.character_id); assert.equal(state?.youAccepted, false); assert.equal(state?.theyAccepted, false); void bId; } finally { harness.close(); } }); test("cancel closes the trade for both players", () => { const harness = makeSimulation({ workerCount: 1 }); try { const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); harness.simulation.spawnCharacter(1, a); const bId = harness.simulation.spawnCharacter(2, b); harness.simulation.step(); openTrade(harness, 1, a, 2, bId); harness.simulation.enqueue(1, { type: "trade-cancel", characterId: a.character_id }); const result = harness.simulation.step(); assert.equal(result.events.filter((event) => event.type === "trade-closed").length, 2); } finally { harness.close(); } }); test("changing an offer clears accept flags", () => { const harness = makeSimulation({ workerCount: 1 }); try { const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const bagA = emptySlots(); bagA[0] = apple(4); harness.stateDb.saveBag(a.character_id, { inventory: bagA, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); harness.simulation.spawnCharacter(1, a); const bId = harness.simulation.spawnCharacter(2, b); harness.simulation.step(); openTrade(harness, 1, a, 2, bId); harness.simulation.enqueue(1, { type: "trade-offer-set", characterId: a.character_id, bagSlotIndex: 0, quantity: 1 }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "trade-accept", characterId: a.character_id }); let state = harness.simulation.step().events.find((event) => event.type === "trade-state" && event.characterId === a.character_id); assert.equal(state?.youAccepted, true); harness.simulation.enqueue(1, { type: "trade-offer-set", characterId: a.character_id, bagSlotIndex: 0, quantity: 2 }); state = harness.simulation.step().events.find((event) => event.type === "trade-state" && event.characterId === a.character_id); assert.equal(state?.youAccepted, false); assert.equal(state?.theyAccepted, false); void bId; } finally { harness.close(); } });