Files
obsidian-excalidraw-plugin/scripts/testing/embedBridge.source.test.mjs
T

47 lines
3.5 KiB
JavaScript

/** Exercise the real bridge's scheduling with public CM ranges and controlled DOM observers. */
import assert from "node:assert/strict";
import test from "node:test";
import { EditorState, StateField } from "@codemirror/state";
import * as cmView from "@codemirror/view";
import { sourceHarness } from "./sourceHarness.mjs";
test("invalid render generations cannot remount controls or starve the renderer debounce", async () => {
const pending = [], originalQueue = globalThis.queueMicrotask, observers = [];
let generation = true, loaded = 0, unloaded = 0, retireWhileMapping = false;
globalThis.queueMicrotask = callback => pending.push(callback);
const file = { path: "Source.md" }, drawing = { path: "Map.excalidraw.md" }, text = "![[Map.excalidraw.md|180]]";
const editorInfoField = StateField.define({ create: () => ({ file }), update: value => value });
const editorLivePreviewField = StateField.define({ create: () => true, update: value => value });
const doc = { defaultView: { MutationObserver: class {
constructor(callback) { this.callback = callback; observers.push(this); }
observe() { this.connected = true; }
disconnect() { this.connected = false; }
} } };
const frame = { isConnected: true, ownerDocument: doc }, parent = { childNodes: [] };
const native = { parentNode: parent, closest: () => null, contains: el => el === frame }; parent.childNodes.push(native);
const contentDOM = { querySelectorAll: () => [image], contains: el => el === frame };
const image = { parentElement: frame, closest: selector => selector === ".internal-embed" ? native : contentDOM };
const actions = { target: drawing.path, isCurrent: () => generation };
const mutation = () => { for (const observer of observers) if (observer.connected) observer.callback(); };
const graph = sourceHarness({
obsidian: { editorInfoField, editorLivePreviewField },
"@codemirror/view": { ...cmView, ViewPlugin: { fromClass: value => value } },
"../managers/DrawingEmbedControls": { DrawingEmbedControls: class { load() { loaded++; mutation(); } unload() { unloaded++; mutation(); } } },
"../managers/drawingEmbedActions": { getDrawingEmbedActions: () => actions },
});
try {
const { createDrawingEmbedExtension } = await graph.importModule("src/core/editor/drawingEmbedExtension.ts");
const plugin = { isExcalidrawFile: value => value === drawing, app: { metadataCache: { getFirstLinkpathDest: () => drawing } } };
const bridge = createDrawingEmbedExtension(plugin);
const view = { dom: { ownerDocument: doc }, contentDOM, state: EditorState.create({ doc: text + "\n", extensions: [editorInfoField, editorLivePreviewField] }),
posAtDOM(el, offset) { if (retireWhileMapping) generation = false; return el === parent && offset === 1 ? text.length + 1 : 0; } };
const drain = () => { for (let i = 0; pending.length && i < 10; i++) pending.shift()(); assert.equal(pending.length, 0, "unbounded remount/mutation loop"); };
new bridge.extension(view); drain(); assert.equal(loaded, 1);
generation = false; mutation(); drain(); assert.equal(loaded, 1); assert.equal(unloaded, 1);
generation = true; mutation(); drain(); assert.equal(loaded, 2);
generation = false; mutation(); drain();
generation = true; retireWhileMapping = true; mutation(); drain(); assert.equal(loaded, 2, "source invalidated during DOM mapping was mounted");
bridge.dispose(); drain(); assert.ok(observers.every(observer => !observer.connected));
} finally { graph.close(); globalThis.queueMicrotask = originalQueue; }
});