| 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 can credit a specific alchemy recipe paper", () => |
| 205 | { |
| 206 | const harness = makeSimulation({ workerCount: 1 }); |
| 207 | try |
| 208 | { |
| 209 | placeBank(harness.worldDb, 8, 8); |
| 210 | harness.simulation.courier.refreshBankIndex(); |
| 211 | const character = makeCharacter(harness.stateDb, { |
| 212 | tileX: 8, |
| 213 | tileY: 8, |
| 214 | firstName: "Brew", |
| 215 | lastName: "Student" |
| 216 | }); |
| 217 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 218 | const result = gifts.sendGift({ |
| 219 | characterId: character.character_id, |
| 220 | message: "Study well.", |
| 221 | items: [{ |
| 222 | itemId: ItemId.recipePaper, |
| 223 | quantity: 2, |
| 224 | listingKey: recipeListingKey("lesserHealthTonic") |
| 225 | }] |
| 226 | }); |
| 227 | assert.equal(result.ok, true); |
| 228 | assert.equal(result.items[0].displayName, recipePaperDisplayName("lesserHealthTonic")); |
| 229 | |
| 230 | const vault = harness.stateDb.getBag(character.character_id).vault ?? []; |
| 231 | const papers = vault.filter((slot) => |
| 232 | slot?.itemId === ItemId.recipePaper |
| 233 | && slot.instance?.potionKey === "lesserHealthTonic" |
| 234 | ); |
| 235 | assert.equal(papers.length, 2); |
| 236 | assert.equal(papers[0].instance.kind, "recipe-paper"); |
| 237 | assert.ok(papers[0].instance.body?.includes("Alchemy level required")); |
| 238 | assert.notEqual(papers[0].instance.recipeId, papers[1].instance.recipeId); |
| 239 | |
| 240 | const held = harness.stateDb.listHeldCourierDeliveries(character.character_id); |
| 241 | const giftHold = held.find((row) => |
| 242 | row.cargo?.stack?.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR |
| 243 | ); |
| 244 | assert.ok(giftHold); |
| 245 | assert.match(giftHold.cargo.stack.instance.body, /Lesser health tonic recipe/i); |
| 246 | } |
| 247 | finally |
| 248 | { harness.close(); } |
| 249 | }); |
| 250 | |
| 251 | test("admin gift rejects blank recipe paper without a potion key", () => |
| 252 | { |
| 253 | const harness = makeSimulation({ workerCount: 1 }); |
| 254 | try |
| 255 | { |
| 256 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 257 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 258 | assert.throws( |
| 259 | () => gifts.sendGift({ |
| 260 | characterId: character.character_id, |
| 261 | items: [{ itemId: ItemId.recipePaper, quantity: 1 }] |
| 262 | }), |
| 263 | /at least one item/i |
| 264 | ); |
| 265 | } |
| 266 | finally |
| 267 | { harness.close(); } |
| 268 | }); |