| 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 { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 22 | |
| 23 | function placeBank (worldDb, tileX, tileY) |
| 24 | { |
| 25 | const chunkX = Math.floor(tileX / 16); |
| 26 | const chunkY = Math.floor(tileY / 16); |
| 27 | const localX = ((tileX % 16) + 16) % 16; |
| 28 | const localY = ((tileY % 16) + 16) % 16; |
| 29 | const revisionId = worldDb.getActiveRevisionId(); |
| 30 | const row = worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 31 | const data = row |
| 32 | ? decodeChunkBlob(row.data_blob, undefined, chunkX, chunkY) |
| 33 | : createEmptyChunkData(); |
| 34 | data.open = true; |
| 35 | data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 }; |
| 36 | worldDb.saveChunkVersion(chunkX, chunkY, revisionId, true, encodeChunkBlob(data)); |
| 37 | } |
| 38 | |
| 39 | test("character names allow letters and hyphens only", () => |
| 40 | { |
| 41 | assert.equal(validateCharacterNamePart("Anne-Marie").ok, true); |
| 42 | assert.equal(validateCharacterNamePart("Bob").ok, true); |
| 43 | assert.equal(validateCharacterNamePart("Bob Smith").ok, false); |
| 44 | assert.equal(validateCharacterNamePart("Bob2").ok, false); |
| 45 | assert.equal(validateCharacterNamePart("O'Brien").ok, false); |
| 46 | assert.equal(validateCharacterNamePart("---").ok, false); |
| 47 | }); |
| 48 | |
| 49 | test("Questward surname is admin-only", () => |
| 50 | { |
| 51 | const denied = validateCharacterNames({ |
| 52 | firstName: "John", |
| 53 | lastName: "Questward", |
| 54 | isAdmin: false |
| 55 | }); |
| 56 | assert.equal(denied.ok, false); |
| 57 | const allowed = validateCharacterNames({ |
| 58 | firstName: "John", |
| 59 | lastName: RESERVED_ADMIN_SURNAME, |
| 60 | isAdmin: true |
| 61 | }); |
| 62 | assert.equal(allowed.ok, true); |
| 63 | assert.equal(allowed.lastName, RESERVED_ADMIN_SURNAME); |
| 64 | }); |
| 65 | |
| 66 | test("gift letter body and signature match Questward CEO format", () => |
| 67 | { |
| 68 | const body = formatGiftLetterBody({ |
| 69 | characterDisplayName: "Tim Falken", |
| 70 | message: "Enjoy these supplies.", |
| 71 | items: [{ displayName: "Stick", quantity: 3 }] |
| 72 | }); |
| 73 | assert.match(body, /^Dear Tim Falken\n/); |
| 74 | assert.match(body, /Enjoy these supplies\./); |
| 75 | assert.match(body, /• 3× Stick/); |
| 76 | assert.match(body, /Thank you for playing Questward\./); |
| 77 | assert.equal( |
| 78 | formatLetterSignature({ |
| 79 | authorDisplayName: QUESTWARD_GIFT_AUTHOR, |
| 80 | authorOrganization: QUESTWARD_GIFT_ORG |
| 81 | }), |
| 82 | "Signed:\nJohn Questward\nCEO of Questward" |
| 83 | ); |
| 84 | |
| 85 | const noMessage = formatGiftLetterBody({ |
| 86 | characterDisplayName: "Tim Falken", |
| 87 | message: " ", |
| 88 | items: [{ displayName: "Stick", quantity: 1 }] |
| 89 | }); |
| 90 | assert.equal( |
| 91 | noMessage, |
| 92 | [ |
| 93 | "Dear Tim Falken", |
| 94 | "", |
| 95 | "The following items were added to your bank:", |
| 96 | "• 1× Stick", |
| 97 | "", |
| 98 | "Thank you for playing Questward." |
| 99 | ].join("\n") |
| 100 | ); |
| 101 | }); |
| 102 | |
| 103 | test("gift item origins use natural verbs with John Questward", () => |
| 104 | { |
| 105 | assert.equal( |
| 106 | originLabel(giftItemOrigin(ItemId.stick)), |
| 107 | "Carved by John Questward" |
| 108 | ); |
| 109 | assert.equal( |
| 110 | originLabel(giftItemOrigin(CraftingItemId.copperOre)), |
| 111 | "Mined by John Questward" |
| 112 | ); |
| 113 | const made = giftItemOrigin(ItemId.letter); |
| 114 | assert.equal(made.kind, ItemOriginKind.crafted); |
| 115 | assert.equal(made.displayName, QUESTWARD_GIFT_AUTHOR); |
| 116 | assert.equal(made.verb, CraftOriginVerb.made); |
| 117 | assert.equal(giftItemOrigin(CraftingItemId.copperOre).verb, GatherOriginVerb.mined); |
| 118 | }); |
| 119 | |
| 120 | test("admin gift credits bank and queues a system letter", () => |
| 121 | { |
| 122 | const harness = makeSimulation({ workerCount: 1 }); |
| 123 | try |
| 124 | { |
| 125 | placeBank(harness.worldDb, 8, 8); |
| 126 | harness.simulation.courier.refreshBankIndex(); |
| 127 | |
| 128 | const character = makeCharacter(harness.stateDb, { |
| 129 | tileX: 8, |
| 130 | tileY: 8, |
| 131 | firstName: "Gift", |
| 132 | lastName: "Target" |
| 133 | }); |
| 134 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 135 | const before = harness.stateDb.getBag(character.character_id); |
| 136 | const stickBefore = (before.vault ?? []).reduce( |
| 137 | (sum, slot) => sum + (slot?.itemId === ItemId.stick ? (slot.quantity | 0) : 0), |
| 138 | 0 |
| 139 | ); |
| 140 | |
| 141 | const result = gifts.sendGift({ |
| 142 | characterId: character.character_id, |
| 143 | message: "A small thank-you.", |
| 144 | items: [{ itemId: ItemId.stick, quantity: 5 }] |
| 145 | }); |
| 146 | assert.equal(result.ok, true); |
| 147 | |
| 148 | const after = harness.stateDb.getBag(character.character_id); |
| 149 | const stickStack = (after.vault ?? []).find((slot) => slot?.itemId === ItemId.stick); |
| 150 | assert.equal(stickStack?.quantity, stickBefore + 5); |
| 151 | assert.equal( |
| 152 | originLabel(stickStack?.origins?.[0]?.origin), |
| 153 | "Carved by John Questward" |
| 154 | ); |
| 155 | |
| 156 | const held = harness.stateDb.listHeldCourierDeliveries(character.character_id); |
| 157 | const foundHeld = held.some((row) => |
| 158 | row.cargo?.stack?.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR |
| 159 | ); |
| 160 | assert.ok(foundHeld, "expected a held gift letter for the offline character"); |
| 161 | } |
| 162 | finally |
| 163 | { harness.close(); } |
| 164 | }); |