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 {test} from "node:test";
import * as assert from "node:assert/strict";
import {readFileSync} from "node:fs";
import {resolve} from "node:path";
import {runInNewContext} from "node:vm";
import {ModuleKind, transpileModule} from "typescript";
import * as gestures from "./touchPanelGesture";
import {getTouchAxis} from "./touchGesture";
const {createMockRequire} = require("../../../tests/fixtures/mockModules.cjs");
const loadModule = (name: string, globals: Record<string, unknown>) => {
const exports: Record<string, (...args: unknown[]) => void> = {};
const source = readFileSync(resolve(process.cwd(), `src/mobile/util/${name}.ts`), "utf8");
runInNewContext(transpileModule(source, {compilerOptions: {module: ModuleKind.CommonJS}}).outputText,
{exports, ...globals});
return exports;
};
const classList = (...classes: string[]) => {
const values = new Set(classes);
return {
contains: (name: string) => values.has(name),
add: (name: string) => values.add(name),
remove: (name: string) => values.delete(name),
};
};
test("sidebar cycling follows visible tab order and dispatches plugin activation", () => {
const activated: string[] = [];
const revealed: string[] = [];
const tabs = ["file", "agent", "plugin-custom", "tag"].map(type => ({
dataset: {type: `sidebar-${type}-tab`, mobilePluginDockTab: type === "plugin-custom" ? type : undefined},
classList: classList(...(type === "file" ? ["toolbar__icon--active"] : type === "agent" ? ["fn__none"] : [])),
scrollIntoView: () => revealed.push(type),
}));
let currentTabs = tabs;
const toolbar = {
querySelectorAll: () => currentTabs,
dispatchEvent: (event: {detail: string}) => {
activated.push(event.detail);
tabs.forEach(tab => {
tab.classList.remove("toolbar__icon--active");
if (tab.dataset.type === `sidebar-${event.detail}-tab`) {
tab.classList.add("toolbar__icon--active");
}
});
},
};
const sidebar = loadModule("sidebar", {
require: createMockRequire({"./keyboardToolbar": {}, "./closePanel": {},
"../../protyle/render/av/cellEditor": {}}),
document: {getElementById: () => ({querySelector: () => toolbar})},
CustomEvent: class {
detail: string;
constructor(_type: string, options: {detail: string}) { this.detail = options.detail; }
},
});
sidebar.switchToNextSidebarTab("left");
sidebar.switchToNextSidebarTab("left");
sidebar.switchToNextSidebarTab("left");
assert.deepEqual(activated, ["plugin-custom", "tag", "file"]);
assert.deepEqual(revealed, activated);
currentTabs = [tabs[3], tabs[0], tabs[2]];
sidebar.switchToNextSidebarTab("right");
assert.equal(activated.at(-1), "plugin-custom");
currentTabs = [tabs[0], tabs[1]];
sidebar.switchToNextSidebarTab("left");
currentTabs = [];
sidebar.switchToNextSidebarTab("right");
assert.equal(activated.length, 4);
});
const createTouchHarness = (side: "left" | "right", options: {
scrollable?: boolean, disabled?: boolean, closed?: boolean, iosDevice?: boolean,
editorSelection?: "editor" | "outside",
} = {}) => {
const actions: string[] = [];
const panel = {
classList: classList(),
style: {transform: "translateX(0px)", removeProperty() { this.transform = ""; }},
contains: () => false,
};
const mask = {classList: classList(...(options.closed ? ["fn__none"] : [])), style: {}};
const target = {
id: options.closed ? "editor" : side === "left" ? "sidebar" : "sidebarRight",
tagName: "DIV", dataset: {}, parentElement: null as HTMLElement | null,
preventSwipe: false,
closest: (): null => null,
scrollWidth: options.scrollable ? 600 : 300, clientWidth: 300, scrollLeft: 100,
};
let now = 1000;
let blurred = 0;
let forcedBlur = false;
const editorNode = {};
const outsideNode = {};
const activeElement = {blur: () => blurred++};
const range = {collapsed: true, startContainer: options.editorSelection === "editor" ? editorNode : outsideNode,
endContainer: options.editorSelection === "editor" ? editorNode : outsideNode,
cloneRange() { return {...this}; }};
const selection = {rangeCount: options.editorSelection ? 1 : 0, getRangeAt: () => range,
removeAllRanges() { this.rangeCount = 0; }};
const mobile: {touchRange?: typeof range} = {};
const editor = {protyle: {wysiwyg: {element: {contains: (node: unknown) =>
node === editorNode || node === activeElement}}, disabled: false, options: {render: {gutter: false}},
toolbar: {isMultiSelectMode: () => false}}};
const modules: Record<string, unknown> = {
"./sidebar": {
getSidebarElement: () => panel,
getSidebarDock: () => ({}),
popSidebar: (value: string, render = true) => {
panel.style.transform = "translateX(0px)";
actions.push(render ? `open:${value}` : "restore");
},
switchToNextSidebarTab: (value: string) => actions.push(`next:${value}`),
},
"./touchPanelGesture": gestures,
"./touchGesture": {getTouchAxis},
"./mobileBarsConfig": {getMobileSidebarConfig: () => ({sidebarSwipe: !options.disabled})},
"../../protyle/util/hasClosest": {
hasClosestByAttribute: (element: typeof target, key: string, value: string) =>
(key === "id" && element.id === value) || (key === "data-prevent-swipe" && element.preventSwipe) ?
element : undefined,
hasClosestByClassName: (): undefined => undefined,
hasTopClosestByClassName: (): undefined => undefined,
hasClosestBlock: (): undefined => undefined,
},
"./closePanel": {closePanel: () => actions.push("close"), showPanelMask: () => {}},
"./keyboardToolbar": {activeBlur: (force: boolean) => { forcedBlur = force; }, resetAndroidBoundedSelectionGesture: () => {}},
"../../protyle/util/compatibility": {isInHarmony: () => false, isInAndroid: () => false,
isIPhone: () => false, isIOSDevice: () => options.iosDevice === true},
"../../protyle/wysiwyg/nativeTextMenu": {usesNativeTextMenu: () => false},
"../../protyle/util/selection": {},
"../../protyle/wysiwyg/getBlock": {},
"../menu/model": {},
"./blockSelection": {},
"./multiSelectToolbar": {},
"../editor": {getCurrentEditor: () => options.editorSelection ? editor : undefined},
"./touchSelection": {hasVisibleSelectionText: () => false},
"../../protyle/util/inlineElementMarker": {stripSemanticMarkersFromRangeText: () => ""},
"../../constants": {Constants: {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_LONGPRESS: 500, TIMEOUT_MULTIPLE_SELECT: 500}},
};
const touch = loadModule("touch", {
require: createMockRequire(modules),
document: {
activeElement,
querySelector: (selector: string) => selector === ".side-mask" ? mask : {classList: classList("fn__none")},
getElementById: (): null => null,
},
window: {innerWidth: 360, innerHeight: 800, siyuan: {mobile, zIndex: 0}, getSelection: () => selection},
getSelection: () => selection,
getComputedStyle: () => ({overflowX: options.scrollable ? "auto" : "visible"}),
Date: {now: () => now},
});
const event = (x: number, y = 200, count = 1) => ({
target, touches: Array.from({length: count}, () => ({target, clientX: x, clientY: y})),
changedTouches: [{target, clientX: x, clientY: y}],
defaultPrevented: false,
preventDefault() { this.defaultPrevented = true; },
});
return {
actions, panel, target, selection, mobile, range, blurred: () => blurred, forcedBlur: () => forcedBlur,
start: (x = 180, y = 200) => touch.handleTouchStart(event(x, y)),
move: (x: number, y = 200, count = 1) => {
const moveEvent = event(x, y, count);
touch.handleTouchMove(moveEvent);
return moveEvent;
},
end: (x: number, y = 200) => { now += 100; touch.handleTouchEnd(event(x, y)); },
handledMove: (x: number) => touch.handleTouchMove({...event(x), defaultPrevented: true}),
handledEnd: (x: number) => touch.handleTouchEnd({...event(x), defaultPrevented: true}),
cancel: () => touch.handleTouchCancel(),
};
};
for (const side of ["left", "right"] as const) {
const nextX = side === "left" ? 240 : 120;
const closeX = side === "left" ? 120 : 240;
for (const iosDevice of [false, true]) {
test(`${side} sidebar saves and clears the native editor caret with iOS ${iosDevice}`, () => {
const harness = createTouchHarness(side, {closed: true, editorSelection: "editor", iosDevice});
harness.start();
assert.equal(harness.move(185).defaultPrevented, false);
assert.equal(harness.selection.rangeCount, 1);
assert.equal(harness.move(nextX).defaultPrevented, true);
assert.equal(harness.selection.rangeCount, 0);
assert.equal(harness.blurred(), 1);
assert.equal(harness.forcedBlur(), true);
assert.deepEqual(harness.mobile.touchRange, harness.range);
assert.notEqual(harness.mobile.touchRange, harness.range);
harness.move(nextX);
assert.equal(harness.blurred(), 1);
harness.end(nextX);
assert.deepEqual(harness.actions, [`open:${side}`]);
assert.equal(harness.selection.rangeCount, 0);
});
test(`${side} sidebar preserves unrelated selections and vertical gestures with iOS ${iosDevice}`, () => {
const outside = createTouchHarness(side, {closed: true, editorSelection: "outside", iosDevice});
outside.start();
outside.move(nextX);
assert.equal(outside.selection.rangeCount, 1);
assert.equal(outside.mobile.touchRange, undefined);
const vertical = createTouchHarness(side, {closed: true, editorSelection: "editor", iosDevice});
vertical.start();
assert.equal(vertical.move(185, 240).defaultPrevented, false);
assert.equal(vertical.selection.rangeCount, 1);
assert.equal(vertical.blurred(), 0);
});
}
test(`${side} sidebar does not reuse a gesture consumed by editor block selection`, () => {
const harness = createTouchHarness(side, {closed: true});
harness.start();
harness.handledMove(nextX);
harness.handledEnd(nextX);
assert.deepEqual(harness.actions, []);
harness.start();
harness.move(nextX);
harness.end(nextX);
assert.deepEqual(harness.actions, [`open:${side}`]);
});
test(`${side} sidebar ignores the complete gesture inside a protected canvas and resumes outside it`, () => {
const harness = createTouchHarness(side, {closed: true});
harness.start();
harness.move(nextX);
harness.end(nextX);
harness.actions.length = 0;
harness.target.preventSwipe = true;
harness.start();
harness.target.preventSwipe = false;
harness.move(nextX);
harness.end(nextX);
harness.cancel();
assert.deepEqual(harness.actions, []);
harness.target.preventSwipe = true;
harness.start(1, 1);
harness.end(1, 1);
harness.cancel();
assert.deepEqual(harness.actions, [], "a protected tap near the origin cannot reuse previous swipe state");
harness.target.preventSwipe = false;
harness.start();
harness.move(nextX);
harness.end(nextX);
assert.deepEqual(harness.actions, [`open:${side}`]);
});
test(`${side} sidebar restores its selected tab only after a committed swipe`, () => {
const harness = createTouchHarness(side, {closed: true});
harness.start();
harness.move(nextX);
assert.deepEqual(harness.actions, []);
harness.end(nextX);
assert.deepEqual(harness.actions, [`open:${side}`]);
const disabled = createTouchHarness(side, {closed: true, disabled: true});
disabled.start();
disabled.move(nextX);
disabled.end(nextX);
assert.deepEqual(disabled.actions, []);
});
test(`${side} sidebar switches once on release and retains closing gestures`, () => {
const next = createTouchHarness(side, {disabled: true});
next.start();
next.move(nextX);
next.move(nextX);
assert.deepEqual(next.actions, []);
assert.equal(next.panel.style.transform, "translateX(0px)");
next.end(nextX);
assert.deepEqual(next.actions, ["restore", `next:${side}`]);
const close = createTouchHarness(side);
close.start();
close.move(closeX);
assert.notEqual(close.panel.style.transform, "translateX(0px)");
close.end(closeX);
assert.deepEqual(close.actions, ["close"]);
});
test(`${side} sidebar ignores short, vertical, canceled, reversed and multi-touch gestures`, () => {
for (const kind of ["short", "vertical", "cancel", "reverse", "multi"]) {
const harness = createTouchHarness(side);
harness.start();
if (kind === "short") {
harness.move(190);
harness.end(190);
} else if (kind === "vertical") {
harness.move(185, 240);
harness.move(nextX, 250);
harness.end(nextX, 250);
} else {
harness.move(nextX, 200, kind === "multi" ? 2 : 1);
if (kind === "cancel") {
harness.cancel();
} else {
harness.end(kind === "reverse" ? 180 : nextX);
}
}
assert.equal(harness.actions.some(action => action.startsWith("next:") || action === "close"), false, kind);
}
});
test(`${side} sidebar leaves horizontal scrolling to content until the next gesture`, () => {
const harness = createTouchHarness(side, {scrollable: true});
harness.start();
harness.move(nextX);
harness.target.scrollLeft = side === "left" ? 0 : 300;
harness.move(nextX);
harness.end(nextX);
assert.deepEqual(harness.actions, ["restore"]);
harness.start();
harness.move(nextX);
harness.end(nextX);
assert.deepEqual(harness.actions, ["restore", "restore", `next:${side}`]);
});
test(`${side} sidebar closes only on a fresh gesture after content reaches its horizontal edge`, () => {
const harness = createTouchHarness(side, {scrollable: true});
harness.start();
harness.move(closeX);
harness.target.scrollLeft = side === "left" ? 300 : 0;
harness.move(closeX);
harness.end(closeX);
assert.deepEqual(harness.actions, ["restore"]);
harness.start();
harness.move(closeX);
harness.end(closeX);
assert.deepEqual(harness.actions, ["restore", "close"]);
});
}