| 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 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 11 | |
| 12 | function tempDirs () |
| 13 | { |
| 14 | const dir = mkdtempSync(join(tmpdir(), "mmo-editor-log-")); |
| 15 | return { |
| 16 | worldDb: new WorldDatabase(join(dir, "world.sqlite")), |
| 17 | stateDb: new StateDatabase(join(dir, "state.sqlite")) |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | test("quest and dialogue diffs describe create and field edits", () => |
| 22 | { |
| 23 | const created = diffQuests(null, normalizeQuestDef({ questId: "q1", title: "Fetch" })); |
| 24 | assert.match(created[0], /created/); |
| 25 | const previous = normalizeQuestDef({ questId: "q1", title: "Fetch", description: "A" }); |
| 26 | const next = normalizeQuestDef({ questId: "q1", title: "Fetch more", description: "B", cancelable: true }); |
| 27 | const bullets = diffQuests(previous, next); |
| 28 | assert.ok(bullets.some((line) => /Title/.test(line))); |
| 29 | assert.ok(bullets.some((line) => /Description/.test(line))); |
| 30 | assert.ok(bullets.some((line) => /Cancelable/.test(line))); |
| 31 | |
| 32 | const dialogueCreated = diffDialogues(null, normalizeDialogueTree({ dialogueId: "d1", label: "Hi" })); |
| 33 | assert.match(dialogueCreated[0], /created/); |
| 34 | const prevTree = normalizeDialogueTree({ dialogueId: "d1", label: "Hi", unique: false }); |
| 35 | const nextTree = normalizeDialogueTree({ dialogueId: "d1", label: "Hello", unique: true }); |
| 36 | const dialogueBullets = diffDialogues(prevTree, nextTree); |
| 37 | assert.ok(dialogueBullets.some((line) => /Label/.test(line))); |
| 38 | assert.ok(dialogueBullets.some((line) => /Unique/.test(line))); |
| 39 | }); |
| 40 | |
| 41 | test("quest and dialogue saves append changelogs with usernames", () => |
| 42 | { |
| 43 | const { worldDb, stateDb } = tempDirs(); |
| 44 | try |
| 45 | { |
| 46 | stateDb.createAccount("auth:42", true, null, "timfa"); |
| 47 | const editor = new EditorService(worldDb, stateDb); |
| 48 | const saved = editor.saveQuest({ |
| 49 | questId: "welcome", |
| 50 | title: "Welcome", |
| 51 | description: "Say hi" |
| 52 | }, "auth:42"); |
| 53 | assert.equal(saved.changelog.length, 1); |
| 54 | assert.equal(saved.changelog[0].editorDisplayName, "timfa"); |
| 55 | assert.equal(saved.changelog[0].editor_admin_id, "auth:42"); |
| 56 | assert.match(saved.changelog[0].diffEntries[0], /created/); |
| 57 | |
| 58 | const dialogue = editor.saveDialogue({ |
| 59 | dialogueId: "greeter", |
| 60 | label: "Greeter", |
| 61 | unique: true |
| 62 | }, "auth:42"); |
| 63 | assert.equal(dialogue.changelog.length, 1); |
| 64 | assert.equal(dialogue.changelog[0].editorDisplayName, "timfa"); |
| 65 | assert.match(dialogue.changelog[0].diffEntries[0], /created/); |
| 66 | |
| 67 | editor.saveSettings({ nightDarkness: 0.2 }, "auth:42"); |
| 68 | const global = editor.loadGlobalChangelog(); |
| 69 | assert.ok(global.changelog.length >= 3); |
| 70 | assert.ok(global.changelog.every((entry) => entry.editorDisplayName === "timfa")); |
| 71 | assert.deepEqual( |
| 72 | new Set(global.changelog.map((entry) => entry.kind)), |
| 73 | new Set(["quest", "dialogue", "settings"]) |
| 74 | ); |
| 75 | } |
| 76 | finally |
| 77 | { |
| 78 | worldDb.close(); |
| 79 | stateDb.close(); |
| 80 | } |
| 81 | }); |
| 82 | |
| 83 | test("saveChunk persists Void in a non-overworld dimension and returns it on the ack payload", () => |
| 84 | { |
| 85 | const { worldDb, stateDb } = tempDirs(); |
| 86 | try |
| 87 | { |
| 88 | stateDb.createAccount("auth:42", true, null, "timfa"); |
| 89 | const editor = new EditorService(worldDb, stateDb); |
| 90 | editor.createDimension("void_hall", "Void Hall", "auth:42"); |
| 91 | const loaded = editor.loadChunk(1, 2, "void_hall"); |
| 92 | const blocks = loaded.data.blocks.slice(); |
| 93 | blocks[0] = { typeId: BlockType.void, state: null, config: null }; |
| 94 | const saved = editor.saveChunk(1, 2, { |
| 95 | open: true, |
| 96 | grounds: loaded.data.grounds, |
| 97 | blocks, |
| 98 | spawners: [] |
| 99 | }, "auth:42", "void_hall"); |
| 100 | assert.equal(saved.dimensionId, "void_hall"); |
| 101 | assert.equal(saved.chunkX, 1); |
| 102 | assert.equal(saved.chunkY, 2); |
| 103 | assert.equal(saved.data.blocks[0]?.typeId, BlockType.void); |
| 104 | assert.ok(saved.changelog.some((entry) => |
| 105 | (entry.diffEntries ?? []).some((line) => line.includes("Block Void placed")))); |
| 106 | const reloaded = editor.loadChunk(1, 2, "void_hall"); |
| 107 | assert.equal(reloaded.data.blocks[0]?.typeId, BlockType.void); |
| 108 | assert.equal(reloaded.dimensionId, "void_hall"); |
| 109 | } |
| 110 | finally |
| 111 | { |
| 112 | worldDb.close(); |
| 113 | stateDb.close(); |
| 114 | } |
| 115 | }); |