BlueGit

timfa questward

Files

questward / server / tests / letters.test.js

letters.test.js

letters.test.js 7.2 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { ItemId } from "../../shared/definitions/items.js";
4import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
5
6test("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(
32 after.inventory[letterSlot].origins?.[0]?.origin?.verb,
33 "Written"
34 );
35 assert.equal(
36 after.inventory[letterSlot].origins?.[0]?.origin?.displayName,
37 "Ada Writer"
38 );
39 assert.equal(after.inventory[letterSlot].instance.opened, true);
40 assert.equal(
41 after.inventory.filter((slot) => slot?.itemId === ItemId.paper)
42 .reduce((sum, slot) => sum + slot.quantity, 0),
43 paperQty - 1
44 );
45
46 harness.simulation.enqueue(1, {
47 type: "edit-letter",
48 characterId: character.character_id,
49 slotIndex: letterSlot,
50 body: "Updated body"
51 });
52 harness.simulation.step();
53 assert.equal(harness.simulation.bagOf(entityId).inventory[letterSlot].instance.body, "Updated body");
54 }
55 finally
56 { harness.close(); }
57});
58
59test("sending a letter seals it closed and rejects other authors", () =>
60{
61 const harness = makeSimulation({ workerCount: 1 });
62 try
63 {
64 const writer = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Writer" });
65 const other = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Other" });
66 const writerId = harness.simulation.spawnCharacter(1, writer);
67 harness.simulation.spawnCharacter(2, other);
68 const paperSlot = harness.simulation.bagOf(writerId).inventory.findIndex((slot) => slot?.itemId === ItemId.paper);
69 harness.simulation.enqueue(1, {
70 type: "write-letter",
71 characterId: writer.character_id,
72 slotIndex: paperSlot,
73 body: "Secret"
74 });
75 harness.simulation.step();
76 const letterSlot = harness.simulation.bagOf(writerId).inventory.findIndex((slot) => slot?.itemId === ItemId.letter);
77 assert.ok(letterSlot >= 0);
78
79 const sealed = harness.simulation.dispatchPigeon({
80 characterId: writer.character_id,
81 destination: { kind: "player", firstName: "Bob", lastName: "Other" },
82 cargo: { kind: "item", slotIndex: letterSlot, quantity: 1 }
83 });
84 assert.equal(sealed.transit.cargo.stack.instance.opened, false);
85
86 const otherId = harness.simulation.characterEntities.get(other.character_id);
87 const otherBag = harness.simulation.bagOf(otherId);
88 const stealSlot = otherBag.inventory.findIndex((slot) => !slot);
89 assert.ok(stealSlot >= 0);
90 otherBag.inventory[stealSlot] = {
91 itemId: ItemId.letter,
92 quantity: 1,
93 origin: null,
94 instance: {
95 kind: "letter",
96 letterId: "stolen",
97 authorCharacterId: writer.character_id,
98 authorDisplayName: "Ada Writer",
99 body: "Stolen",
100 opened: true,
101 writtenAt: Date.now()
102 }
103 };
104 harness.simulation.applyBag(otherId, otherBag);
105 assert.throws(
106 () => harness.simulation.dispatchPigeon({
107 characterId: other.character_id,
108 destination: { kind: "bank" },
109 cargo: { kind: "item", slotIndex: stealSlot, quantity: 1 }
110 }),
111 /only send letters you wrote/i
112 );
113 }
114 finally
115 { harness.close(); }
116});
117
118test("reading a closed letter opens it", () =>
119{
120 const harness = makeSimulation({ workerCount: 1 });
121 try
122 {
123 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Reader" });
124 const entityId = harness.simulation.spawnCharacter(1, character);
125 const bag = harness.simulation.bagOf(entityId);
126 const free = bag.inventory.findIndex((slot) => !slot);
127 bag.inventory[free] = {
128 itemId: ItemId.letter,
129 quantity: 1,
130 origin: null,
131 instance: {
132 kind: "letter",
133 letterId: "mail-1",
134 authorCharacterId: 999,
135 authorDisplayName: "Other Writer",
136 body: "Hello",
137 opened: false,
138 writtenAt: Date.now()
139 }
140 };
141 harness.simulation.applyBag(entityId, bag);
142 harness.simulation.enqueue(1, {
143 type: "open-letter",
144 characterId: character.character_id,
145 slotIndex: free
146 });
147 harness.simulation.step();
148 assert.equal(harness.simulation.bagOf(entityId).inventory[free].instance.opened, true);
149 }
150 finally
151 { harness.close(); }
152});
153
154test("dropped received letters are only pickable by the dropper", () =>
155{
156 const harness = makeSimulation({ workerCount: 1 });
157 try
158 {
159 const owner = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Ada", lastName: "Owner" });
160 const stranger = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Bob", lastName: "Stranger" });
161 const ownerId = harness.simulation.spawnCharacter(1, owner);
162 const strangerId = harness.simulation.spawnCharacter(2, stranger);
163 const bag = harness.simulation.bagOf(ownerId);
164 const free = bag.inventory.findIndex((slot) => !slot);
165 bag.inventory[free] = {
166 itemId: ItemId.letter,
167 quantity: 1,
168 instance: {
169 kind: "letter",
170 letterId: "priv-1",
171 authorCharacterId: 999,
172 authorDisplayName: "Other Writer",
173 body: "Secret",
174 opened: true,
175 writtenAt: Date.now()
176 }
177 };
178 harness.simulation.applyBag(ownerId, bag);
179
180 harness.simulation.enqueue(1, {
181 type: "drop-item",
182 characterId: owner.character_id,
183 slotIndex: free
184 });
185 const dropped = harness.simulation.step();
186 const appear = dropped.events.find((event) => event.type === "ground-item-appear");
187 assert.ok(appear, JSON.stringify(dropped.events));
188 assert.equal(appear.droppedByCharacterId, owner.character_id);
189 assert.equal(appear.itemId, ItemId.letter);
190
191 harness.simulation.enqueue(2, {
192 type: "pickup-item",
193 characterId: stranger.character_id,
194 groundItemId: appear.groundItemId
195 });
196 const stolen = harness.simulation.step();
197 assert.ok(
198 stolen.events.some((event) => event.type === "command-error"),
199 JSON.stringify(stolen.events)
200 );
201 assert.equal(
202 harness.simulation.bagOf(strangerId).inventory.some((slot) => slot?.itemId === ItemId.letter),
203 false
204 );
205
206 harness.simulation.enqueue(1, {
207 type: "pickup-item",
208 characterId: owner.character_id,
209 groundItemId: appear.groundItemId
210 });
211 const reclaim = harness.simulation.step();
212 assert.equal(reclaim.events.some((event) => event.type === "command-error"), false, JSON.stringify(reclaim.events));
213 assert.ok(harness.simulation.bagOf(ownerId).inventory.some((slot) => slot?.itemId === ItemId.letter));
214 }
215 finally
216 { harness.close(); }
217});