An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Resolve CI fixture failures and queued notebook indexing race
Danielf51458705e6d
diff --git a/app/electron/windowGeometry.test.js b/app/electron/windowGeometry.test.jsindex cb060edd4d..c5360461e5 100644--- a/app/electron/windowGeometry.test.js+++ b/app/electron/windowGeometry.test.js@@ -4,6 +4,7 @@ const {test} = require("node:test");const {readFileSync} = require("node:fs");const {runInNewContext} = require("node:vm");const {captureWindowGeometry, normalizeWindowGeometry, restoreWindowGeometry} = require("./windowGeometry");+const {getTitleBarOverlayOptions} = require("./windowControlsOverlay");const state = (values = {}) => ({version: 1, x: 100, y: 80, width: 900, height: 700,maximized: false, fullscreen: false, ...values});@@ -140,6 +141,7 @@ const openWindow = (windowGeometry, appearance = {}, mode = "local") => {runInNewContext(source.slice(start, end), {ipcMain, initializedWindowIds,BrowserWindow: Window, URL, process: {platform: "win32"},+ getTitleBarOverlayOptions: () => getTitleBarOverlayOptions("win32"),getWindowKernelTarget: () => ({origin: "http://127.0.0.1:6806", mode}),getRemoteSession: () => ({}),windowWorkspaces: {get: () => windows[0], associate: () => {}},diff --git a/app/tests/assetPreview.test.js b/app/tests/assetPreview.test.jsindex 0870f42335..8e7d35021f 100644--- a/app/tests/assetPreview.test.js+++ b/app/tests/assetPreview.test.js@@ -277,9 +277,11 @@ const runCases = async source => {}},"../protyle/util/onGet": {disabledProtyle: () => {}},"../protyle/ui/initUI": {removeLoading: () => {}},- "../util/fetch": {fetchPost: (url, _data, callback) => {- if (url.endsWith("getUnusedAssets")) callback({data: ["note.txt", "other.txt", "audio.mp3", "book.pdf"].map(name =>- ({item: "assets/" + name, path: "assets/" + name}))});+ "../util/fetch": {fetchSyncPost: async (url, data) => {+ check.equal(url, "/api/asset/getUnusedAssets");+ check.deepEqual(data, {all: true});+ return {code: 0, data: ["note.txt", "other.txt", "audio.mp3", "book.pdf"].map(name =>+ ({name, item: "assets/" + name, path: "assets/" + name}))};}},});const root = document.createElement("div");@@ -288,6 +290,7 @@ const runCases = async source => {style.textContent = source.styles[isMobile ? 1 : 0];document.body.append(root);await assetsAPI.mountAssetsTab(root, undefined, {});+ await settle();const item = root.querySelector('[data-tab-type="unrefAssets"]');const panel = root.querySelector(".config-assets__preview");item.dispatchEvent(new MouseEvent(isMobile ? "click" : "mouseover", {bubbles: true}));diff --git a/app/tests/detachedWindowLoading.test.js b/app/tests/detachedWindowLoading.test.jsindex 63d8ec450b..05534cbe70 100644--- a/app/tests/detachedWindowLoading.test.js+++ b/app/tests/detachedWindowLoading.test.js@@ -38,7 +38,9 @@ const runRendererCase = async (sources, expected) => {webFrame: {setZoomFactor() {}}};const functions = {isWindow: () => true, setToolbarLeftMac() {}};const compatibility = {isMac: () => false, applyPlatformClasses() {}};- const controls = load(sources.controls, {electron, "../constants": {Constants: constants}, "../util/functions": functions});+ const overlay = load(sources.overlay, {electron, "../constants": {Constants: constants}});+ const controls = load(sources.controls, {electron, "../constants": {Constants: constants}, "../util/functions": functions,+ "./windowControlsOverlay": overlay});const {initWindow} = load(sources.host, {electron, "../constants": {Constants: constants}, "../util/functions": functions,"../protyle/util/compatibility": compatibility, "./windowControls": controls,@@ -176,7 +178,8 @@ if (process.versions.electron && process.type === "browser") {const ts = require("typescript");for (const [key, file] of Object.entries({assets: "util/assets.ts", appearance: "window/appearance.ts",paint: "util/windowPaint.ts", loading: "boot/loading.ts", init: "window/init.ts",- host: "boot/onGetConfig.ts", controls: "boot/windowControls.ts", tabs: "layout/tabUtil.ts"})) {+ host: "boot/onGetConfig.ts", controls: "boot/windowControls.ts", overlay: "boot/windowControlsOverlay.ts",+ tabs: "layout/tabUtil.ts"})) {const source = require("ifdef-loader/preprocessor").parse(fs.readFileSync(path.join(__dirname, "../src", file), "utf8"), {MOBILE: false, BROWSER: false}, false, true);sources[key] = ts.transpileModule(source, {diff --git a/app/tests/settingsMenuPosition.test.js b/app/tests/settingsMenuPosition.test.jsindex 686374ade8..8e544e9e17 100644--- a/app/tests/settingsMenuPosition.test.js+++ b/app/tests/settingsMenuPosition.test.js@@ -15,7 +15,8 @@ for (const [width, height] of [[1000, 760], [493, 376]]) {const exports = {};runInNewContext(transpileModule(readFileSync(file, "utf8"), {compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2022},- }).outputText, {exports, window, document, require: name => modules[name] || {}});+ }).outputText, {exports, window, document, getComputedStyle: () => ({getPropertyValue: () => ""}),+ require: name => modules[name] || {}});return exports;};const topBar = load("src/layout/getTopBarHeight.ts");diff --git a/app/tests/settingsWindow.test.js b/app/tests/settingsWindow.test.jsindex 33c7ef0b87..596bfcbd1b 100644--- a/app/tests/settingsWindow.test.js+++ b/app/tests/settingsWindow.test.js@@ -27,10 +27,13 @@ const rendererModules = (sources) => {"../constants": {Constants: {TIMEOUT_OPENDIALOG: 0, TIMEOUT_DBLCLICK: 0}},});const context = {isSettingsWindow: () => document.body.classList.contains("body--settings")};- const {ipcRenderer} = require("electron");+ const {ipcRenderer, webFrame} = require("electron");+ const overlay = loadRendererModule(sources.overlay, {+ electron: {ipcRenderer, webFrame}, "../constants": {Constants: {SIYUAN_CMD: "siyuan-cmd"}}, "../layout/util": {},+ });const controls = loadRendererModule(sources.controls, {electron: {ipcRenderer}, "../constants": {Constants: {SIYUAN_CMD: "siyuan-cmd", LOCAL_ZOOM: "zoom"}},- "../util/functions": {setToolbarLeftMac() {}},+ "../util/functions": {setToolbarLeftMac() {}}, "./windowControlsOverlay": overlay,});document.body.classList.toggle("body--windows", process.platform !== "darwin");document.body.classList.toggle("body--win32", process.platform !== "darwin");@@ -291,7 +294,21 @@ const runCases = async (sources) => {assert.notEqual(control.ownerDocument, document);assert.equal(setting.dialog.element.querySelector("input"), control);assert.equal(setting.dialog.element.querySelector(".toolbar #drag").textContent, "Plugin");- if (process.platform !== "darwin") {+ const childDocument = control.ownerDocument;+ const nativeControls = childDocument.documentElement.classList.contains("window-controls-overlay");+ if (nativeControls) {+ assert.ok(setting.dialog.element.querySelector("#windowControls.window-controls-overlay__placeholder"));+ const area = childDocument.defaultView.navigator.windowControlsOverlay.getTitlebarAreaRect();+ const childWindow = childDocument.defaultView;+ const captionWidth = childWindow.innerWidth - area.right;+ assert.ok(captionWidth > 0);+ const points = [{x: (area.left + area.width / 2) / childWindow.innerWidth,+ y: area.height / 2 / childWindow.innerHeight}, ...[1, 3, 5].map(part => ({+ x: (area.right + captionWidth * part / 6) / childWindow.innerWidth,+ y: area.height / 2 / childWindow.innerHeight,+ }))];+ assert.deepEqual(await ipcRenderer.invoke("test-settings-hit-test", points), [2, 8, 9, 20]);+ } else if (process.platform !== "darwin") {assert.ok(setting.dialog.element.querySelector("#minWindow"));assert.ok(setting.dialog.element.querySelector("#maxWindow"));assert.ok(setting.dialog.element.querySelector("#closeWindow"));@@ -303,8 +320,7 @@ const runCases = async (sources) => {}await ipcRenderer.invoke("test-settings-size", 493, 376);await wait();- const childDocument = control.ownerDocument;- if (process.platform === "win32") {+ if (process.platform === "win32" && !nativeControls) {const points = ["#drag", "#minWindow", "#maxWindow", "#closeWindow"].map(selector => {const rect = childDocument.querySelector(selector).getBoundingClientRect();return {x: (rect.left + rect.right) / 2 / childDocument.defaultView.innerWidth,@@ -352,7 +368,7 @@ const runCases = async (sources) => {range.selectNodeContents(toolbar.querySelector("#drag"));const caption = range.getBoundingClientRect();assert.ok(Math.abs((caption.left + caption.right) / 2 - childDocument.defaultView.innerWidth / 2) < 1);- if (process.platform !== "darwin") {+ if (process.platform !== "darwin" && !nativeControls) {for (const state of ["body--maximize", "body--fullscreen"]) {childDocument.body.classList.add(state);for (const id of ["minWindow", "maxWindow", "restoreWindow", "closeWindow"]) {@@ -384,7 +400,8 @@ const runCases = async (sources) => {setting.open("Plugin");await ipcRenderer.invoke("test-settings-wait");- if (process.platform === "darwin") setting.close();+ if (nativeControls) await ipcRenderer.invoke("test-settings-close");+ else if (process.platform === "darwin") setting.close();else setting.dialog.element.querySelector("#closeWindow").click();await wait();assert.equal(confirmed, 1);@@ -438,6 +455,7 @@ if (process.versions.electron && process.type === "browser") {const {app, BrowserWindow, ipcMain, screen} = require("electron");const {createServer} = require("node:http");const {createSettingsWindows} = require("../electron/settingsWindows");+ const {updateTitleBarOverlay} = require("../electron/windowControlsOverlay");app.setPath("userData", process.argv[2]);app.whenReady().then(async () => {let owner;@@ -458,6 +476,7 @@ if (process.versions.electron && process.type === "browser") {"\n.config__panel {border-radius: var(--b3-border-radius-b);}\n"]));for (const [key, file] of Object.entries({dialog: "dialog/index.ts", setting: "plugin/Setting.ts",native: "config/setting/nativeWindow.ts", fit: "config/setting/windowDialog.ts", controls: "boot/windowControls.ts",+ overlay: "boot/windowControlsOverlay.ts",settingInterface: "config/setting/interface.ts",paint: "util/windowPaint.ts", frontend: "util/functions.ts", titles: "util/processTitle.ts",mount: "config/setting/mount.ts", collapse: "config/setting/collapse.ts",@@ -519,6 +538,7 @@ if (process.versions.electron && process.type === "browser") {{origin, mode: "local"} : undefined,getWindows: () => [...(owner ? [owner] : []), ...otherOwners],initialize: win => {+ win.webContents.setBackgroundThrottling(false);win.webContents.userAgent = "SiYuan/fixture Electron " + win.webContents.userAgent;win.webContents.on("console-message", details => {if (details.level === "error" && details.message.startsWith("Uncaught")) {@@ -571,6 +591,10 @@ if (process.versions.electron && process.type === "browser") {return states.every(closed => closed === true);});ipcMain.on("siyuan-cmd", (event, command) => {+ if (command?.cmd === "setTitleBarOverlay") {+ assert.ok(updateTitleBarOverlay(BrowserWindow.fromWebContents(event.sender), command));+ return;+ }if (event.sender === owner.webContents) {assert.equal(command, "show");commands.push("show-owner");@@ -580,6 +604,7 @@ if (process.versions.electron && process.type === "browser") {commands.push(command);});ipcMain.handle("test-settings-commands", () => commands);+ ipcMain.handle("test-settings-close", () => [...children][0].close());ipcMain.handle("test-settings-pointer", (event, type, x, y) => {event.sender.sendInputEvent({type, x, y, button: "left", clickCount: 1});});@@ -619,7 +644,9 @@ if (process.versions.electron && process.type === "browser") {});}try {- owner = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});+ owner = new BrowserWindow({show: false, webPreferences: {+ nodeIntegration: true, contextIsolation: false, offscreen: true, backgroundThrottling: false,+ }});owner.webContents.userAgent = "SiYuan/fixture Electron " + owner.webContents.userAgent;owner.webContents.setWindowOpenHandler(details => {const result = policy(owner.webContents, details);diff --git a/app/tests/textSelectionContextMenu.test.js b/app/tests/textSelectionContextMenu.test.jsindex 1355574f95..3d795138bd 100644--- a/app/tests/textSelectionContextMenu.test.js+++ b/app/tests/textSelectionContextMenu.test.js@@ -101,10 +101,13 @@ const cases = async compiled => {const eventErrors = [];const recordEventError = event => { eventErrors.push(event.error); event.preventDefault(); };window.addEventListener("error", recordEventError);- bind.call({element: {contains: node => root.contains(node), addEventListener: (name, callback) => {+ const addEventListener = root.addEventListener.bind(root);+ root.addEventListener = (name, callback) => {callbacks[name] = callback;- if (name === "pointerdown") root.addEventListener(name, callback);- }}}, protyle);+ if (name === "pointerdown") addEventListener(name, callback);+ };+ bind.call({element: root}, protyle);+ delete root.addEventListener;const pointerdown = (target, pointerType = "mouse") => {target.dispatchEvent(new PointerEvent("pointerdown", {bubbles: true, pointerType, button: 0}));check.deepEqual(eventErrors, [], "pointerdown completes without renderer exceptions");diff --git a/kernel/model/crypto_create_sqlcipher_test.go b/kernel/model/crypto_create_sqlcipher_test.goindex 6a088e3601..de80fa9e5f 100644--- a/kernel/model/crypto_create_sqlcipher_test.go+++ b/kernel/model/crypto_create_sqlcipher_test.go@@ -31,6 +31,7 @@ import ("github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/filesys""github.com/siyuan-note/siyuan/kernel/sql"+ "github.com/siyuan-note/siyuan/kernel/task""github.com/siyuan-note/siyuan/kernel/treenode""github.com/siyuan-note/siyuan/kernel/util")@@ -127,6 +128,12 @@ func testCreateEncryptedBoxInitializesBoxDocument(t *testing.T) {if _, err = Mount(boxID); err != nil {t.Fatalf("mount new encrypted notebook: %v", err)}+ if state := treenode.DocumentMetadataState(boxID); state != "building" {+ t.Fatalf("queued encrypted notebook index state = %q, want building", state)+ }+ for task.ContainIndexTask() {+ task.ExecTaskJob()+ }documentID := ast.NewNodeID()if _, err = CreateDocByParent(DocumentCreateRequest{Notebook: boxID, ID: documentID,TitleFormat: "plain", Title: "Encrypted document", Markdown: "Encrypted notebook creation"}); err != nil {diff --git a/kernel/model/document_create_test.go b/kernel/model/document_create_test.goindex dd8fa44f95..6a9a1bdef2 100644--- a/kernel/model/document_create_test.go+++ b/kernel/model/document_create_test.go@@ -17,6 +17,7 @@ import ("github.com/88250/lute/ast""github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/filesys"+ "github.com/siyuan-note/siyuan/kernel/task""github.com/siyuan-note/siyuan/kernel/treenode""github.com/siyuan-note/siyuan/kernel/util")@@ -48,8 +49,19 @@ func TestDocumentCreateInNewNotebook(t *testing.T) {if _, err = Mount(boxID); err != nil {t.Fatal(err)}+ if state := treenode.DocumentMetadataState(boxID); state != "building" {+ t.Fatalf("queued notebook index state = %q, want building", state)+ }request := DocumentCreateRequest{Notebook: boxID, ID: ast.NewNodeID(), TitleFormat: "plain",Title: "Docker smoke", Markdown: "Docker OCR smoke 123"}+ _, err = CreateDocByParent(request)+ var failure *DocumentCreateFailure+ if !errors.As(err, &failure) || failure.Reason != "indexUnavailable" || failure.CommitState != "none" {+ t.Fatalf("queued index allowed document creation: %#v, %v", failure, err)+ }+ for task.ContainIndexTask() {+ task.ExecTaskJob()+ }document, err := CreateDocByParent(request)if err != nil {t.Fatalf("create document immediately after notebook creation: %v", err)diff --git a/kernel/model/index.go b/kernel/model/index.goindex 14225ad6de..ded97aa2c1 100644--- a/kernel/model/index.go+++ b/kernel/model/index.go@@ -124,6 +124,11 @@ func unindex(boxID string) {}func (box *Box) Index() {+ // 排队时即撤销就绪状态,避免创建请求在后台重建开始前通过检查。+ if err := treenode.SetDocumentMetadataState(box.ID, "building"); err != nil {+ logging.LogErrorf("prepare queued document metadata [%s] failed: %s", box.ID, err)+ return+ }task.AppendTask(task.DatabaseIndexRef, removeBoxRefs, box.ID)task.AppendTask(task.DatabaseIndex, indexBox, box.ID)task.AppendTask(task.DatabaseIndexRef, IndexRefs)diff --git a/kernel/model/mount.go b/kernel/model/mount.goindex 08542c7e85..e3787ed148 100644--- a/kernel/model/mount.go+++ b/kernel/model/mount.go@@ -107,7 +107,7 @@ func createBox(name string, initializeBoxDoc bool) (id string, err error) {if initializeBoxDoc {_, err = ensureBoxDoc0(id)if err == nil {- // 新笔记本已处于打开状态,Mount 会直接返回,须在创建时完成元数据初始化。+ // 新笔记本的源摘要已补齐,后续挂载重建会在排队时撤销就绪状态。err = treenode.SetDocumentMetadataState(id, "ready")}if err != nil {