An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
import * as assert from "node:assert/strict";
import {execFile} from "node:child_process";
import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";
import {tmpdir} from "node:os";
import * as path from "node:path";
import {test} from "node:test";
import {promisify} from "node:util";
import {ScriptTarget, transpileModule} from "typescript";
const browserCases = async (source: string, desktopSource: string, reloadSource: string) => {
const check: typeof assert = require("node:assert/strict");
const Constants = {CB_GET_SCROLL: "scroll", CB_GET_HL: "highlight", CB_GET_FOCUS: "focus",
CB_GET_FOCUSFIRST: "focusFirst", ATTRIBUTE_V_SCROLL: "data-v-scroll"};
const focusElementById = new Function("Constants", "resolveVisibleListMindmapBlock", "isPhablet",
"hasClosestByAttribute", source + "\nreturn focusElementById;")(
Constants, (): undefined => undefined, () => false, (): null => null) as (
protyle: IProtyle, action: string[], scrollAttr: IScrollAttr) => void;
const host = document.createElement("div");
const content = document.createElement("div");
const wysiwyg = document.createElement("div");
content.style.cssText = "height: 200px; width: 300px; overflow-y: auto";
wysiwyg.innerHTML = '<div data-node-id="block"><div contenteditable="true">Text</div></div>' +
'<div id="spacer" style="height: 800px"></div>';
content.append(wysiwyg);
host.append(content);
document.body.append(host);
const spacer = wysiwyg.querySelector<HTMLElement>("#spacer");
const editable = wysiwyg.querySelector<HTMLElement>('[contenteditable="true"]');
const noop = (): void => undefined;
const protyle = {element: host, contentElement: content, wysiwyg: {element: wysiwyg},
block: {id: "block", rootID: "root"}, observer: {observe: noop, unobserve: noop}} as unknown as IProtyle;
focusElementById(protyle, [Constants.CB_GET_SCROLL], {scrollTop: 221} as IScrollAttr);
content.scrollTop = 0;
spacer.style.height = "801px";
await new Promise(resolve => setTimeout(resolve, 150));
check.equal(content.scrollTop, 221, "layout changes keep the requested position before editing");
focusElementById(protyle, [Constants.CB_GET_SCROLL], {scrollTop: 221} as IScrollAttr);
content.scrollTop = 0;
editable.dispatchEvent(new InputEvent("beforeinput", {bubbles: true, inputType: "insertText", data: "x"}));
spacer.style.height = "802px";
await new Promise(resolve => setTimeout(resolve, 150));
check.equal(content.scrollTop, 0, "typing releases the old position before layout changes");
const editor = {editor: {protyle}};
const observeDesktopPosition = new Function("editor", "nodeElement", "scrollTop", "options",
"isHiddenTabContent", "scrollCenter", desktopSource + "\nreturn observerLoad;");
const target = wysiwyg.querySelector('[data-node-id="block"]');
const observe = () => observeDesktopPosition(editor, target, 221, {}, () => false, noop) as ResizeObserver;
const firstObserver = observe();
content.scrollTop = 0;
spacer.style.height = "803px";
await new Promise(resolve => setTimeout(resolve, 150));
check.equal(content.scrollTop, 221, "desktop focus compensation follows layout changes before editing");
firstObserver.disconnect();
observe();
content.scrollTop = 0;
editable.dispatchEvent(new InputEvent("beforeinput", {bubbles: true, inputType: "insertText", data: "x"}));
spacer.style.height = "804px";
await new Promise(resolve => setTimeout(resolve, 150));
check.equal(content.scrollTop, 0, "typing cancels desktop focus compensation");
const preserveReloadScroll = new Function("Constants", reloadSource + "\nreturn preserveReloadScroll;") as
(constants: typeof Constants) => (protyle: IProtyle, scrollTop: number) => {
beforeAVRender: () => void;
afterAVRender: () => Promise<void>;
isValid: () => boolean;
cancel: () => void;
};
const preserve = preserveReloadScroll(Constants);
protyle.scroll = {lastScrollTop: 0} as IProtyle["scroll"];
const database = (type: string, rendered: boolean) =>
`<div class="av" data-node-id="av-block" data-av-id="av-data" data-av-type="${type}" ` +
`data-render="${rendered}"${rendered ? ' data-v-scroll="true"' : ""}><div class="av__container">${rendered ?
'<div class="av__scroll" style="width: 250px; overflow: auto"><div style="width: 600px">' +
'<div class="av__body" data-page-size="100" data-group-id="group" style="height: 1000px">' +
'<div class="av__row" data-index="50">Resource</div></div>' +
'<div data-type="av-search">query</div></div></div>' : ""}</div></div>`;
const replaceDatabase = (type = "table") => {
wysiwyg.innerHTML = database(type, false);
content.scrollTop = 650;
check.equal(content.scrollTop, 0, "an empty database clamps the saved scroll position");
};
for (const type of ["table", "gallery", "kanban"]) {
wysiwyg.innerHTML = database(type, true);
wysiwyg.querySelector(".av__scroll").scrollLeft = 120;
content.scrollTop = 650;
const state = preserve(protyle, content.scrollTop);
check.equal(content.style.overflowAnchor, "none", "reload temporarily suppresses browser scroll anchoring");
replaceDatabase(type);
state.beforeAVRender();
check.equal(wysiwyg.querySelector(".av").getAttribute(Constants.ATTRIBUTE_V_SCROLL), "true");
check.equal(wysiwyg.querySelector<HTMLElement>(".av__body").dataset.pageSize, "100");
check.equal(wysiwyg.querySelector<HTMLElement>(".av__row").dataset.index, "50");
check.equal(wysiwyg.querySelector("[data-type='av-search']").textContent, "query");
check.equal(wysiwyg.querySelector(".av__scroll").scrollLeft, 120);
await Promise.resolve();
await state.afterAVRender();
check.equal(content.scrollTop, 650, `${type} restores the position after the database regains height`);
check.equal(protyle.scroll.lastScrollTop, 650);
check.equal(content.style.overflowAnchor, "", "reload releases its temporary anchoring override");
await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
check.equal(content.scrollTop, 650, `${type} remains at the restored position after layout settles`);
}
for (const type of ["wheel", "touchstart", "pointerdown", "keydown", "beforeinput"]) {
wysiwyg.innerHTML = database("table", true);
const state = preserve(protyle, 650);
replaceDatabase();
state.beforeAVRender();
content.dispatchEvent(new Event(type, {bubbles: true}));
check.equal(content.style.overflowAnchor, "", `${type} releases the anchoring override`);
content.scrollTop = 123;
await state.afterAVRender();
check.equal(content.scrollTop, 123, `${type} cancels delayed restoration`);
}
wysiwyg.querySelector(".av").setAttribute("data-render", "true");
const stale = preserve(protyle, 650);
const current = preserve(protyle, 450);
check.equal(stale.isValid(), false, "a newer reload invalidates the pending response");
content.scrollTop = 123;
await stale.afterAVRender();
check.equal(content.scrollTop, 123);
current.cancel();
const navigation = preserve(protyle, 650);
protyle.block.rootID = "other-root";
await navigation.afterAVRender();
check.equal(content.scrollTop, 123, "navigation cancels restoration");
protyle.block.rootID = "root";
const zoom = preserve(protyle, 650);
wysiwyg.innerHTML = database("table", true);
content.scrollTop = 123;
await zoom.afterAVRender();
check.equal(content.scrollTop, 123, "replacing content in the same document cancels restoration");
const changedView = preserve(protyle, 650);
replaceDatabase("gallery");
changedView.beforeAVRender();
check.equal(wysiwyg.querySelector(".av__body"), null, "a changed view does not receive stale table content");
changedView.cancel();
const closed = preserve(protyle, 650);
host.remove();
check.equal(closed.isValid(), false, "closing the editor invalidates the pending response");
await closed.afterAVRender();
return "Positioning cancellation cases passed";
};
test("delayed document positioning preserves database state and respects user interaction", {
skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
timeout: 30000,
}, async () => {
const source = readFileSync(path.join(__dirname, "onGet.ts"), "utf8");
const start = source.indexOf("const focusElementById = ");
const end = source.indexOf("export const setReadonlyByConfig", start);
assert.ok(start >= 0 && end > start);
const compiled = transpileModule(source.slice(start, end), {
compilerOptions: {target: ScriptTarget.ES2021}
}).outputText;
const desktopSource = readFileSync(path.join(__dirname, "../../editor/util.ts"), "utf8");
const desktopStart = desktopSource.indexOf("const userScrollAbort = new AbortController();");
const desktopEndMarker = "observerLoad.observe(editor.editor.protyle.wysiwyg.element);";
const desktopEnd = desktopSource.indexOf(desktopEndMarker, desktopStart) + desktopEndMarker.length;
assert.ok(desktopStart >= 0 && desktopEnd > desktopStart);
const desktopCompiled = transpileModule(desktopSource.slice(desktopStart, desktopEnd), {
compilerOptions: {target: ScriptTarget.ES2021}
}).outputText;
const reloadSource = readFileSync(path.join(__dirname, "reloadScroll.ts"), "utf8")
.replace('import {Constants} from "../../constants";', "").replace("export const", "const");
const reloadCompiled = transpileModule(reloadSource, {
compilerOptions: {target: ScriptTarget.ES2021}
}).outputText;
const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-onget-positioning-test-"));
const script = path.join(temporary, "run.cjs");
writeFileSync(script, `const {app, BrowserWindow} = require("electron");
app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
app.commandLine.appendSwitch("disable-gpu");
app.whenReady().then(async () => {
const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});
try {
await win.loadURL("data:text/html,<html><body></body></html>");
console.log(await win.webContents.executeJavaScript(${JSON.stringify("const __name = value => value; (" +
browserCases.toString() + ")(" + JSON.stringify(compiled) + "," + JSON.stringify(desktopCompiled) + "," +
JSON.stringify(reloadCompiled) + ")")}));
win.destroy();
app.exit(0);
} catch (error) {
console.error(error);
win.destroy();
app.exit(1);
}
});`, "utf8");
const env = {...process.env};
delete env.ELECTRON_RUN_AS_NODE;
try {
const result = await promisify(execFile)(require("electron") as unknown as string, [script],
{env, timeout: 25000, windowsHide: true});
assert.match(result.stdout, /Positioning cancellation cases passed/);
} finally {
if (path.dirname(path.resolve(temporary)) === path.resolve(tmpdir()) &&
path.basename(temporary).startsWith("siyuan-onget-positioning-test-")) {
rmSync(temporary, {recursive: true, force: true});
}
}
});