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 { gatherOrigin, GatherOriginVerb } from "../../shared/items/origin.js"; import { canTradeStack } from "../../shared/items/trade.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 and slices origins", () => { const slots = emptySlots(); slots[0] = apple(3); assert.equal(resolveOfferStacks(slots, [{ bagSlotIndex: 0, quantity: 4 }]), null); const resolved = resolveOfferStacks(slots, [{ bagSlotIndex: 0, quantity: 2 }]); assert.equal(resolved?.length, 1); assert.equal(resolved[0].itemId, ItemId.apple); assert.equal(resolved[0].quantity, 2); assert.equal(resolved[0].origins?.reduce((sum, entry) => sum + (entry.quantity | 0), 0), 2); }); 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(); } }); test("received letters cannot be traded; authored letters can", () => { const harness = makeSimulation({ workerCount: 1 }); try { const a = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ann", lastName: "Author" }); const b = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9, firstName: "Bob", lastName: "Buddy" }); const bagA = emptySlots(); bagA[0] = { itemId: ItemId.letter, quantity: 1, instance: { kind: "letter", letterId: "recv-1", authorCharacterId: 999, authorDisplayName: "Other", body: "Hi", opened: true, writtenAt: Date.now() } }; bagA[1] = { itemId: ItemId.letter, quantity: 1, instance: { kind: "letter", letterId: "own-1", authorCharacterId: a.character_id, authorDisplayName: "Ann Author", body: "Hello", opened: true, writtenAt: Date.now() } }; 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 }); const blocked = harness.simulation.step(); assert.ok(blocked.events.some((event) => event.type === "command-error" && /only trade letters you wrote/i.test(event.message))); harness.simulation.enqueue(1, { type: "trade-offer-set", characterId: a.character_id, bagSlotIndex: 1, quantity: 1 }); const allowed = harness.simulation.step(); assert.equal(allowed.events.some((event) => event.type === "command-error"), false, JSON.stringify(allowed.events)); const state = allowed.events.find((event) => event.type === "trade-state" && event.characterId === a.character_id); assert.equal(state?.yourOffer?.[0]?.itemId, ItemId.letter); assert.equal(state?.yourOffer?.[0]?.instance?.letterId, "own-1"); void bId; } finally { harness.close(); } }); test("trade preserves item origins on partial stacks", () => { 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 mined = gatherOrigin(GatherOriginVerb.mined, "Ann Alpha"); const bagA = emptySlots(); bagA[0] = { itemId: ItemId.apple, quantity: 5, origins: [{ origin: mined, quantity: 5 }] }; harness.stateDb.saveBag(a.character_id, { inventory: bagA, 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 }); const offered = harness.simulation.step(); const preview = offered.events.find((event) => event.type === "trade-state" && event.characterId === b.character_id); assert.equal(preview?.theirOffer?.[0]?.origins?.[0]?.quantity, 2); assert.deepEqual(preview?.theirOffer?.[0]?.origins?.[0]?.origin, mined); 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 }); harness.simulation.step(); const got = harness.simulation.bagOf(bId).inventory.find((slot) => slot?.itemId === ItemId.apple); assert.ok(got); const received = (got.origins ?? []).find((entry) => entry.origin?.kind === mined.kind && entry.origin?.displayName === mined.displayName); assert.ok(received, JSON.stringify(got.origins)); assert.equal(received.quantity, 2); const remain = harness.simulation.bagOf(aId).inventory.find((slot) => slot?.itemId === ItemId.apple); assert.equal(remain?.quantity, 3); assert.deepEqual(remain?.origins, [{ origin: mined, quantity: 3 }]); } finally { harness.close(); } }); test("canTradeStack allows normal goods and authored letters only", () => { assert.equal(canTradeStack({ itemId: ItemId.apple, quantity: 1 }, 1), true); assert.equal(canTradeStack({ itemId: ItemId.letter, quantity: 1, instance: { kind: "letter", authorCharacterId: 1, body: "Hi" } }, 1), true); assert.equal(canTradeStack({ itemId: ItemId.letter, quantity: 1, instance: { kind: "letter", authorCharacterId: 99, body: "Hi" } }, 1), false); });