| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | applyDialogueTextReplacements, |
| 5 | applyTokenCasing, |
| 6 | dialogueReplacementCatalog |
| 7 | } from "../../shared/speech/dialogueText.js"; |
| 8 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 9 | import { normalizeDialogueTree } from "../../shared/quests/normalize.js"; |
| 10 | |
| 11 | test("applyTokenCasing follows placeholder case", () => |
| 12 | { |
| 13 | assert.equal(applyTokenCasing("mr", "ms"), "ms"); |
| 14 | assert.equal(applyTokenCasing("Mr", "ms"), "Ms"); |
| 15 | assert.equal(applyTokenCasing("MR", "ms"), "MS"); |
| 16 | assert.equal(applyTokenCasing("He", "she"), "She"); |
| 17 | }); |
| 18 | |
| 19 | test("dialogue text replacements cover names and gendered forms", () => |
| 20 | { |
| 21 | const male = applyDialogueTextReplacements( |
| 22 | "Hello [Mr] [playername] [playersurname]. [He] brings [his] bag, [playerfullname].", |
| 23 | { firstName: "Ada", lastName: "Worker", sex: "male" } |
| 24 | ); |
| 25 | assert.equal(male, "Hello Mr Ada Worker. He brings his bag, Ada Worker."); |
| 26 | |
| 27 | const female = applyDialogueTextReplacements( |
| 28 | "Hello [Mr] [playername]. [He] / [him] / [his] / [man] / [himself].", |
| 29 | { firstName: "Bea", lastName: "Smith", sex: "female" } |
| 30 | ); |
| 31 | assert.equal(female, "Hello Ms Bea. She / her / her / woman / herself."); |
| 32 | }); |
| 33 | |
| 34 | test("dialogueReplacementCatalog lists player and gender tokens", () => |
| 35 | { |
| 36 | const tokens = dialogueReplacementCatalog().map((row) => row.token); |
| 37 | assert.ok(tokens.includes("[playername]")); |
| 38 | assert.ok(tokens.includes("[playerfullname]")); |
| 39 | assert.ok(tokens.includes("[playersurname]")); |
| 40 | assert.ok(tokens.includes("[he]")); |
| 41 | assert.ok(tokens.includes("[mr]")); |
| 42 | }); |
| 43 | |
| 44 | test("dialogue step lines are substituted for the player identity", () => |
| 45 | { |
| 46 | const harness = makeSimulation({ workerCount: 1 }); |
| 47 | try |
| 48 | { |
| 49 | const character = makeCharacter(harness.stateDb, { |
| 50 | tileX: 8, |
| 51 | tileY: 8, |
| 52 | firstName: "Ada", |
| 53 | lastName: "Worker", |
| 54 | sex: "female" |
| 55 | }); |
| 56 | harness.simulation.spawnCharacter(1, character); |
| 57 | harness.simulation.step(); |
| 58 | |
| 59 | const tree = normalizeDialogueTree({ |
| 60 | dialogueId: "greet-name", |
| 61 | steps: [{ |
| 62 | stepId: "hi", |
| 63 | lines: [{ speaker: "npc", text: "Welcome, [Mr] [playername]." }] |
| 64 | }] |
| 65 | }); |
| 66 | const revisionId = harness.worldDb.getActiveRevisionId(); |
| 67 | harness.worldDb.saveDialogueTree(revisionId, tree); |
| 68 | harness.simulation.questContent.reload(); |
| 69 | |
| 70 | const opened = harness.simulation.quests.tryOpenDialogue( |
| 71 | character.character_id, |
| 72 | 9100, |
| 73 | "greet-name" |
| 74 | ); |
| 75 | assert.equal(opened.ok, true); |
| 76 | const step = (opened.events ?? []).find((e) => e.type === "dialogueStep"); |
| 77 | assert.equal(step.lines[0].text, "Welcome, Ms Ada."); |
| 78 | } |
| 79 | finally |
| 80 | { harness.close(); } |
| 81 | }); |