BlueGit

timfa questward

Files

questward / server / tests / editor-changelog.test.js

editor-changelog.test.js

fix: apply editor chunk save acks so dirty drafts clear
timfa project_85689633_bot_782f591ee30607a5698798444cb41267 · pushed by timfa · 2 months ago
cf62e091 History
editor-changelog.test.js 4.3 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { mkdtempSync } from "node:fs";
4import { tmpdir } from "node:os";
5import { join } from "node:path";
6import { WorldDatabase } from "../src/persistence/worldDb.js";
7import { StateDatabase } from "../src/persistence/stateDb.js";
8import { EditorService, diffQuests, diffDialogues } from "../src/editor/service.js";
9import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";
10import { BlockType } from "../../shared/definitions/tiles.js";
11
12function 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
21test("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
41test("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
83test("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});