| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { ItemId } from "../../shared/definitions/items.js"; |
| 4 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 5 | |
| 6 | test("writing paper creates an opened letter only the author can edit", () => |
| 7 | { |
| 8 | const harness = makeSimulation({ workerCount: 1 }); |
| 9 | try |
| 10 | { |
| 11 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Writer" }); |
| 12 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 13 | const bag = harness.simulation.bagOf(entityId); |
| 14 | const paperSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.paper); |
| 15 | assert.ok(paperSlot >= 0); |
| 16 | const paperQty = bag.inventory[paperSlot].quantity; |
| 17 | |
| 18 | harness.simulation.enqueue(1, { |
| 19 | type: "write-letter", |
| 20 | characterId: character.character_id, |
| 21 | slotIndex: paperSlot, |
| 22 | body: "Hello from Ada" |
| 23 | }); |
| 24 | harness.simulation.step(); |
| 25 | const after = harness.simulation.bagOf(entityId); |
| 26 | const letterSlot = after.inventory.findIndex((slot) => slot?.itemId === ItemId.letter); |
| 27 | assert.ok(letterSlot >= 0); |
| 28 | assert.equal(after.inventory[letterSlot].instance.authorCharacterId, character.character_id); |
| 29 | assert.equal(after.inventory[letterSlot].instance.body, "Hello from Ada"); |
| 30 | assert.equal(after.inventory[letterSlot].instance.authorDisplayName, "Ada Writer"); |
| 31 | assert.equal(after.inventory[letterSlot].instance.opened, true); |
| 32 | assert.equal( |
| 33 | after.inventory.filter((slot) => slot?.itemId === ItemId.paper) |
| 34 | .reduce((sum, slot) => sum + slot.quantity, 0), |
| 35 | paperQty - 1 |
| 36 | ); |
| 37 | |
| 38 | harness.simulation.enqueue(1, { |
| 39 | type: "edit-letter", |
| 40 | characterId: character.character_id, |
| 41 | slotIndex: letterSlot, |
| 42 | body: "Updated body" |
| 43 | }); |
| 44 | harness.simulation.step(); |
| 45 | assert.equal(harness.simulation.bagOf(entityId).inventory[letterSlot].instance.body, "Updated body"); |
| 46 | } |
| 47 | finally |
| 48 | { harness.close(); } |
| 49 | }); |
| 50 | |
| 51 | test("sending a letter seals it closed and rejects other authors", () => |
| 52 | { |
| 53 | const harness = makeSimulation({ workerCount: 1 }); |
| 54 | try |
| 55 | { |
| 56 | const writer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Writer" }); |
| 57 | const other = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Other" }); |
| 58 | const writerId = harness.simulation.spawnCharacter(1, writer); |
| 59 | harness.simulation.spawnCharacter(2, other); |
| 60 | const paperSlot = harness.simulation.bagOf(writerId).inventory.findIndex((slot) => slot?.itemId === ItemId.paper); |
| 61 | harness.simulation.enqueue(1, { |
| 62 | type: "write-letter", |
| 63 | characterId: writer.character_id, |
| 64 | slotIndex: paperSlot, |
| 65 | body: "Secret" |
| 66 | }); |
| 67 | harness.simulation.step(); |
| 68 | const letterSlot = harness.simulation.bagOf(writerId).inventory.findIndex((slot) => slot?.itemId === ItemId.letter); |
| 69 | assert.ok(letterSlot >= 0); |
| 70 | |
| 71 | const sealed = harness.simulation.dispatchPigeon({ |
| 72 | characterId: writer.character_id, |
| 73 | destination: { kind: "player", firstName: "Bob", lastName: "Other" }, |
| 74 | cargo: { kind: "item", slotIndex: letterSlot, quantity: 1 } |
| 75 | }); |
| 76 | assert.equal(sealed.transit.cargo.stack.instance.opened, false); |
| 77 | |
| 78 | const otherId = harness.simulation.characterEntities.get(other.character_id); |
| 79 | const otherBag = harness.simulation.bagOf(otherId); |
| 80 | const stealSlot = otherBag.inventory.findIndex((slot) => !slot); |
| 81 | assert.ok(stealSlot >= 0); |
| 82 | otherBag.inventory[stealSlot] = { |
| 83 | itemId: ItemId.letter, |
| 84 | quantity: 1, |
| 85 | origin: null, |
| 86 | instance: { |
| 87 | kind: "letter", |
| 88 | letterId: "stolen", |
| 89 | authorCharacterId: writer.character_id, |
| 90 | authorDisplayName: "Ada Writer", |
| 91 | body: "Stolen", |
| 92 | opened: true, |
| 93 | writtenAt: Date.now() |
| 94 | } |
| 95 | }; |
| 96 | harness.simulation.applyBag(otherId, otherBag); |
| 97 | assert.throws( |
| 98 | () => harness.simulation.dispatchPigeon({ |
| 99 | characterId: other.character_id, |
| 100 | destination: { kind: "bank" }, |
| 101 | cargo: { kind: "item", slotIndex: stealSlot, quantity: 1 } |
| 102 | }), |
| 103 | /only send letters you wrote/i |
| 104 | ); |
| 105 | } |
| 106 | finally |
| 107 | { harness.close(); } |
| 108 | }); |
| 109 | |
| 110 | test("reading a closed letter opens it", () => |
| 111 | { |
| 112 | const harness = makeSimulation({ workerCount: 1 }); |
| 113 | try |
| 114 | { |
| 115 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Reader" }); |
| 116 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 117 | const bag = harness.simulation.bagOf(entityId); |
| 118 | const free = bag.inventory.findIndex((slot) => !slot); |
| 119 | bag.inventory[free] = { |
| 120 | itemId: ItemId.letter, |
| 121 | quantity: 1, |
| 122 | origin: null, |
| 123 | instance: { |
| 124 | kind: "letter", |
| 125 | letterId: "mail-1", |
| 126 | authorCharacterId: 999, |
| 127 | authorDisplayName: "Other Writer", |
| 128 | body: "Hello", |
| 129 | opened: false, |
| 130 | writtenAt: Date.now() |
| 131 | } |
| 132 | }; |
| 133 | harness.simulation.applyBag(entityId, bag); |
| 134 | harness.simulation.enqueue(1, { |
| 135 | type: "open-letter", |
| 136 | characterId: character.character_id, |
| 137 | slotIndex: free |
| 138 | }); |
| 139 | harness.simulation.step(); |
| 140 | assert.equal(harness.simulation.bagOf(entityId).inventory[free].instance.opened, true); |
| 141 | } |
| 142 | finally |
| 143 | { harness.close(); } |
| 144 | }); |