import test from "node:test"; import assert from "node:assert/strict"; import { validateCharacterNamePart, validateCharacterNames, RESERVED_ADMIN_SURNAME } from "../../shared/characters/names.js"; import { formatGiftLetterBody, QUESTWARD_GIFT_AUTHOR, QUESTWARD_GIFT_ORG } from "../../shared/courier/giftLetters.js"; import { giftItemOrigin } from "../../shared/courier/giftOrigin.js"; import { formatLetterSignature } from "../../shared/courier/systemLetters.js"; import { ItemId } from "../../shared/definitions/items.js"; import { CraftingItemId } from "../../shared/definitions/craftingItems.js"; import { originLabel, ItemOriginKind, CraftOriginVerb, GatherOriginVerb } from "../../shared/items/origin.js"; import { BlockType } from "../../shared/definitions/tiles.js"; import { createEmptyChunkData, encodeChunkBlob, decodeChunkBlob } from "../src/world/chunkData.js"; import { GiftService } from "../src/gifts/service.js"; import { recipeListingKey, recipePaperDisplayName } from "../../shared/market/listingKey.js"; import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js"; function placeBank (worldDb, tileX, tileY) { const chunkX = Math.floor(tileX / 16); const chunkY = Math.floor(tileY / 16); const localX = ((tileX % 16) + 16) % 16; const localY = ((tileY % 16) + 16) % 16; const revisionId = worldDb.getActiveRevisionId(); const row = worldDb.getChunkVersion(chunkX, chunkY, revisionId); const data = row ? decodeChunkBlob(row.data_blob, undefined, chunkX, chunkY) : createEmptyChunkData(); data.open = true; data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 }; worldDb.saveChunkVersion(chunkX, chunkY, revisionId, true, encodeChunkBlob(data)); } test("character names allow letters and hyphens only", () => { assert.equal(validateCharacterNamePart("Anne-Marie").ok, true); assert.equal(validateCharacterNamePart("Bob").ok, true); assert.equal(validateCharacterNamePart("Bob Smith").ok, false); assert.equal(validateCharacterNamePart("Bob2").ok, false); assert.equal(validateCharacterNamePart("O'Brien").ok, false); assert.equal(validateCharacterNamePart("---").ok, false); }); test("Questward surname is admin-only", () => { const denied = validateCharacterNames({ firstName: "John", lastName: "Questward", isAdmin: false }); assert.equal(denied.ok, false); const allowed = validateCharacterNames({ firstName: "John", lastName: RESERVED_ADMIN_SURNAME, isAdmin: true }); assert.equal(allowed.ok, true); assert.equal(allowed.lastName, RESERVED_ADMIN_SURNAME); }); test("gift letter body and signature match Questward CEO format", () => { const body = formatGiftLetterBody({ characterDisplayName: "Tim Falken", message: "Enjoy these supplies.", items: [{ displayName: "Stick", quantity: 3 }] }); assert.match(body, /^Dear Tim Falken\n/); assert.match(body, /Enjoy these supplies\./); assert.match(body, /• 3× Stick/); assert.match(body, /Thank you for playing Questward\./); assert.equal( formatLetterSignature({ authorDisplayName: QUESTWARD_GIFT_AUTHOR, authorOrganization: QUESTWARD_GIFT_ORG }), "Signed:\nJohn Questward\nCEO of Questward" ); const noMessage = formatGiftLetterBody({ characterDisplayName: "Tim Falken", message: " ", items: [{ displayName: "Stick", quantity: 1 }] }); assert.equal( noMessage, [ "Dear Tim Falken", "", "The following items were added to your bank:", "• 1× Stick", "", "Thank you for playing Questward." ].join("\n") ); }); test("gift item origins use natural verbs with John Questward", () => { assert.equal( originLabel(giftItemOrigin(ItemId.stick)), "Carved by John Questward" ); assert.equal( originLabel(giftItemOrigin(CraftingItemId.copperOre)), "Mined by John Questward" ); const made = giftItemOrigin(ItemId.letter); assert.equal(made.kind, ItemOriginKind.crafted); assert.equal(made.displayName, QUESTWARD_GIFT_AUTHOR); assert.equal(made.verb, CraftOriginVerb.made); assert.equal(giftItemOrigin(CraftingItemId.copperOre).verb, GatherOriginVerb.mined); }); test("admin gift credits bank and queues a system letter", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.courier.refreshBankIndex(); harness.simulation.applySettings({ pigeonTakeoffTilesPerSec: 40, pigeonCruiseTilesPerSec: 40, pigeonLandingTilesPerSec: 40, simulationTickHz: 20 }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Gift", lastName: "Target" }); const gifts = new GiftService(harness.stateDb, harness.simulation); const before = harness.stateDb.getBag(character.character_id); const stickBefore = (before.vault ?? []).reduce( (sum, slot) => sum + (slot?.itemId === ItemId.stick ? (slot.quantity | 0) : 0), 0 ); const result = gifts.sendGift({ characterId: character.character_id, message: "A small thank-you.", items: [{ itemId: ItemId.stick, quantity: 5 }] }); assert.equal(result.ok, true); const after = harness.stateDb.getBag(character.character_id); const stickStack = (after.vault ?? []).find((slot) => slot?.itemId === ItemId.stick); assert.equal(stickStack?.quantity, stickBefore + 5); assert.equal( originLabel(stickStack?.origins?.[0]?.origin), "Carved by John Questward" ); assert.equal( (after.vault ?? []).some((slot) => slot?.itemId === ItemId.letter), false, "gift letter must not be deposited in the bank" ); const held = harness.stateDb.listHeldCourierDeliveries(character.character_id); const giftHold = held.find((row) => row.cargo?.stack?.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR ); assert.ok(giftHold, "expected a held gift letter for the offline character"); assert.equal(giftHold.destinationKind, "player"); const entityId = harness.simulation.spawnCharacter(1, character); const active = harness.stateDb.listActiveCourierTransits(); assert.equal(active.length, 1); assert.equal(active[0].destinationKind, "player"); assert.equal(active[0].cargo.stack.instance.authorDisplayName, QUESTWARD_GIFT_AUTHOR); const events = stepTimes(harness.simulation, 40); const bag = harness.simulation.bagOf(entityId); const letter = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.letter); assert.ok(letter, "gift letter should arrive in inventory via pigeon"); assert.equal(letter.instance.authorDisplayName, QUESTWARD_GIFT_AUTHOR); assert.equal(letter.instance.authorOrganization, QUESTWARD_GIFT_ORG); assert.match(letter.instance.body, /A small thank-you/); assert.ok(events.some((event) => event.type === "item-bubble" && event.entityId === entityId && event.itemId === ItemId.letter )); assert.equal( (harness.stateDb.getBag(character.character_id).vault ?? []) .some((slot) => slot?.itemId === ItemId.letter), false ); } finally { harness.close(); } }); test("admin gift to an online character pigeons the letter immediately", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.courier.refreshBankIndex(); harness.simulation.applySettings({ pigeonTakeoffTilesPerSec: 40, pigeonCruiseTilesPerSec: 40, pigeonLandingTilesPerSec: 40, simulationTickHz: 20 }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Online", lastName: "Target" }); const entityId = harness.simulation.spawnCharacter(1, character); assert.equal(harness.simulation.isCharacterOnline(character.character_id), true); const gifts = new GiftService(harness.stateDb, harness.simulation); const result = gifts.sendGift({ characterId: character.character_id, message: "While you are here.", items: [{ itemId: ItemId.stick, quantity: 2 }] }); assert.equal(result.ok, true); assert.equal(harness.stateDb.listHeldCourierDeliveries(character.character_id).length, 0); const active = harness.stateDb.listActiveCourierTransits(); assert.equal(active.length, 1); assert.equal(active[0].destinationKind, "player"); assert.equal(active[0].cargo.stack.instance.authorDisplayName, QUESTWARD_GIFT_AUTHOR); const events = stepTimes(harness.simulation, 40); const bag = harness.simulation.bagOf(entityId); const letter = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.letter); assert.ok(letter, "online gift letter should arrive without relog"); assert.match(letter.instance.body, /While you are here/); assert.ok(events.some((event) => event.type === "item-bubble" && event.entityId === entityId && event.itemId === ItemId.letter )); } finally { harness.close(); } }); test("admin gift can credit a specific alchemy recipe paper", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.courier.refreshBankIndex(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Brew", lastName: "Student" }); const gifts = new GiftService(harness.stateDb, harness.simulation); const result = gifts.sendGift({ characterId: character.character_id, message: "Study well.", items: [{ itemId: ItemId.recipePaper, quantity: 2, listingKey: recipeListingKey("lesserHealthTonic") }] }); assert.equal(result.ok, true); assert.equal(result.items[0].displayName, recipePaperDisplayName("lesserHealthTonic")); const vault = harness.stateDb.getBag(character.character_id).vault ?? []; const papers = vault.filter((slot) => slot?.itemId === ItemId.recipePaper && slot.instance?.potionKey === "lesserHealthTonic" ); assert.equal(papers.length, 2); assert.equal(papers[0].instance.kind, "recipe-paper"); assert.ok(papers[0].instance.body?.includes("Alchemy level required")); assert.notEqual(papers[0].instance.recipeId, papers[1].instance.recipeId); const held = harness.stateDb.listHeldCourierDeliveries(character.character_id); const giftHold = held.find((row) => row.cargo?.stack?.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR ); assert.ok(giftHold); assert.match(giftHold.cargo.stack.instance.body, /Lesser health tonic recipe/i); } finally { harness.close(); } }); test("admin gift rejects blank recipe paper without a potion key", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const gifts = new GiftService(harness.stateDb, harness.simulation); assert.throws( () => gifts.sendGift({ characterId: character.character_id, items: [{ itemId: ItemId.recipePaper, quantity: 1 }] }), /at least one item/i ); } finally { harness.close(); } }); test("admin gift can credit a specific potion brew", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.courier.refreshBankIndex(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Sip", lastName: "Tester" }); const gifts = new GiftService(harness.stateDb, harness.simulation); const result = gifts.sendGift({ characterId: character.character_id, message: "Drink responsibly.", items: [{ itemId: ItemId.potion, quantity: 3, potionKey: "lesserHealthTonic" }] }); assert.equal(result.ok, true); assert.equal(result.items[0].displayName, "Lesser health tonic"); const vault = harness.stateDb.getBag(character.character_id).vault ?? []; const stack = vault.find((slot) => slot?.itemId === ItemId.potion && slot.instance?.potionKey === "lesserHealthTonic" ); assert.ok(stack); assert.equal(stack.quantity, 3); assert.equal(stack.instance.kind, "potion"); assert.equal(stack.instance.displayName, "Lesser health tonic"); assert.equal(stack.instance.effect?.type, "health-regen"); assert.equal( originLabel(stack.origins?.[0]?.origin), "Brewed by John Questward" ); } finally { harness.close(); } }); test("admin gift rejects blank potion without a potion key", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const gifts = new GiftService(harness.stateDb, harness.simulation); assert.throws( () => gifts.sendGift({ characterId: character.character_id, items: [{ itemId: ItemId.potion, quantity: 1 }] }), /at least one item/i ); } finally { harness.close(); } });