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: Keep duplicated document paths consistent with titles https://github.com/siyuan-note/siyuan/issues/20103
Daniela636eb7d3ca6
diff --git a/kernel/model/duplicate_doc_hpath_test.go b/kernel/model/duplicate_doc_hpath_test.gonew file mode 100644index 0000000000..0679411a88--- /dev/null+++ b/kernel/model/duplicate_doc_hpath_test.go@@ -0,0 +1,144 @@+//go:build fts5 && (sqlcipher || libsqlcipher)++package model++import (+ "archive/zip"+ "bytes"+ "context"+ "net/url"+ "os"+ "os/exec"+ "path"+ "path/filepath"+ "strings"+ "testing"+ "time"++ "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/sql"+ "github.com/siyuan-note/siyuan/kernel/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestDuplicateDocHPath(t *testing.T) {+ const helper = "SIYUAN_TEST_DUPLICATE_DOC_HPATH"+ if os.Getenv(helper) != "1" {+ // 隔离数据库、密钥和异步任务,使用临时工作空间验证路径寻址。+ 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")+ if output, err := command.CombinedOutput(); err != nil {+ t.Fatalf("duplicate hpath subprocess failed: %v\n%s", err, output)+ }+ return+ }+ prepareHPathRefreshTest(t)+ Conf.Flashcard = conf.NewFlashcard()+ const password = "duplicate-hpath-test-password"+ if err := EnableEncryptedNotebook(password); err != nil {+ t.Fatal(err)+ }+ for _, encrypted := range []bool{false, true} {+ boxID := ast.NewNodeID()+ if encrypted {+ var err error+ boxID, err = CreateEncryptedBox("Encrypted", password)+ if err != nil {+ t.Fatal(err)+ }+ if _, err = Mount(boxID); err != nil {+ t.Fatal(err)+ }+ } else {+ box := &Box{ID: boxID}+ boxConf := conf.NewBoxConf()+ boxConf.Closed = false+ if err := box.SaveConf(boxConf); err != nil {+ t.Fatal(err)+ }+ }+ for _, nested := range []bool{false, true} {+ parentPath, parentHPath := "/", ""+ if nested {+ parent := newHPathTestDoc(t, boxID, "/", "Parent", 1)+ parentPath, parentHPath = parent.Path, parent.HPath+ }+ source := newHPathTestDoc(t, boxID, parentPath, "Example Document", 1)+ // 保存规范化后的源文件,验证重复和导出不会改写原文档或密文。+ source, err := filesys.LoadTree(boxID, source.Path, util.NewLute())+ if err != nil {+ t.Fatal(err)+ }+ sourceFile := filepath.Join(util.DataDir, boxID, source.Path)+ before, err := os.ReadFile(sourceFile)+ if err != nil || util.IsCiphertext(before) != encrypted {+ t.Fatalf("invalid source fixture: encrypted=%v, %v", encrypted, err)+ }+ DuplicateDoc(source)+ title := source.Root.IALAttr("title")+ wantHPath := parentHPath + "/" + title+ if source.HPath != wantHPath || !strings.HasPrefix(title, "Example Document (Duplicated ") {+ t.Fatalf("invalid duplicate path: title=%q, hpath=%q", title, source.HPath)+ }+ hpath, err := GetHPathByID(source.ID)+ if err != nil || hpath != wantHPath {+ t.Fatalf("path by ID disagrees: %q, %v", hpath, err)+ }+ ids, err := GetIDsByHPath(wantHPath, boxID)+ if err != nil || len(ids) != 1 || ids[0] != source.ID {+ t.Fatalf("duplicate is not addressable: %v, %v", ids, err)+ }+ sql.FlushQueue()+ block := sql.GetBlockInBox(source.ID, boxID)+ if block == nil || block.Content != title || block.HPath != wantHPath {+ t.Fatalf("SQL title and path disagree: %#v", block)+ }+ childID, err := CreateWithMarkdown("", boxID, wantHPath+"/Child", "Child content", "", "", false, "", nil)+ if err != nil {+ t.Fatal(err)+ }+ child := treenode.GetBlockTreeInBox(childID, boxID)+ if child == nil || path.Dir(child.Path) != strings.TrimSuffix(source.Path, ".sy") {+ t.Fatalf("child created under a different parent: %#v", child)+ }+ exportURI := ExportSYs([]string{source.ID})+ if exportURI == "" {+ t.Fatal("export returned an empty path")+ }+ exportPath, err := url.PathUnescape(exportURI)+ if err != nil {+ t.Fatal(err)+ }+ exportPath = filepath.Join(util.TempDir, filepath.FromSlash(strings.TrimPrefix(exportPath, "/")))+ if encrypted {+ var ok bool+ _, exportPath, ok = ResolveManagedEncryptedExport(strings.TrimPrefix(exportURI, "/export/"))+ if !ok {+ t.Fatal("encrypted export is not registered")+ }+ }+ archive, err := zip.OpenReader(exportPath)+ if err != nil {+ t.Fatal(err)+ }+ found := false+ for _, file := range archive.File {+ if file.Name == util.FilterFileName(title)+"/"+source.ID+".sy" {+ found = true+ }+ }+ archive.Close()+ if !found {+ t.Fatalf("export folder disagrees with title: %q", title)+ }+ after, err := os.ReadFile(sourceFile)+ if err != nil || !bytes.Equal(before, after) {+ t.Fatalf("source changed: encrypted=%v, nested=%v, %v", encrypted, nested, err)+ }+ }+ }+}diff --git a/kernel/model/reset_tree_test.go b/kernel/model/reset_tree_test.gonew file mode 100644index 0000000000..7c27935486--- /dev/null+++ b/kernel/model/reset_tree_test.go@@ -0,0 +1,63 @@+package model++import (+ "path"+ "strings"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestResetTreeTitleAndHPath(t *testing.T) {+ originalConf := Conf+ const testLang = "reset-tree-test"+ originalLang, hadLang := util.Langs[testLang]+ util.Langs[testLang] = map[int]string{16: "Untitled"}+ Conf = NewAppConf()+ Conf.Lang = testLang+ t.Cleanup(func() {+ Conf = originalConf+ if hadLang {+ util.Langs[testLang] = originalLang+ } else {+ delete(util.Langs, testLang)+ }+ })+ for _, test := range []struct {+ name string+ title string+ suffix string+ }{+ {name: "duplicate", title: "Example", suffix: "Duplicated"},+ {name: "conflict", title: "Example", suffix: "Conflicted"},+ {name: "reset without suffix", title: "Example"},+ {name: "untitled", title: Conf.language(16), suffix: "Duplicated"},+ {name: "preserve title spaces", title: "Example Document ", suffix: "Duplicated"},+ } {+ for _, parent := range []string{"", "/Parent"} {+ t.Run(test.name+parent, func(t *testing.T) {+ docPath := "/20261004000001-doc0001.sy"+ if parent != "" {+ docPath = "/20261004000000-parent1" + docPath+ }+ tree := treenode.NewTree("20261004000000-box0001", docPath, parent+"/"+test.title, test.title)+ resetTree(tree, test.suffix, false)+ title := tree.Root.IALAttr("title")+ if tree.HPath != parent+"/"+title {+ t.Fatalf("title and path disagree: title=%q, hpath=%q", title, tree.HPath)+ }+ if test.suffix == "" || test.title == Conf.language(16) {+ if title != test.title {+ t.Fatalf("unexpected title: %q", title)+ }+ } else if !strings.HasPrefix(title, test.title+" ("+test.suffix+" ") || !strings.HasSuffix(title, ")") {+ t.Fatalf("unexpected suffix: %q", title)+ }+ if path.Dir(tree.Path) != path.Dir(docPath) || path.Base(tree.Path) != tree.ID+".sy" {+ t.Fatalf("unexpected document path: %q", tree.Path)+ }+ })+ }+ }+}diff --git a/kernel/model/tree.go b/kernel/model/tree.goindex b4002d6575..72136b4152 100644--- a/kernel/model/tree.go+++ b/kernel/model/tree.go@@ -65,7 +65,7 @@ func resetTree(tree *parse.Tree, titleSuffix string, removeAvBinding bool) {tree.Root.RemoveIALAttr("scroll")p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy"tree.Path = p- tree.HPath = tree.HPath + " " + titleSuffix+ tree.HPath = tree.HPath + titleSuffix// 重置块 IDblockIDs := map[string]string{oldRootID: tree.ID}