| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | validateCharacterNamePart, |
| 5 | validateCharacterNames, |
| 6 | RESERVED_ADMIN_SURNAME |
| 7 | } from "../../shared/characters/names.js"; |
| 8 | import { |
| 9 | formatGiftLetterBody, |
| 10 | QUESTWARD_GIFT_AUTHOR, |
| 11 | QUESTWARD_GIFT_ORG |
| 12 | } from "../../shared/courier/giftLetters.js"; |
| 13 | import { giftItemOrigin } from "../../shared/courier/giftOrigin.js"; |
| 14 | import { formatLetterSignature } from "../../shared/courier/systemLetters.js"; |
| 15 | import { ItemId } from "../../shared/definitions/items.js"; |
| 16 | import { CraftingItemId } from "../../shared/definitions/craftingItems.js"; |
| 17 | import { originLabel, ItemOriginKind, CraftOriginVerb, GatherOriginVerb } from "../../shared/items/origin.js"; |
| 18 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 19 | import { createEmptyChunkData, encodeChunkBlob, decodeChunkBlob } from "../src/world/chunkData.js"; |
| 20 | import { GiftService } from "../src/gifts/service.js"; |
| 21 | import { recipeListingKey, recipePaperDisplayName } from "../../shared/market/listingKey.js"; |
| 22 | import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js"; |
| 23 | |
| 24 | function placeBank (worldDb, tileX, tileY) |
| 25 | { |
| 26 | const chunkX = Math.floor(tileX / 16); |
| 27 | const chunkY = Math.floor(tileY / 16); |
| 28 | const localX = ((tileX % 16) + 16) % 16; |
| 29 | const localY = ((tileY % 16) + 16) % 16; |
| 30 | const revisionId = worldDb.getActiveRevisionId(); |
| 31 | const row = worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 32 | const data = row |
| 33 | ? decodeChunkBlob(row.data_blob, undefined, chunkX, chunkY) |
| 34 | : createEmptyChunkData(); |
| 35 | data.open = true; |
| 36 | data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 }; |
| 37 | worldDb.saveChunkVersion(chunkX, chunkY, revisionId, true, encodeChunkBlob(data)); |
| 38 | } |
| 39 | |
| 40 | test("character names allow letters and hyphens only", () => |
| 41 | { |
| 42 | assert.equal(validateCharacterNamePart("Anne-Marie").ok, true); |
| 43 | assert.equal(validateCharacterNamePart("Bob").ok, true); |
| 44 | assert.equal(validateCharacterNamePart("Bob Smith").ok, false); |
| 45 | assert.equal(validateCharacterNamePart("Bob2").ok, false); |
| 46 | assert.equal(validateCharacterNamePart("O'Brien").ok, false); |
| 47 | assert.equal(validateCharacterNamePart("---").ok, false); |
| 48 | }); |
| 49 | |
| 50 | test("Questward surname is admin-only", () => |
| 51 | { |
| 52 | const denied = validateCharacterNames({ |
| 53 | firstName: "John", |
| 54 | lastName: "Questward", |
| 55 | isAdmin: false |
| 56 | }); |
| 57 | assert.equal(denied.ok, false); |
| 58 | const allowed = validateCharacterNames({ |
| 59 | firstName: "John", |
| 60 | lastName: RESERVED_ADMIN_SURNAME, |
| 61 | isAdmin: true |
| 62 | }); |
| 63 | assert.equal(allowed.ok, true); |
| 64 | assert.equal(allowed.lastName, RESERVED_ADMIN_SURNAME); |
| 65 | }); |
| 66 | |
| 67 | test("gift letter body and signature match Questward CEO format", () => |
| 68 | { |
| 69 | const body = formatGiftLetterBody({ |
| 70 | characterDisplayName: "Tim Falken", |
| 71 | message: "Enjoy these supplies.", |
| 72 | items: [{ displayName: "Stick", quantity: 3 }] |
| 73 | }); |
| 74 | assert.match(body, /^Dear Tim Falken\n/); |
| 75 | assert.match(body, /Enjoy these supplies\./); |
| 76 | assert.match(body, /• 3× Stick/); |
| 77 | assert.match(body, /Thank you for playing Questward\./); |
| 78 | assert.equal( |
| 79 | formatLetterSignature({ |
| 80 | authorDisplayName: QUESTWARD_GIFT_AUTHOR, |
| 81 | authorOrganization: QUESTWARD_GIFT_ORG |
| 82 | }), |
| 83 | "Signed:\nJohn Questward\nCEO of Questward" |
| 84 | ); |
| 85 | |
| 86 | const noMessage = formatGiftLetterBody({ |
| 87 | characterDisplayName: "Tim Falken", |
| 88 | message: " ", |
| 89 | items: [{ displayName: "Stick", quantity: 1 }] |
| 90 | }); |
| 91 | assert.equal( |
| 92 | noMessage, |
| 93 | [ |
| 94 | "Dear Tim Falken", |
| 95 | "", |
| 96 | "The following items were added to your bank:", |
| 97 | "• 1× Stick", |
| 98 | "", |
| 99 | "Thank you for playing Questward." |
| 100 | ].join("\n") |
| 101 | ); |
| 102 | }); |
| 103 | |
| 104 | test("gift item origins use natural verbs with John Questward", () => |
| 105 | { |
| 106 | assert.equal( |
| 107 | originLabel(giftItemOrigin(ItemId.stick)), |
| 108 | "Carved by John Questward" |
| 109 | ); |
| 110 | assert.equal( |
| 111 | originLabel(giftItemOrigin(CraftingItemId.copperOre)), |
| 112 | "Mined by John Questward" |
| 113 | ); |
| 114 | const made = giftItemOrigin(ItemId.letter); |
| 115 | assert.equal(made.kind, ItemOriginKind.crafted); |
| 116 | assert.equal(made.displayName, QUESTWARD_GIFT_AUTHOR); |
| 117 | assert.equal(made.verb, CraftOriginVerb.made); |
| 118 | assert.equal(giftItemOrigin(CraftingItemId.copperOre).verb, GatherOriginVerb.mined); |
| 119 | }); |
| 120 | |
| 121 | test("admin gift credits bank and queues a system letter", () => |
| 122 | { |
| 123 | const harness = makeSimulation({ workerCount: 1 }); |
| 124 | try |
| 125 | { |
| 126 | placeBank(harness.worldDb, 8, 8); |
| 127 | harness.simulation.courier.refreshBankIndex(); |
| 128 | harness.simulation.applySettings({ |
| 129 | pigeonTakeoffTilesPerSec: 40, |
| 130 | pigeonCruiseTilesPerSec: 40, |
| 131 | pigeonLandingTilesPerSec: 40, |
| 132 | simulationTickHz: 20 |
| 133 | }); |
| 134 | |
| 135 | const character = makeCharacter(harness.stateDb, { |
| 136 | tileX: 8, |
| 137 | tileY: 8, |
| 138 | firstName: "Gift", |
| 139 | lastName: "Target" |
| 140 | }); |
| 141 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 142 | const before = harness.stateDb.getBag(character.character_id); |
| 143 | const stickBefore = (before.vault ?? []).reduce( |
| 144 | (sum, slot) => sum + (slot?.itemId === ItemId.stick ? (slot.quantity | 0) : 0), |
| 145 | 0 |
| 146 | ); |
| 147 | |
| 148 | const result = gifts.sendGift({ |
| 149 | characterId: character.character_id, |
| 150 | message: "A small thank-you.", |
| 151 | items: [{ itemId: ItemId.stick, quantity: 5 }] |
| 152 | }); |
| 153 | assert.equal(result.ok, true); |
| 154 | |
| 155 | const after = harness.stateDb.getBag(character.character_id); |
| 156 | const stickStack = (after.vault ?? []).find((slot) => slot?.itemId === ItemId.stick); |
| 157 | assert.equal(stickStack?.quantity, stickBefore + 5); |
| 158 | assert.equal( |
| 159 | originLabel(stickStack?.origins?.[0]?.origin), |
| 160 | "Carved by John Questward" |
| 161 | ); |
| 162 | assert.equal( |
| 163 | (after.vault ?? []).some((slot) => slot?.itemId === ItemId.letter), |
| 164 | false, |
| 165 | "gift letter must not be deposited in the bank" |
| 166 | ); |
| 167 | |
| 168 | const held = harness.stateDb.listHeldCourierDeliveries(character.character_id); |
| 169 | const giftHold = held.find((row) => |
| 170 | row.cargo?.stack?.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR |
| 171 | ); |
| 172 | assert.ok(giftHold, "expected a held gift letter for the offline character"); |
| 173 | assert.equal(giftHold.destinationKind, "player"); |
| 174 | |
| 175 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 176 | const active = harness.stateDb.listActiveCourierTransits(); |
| 177 | assert.equal(active.length, 1); |
| 178 | assert.equal(active[0].destinationKind, "player"); |
| 179 | assert.equal(active[0].cargo.stack.instance.authorDisplayName, QUESTWARD_GIFT_AUTHOR); |
| 180 | |
| 181 | const events = stepTimes(harness.simulation, 40); |
| 182 | |
| 183 | const bag = harness.simulation.bagOf(entityId); |
| 184 | const letter = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.letter); |
| 185 | assert.ok(letter, "gift letter should arrive in inventory via pigeon"); |
| 186 | assert.equal(letter.instance.authorDisplayName, QUESTWARD_GIFT_AUTHOR); |
| 187 | assert.equal(letter.instance.authorOrganization, QUESTWARD_GIFT_ORG); |
| 188 | assert.match(letter.instance.body, /A small thank-you/); |
| 189 | assert.ok(events.some((event) => |
| 190 | event.type === "item-bubble" |
| 191 | && event.entityId === entityId |
| 192 | && event.itemId === ItemId.letter |
| 193 | )); |
| 194 | assert.equal( |
| 195 | (harness.stateDb.getBag(character.character_id).vault ?? []) |
| 196 | .some((slot) => slot?.itemId === ItemId.letter), |
| 197 | false |
| 198 | ); |
| 199 | } |
| 200 | finally |
| 201 | { harness.close(); } |
| 202 | }); |
| 203 | |
| 204 | test("admin gift to an online character pigeons the letter immediately", () => |
| 205 | { |
| 206 | const harness = makeSimulation({ workerCount: 1 }); |
| 207 | try |
| 208 | { |
| 209 | placeBank(harness.worldDb, 8, 8); |
| 210 | harness.simulation.courier.refreshBankIndex(); |
| 211 | harness.simulation.applySettings({ |
| 212 | pigeonTakeoffTilesPerSec: 40, |
| 213 | pigeonCruiseTilesPerSec: 40, |
| 214 | pigeonLandingTilesPerSec: 40, |
| 215 | simulationTickHz: 20 |
| 216 | }); |
| 217 | |
| 218 | const character = makeCharacter(harness.stateDb, { |
| 219 | tileX: 8, |
| 220 | tileY: 8, |
| 221 | firstName: "Online", |
| 222 | lastName: "Target" |
| 223 | }); |
| 224 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 225 | assert.equal(harness.simulation.isCharacterOnline(character.character_id), true); |
| 226 | |
| 227 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 228 | const result = gifts.sendGift({ |
| 229 | characterId: character.character_id, |
| 230 | message: "While you are here.", |
| 231 | items: [{ itemId: ItemId.stick, quantity: 2 }] |
| 232 | }); |
| 233 | assert.equal(result.ok, true); |
| 234 | assert.equal(harness.stateDb.listHeldCourierDeliveries(character.character_id).length, 0); |
| 235 | const active = harness.stateDb.listActiveCourierTransits(); |
| 236 | assert.equal(active.length, 1); |
| 237 | assert.equal(active[0].destinationKind, "player"); |
| 238 | assert.equal(active[0].cargo.stack.instance.authorDisplayName, QUESTWARD_GIFT_AUTHOR); |
| 239 | |
| 240 | const events = stepTimes(harness.simulation, 40); |
| 241 | const bag = harness.simulation.bagOf(entityId); |
| 242 | const letter = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.letter); |
| 243 | assert.ok(letter, "online gift letter should arrive without relog"); |
| 244 | assert.match(letter.instance.body, /While you are here/); |
| 245 | assert.ok(events.some((event) => |
| 246 | event.type === "item-bubble" |
| 247 | && event.entityId === entityId |
| 248 | && event.itemId === ItemId.letter |
| 249 | )); |
| 250 | } |
| 251 | finally |
| 252 | { harness.close(); } |
| 253 | }); |
| 254 | |
| 255 | test("admin gift can credit a specific alchemy recipe paper", () => |
| 256 | { |
| 257 | const harness = makeSimulation({ workerCount: 1 }); |
| 258 | try |
| 259 | { |
| 260 | placeBank(harness.worldDb, 8, 8); |
| 261 | harness.simulation.courier.refreshBankIndex(); |
| 262 | const character = makeCharacter(harness.stateDb, { |
| 263 | tileX: 8, |
| 264 | tileY: 8, |
| 265 | firstName: "Brew", |
| 266 | lastName: "Student" |
| 267 | }); |
| 268 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 269 | const result = gifts.sendGift({ |
| 270 | characterId: character.character_id, |
| 271 | message: "Study well.", |
| 272 | items: [{ |
| 273 | itemId: ItemId.recipePaper, |
| 274 | quantity: 2, |
| 275 | listingKey: recipeListingKey("lesserHealthTonic") |
| 276 | }] |
| 277 | }); |
| 278 | assert.equal(result.ok, true); |
| 279 | assert.equal(result.items[0].displayName, recipePaperDisplayName("lesserHealthTonic")); |
| 280 | |
| 281 | const vault = harness.stateDb.getBag(character.character_id).vault ?? []; |
| 282 | const papers = vault.filter((slot) => |
| 283 | slot?.itemId === ItemId.recipePaper |
| 284 | && slot.instance?.potionKey === "lesserHealthTonic" |
| 285 | ); |
| 286 | assert.equal(papers.length, 2); |
| 287 | assert.equal(papers[0].instance.kind, "recipe-paper"); |
| 288 | assert.ok(papers[0].instance.body?.includes("Alchemy level required")); |
| 289 | assert.notEqual(papers[0].instance.recipeId, papers[1].instance.recipeId); |
| 290 | |
| 291 | const held = harness.stateDb.listHeldCourierDeliveries(character.character_id); |
| 292 | const giftHold = held.find((row) => |
| 293 | row.cargo?.stack?.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR |
| 294 | ); |
| 295 | assert.ok(giftHold); |
| 296 | assert.match(giftHold.cargo.stack.instance.body, /Lesser health tonic recipe/i); |
| 297 | } |
| 298 | finally |
| 299 | { harness.close(); } |
| 300 | }); |
| 301 | |
| 302 | test("admin gift rejects blank recipe paper without a potion key", () => |
| 303 | { |
| 304 | const harness = makeSimulation({ workerCount: 1 }); |
| 305 | try |
| 306 | { |
| 307 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 308 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 309 | assert.throws( |
| 310 | () => gifts.sendGift({ |
| 311 | characterId: character.character_id, |
| 312 | items: [{ itemId: ItemId.recipePaper, quantity: 1 }] |
| 313 | }), |
| 314 | /at least one item/i |
| 315 | ); |
| 316 | } |
| 317 | finally |
| 318 | { harness.close(); } |
| 319 | }); |
| 320 | |
| 321 | test("admin gift can credit a specific potion brew", () => |
| 322 | { |
| 323 | const harness = makeSimulation({ workerCount: 1 }); |
| 324 | try |
| 325 | { |
| 326 | placeBank(harness.worldDb, 8, 8); |
| 327 | harness.simulation.courier.refreshBankIndex(); |
| 328 | const character = makeCharacter(harness.stateDb, { |
| 329 | tileX: 8, |
| 330 | tileY: 8, |
| 331 | firstName: "Sip", |
| 332 | lastName: "Tester" |
| 333 | }); |
| 334 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 335 | const result = gifts.sendGift({ |
| 336 | characterId: character.character_id, |
| 337 | message: "Drink responsibly.", |
| 338 | items: [{ |
| 339 | itemId: ItemId.potion, |
| 340 | quantity: 3, |
| 341 | potionKey: "lesserHealthTonic" |
| 342 | }] |
| 343 | }); |
| 344 | assert.equal(result.ok, true); |
| 345 | assert.equal(result.items[0].displayName, "Lesser health tonic"); |
| 346 | |
| 347 | const vault = harness.stateDb.getBag(character.character_id).vault ?? []; |
| 348 | const stack = vault.find((slot) => |
| 349 | slot?.itemId === ItemId.potion |
| 350 | && slot.instance?.potionKey === "lesserHealthTonic" |
| 351 | ); |
| 352 | assert.ok(stack); |
| 353 | assert.equal(stack.quantity, 3); |
| 354 | assert.equal(stack.instance.kind, "potion"); |
| 355 | assert.equal(stack.instance.displayName, "Lesser health tonic"); |
| 356 | assert.equal(stack.instance.effect?.type, "health-regen"); |
| 357 | assert.equal( |
| 358 | originLabel(stack.origins?.[0]?.origin), |
| 359 | "Brewed by John Questward" |
| 360 | ); |
| 361 | } |
| 362 | finally |
| 363 | { harness.close(); } |
| 364 | }); |
| 365 | |
| 366 | test("admin gift rejects blank potion without a potion key", () => |
| 367 | { |
| 368 | const harness = makeSimulation({ workerCount: 1 }); |
| 369 | try |
| 370 | { |
| 371 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 372 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 373 | assert.throws( |
| 374 | () => gifts.sendGift({ |
| 375 | characterId: character.character_id, |
| 376 | items: [{ itemId: ItemId.potion, quantity: 1 }] |
| 377 | }), |
| 378 | /at least one item/i |
| 379 | ); |
| 380 | } |
| 381 | finally |
| 382 | { harness.close(); } |
| 383 | }); |