mirror of
https://github.com/zsviczian/obsidian-excalidraw-plugin.git
synced 2026-11-27 06:32:42 +00:00
163 lines
6.5 KiB
JavaScript
163 lines
6.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { captureTextUndoState } from "./textUndoDiagnostics.mjs";
|
|
import { prepareTextUndoFixture } from "./textUndo.mjs";
|
|
|
|
function fixture() {
|
|
const history = { undoStack: [{}], redoStack: [{}, {}] };
|
|
const button = (x, disabled) => ({
|
|
disabled,
|
|
bounds: { x, y: 10, width: 30, height: 20 },
|
|
getBoundingClientRect() {
|
|
return this.bounds;
|
|
},
|
|
closest() {
|
|
return this;
|
|
},
|
|
});
|
|
const undo = button(10, false),
|
|
redo = button(50, true);
|
|
const root = {
|
|
className: "excalidraw",
|
|
getBoundingClientRect: () => ({ x: 0, y: 0, width: 800, height: 600 }),
|
|
__reactFiber$owned: { return: { stateNode: { history } } },
|
|
};
|
|
const win = {
|
|
closed: false,
|
|
innerWidth: 800,
|
|
innerHeight: 600,
|
|
getComputedStyle: () => ({ display: "block", visibility: "visible" }),
|
|
require: () => ({ getCurrentWebContents: () => ({ id: 42 }) }),
|
|
};
|
|
const doc = {
|
|
defaultView: win,
|
|
hasFocus: () => true,
|
|
elementFromPoint: (x) => (x < 50 ? undo : redo),
|
|
};
|
|
const element = {
|
|
id: "owned",
|
|
type: "text",
|
|
text: "label",
|
|
originalText: "label",
|
|
rawText: "[[file|label]]",
|
|
version: 2,
|
|
};
|
|
const query = (selector) =>
|
|
selector === ".excalidraw"
|
|
? root
|
|
: selector.includes("button-undo")
|
|
? undo
|
|
: selector.includes("button-redo")
|
|
? redo
|
|
: null;
|
|
const view = {
|
|
file: { path: "__excalidraw_cli_smoke_owned.md" },
|
|
textMode: "parsed",
|
|
compatibilityMode: false,
|
|
isDirty: () => true,
|
|
isClosingOrMigrating: () => false,
|
|
containerEl: { ownerDocument: doc, querySelector: query },
|
|
excalidrawData: {
|
|
getParsedResult: () => ({ raw: element.rawText, parsed: element.text }),
|
|
},
|
|
excalidrawAPI: {
|
|
getAppState: () => ({
|
|
viewModeEnabled: false,
|
|
selectedElementIds: { owned: true },
|
|
}),
|
|
getSceneElementsIncludingDeleted: () => [element],
|
|
getEditorInterface: () => ({ formFactor: "desktop" }),
|
|
},
|
|
};
|
|
const workspace = {
|
|
containerEl: { ownerDocument: doc },
|
|
activeLeaf: {
|
|
id: "other",
|
|
view: { file: { path: "Notes/Other.md" }, getViewType: () => "markdown" },
|
|
},
|
|
getLayout: () => ({ active: "other", main: { id: "split" } }),
|
|
};
|
|
return { history, undo, redo, root, win, doc, view, workspace };
|
|
}
|
|
|
|
test("text-history diagnostics preserve owning window, leaked active target, controls and actual stack depths without mutation", () => {
|
|
const f = fixture(),
|
|
snapshot = captureTextUndoState(f.view, f.workspace);
|
|
assert.equal(snapshot.owner.contentsId, 42);
|
|
assert.equal(snapshot.owner.main, true);
|
|
assert.equal(snapshot.activeLeaf.file, "Notes/Other.md");
|
|
assert.equal(snapshot.targetDrawing, f.view.file.path);
|
|
assert.equal(snapshot.controls.undo.disabled, false);
|
|
assert.equal(snapshot.controls.redo.disabled, true);
|
|
assert.equal(snapshot.controls.undo.visible, true);
|
|
assert.deepEqual(snapshot.history, {
|
|
source: "read-only owning App fiber",
|
|
undoDepth: 1,
|
|
redoDepth: 2,
|
|
undoEntries: [{ id: undefined, elements: undefined, appState: undefined }],
|
|
redoEntries: [
|
|
{ id: undefined, elements: undefined, appState: undefined },
|
|
{ id: undefined, elements: undefined, appState: undefined },
|
|
],
|
|
});
|
|
assert.equal(snapshot.elements[0].cache.raw, "[[file|label]]");
|
|
assert.deepEqual(f.history, { undoStack: [{}], redoStack: [{}, {}] });
|
|
assert.doesNotThrow(() => JSON.stringify(snapshot));
|
|
});
|
|
|
|
test("missing and hidden history controls remain unavailable evidence and cyclic private fibers stay bounded", () => {
|
|
const f = fixture(),
|
|
original = f.view.containerEl.querySelector;
|
|
f.view.containerEl.querySelector = (selector) =>
|
|
selector.includes("button-redo") ? null : original(selector);
|
|
f.undo.bounds.width = 0;
|
|
f.win.getComputedStyle = () => ({ display: "none", visibility: "hidden" });
|
|
const cyclic = {};
|
|
cyclic.return = cyclic;
|
|
f.root["__reactFiber$owned"] = cyclic;
|
|
const snapshot = captureTextUndoState(f.view, f.workspace);
|
|
assert.equal(snapshot.controls.redo.present, false);
|
|
assert.equal(snapshot.controls.redo.disabled, null);
|
|
assert.equal(snapshot.controls.redo.visible, false);
|
|
assert.equal(snapshot.controls.undo.visible, false);
|
|
assert.equal(snapshot.history.source, "not exposed by public API");
|
|
assert.doesNotThrow(() => JSON.stringify(snapshot));
|
|
});
|
|
|
|
test("text fixture awaits the normal mode reload before clearing the replacement API", async () => {
|
|
const calls = [];
|
|
const api = {
|
|
updateScene: () => calls.push("clear scene"),
|
|
history: { clear: () => calls.push("clear history") },
|
|
setActiveTool: () => calls.push("text tool"),
|
|
getAppState:()=>({activeTool:{type:'text'}}),
|
|
};
|
|
const view = { textMode: "parsed", excalidrawData: { textMode: "parsed" },
|
|
excalidrawAPI: {}, containerEl: { querySelector: () => ({ getBoundingClientRect: () => ({ x: 0, y: 0, width: 100, height: 80 }) }) },
|
|
async changeTextMode(...args) {
|
|
assert.deepEqual(args, ["raw"], "reload=false would leave the model parsed");
|
|
calls.push("reload"); await Promise.resolve();
|
|
this.textMode = this.excalidrawData.textMode = "raw";
|
|
this.excalidrawAPI = api;
|
|
} };
|
|
const result = await prepareTextUndoFixture(view, "raw");
|
|
assert.deepEqual(calls, ["reload", "clear scene", "clear history", "text tool"]);
|
|
assert.deepEqual(result.modeSetup.after, { view: "raw", model: "raw" });
|
|
assert.deepEqual({ x: result.x, y: result.y }, { x: 50, y: 40 });
|
|
});
|
|
|
|
test("incoherent text modes fail before scene or history mutation", async () => {
|
|
const view = { textMode: "raw", excalidrawData: { textMode: "parsed" },
|
|
async changeTextMode() {} };
|
|
await assert.rejects(prepareTextUndoFixture(view, "raw"), /did not synchronize/);
|
|
});
|
|
|
|
|
|
test("text fixture waits for the imperative tool update to commit before returning an input target",async()=>{
|
|
const original=globalThis.window;globalThis.window={setTimeout};let active='selection',timer;
|
|
const api={updateScene(){},history:{clear(){}},setActiveTool(){timer=setTimeout(()=>{active='text';},25);},getAppState:()=>({activeTool:{type:active}})};
|
|
const view={textMode:'parsed',excalidrawData:{textMode:'parsed'},excalidrawAPI:api,containerEl:{querySelector:()=>({getBoundingClientRect:()=>({x:0,y:0,width:100,height:80})})}};
|
|
try{const result=await prepareTextUndoFixture(view,'parsed');assert.equal(active,'text');assert.equal(result.x,50);}
|
|
finally{clearTimeout(timer);if(original===undefined)delete globalThis.window;else globalThis.window=original;}
|
|
});
|