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: Restore named notebook imports and stabilize regression tests https://github.com/siyuan-note/siyuan/issues/19938
Daniel71edaaa915b6
diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.ymlindex 42bcff4c2b..22c46e8aea 100644--- a/.github/workflows/cd.yml+++ b/.github/workflows/cd.yml@@ -243,6 +243,7 @@ jobs:SIYUAN_OCR_TEST_ASSETS: ${{ github.workspace }}/app/stage/ocr/modelsSIYUAN_OCR_TEST_LIBRARY: ${{ github.workspace }}/app/stage/ocr/runtime/linux-amd64/libonnxruntime.so- name: Test default structured title migration+ id: test_default_structured_title_migrationcontinue-on-error: truerun: go test -tags "fts5 sqlcipher" ./documentaddressing ./filesys ./model ./api ./cli/cmd -run '^TestStructuredTitles' -count=1working-directory: kernel@@ -329,6 +330,7 @@ jobs:CC: gcc- name: Test Windows default structured title migration+ id: test_windows_default_structured_title_migrationcontinue-on-error: trueshell: msys2 {0}run: go test -tags "fts5 sqlcipher" ./documentaddressing ./filesys ./model ./api ./cli/cmd -run '^TestStructuredTitles' -count=1diff --git a/app/src/layout/dock/agent/AgentComposerResize.test.ts b/app/src/layout/dock/agent/AgentComposerResize.test.tsindex 36adafcbb6..fe13fcbcd4 100644--- a/app/src/layout/dock/agent/AgentComposerResize.test.ts+++ b/app/src/layout/dock/agent/AgentComposerResize.test.ts@@ -24,6 +24,9 @@ const cases = async (source: string, css: string, mobile: boolean) => {'<div class="protyle-content"><div class="protyle-wysiwyg" spellcheck="false"></div></div></div>' +'<div class="agent-chat__buttons"><button class="b3-button">Send</button></div></div></div>';const panel = document.querySelector<HTMLElement>(".sy__agentChat");+ // 原生窗口可能受运行器屏幕尺寸限制,面板必须完全位于实际视口内。+ const panelHeight = Math.min(760, window.innerHeight);+ panel.style.height = `${panelHeight}px`;const composer = panel.querySelector<HTMLElement>(".agent-chat__composer-host");const content = composer.querySelector<HTMLElement>(".protyle-content");const editor = composer.querySelector<HTMLElement>(".protyle-wysiwyg");@@ -85,7 +88,7 @@ const cases = async (source: string, css: string, mobile: boolean) => {key("ArrowDown");check.equal(composer.style.maxHeight, "240px");key("End");- check.equal(composer.style.maxHeight, "600px", `panel ${panel.clientHeight} ${getComputedStyle(panel).display} ${getComputedStyle(panel).minHeight}`);+ check.equal(composer.style.maxHeight, `${panelHeight - 160}px`, "End uses the available panel height");await tick();check.ok(panel.querySelector(".agent-chat__buttons").getBoundingClientRect().bottom <= panel.getBoundingClientRect().bottom);check.ok(panel.querySelector(".agent-chat__messages-wrap").clientHeight > 30);@@ -182,7 +185,7 @@ test("composer grows, resizes and preserves focus, handle visibility and scrollapp.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});app.whenReady().then(async () => {const win = new BrowserWindow({show: false, width: 1000, height: 900,- webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true, backgroundThrottling: false}});win.webContents.debugger.attach("1.3");ipcMain.handle("composer-input", (_event, method, params) => win.webContents.debugger.sendCommand(method, params));ipcMain.handle("composer-drag", async (_event, {x, y, dy, touch, escape}) => {diff --git a/app/src/layout/dock/backlinkDatabaseFilterPosition.test.ts b/app/src/layout/dock/backlinkDatabaseFilterPosition.test.tsindex 7f9acef03c..9f64e58a7e 100644--- a/app/src/layout/dock/backlinkDatabaseFilterPosition.test.ts+++ b/app/src/layout/dock/backlinkDatabaseFilterPosition.test.ts@@ -23,6 +23,8 @@ const browserCases = async (sources: string[], css: string) => {toolbar.innerHTML = '<button style="position:fixed;left:1420px;top:430px;width:24px;height:24px">Filter</button>';document.body.append(toolbar);const target = toolbar.firstElementChild as HTMLElement;+ // 保留右侧空间,避免运行器屏幕限制原生窗口后把锚点放在视口外。+ target.style.left = `${window.innerWidth - 380}px`;const originalRect = target.getBoundingClientRect();type Selection = {avID: string, filters: IAVFilter[]};let finishFields: () => void;@@ -122,7 +124,8 @@ test("backlink database filters stay anchored during source refresh, field loadiapp.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});app.commandLine.appendSwitch("disable-gpu");app.whenReady().then(async () => {- const win = new BrowserWindow({show: false, width: 1800, height: 760, webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});+ const win = new BrowserWindow({show: false, width: 1800, height: 760,+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true, backgroundThrottling: false}});try {await win.loadURL("data:text/html,<html><body></body></html>");console.log(await win.webContents.executeJavaScript(${JSON.stringify(expression)}));diff --git a/app/tests/cellInputPosition.test.js b/app/tests/cellInputPosition.test.jsindex f575791f93..12cef0ac28 100644--- a/app/tests/cellInputPosition.test.js+++ b/app/tests/cellInputPosition.test.js@@ -2,11 +2,11 @@ const assert = require("node:assert/strict");const fs = require("node:fs");const os = require("node:os");const path = require("node:path");+const {compileAnchoredInputRuntime} = require("./fixtures/anchoredInputRuntime.cjs");const runCases = async (source, css) => {const assert = require("node:assert/strict");- const exports = {};- new Function("exports", source)(exports);+ const exports = new Function("return " + source)();const style = document.createElement("style");style.textContent = css;document.head.append(style);@@ -86,10 +86,7 @@ if (process.versions.electron && process.type === "browser") {let code = 0;try {await win.loadURL("data:text/html,<html><body></body></html>");- const ts = require("typescript");- const source = ts.transpileModule(fs.readFileSync(path.join(__dirname, "../src/dialog/anchoredInputPosition.ts"), "utf8"), {- compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},- }).outputText;+ const source = compileAnchoredInputRuntime("./anchoredInputPosition");const css = require("sass").compile(path.join(__dirname, "../src/assets/scss/component/_text-field.scss")).css;await win.webContents.executeJavaScript(`(${runCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(css)})`);} catch (error) {diff --git a/app/tests/detachedWindowLoading.test.js b/app/tests/detachedWindowLoading.test.jsindex 6dc39e8efd..63d8ec450b 100644--- a/app/tests/detachedWindowLoading.test.js+++ b/app/tests/detachedWindowLoading.test.js@@ -219,13 +219,17 @@ if (process.versions.electron && process.type === "browser") {{mode: "dark", hex: "#1e1e1e", background: "rgb(30, 30, 30)", gate: "state", document: "timeout"},]) {win = new BrowserWindow({show: false, backgroundColor: expected.hex, width: 640, height: 480,- webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true, backgroundThrottling: false}});+ win.webContents.debugger.attach("1.3");assert.equal(win.getBackgroundColor(), expected.hex.toUpperCase());const url = new URL(`http://127.0.0.1:${server.address().port}/stage/build/app/window.html`);url.searchParams.set("windowThemeMode", expected.mode);url.searchParams.set("windowBackground", expected.hex);url.searchParams.set("windowForeground", "#abcdef");await win.loadURL(url.href);+ await win.webContents.debugger.sendCommand("Emulation.setEmulatedMedia", {+ features: [{name: "prefers-reduced-motion", value: "no-preference"}],+ });await win.webContents.executeJavaScript(`(${runRendererCase.toString()})(${JSON.stringify(sources)}, ${JSON.stringify(expected)})`);const first = await win.webContents.capturePage();const bitmap = first.toBitmap();diff --git a/app/tests/fixtures/anchoredInputRuntime.cjs b/app/tests/fixtures/anchoredInputRuntime.cjsindex f254d96870..a501312885 100644--- a/app/tests/fixtures/anchoredInputRuntime.cjs+++ b/app/tests/fixtures/anchoredInputRuntime.cjs@@ -2,9 +2,14 @@ const {readFileSync} = require("node:fs");const path = require("node:path");const ts = require("typescript");-const compileAnchoredInputRuntime = () => {- const sources = Object.fromEntries(["anchoredInput", "anchoredInputPosition"].map(name => ["./" + name,- ts.transpileModule(readFileSync(path.join(__dirname, "../../src/dialog", name + ".ts"), "utf8"), {+const compileAnchoredInputRuntime = (entry = "./anchoredInput") => {+ const sources = Object.fromEntries([+ ["./anchoredInput", "dialog/anchoredInput"],+ ["./anchoredInputPosition", "dialog/anchoredInputPosition"],+ ["../util/functions", "util/functions"],+ ["./hostCapabilities", "util/hostCapabilities"],+ ].map(([name, file]) => [name,+ ts.transpileModule(readFileSync(path.join(__dirname, "../../src", file + ".ts"), "utf8"), {compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},}).outputText]));// 保留输入层及定位实现,只隔离系统键盘桥;未知依赖立即报错。@@ -19,7 +24,7 @@ const compileAnchoredInputRuntime = () => {new Function("require", "exports", sources[name])(load, exports);return exports;};- return load("./anchoredInput");+ return load(${JSON.stringify(entry)});})()`;};diff --git a/app/tests/loadingIndicator.test.js b/app/tests/loadingIndicator.test.jsindex aea2735c25..0961d09d2b 100644--- a/app/tests/loadingIndicator.test.js+++ b/app/tests/loadingIndicator.test.js@@ -179,6 +179,9 @@ if (process.versions.electron && process.type === "browser") {fs.writeFileSync(path.join(process.env.SIYUAN_LOADING_SCREENSHOTS, `${name}.png`), (await win.webContents.capturePage()).toPNG());});await win.loadURL("about:blank");+ await win.webContents.debugger.sendCommand("Emulation.setEmulatedMedia", {+ features: [{name: "prefers-reduced-motion", value: "no-preference"}],+ });await win.webContents.executeJavaScript(`(${browserCases.toString()})(${JSON.stringify(sources)}, ${JSON.stringify(entries)}, ${JSON.stringify(themes)})`);console.log("Verified loading themes, customization, accessibility, editor timing, mobile refresh and marketplace failure");win.destroy();diff --git a/kernel/model/document_addressing.go b/kernel/model/document_addressing.goindex 94cb5a4e37..7fa26ef379 100644--- a/kernel/model/document_addressing.go+++ b/kernel/model/document_addressing.go@@ -186,6 +186,22 @@ func checkImportedDocumentAddressing(directory string) error {}boxID = meta.BoxDocID}+ if boxID == "" {+ // 按名称导出的笔记本可能没有笔记本文档;从寻址记录取得身份后仍校验完整源记录。+ data, err = filelock.ReadFile(filepath.Join(directory, documentaddressing.FloorPath))+ if err != nil && !errors.Is(err, os.ErrNotExist) {+ return err+ }+ if err == nil {+ var record struct {+ Notebook string `json:"notebook"`+ }+ if json.Unmarshal(data, &record) != nil || !ast.IsNodeIDPattern(record.Notebook) {+ return documentaddressing.ErrInvalid+ }+ boxID = record.Notebook+ }+ }return documentaddressing.CheckImport(util.DataDir, util.ConfDir, boxID, directory)}diff --git a/kernel/model/document_addressing_test.go b/kernel/model/document_addressing_test.goindex 0fa694a5af..dde1d0ea86 100644--- a/kernel/model/document_addressing_test.go+++ b/kernel/model/document_addressing_test.go@@ -7,6 +7,7 @@ import ("os""path/filepath""reflect"+ "strings""testing""time"@@ -447,6 +448,54 @@ func TestDocumentAddressingImportRemapsOnlyWholeNotebookFloor(t *testing.T) {}}+func TestDocumentAddressingImportFloorIdentity(t *testing.T) {+ oldData, oldConfDir := util.DataDir, util.ConfDir+ util.DataDir, util.ConfDir = t.TempDir(), t.TempDir()+ t.Cleanup(func() { util.DataDir, util.ConfDir = oldData, oldConfDir })+ const sourceID, otherID = "20260101120000-abcdefg", "20260101120001-hijklmn"+ valid := `{"version":1,"notebook":"` + sourceID + `","minimumMode":2}`+ for _, test := range []struct {+ name, directory, boxDocID, floor string+ wantErr error+ }{+ {name: "named export", directory: "Named notebook", floor: valid},+ {name: "ID directory", directory: sourceID, floor: valid},+ {name: "directory identity conflict", directory: otherID, floor: valid, wantErr: documentaddressing.ErrInvalid},+ {name: "box document identity conflict", directory: "Named notebook", boxDocID: otherID, floor: valid, wantErr: documentaddressing.ErrInvalid},+ {name: "invalid JSON", directory: "Named notebook", floor: `{`, wantErr: documentaddressing.ErrInvalid},+ {name: "invalid identity", directory: "Named notebook", floor: strings.ReplaceAll(valid, sourceID, "invalid"), wantErr: documentaddressing.ErrInvalid},+ {name: "unknown format", directory: "Named notebook", floor: strings.Replace(valid, `"version":1`, `"version":2`, 1), wantErr: documentaddressing.ErrInvalid},+ {name: "unsupported mode", directory: "Named notebook", floor: strings.Replace(valid, `"minimumMode":2`, `"minimumMode":3`, 1), wantErr: documentaddressing.ErrUnsupported},+ {name: "duplicate identity", directory: "Named notebook", floor: strings.Replace(valid, `"version":1`, `"version":1,"notebook":"`+otherID+`"`, 1), wantErr: documentaddressing.ErrInvalid},+ } {+ t.Run(test.name, func(t *testing.T) {+ root := filepath.Join(t.TempDir(), test.directory)+ if err := os.MkdirAll(filepath.Join(root, ".siyuan"), 0755); err != nil {+ t.Fatal(err)+ }+ floorPath := filepath.Join(root, documentaddressing.FloorPath)+ if err := os.WriteFile(floorPath, []byte(test.floor), 0644); err != nil {+ t.Fatal(err)+ }+ if test.boxDocID != "" {+ data, err := json.Marshal(boxDocMeta{Spec: boxDocMetaSpec, BoxDocID: test.boxDocID})+ if err != nil {+ t.Fatal(err)+ }+ if err = os.WriteFile(filepath.Join(root, ".siyuan", boxDocMetaName), data, 0644); err != nil {+ t.Fatal(err)+ }+ }+ if err := checkImportedDocumentAddressing(root); !errors.Is(err, test.wantErr) {+ t.Fatalf("import identity: got %v, want %v", err, test.wantErr)+ }+ if data, err := os.ReadFile(floorPath); err != nil || string(data) != test.floor {+ t.Fatalf("source metadata changed: %q, %v", data, err)+ }+ })+ }+}+func TestDocumentAddressingDataImportRejectsBeforeAnyWrite(t *testing.T) {fixture := setupFileOperationTest(t)oldConfDir := util.ConfDir@@ -502,9 +551,25 @@ func TestDocumentAddressingNativeExportPreservesFloor(t *testing.T) {if err := os.WriteFile(name, source, 0644); err != nil {t.Fatal(err)}- archive := openExportArchive(t, ExportNotebookSY(boxID))- data := readArchiveFile(t, archive, boxID+"/"+documentaddressing.FloorPath)- if data != string(source) {- t.Fatal("export lost capability floor")+ for _, name := range []string{boxID, "Named-notebook"} {+ box := &Box{ID: boxID}+ boxConf := box.GetConf()+ boxConf.Name = name+ if err := box.SaveConf(boxConf); err != nil {+ t.Fatal(err)+ }+ exported := ExportNotebookSY(boxID)+ archive := openExportArchive(t, exported)+ data := readArchiveFile(t, archive, name+"/"+documentaddressing.FloorPath)+ if data != string(source) {+ t.Fatal("export lost capability floor")+ }+ archivePath, err := exportedFilePath(exported)+ if err != nil {+ t.Fatal(err)+ }+ if _, err = inspectSYNotebookArchive(archivePath); err != nil {+ t.Fatalf("exported notebook %q cannot be imported: %v", name, err)+ }}}diff --git a/kernel/model/duplicate_doc_hpath_test.go b/kernel/model/duplicate_doc_hpath_test.goindex 4bb11b0a60..98930ce5a5 100644--- a/kernel/model/duplicate_doc_hpath_test.go+++ b/kernel/model/duplicate_doc_hpath_test.go@@ -25,18 +25,20 @@ import (func TestDuplicateDocHPath(t *testing.T) {const helper = "SIYUAN_TEST_DUPLICATE_DOC_HPATH"- if os.Getenv(helper) != "1" {+ workspace := os.Getenv(helper)+ if workspace == "" {// 隔离数据库、密钥和异步任务,使用临时工作空间验证路径寻址。ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)defer cancel()command := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestDuplicateDocHPath$", "-test.v")- command.Env = append(os.Environ(), helper+"=1")+ // 子进程退出后由父测试删除工作空间,避免临时目录清理与后台写入竞争。+ command.Env = append(os.Environ(), helper+"="+t.TempDir())if output, err := command.CombinedOutput(); err != nil {t.Fatalf("duplicate hpath subprocess failed: %v\n%s", err, output)}return}- prepareHPathRefreshTest(t)+ prepareHPathRefreshTestAt(t, workspace)Conf.Flashcard.Store(conf.NewFlashcard())const password = "duplicate-hpath-test-password"if err := EnableEncryptedNotebook(password); err != nil {diff --git a/kernel/model/hpath_refresh_test.go b/kernel/model/hpath_refresh_test.goindex feed8a2b5b..61da98eed0 100644--- a/kernel/model/hpath_refresh_test.go+++ b/kernel/model/hpath_refresh_test.go@@ -299,7 +299,12 @@ func TestDocumentHPathRefresh(t *testing.T) {}func prepareHPathRefreshTest(t *testing.T) {- root := t.TempDir()+ t.Helper()+ prepareHPathRefreshTestAt(t, t.TempDir())+}++func prepareHPathRefreshTestAt(t *testing.T, root string) {+ t.Helper()util.WorkspaceDir = rootutil.DataDir, util.TempDir, util.ConfDir = filepath.Join(root, "data"), filepath.Join(root, "temp"), filepath.Join(root, "conf")util.HistoryDir, util.QueueDir = filepath.Join(root, "history"), filepath.Join(util.TempDir, "queue")diff --git a/kernel/model/notebook_bundle_fts5_test.go b/kernel/model/notebook_bundle_fts5_test.gonew file mode 100644index 0000000000..704e9fe874--- /dev/null+++ b/kernel/model/notebook_bundle_fts5_test.go@@ -0,0 +1,115 @@+//go:build fts5++package model++import (+ "bytes"+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/88250/lute/ast"+ "github.com/siyuan-note/siyuan/kernel/documentaddressing"+ "github.com/siyuan-note/siyuan/kernel/filesys"+ "github.com/siyuan-note/siyuan/kernel/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestImportNotebooksSYWithAddressingFloor(t *testing.T) {+ const sourceBox, targetBox = "20261024000000-abcdefg", "20261024000001-hijklmn"+ const sourceDoc, targetDoc = "20261024000002-opqrstu", "20261024000003-vwxyzab"+ setupExportRelatedTest(t, sourceBox, targetBox)+ setupNotebookDocumentImportDatabase(t)+ original := map[string][]byte{}+ for index, boxID := range []string{sourceBox, targetBox} {+ box := &Box{ID: boxID}+ boxConf := box.GetConf()+ boxConf.Name = fmt.Sprintf("Named Notebook %d", index)+ if err := box.SaveConf(boxConf); err != nil {+ t.Fatal(err)+ }+ floor := []byte(`{"version":1,"notebook":"` + boxID + `","minimumMode":2}`)+ if err := os.WriteFile(filepath.Join(util.DataDir, boxID, documentaddressing.FloorPath), floor, 0644); err != nil {+ t.Fatal(err)+ }+ id, title := sourceDoc, "Source"+ if index == 1 {+ id, title = targetDoc, "Target"+ }+ tree := treenode.NewTree(boxID, "/"+id+".sy", "/"+title, title)+ if index == 0 {+ paragraph := treenode.NewParagraph(ast.NewNodeID())+ paragraph.AppendChild(&ast.Node{Type: ast.NodeTextMark, TextMarkType: "block-ref",+ TextMarkBlockRefID: targetDoc, TextMarkBlockRefSubtype: "d", TextMarkTextContent: "Target"})+ tree.Root.AppendChild(paragraph)+ }+ writeExportRelatedTestTree(t, tree)+ name := filepath.Join(util.DataDir, boxID, id+".sy")+ data, err := os.ReadFile(name)+ if err != nil {+ t.Fatal(err)+ }+ original[name] = data+ if _, err = os.Stat(boxDocMetaPath(boxID)); !os.IsNotExist(err) {+ t.Fatal("fixture must not depend on notebook document metadata", err)+ }+ }+ archive, err := exportedFilePath(ExportNotebooksSY([]string{sourceBox, targetBox}))+ if err != nil {+ t.Fatal(err)+ }+ imported, bundle, err := ImportSYNotebookBundle(archive)+ if err != nil || !bundle || len(imported) != 2 {+ t.Fatalf("notebook bundle round trip failed: %v, %v, %v", imported, bundle, err)+ }+ roots := map[string]*ast.Node{}+ for _, boxID := range imported {+ if boxID == sourceBox || boxID == targetBox {+ t.Fatal("import reused a source notebook identity")+ }+ root := filepath.Join(util.DataDir, boxID)+ if err = documentaddressing.CheckSource(boxID, root); err != nil {+ t.Fatal("imported addressing floor does not match the new notebook", err)+ }+ entries, readErr := os.ReadDir(root)+ if readErr != nil {+ t.Fatal(readErr)+ }+ for _, entry := range entries {+ if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sy") {+ continue+ }+ tree, loadErr := filesys.LoadTree(boxID, "/"+entry.Name(), util.NewLute())+ if loadErr != nil {+ t.Fatal(loadErr)+ }+ if tree.ID == sourceDoc || tree.ID == targetDoc {+ t.Fatal("import reused a source document identity")+ }+ roots[treenode.ReadDocumentTitle(tree.Root).Text] = tree.Root+ }+ }+ if len(roots) != 2 || roots["Source"] == nil || roots["Target"] == nil {+ t.Fatalf("imported documents are missing: %v", roots)+ }+ references := 0+ ast.Walk(roots["Source"], func(node *ast.Node, entering bool) ast.WalkStatus {+ if entering && node.Type == ast.NodeTextMark && node.TextMarkType == "block-ref" {+ references+++ if node.TextMarkBlockRefID != roots["Target"].ID {+ t.Errorf("cross-notebook reference was not remapped: %s", node.TextMarkBlockRefID)+ }+ }+ return ast.WalkContinue+ })+ if references != 1 {+ t.Fatalf("got %d cross-notebook references, want 1", references)+ }+ for name, before := range original {+ if after, readErr := os.ReadFile(name); readErr != nil || !bytes.Equal(after, before) {+ t.Fatal("round trip changed the source document", readErr)+ }+ }+}