import test from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { WorldDatabase } from "../src/persistence/worldDb.js"; import { StateDatabase } from "../src/persistence/stateDb.js"; import { EditorService, diffQuests, diffDialogues } from "../src/editor/service.js"; import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js"; import { BlockType } from "../../shared/definitions/tiles.js"; function tempDirs () { const dir = mkdtempSync(join(tmpdir(), "mmo-editor-log-")); return { worldDb: new WorldDatabase(join(dir, "world.sqlite")), stateDb: new StateDatabase(join(dir, "state.sqlite")) }; } test("quest and dialogue diffs describe create and field edits", () => { const created = diffQuests(null, normalizeQuestDef({ questId: "q1", title: "Fetch" })); assert.match(created[0], /created/); const previous = normalizeQuestDef({ questId: "q1", title: "Fetch", description: "A" }); const next = normalizeQuestDef({ questId: "q1", title: "Fetch more", description: "B", cancelable: true }); const bullets = diffQuests(previous, next); assert.ok(bullets.some((line) => /Title/.test(line))); assert.ok(bullets.some((line) => /Description/.test(line))); assert.ok(bullets.some((line) => /Cancelable/.test(line))); const dialogueCreated = diffDialogues(null, normalizeDialogueTree({ dialogueId: "d1", label: "Hi" })); assert.match(dialogueCreated[0], /created/); const prevTree = normalizeDialogueTree({ dialogueId: "d1", label: "Hi", unique: false }); const nextTree = normalizeDialogueTree({ dialogueId: "d1", label: "Hello", unique: true }); const dialogueBullets = diffDialogues(prevTree, nextTree); assert.ok(dialogueBullets.some((line) => /Label/.test(line))); assert.ok(dialogueBullets.some((line) => /Unique/.test(line))); }); test("quest and dialogue saves append changelogs with usernames", () => { const { worldDb, stateDb } = tempDirs(); try { stateDb.createAccount("auth:42", true, null, "timfa"); const editor = new EditorService(worldDb, stateDb); const saved = editor.saveQuest({ questId: "welcome", title: "Welcome", description: "Say hi" }, "auth:42"); assert.equal(saved.changelog.length, 1); assert.equal(saved.changelog[0].editorDisplayName, "timfa"); assert.equal(saved.changelog[0].editor_admin_id, "auth:42"); assert.match(saved.changelog[0].diffEntries[0], /created/); const dialogue = editor.saveDialogue({ dialogueId: "greeter", label: "Greeter", unique: true }, "auth:42"); assert.equal(dialogue.changelog.length, 1); assert.equal(dialogue.changelog[0].editorDisplayName, "timfa"); assert.match(dialogue.changelog[0].diffEntries[0], /created/); editor.saveSettings({ nightDarkness: 0.2 }, "auth:42"); const global = editor.loadGlobalChangelog(); assert.ok(global.changelog.length >= 3); assert.ok(global.changelog.every((entry) => entry.editorDisplayName === "timfa")); assert.deepEqual( new Set(global.changelog.map((entry) => entry.kind)), new Set(["quest", "dialogue", "settings"]) ); } finally { worldDb.close(); stateDb.close(); } }); test("saveChunk persists Void in a non-overworld dimension and returns it on the ack payload", () => { const { worldDb, stateDb } = tempDirs(); try { stateDb.createAccount("auth:42", true, null, "timfa"); const editor = new EditorService(worldDb, stateDb); editor.createDimension("void_hall", "Void Hall", "auth:42"); const loaded = editor.loadChunk(1, 2, "void_hall"); const blocks = loaded.data.blocks.slice(); blocks[0] = { typeId: BlockType.void, state: null, config: null }; const saved = editor.saveChunk(1, 2, { open: true, grounds: loaded.data.grounds, blocks, spawners: [] }, "auth:42", "void_hall"); assert.equal(saved.dimensionId, "void_hall"); assert.equal(saved.chunkX, 1); assert.equal(saved.chunkY, 2); assert.equal(saved.data.blocks[0]?.typeId, BlockType.void); assert.ok(saved.changelog.some((entry) => (entry.diffEntries ?? []).some((line) => line.includes("Block Void placed")))); const reloaded = editor.loadChunk(1, 2, "void_hall"); assert.equal(reloaded.data.blocks[0]?.typeId, BlockType.void); assert.equal(reloaded.dimensionId, "void_hall"); } finally { worldDb.close(); stateDb.close(); } });