| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mkdtempSync } from "node:fs"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import { WorldDatabase } from "../src/persistence/worldDb.js"; |
| 7 | import { StateDatabase } from "../src/persistence/stateDb.js"; |
| 8 | import { EditorService, diffQuests, diffDialogues } from "../src/editor/service.js"; |
| 9 | import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js"; |
| 10 | |
| 11 | function tempDirs () |
| 12 | { |
| 13 | const dir = mkdtempSync(join(tmpdir(), "mmo-editor-log-")); |
| 14 | return { |
| 15 | worldDb: new WorldDatabase(join(dir, "world.sqlite")), |
| 16 | stateDb: new StateDatabase(join(dir, "state.sqlite")) |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | test("quest and dialogue diffs describe create and field edits", () => |
| 21 | { |
| 22 | const created = diffQuests(null, normalizeQuestDef({ questId: "q1", title: "Fetch" })); |
| 23 | assert.match(created[0], /created/); |
| 24 | const previous = normalizeQuestDef({ questId: "q1", title: "Fetch", description: "A" }); |
| 25 | const next = normalizeQuestDef({ questId: "q1", title: "Fetch more", description: "B", cancelable: true }); |
| 26 | const bullets = diffQuests(previous, next); |
| 27 | assert.ok(bullets.some((line) => /Title/.test(line))); |
| 28 | assert.ok(bullets.some((line) => /Description/.test(line))); |
| 29 | assert.ok(bullets.some((line) => /Cancelable/.test(line))); |
| 30 | |
| 31 | const dialogueCreated = diffDialogues(null, normalizeDialogueTree({ dialogueId: "d1", label: "Hi" })); |
| 32 | assert.match(dialogueCreated[0], /created/); |
| 33 | const prevTree = normalizeDialogueTree({ dialogueId: "d1", label: "Hi", unique: false }); |
| 34 | const nextTree = normalizeDialogueTree({ dialogueId: "d1", label: "Hello", unique: true }); |
| 35 | const dialogueBullets = diffDialogues(prevTree, nextTree); |
| 36 | assert.ok(dialogueBullets.some((line) => /Label/.test(line))); |
| 37 | assert.ok(dialogueBullets.some((line) => /Unique/.test(line))); |
| 38 | }); |
| 39 | |
| 40 | test("quest and dialogue saves append changelogs with usernames", () => |
| 41 | { |
| 42 | const { worldDb, stateDb } = tempDirs(); |
| 43 | try |
| 44 | { |
| 45 | stateDb.createAccount("auth:42", true, null, "timfa"); |
| 46 | const editor = new EditorService(worldDb, stateDb); |
| 47 | const saved = editor.saveQuest({ |
| 48 | questId: "welcome", |
| 49 | title: "Welcome", |
| 50 | description: "Say hi" |
| 51 | }, "auth:42"); |
| 52 | assert.equal(saved.changelog.length, 1); |
| 53 | assert.equal(saved.changelog[0].editorDisplayName, "timfa"); |
| 54 | assert.equal(saved.changelog[0].editor_admin_id, "auth:42"); |
| 55 | assert.match(saved.changelog[0].diffEntries[0], /created/); |
| 56 | |
| 57 | const dialogue = editor.saveDialogue({ |
| 58 | dialogueId: "greeter", |
| 59 | label: "Greeter", |
| 60 | unique: true |
| 61 | }, "auth:42"); |
| 62 | assert.equal(dialogue.changelog.length, 1); |
| 63 | assert.equal(dialogue.changelog[0].editorDisplayName, "timfa"); |
| 64 | assert.match(dialogue.changelog[0].diffEntries[0], /created/); |
| 65 | |
| 66 | editor.saveSettings({ nightDarkness: 0.2 }, "auth:42"); |
| 67 | const global = editor.loadGlobalChangelog(); |
| 68 | assert.ok(global.changelog.length >= 3); |
| 69 | assert.ok(global.changelog.every((entry) => entry.editorDisplayName === "timfa")); |
| 70 | assert.deepEqual( |
| 71 | new Set(global.changelog.map((entry) => entry.kind)), |
| 72 | new Set(["quest", "dialogue", "settings"]) |
| 73 | ); |
| 74 | } |
| 75 | finally |
| 76 | { |
| 77 | worldDb.close(); |
| 78 | stateDb.close(); |
| 79 | } |
| 80 | }); |