import test from "node:test"; import assert from "node:assert/strict"; import { ItemId } from "../../shared/definitions/items.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; test("writing paper creates an opened letter only the author can edit", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Writer" }); const entityId = harness.simulation.spawnCharacter(1, character); const bag = harness.simulation.bagOf(entityId); const paperSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.paper); assert.ok(paperSlot >= 0); const paperQty = bag.inventory[paperSlot].quantity; harness.simulation.enqueue(1, { type: "write-letter", characterId: character.character_id, slotIndex: paperSlot, body: "Hello from Ada" }); harness.simulation.step(); const after = harness.simulation.bagOf(entityId); const letterSlot = after.inventory.findIndex((slot) => slot?.itemId === ItemId.letter); assert.ok(letterSlot >= 0); assert.equal(after.inventory[letterSlot].instance.authorCharacterId, character.character_id); assert.equal(after.inventory[letterSlot].instance.body, "Hello from Ada"); assert.equal(after.inventory[letterSlot].instance.authorDisplayName, "Ada Writer"); assert.equal( after.inventory[letterSlot].origins?.[0]?.origin?.verb, "Written" ); assert.equal( after.inventory[letterSlot].origins?.[0]?.origin?.displayName, "Ada Writer" ); assert.equal(after.inventory[letterSlot].instance.opened, true); assert.equal( after.inventory.filter((slot) => slot?.itemId === ItemId.paper) .reduce((sum, slot) => sum + slot.quantity, 0), paperQty - 1 ); harness.simulation.enqueue(1, { type: "edit-letter", characterId: character.character_id, slotIndex: letterSlot, body: "Updated body" }); harness.simulation.step(); assert.equal(harness.simulation.bagOf(entityId).inventory[letterSlot].instance.body, "Updated body"); } finally { harness.close(); } }); test("sending a letter seals it closed and rejects other authors", () => { const harness = makeSimulation({ workerCount: 1 }); try { const writer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Writer" }); const other = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Other" }); const writerId = harness.simulation.spawnCharacter(1, writer); harness.simulation.spawnCharacter(2, other); const paperSlot = harness.simulation.bagOf(writerId).inventory.findIndex((slot) => slot?.itemId === ItemId.paper); harness.simulation.enqueue(1, { type: "write-letter", characterId: writer.character_id, slotIndex: paperSlot, body: "Secret" }); harness.simulation.step(); const letterSlot = harness.simulation.bagOf(writerId).inventory.findIndex((slot) => slot?.itemId === ItemId.letter); assert.ok(letterSlot >= 0); const sealed = harness.simulation.dispatchPigeon({ characterId: writer.character_id, destination: { kind: "player", firstName: "Bob", lastName: "Other" }, cargo: { kind: "item", slotIndex: letterSlot, quantity: 1 } }); assert.equal(sealed.transit.cargo.stack.instance.opened, false); const otherId = harness.simulation.characterEntities.get(other.character_id); const otherBag = harness.simulation.bagOf(otherId); const stealSlot = otherBag.inventory.findIndex((slot) => !slot); assert.ok(stealSlot >= 0); otherBag.inventory[stealSlot] = { itemId: ItemId.letter, quantity: 1, origin: null, instance: { kind: "letter", letterId: "stolen", authorCharacterId: writer.character_id, authorDisplayName: "Ada Writer", body: "Stolen", opened: true, writtenAt: Date.now() } }; harness.simulation.applyBag(otherId, otherBag); assert.throws( () => harness.simulation.dispatchPigeon({ characterId: other.character_id, destination: { kind: "bank" }, cargo: { kind: "item", slotIndex: stealSlot, quantity: 1 } }), /only send letters you wrote/i ); } finally { harness.close(); } }); test("reading a closed letter opens it", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Reader" }); const entityId = harness.simulation.spawnCharacter(1, character); const bag = harness.simulation.bagOf(entityId); const free = bag.inventory.findIndex((slot) => !slot); bag.inventory[free] = { itemId: ItemId.letter, quantity: 1, origin: null, instance: { kind: "letter", letterId: "mail-1", authorCharacterId: 999, authorDisplayName: "Other Writer", body: "Hello", opened: false, writtenAt: Date.now() } }; harness.simulation.applyBag(entityId, bag); harness.simulation.enqueue(1, { type: "open-letter", characterId: character.character_id, slotIndex: free }); harness.simulation.step(); assert.equal(harness.simulation.bagOf(entityId).inventory[free].instance.opened, true); } finally { harness.close(); } }); test("dropped received letters are only pickable by the dropper", () => { const harness = makeSimulation({ workerCount: 1 }); try { const owner = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Owner" }); const stranger = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Bob", lastName: "Stranger" }); const ownerId = harness.simulation.spawnCharacter(1, owner); const strangerId = harness.simulation.spawnCharacter(2, stranger); const bag = harness.simulation.bagOf(ownerId); const free = bag.inventory.findIndex((slot) => !slot); bag.inventory[free] = { itemId: ItemId.letter, quantity: 1, instance: { kind: "letter", letterId: "priv-1", authorCharacterId: 999, authorDisplayName: "Other Writer", body: "Secret", opened: true, writtenAt: Date.now() } }; harness.simulation.applyBag(ownerId, bag); harness.simulation.enqueue(1, { type: "drop-item", characterId: owner.character_id, slotIndex: free }); const dropped = harness.simulation.step(); const appear = dropped.events.find((event) => event.type === "ground-item-appear"); assert.ok(appear, JSON.stringify(dropped.events)); assert.equal(appear.droppedByCharacterId, owner.character_id); assert.equal(appear.itemId, ItemId.letter); harness.simulation.enqueue(2, { type: "pickup-item", characterId: stranger.character_id, groundItemId: appear.groundItemId }); const stolen = harness.simulation.step(); assert.ok( stolen.events.some((event) => event.type === "command-error"), JSON.stringify(stolen.events) ); assert.equal( harness.simulation.bagOf(strangerId).inventory.some((slot) => slot?.itemId === ItemId.letter), false ); harness.simulation.enqueue(1, { type: "pickup-item", characterId: owner.character_id, groundItemId: appear.groundItemId }); const reclaim = harness.simulation.step(); assert.equal(reclaim.events.some((event) => event.type === "command-error"), false, JSON.stringify(reclaim.events)); assert.ok(harness.simulation.bagOf(ownerId).inventory.some((slot) => slot?.itemId === ItemId.letter)); } finally { harness.close(); } });