BlueGit

timfa questward

Files

questward / client / tests / editor-save-ack.test.js

editor-save-ack.test.js

fix: discard chunk-editor drafts so they cannot overwrite another chunk
timfa project_85689633_bot_782f591ee30607a5698798444cb41267 · pushed by timfa · 2 months ago
dbb8218b History
editor-save-ack.test.js 3.5 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { OVERWORLD_ID } from "../../shared/world/dimensions.js";
4import {
5 editorChunkSaveKey,
6 editorDirtyChunk,
7 editorDraftOwner,
8 sameEditorChunk,
9 shouldApplySelectedEditorChunk
10} from "../src/editor/saveAck.js";
11
12test("editorChunkSaveKey is dimension-scoped", () =>
13{
14 assert.equal(editorChunkSaveKey(OVERWORLD_ID, 1, 2), "overworld:1,2");
15 assert.equal(editorChunkSaveKey("Cave A", 1, 2), "cave-a:1,2");
16 assert.notEqual(
17 editorChunkSaveKey(OVERWORLD_ID, 0, 0),
18 editorChunkSaveKey("cave_a", 0, 0)
19 );
20 assert.equal(editorChunkSaveKey(undefined, 0, 0), editorChunkSaveKey(OVERWORLD_ID, 0, 0));
21});
22
23test("save ack applies even when the local draft is still dirty", () =>
24{
25 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: true, isDirty: true }), true);
26 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: true, isDirty: false }), true);
27});
28
29test("non-ack reload keeps unsaved paint on the selected chunk", () =>
30{
31 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: true }), false);
32 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: false }), true);
33});
34
35test("sameEditorChunk is dimension-aware", () =>
36{
37 const overworld = editorDraftOwner(OVERWORLD_ID, 0, 0);
38 const cave = editorDraftOwner("Cave A", 0, 0);
39 assert.equal(sameEditorChunk(overworld, editorDraftOwner(OVERWORLD_ID, 0, 0)), true);
40 assert.equal(sameEditorChunk(overworld, editorDraftOwner(OVERWORLD_ID, 1, 0)), false);
41 assert.equal(sameEditorChunk(overworld, cave), false);
42});
43
44test("dirty-chunk identity prefers the draft owner over the viewed chunk", () =>
45{
46 const edited = editorDraftOwner(OVERWORLD_ID, 2, 3);
47 const viewed = editorDraftOwner(OVERWORLD_ID, 4, 5);
48 assert.deepEqual(editorDirtyChunk(edited, viewed), edited);
49 assert.deepEqual(editorDirtyChunk(null, viewed), viewed);
50 assert.deepEqual(
51 editorDirtyChunk(
52 editorDraftOwner("Cave A", 0, 0),
53 editorDraftOwner(OVERWORLD_ID, 0, 0)
54 ),
55 editorDraftOwner("Cave A", 0, 0)
56 );
57});
58
59test("incoming payload for a different chunk does not inherit leftover dirty paint", () =>
60{
61 const chunkA = editorDraftOwner(OVERWORLD_ID, 0, 0);
62 const chunkB = editorDraftOwner(OVERWORLD_ID, 1, 0);
63 assert.equal(
64 shouldApplySelectedEditorChunk({
65 isSaveAck: false,
66 isDirty: true,
67 incoming: chunkB,
68 draftOwner: chunkA
69 }),
70 true
71 );
72 assert.equal(
73 shouldApplySelectedEditorChunk({
74 isSaveAck: false,
75 isDirty: true,
76 incoming: chunkA,
77 draftOwner: chunkA
78 }),
79 false
80 );
81 assert.equal(
82 shouldApplySelectedEditorChunk({
83 isSaveAck: false,
84 isDirty: true,
85 incoming: editorDraftOwner("Cave A", 0, 0),
86 draftOwner: chunkA
87 }),
88 true
89 );
90});
91
92test("discard-then-navigate regression: leftover A edits must not save onto B", () =>
93{
94 const chunkA = editorDraftOwner(OVERWORLD_ID, 0, 0);
95 const chunkB = editorDraftOwner(OVERWORLD_ID, 1, 0);
96
97 // If navigation already moved the view to B while A's draft was still in
98 // memory (the old bug), save/discard still target A and B's load replaces A paint.
99 assert.deepEqual(editorDirtyChunk(chunkA, chunkB), chunkA);
100 assert.equal(
101 shouldApplySelectedEditorChunk({
102 isSaveAck: false,
103 isDirty: true,
104 incoming: chunkB,
105 draftOwner: chunkA
106 }),
107 true
108 );
109 assert.notEqual(
110 editorChunkSaveKey(chunkA.dimensionId, chunkA.chunkX, chunkA.chunkY),
111 editorChunkSaveKey(chunkB.dimensionId, chunkB.chunkX, chunkB.chunkY)
112 );
113 assert.equal(
114 shouldApplySelectedEditorChunk({
115 isSaveAck: false,
116 isDirty: false,
117 incoming: chunkB,
118 draftOwner: null
119 }),
120 true
121 );
122});