| 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 { formatLetterSignature } from "../../shared/courier/systemLetters.js"; |
| 14 | import { ItemId } from "../../shared/definitions/items.js"; |
| 15 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 16 | import { createEmptyChunkData, encodeChunkBlob, decodeChunkBlob } from "../src/world/chunkData.js"; |
| 17 | import { GiftService } from "../src/gifts/service.js"; |
| 18 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 19 | |
| 20 | function placeBank (worldDb, tileX, tileY) |
| 21 | { |
| 22 | const chunkX = Math.floor(tileX / 16); |
| 23 | const chunkY = Math.floor(tileY / 16); |
| 24 | const localX = ((tileX % 16) + 16) % 16; |
| 25 | const localY = ((tileY % 16) + 16) % 16; |
| 26 | const revisionId = worldDb.getActiveRevisionId(); |
| 27 | const row = worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 28 | const data = row |
| 29 | ? decodeChunkBlob(row.data_blob, undefined, chunkX, chunkY) |
| 30 | : createEmptyChunkData(); |
| 31 | data.open = true; |
| 32 | data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 }; |
| 33 | worldDb.saveChunkVersion(chunkX, chunkY, revisionId, true, encodeChunkBlob(data)); |
| 34 | } |
| 35 | |
| 36 | test("character names allow letters and hyphens only", () => |
| 37 | { |
| 38 | assert.equal(validateCharacterNamePart("Anne-Marie").ok, true); |
| 39 | assert.equal(validateCharacterNamePart("Bob").ok, true); |
| 40 | assert.equal(validateCharacterNamePart("Bob Smith").ok, false); |
| 41 | assert.equal(validateCharacterNamePart("Bob2").ok, false); |
| 42 | assert.equal(validateCharacterNamePart("O'Brien").ok, false); |
| 43 | assert.equal(validateCharacterNamePart("---").ok, false); |
| 44 | }); |
| 45 | |
| 46 | test("Questward surname is admin-only", () => |
| 47 | { |
| 48 | const denied = validateCharacterNames({ |
| 49 | firstName: "John", |
| 50 | lastName: "Questward", |
| 51 | isAdmin: false |
| 52 | }); |
| 53 | assert.equal(denied.ok, false); |
| 54 | const allowed = validateCharacterNames({ |
| 55 | firstName: "John", |
| 56 | lastName: RESERVED_ADMIN_SURNAME, |
| 57 | isAdmin: true |
| 58 | }); |
| 59 | assert.equal(allowed.ok, true); |
| 60 | assert.equal(allowed.lastName, RESERVED_ADMIN_SURNAME); |
| 61 | }); |
| 62 | |
| 63 | test("gift letter body and signature match Questward CEO format", () => |
| 64 | { |
| 65 | const body = formatGiftLetterBody({ |
| 66 | characterDisplayName: "Tim Falken", |
| 67 | message: "Enjoy these supplies.", |
| 68 | items: [{ displayName: "Stick", quantity: 3 }] |
| 69 | }); |
| 70 | assert.match(body, /^Dear Tim Falken\n/); |
| 71 | assert.match(body, /Enjoy these supplies\./); |
| 72 | assert.match(body, /• 3× Stick/); |
| 73 | assert.match(body, /Thank you for playing Questward\./); |
| 74 | assert.equal( |
| 75 | formatLetterSignature({ |
| 76 | authorDisplayName: QUESTWARD_GIFT_AUTHOR, |
| 77 | authorOrganization: QUESTWARD_GIFT_ORG |
| 78 | }), |
| 79 | "Signed:\nJohn Questward\nCEO of Questward" |
| 80 | ); |
| 81 | }); |
| 82 | |
| 83 | test("admin gift credits bank and queues a system letter", () => |
| 84 | { |
| 85 | const harness = makeSimulation({ workerCount: 1 }); |
| 86 | try |
| 87 | { |
| 88 | placeBank(harness.worldDb, 8, 8); |
| 89 | harness.simulation.courier.refreshBankIndex(); |
| 90 | |
| 91 | const character = makeCharacter(harness.stateDb, { |
| 92 | tileX: 8, |
| 93 | tileY: 8, |
| 94 | firstName: "Gift", |
| 95 | lastName: "Target" |
| 96 | }); |
| 97 | const gifts = new GiftService(harness.stateDb, harness.simulation); |
| 98 | const before = harness.stateDb.getBag(character.character_id); |
| 99 | const stickBefore = (before.vault ?? []).reduce( |
| 100 | (sum, slot) => sum + (slot?.itemId === ItemId.stick ? (slot.quantity | 0) : 0), |
| 101 | 0 |
| 102 | ); |
| 103 | |
| 104 | const result = gifts.sendGift({ |
| 105 | characterId: character.character_id, |
| 106 | message: "A small thank-you.", |
| 107 | items: [{ itemId: ItemId.stick, quantity: 5 }] |
| 108 | }); |
| 109 | assert.equal(result.ok, true); |
| 110 | |
| 111 | const after = harness.stateDb.getBag(character.character_id); |
| 112 | const stickAfter = (after.vault ?? []).reduce( |
| 113 | (sum, slot) => sum + (slot?.itemId === ItemId.stick ? (slot.quantity | 0) : 0), |
| 114 | 0 |
| 115 | ); |
| 116 | assert.equal(stickAfter, stickBefore + 5); |
| 117 | |
| 118 | const held = harness.stateDb.listHeldCourierDeliveries(character.character_id); |
| 119 | const foundHeld = held.some((row) => |
| 120 | row.cargo?.stack?.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR |
| 121 | ); |
| 122 | assert.ok(foundHeld, "expected a held gift letter for the offline character"); |
| 123 | } |
| 124 | finally |
| 125 | { harness.close(); } |
| 126 | }); |