Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:recycle: Resolve configuration section defaults in their constructors

Jeffrey Chen0db91daff2e9
diff --git a/kernel/api/box_lease_test.go b/kernel/api/box_lease_test.go
index e7477bb5a0..9f244f363c 100644
--- a/kernel/api/box_lease_test.go
+++ b/kernel/api/box_lease_test.go
@@ -225,7 +225,7 @@ func testNotebookResponseLease(t *testing.T, explicitNotebook, batch bool) {
model.Conf.Lang = "en"
util.TimeLangs = map[string]map[string]any{"en": language.Time}
}
- *model.Conf.FileTree.Load().BoxDocEnabled = true
+ model.Conf.FileTree.Load().BoxDocEnabled = true
sql.InitDatabase(true)
sql.InitHistoryDatabase(true)
sql.InitAssetContentDatabase(true)
diff --git a/kernel/api/contract_export_lease_test.go b/kernel/api/contract_export_lease_test.go
index 1d2246dfec..90ce0c16e4 100644
--- a/kernel/api/contract_export_lease_test.go
+++ b/kernel/api/contract_export_lease_test.go
@@ -69,7 +69,7 @@ func TestAPIContractExportNotebookResponseLease(t *testing.T) {
model.Conf.Search.Store(confSectionValue2)
}
model.Conf.Appearance.Store(conf.NewAppearance())
- *model.Conf.FileTree.Load().BoxDocEnabled = true
+ model.Conf.FileTree.Load().BoxDocEnabled = true
sql.InitDatabase(true)
sql.InitHistoryDatabase(true)
sql.InitAssetContentDatabase(true)
diff --git a/kernel/api/notebook_test.go b/kernel/api/notebook_test.go
index f58a07945f..e6f9b835ea 100644
--- a/kernel/api/notebook_test.go
+++ b/kernel/api/notebook_test.go
@@ -43,7 +43,7 @@ func TestListNotebooksSortsBySubDocCount(t *testing.T) {
model.Conf.Sync.Store(conf.NewSync())
model.Conf.FileTree.Store(conf.NewFileTree())
model.Conf.NotebookCrypto = conf.NewNotebookCrypto()
- *model.Conf.FileTree.Load().BoxDocEnabled = true
+ model.Conf.FileTree.Load().BoxDocEnabled = true
t.Cleanup(func() {
if err := model.SetPublishAccess(oldPublishAccess); err != nil {
t.Errorf("restore publish access failed: %v", err)
diff --git a/kernel/api/setting_contract.go b/kernel/api/setting_contract.go
index e9d9a4173c..0490f274e9 100644
--- a/kernel/api/setting_contract.go
+++ b/kernel/api/setting_contract.go
@@ -509,8 +509,10 @@ func settingFileTreePayload(value *conf.FileTree) *apicontract.SettingFileTree {
result.CloseTabOnDoubleClick = value.CloseTabOnDoubleClick
result.DocIconClickMode = value.DocIconClickMode
result.ParentDocTitleClickMode = value.ParentDocTitleClickMode
- result.BoxDocEnabled = value.BoxDocEnabled
- result.UseSVGDefaultIcon = value.UseSVGDefaultIcon
+ boxDocEnabled := value.BoxDocEnabled
+ result.BoxDocEnabled = &boxDocEnabled
+ useSVGDefaultIcon := value.UseSVGDefaultIcon
+ result.UseSVGDefaultIcon = &useSVGDefaultIcon
result.RefCreateSaveBox = value.RefCreateSaveBox
result.RefCreateSavePath = value.RefCreateSavePath
result.DocCreateSaveBox = value.DocCreateSaveBox
diff --git a/kernel/conf/ai.go b/kernel/conf/ai.go
index 9bc621700d..3d7b9f162f 100644
--- a/kernel/conf/ai.go
+++ b/kernel/conf/ai.go
@@ -238,34 +238,6 @@ func (decision *Decision) Normalize() {
decision.Endpoint, decision.APIKey, decision.Name, decision.Timeout = "", "", "", 0
}
-// MergePrevious 合并旧客户端输入或单个供应商更新,保留其他配置和活动供应商。
-func (decision *Decision) MergePrevious(previous *Decision) {
- if previous == nil {
- return
- }
- old := previous.Clone()
- old.Normalize()
- if decision.Profiles == nil {
- old.Enabled = decision.Enabled
- if decision.Provider != "" {
- old.Provider = decision.Provider
- }
- if decision.Endpoint != "" || decision.APIKey != "" || decision.Name != "" || decision.Timeout != 0 {
- old.Profiles["typesafe"] = &DecisionProfile{decision.Endpoint, decision.APIKey, decision.Name, decision.Timeout}
- }
- *decision = *old
- return
- }
- if strings.TrimSpace(decision.Provider) == "" {
- decision.Provider = old.Provider
- }
- for provider, profile := range old.Profiles {
- if _, exists := decision.Profiles[provider]; !exists {
- decision.Profiles[provider] = profile
- }
- }
-}
-
func (decision *Decision) Clone() *Decision {
if decision == nil {
return nil
diff --git a/kernel/conf/ai_decision_test.go b/kernel/conf/ai_decision_test.go
index 72f4436f1c..cdd002ed2e 100644
--- a/kernel/conf/ai_decision_test.go
+++ b/kernel/conf/ai_decision_test.go
@@ -39,24 +39,3 @@ func TestAIDecisionConfiguration(t *testing.T) {
t.Fatal("unknown provider silently accepted")
}
}
-
-func TestAIDecisionMergeLegacy(t *testing.T) {
- previous := defaultDecision()
- previous.Provider = "openai"
- previous.Profiles["openai"].APIKey = "openai-key"
- legacy := &Decision{Enabled: true, Endpoint: "https://legacy.example", APIKey: "typesafe-key", Name: "legacy", Timeout: 45}
- legacy.MergePrevious(previous)
- legacy.Normalize()
- if legacy.Provider != "openai" || legacy.Profiles["openai"].APIKey != "openai-key" || legacy.Profiles["typesafe"].APIKey != "typesafe-key" {
- t.Fatal("legacy save lost active provider or credentials")
- }
- legacy.Profiles["openai"].APIKey = ""
- if previous.Profiles["openai"].APIKey != "openai-key" {
- t.Fatal("merge aliases previous config")
- }
- legacy.APIKey = "stale-flat-key"
- legacy.Normalize()
- if legacy.Profiles["typesafe"].APIKey != "typesafe-key" {
- t.Fatal("stale flat config replaced profile")
- }
-}
diff --git a/kernel/conf/appearance.go b/kernel/conf/appearance.go
index fd43c7816a..67a07f793e 100644
--- a/kernel/conf/appearance.go
+++ b/kernel/conf/appearance.go
@@ -20,7 +20,7 @@ import "github.com/siyuan-note/siyuan/kernel/util"
type Appearance struct {
CompactMode bool `json:"compactMode"` // 桌面端紧凑模式
- MenuMouseScroll bool `json:"menuMouseScroll"` // 桌面端菜单鼠标滚动
+ MenuMouseScroll bool `json:"menuMouseScroll"` // 桌面端菜单鼠标滚动,新工作空间默认启用
PDFTheme *PDFTheme `json:"pdfTheme"` // PDF 阅读主题与标注颜色
OutlineKeepCurrentExpand bool `json:"outlineKeepCurrentExpand"` // 大纲保持当前标题展开
MobileBars *MobileBars `json:"mobileBars"` // 移动端工具栏与侧栏访问偏好
@@ -86,6 +86,7 @@ func NewAppearance() *Appearance {
// NewAppearanceForContainer 使用显式容器参数选择状态栏默认项。
func NewAppearanceForContainer(mobile bool) *Appearance {
return &Appearance{
+ MenuMouseScroll: true,
PDFTheme: NewPDFTheme(),
MobileBars: NewMobileBars(),
GlobalFontFamilies: []*EditorFont{},
diff --git a/kernel/conf/filetree.go b/kernel/conf/filetree.go
index 6894f329af..4de5f9e0ea 100644
--- a/kernel/conf/filetree.go
+++ b/kernel/conf/filetree.go
@@ -27,8 +27,8 @@ type FileTree struct {
CloseTabOnDoubleClick bool `json:"closeTabOnDoubleClick"` // 是否使用双击关闭页签
DocIconClickMode int `json:"docIconClickMode"` // 单击文档图标或笔记本图标的行为:0 修改图标,1 展开或折叠子文档
ParentDocTitleClickMode int `json:"parentDocTitleClickMode"` // 点击父文档标题的行为:0 打开文档,1 展开或折叠子文档,2 单击展开或折叠子文档、双击打开父文档
- BoxDocEnabled *bool `json:"boxDocEnabled"` // 是否启用顶层笔记本文档
- UseSVGDefaultIcon *bool `json:"useSVGDefaultIcon"` // 是否使用 SVG 默认图标
+ BoxDocEnabled bool `json:"boxDocEnabled"` // 是否启用顶层笔记本文档
+ UseSVGDefaultIcon bool `json:"useSVGDefaultIcon"` // 是否使用 SVG 默认图标
RefCreateSaveBox string `json:"refCreateSaveBox"` // 块引时新建文档存储笔记本
RefCreateSavePath string `json:"refCreateSavePath"` // 块引时新建文档存储路径
DocCreateSaveBox string `json:"docCreateSaveBox"` // 新建文档存储笔记本
@@ -58,8 +58,8 @@ func NewFileTree() *FileTree {
CloseTabOnDoubleClick: false,
DocIconClickMode: 0,
ParentDocTitleClickMode: 0,
- BoxDocEnabled: new(bool),
- UseSVGDefaultIcon: new(true),
+ BoxDocEnabled: false,
+ UseSVGDefaultIcon: true,
Sort: util.SortModeCustom,
MaxListCount: 512,
MaxOpenTabCount: 8,
diff --git a/kernel/conf/filetree_test.go b/kernel/conf/filetree_test.go
index d352d201c2..66d9ccd903 100644
--- a/kernel/conf/filetree_test.go
+++ b/kernel/conf/filetree_test.go
@@ -29,16 +29,10 @@ func TestNewFileTreeDefaults(t *testing.T) {
if 0 != fileTree.ParentDocTitleClickMode {
t.Fatal("clicking a parent document title should open the document by default")
}
- if nil == fileTree.BoxDocEnabled {
- t.Fatal("box document setting should be initialized")
- }
- if *fileTree.BoxDocEnabled {
+ if fileTree.BoxDocEnabled {
t.Fatal("box documents should be disabled for new users")
}
- if nil == fileTree.UseSVGDefaultIcon {
- t.Fatal("default icon setting should be initialized")
- }
- if !*fileTree.UseSVGDefaultIcon {
+ if !fileTree.UseSVGDefaultIcon {
t.Fatal("SVG default icons should be enabled for new users")
}
}
diff --git a/kernel/conf/preferences_test.go b/kernel/conf/preferences_test.go
index e6cf08d200..d800083a71 100644
--- a/kernel/conf/preferences_test.go
+++ b/kernel/conf/preferences_test.go
@@ -44,4 +44,8 @@ func TestPreferenceDefaultsAndNormalization(t *testing.T) {
if NewAI().Agent.StreamingMarkdown || NormalizeAI(&AI{}).Agent.StreamingMarkdown {
t.Fatal("streaming Markdown must default to disabled")
}
+ // 新工作空间默认启用菜单鼠标滚动,已有工作空间缺少该字段时保持解析后的 false。
+ if !NewAppearance().MenuMouseScroll || (&Appearance{}).MenuMouseScroll {
+ t.Fatal("menu mouse scroll default changed")
+ }
}
diff --git a/kernel/conf/settings_normalize.go b/kernel/conf/settings_normalize.go
index a00ea3a506..4c0bbf588b 100644
--- a/kernel/conf/settings_normalize.go
+++ b/kernel/conf/settings_normalize.go
@@ -51,9 +51,6 @@ func NormalizeFileTree(fileTree *FileTree) *FileTree {
if fileTree.CreateDocAtTop == nil {
fileTree.CreateDocAtTop = new(true)
}
- if fileTree.BoxDocEnabled == nil {
- fileTree.BoxDocEnabled = new(false)
- }
fileTree.RecentDocsMaxListCount = max(MinFileTreeRecentDocsListCount, min(MaxFileTreeRecentDocsListCount, fileTree.RecentDocsMaxListCount))
switch fileTree.RecentDocsSort {
case "viewedAt", "created", "updated", "openAt", "closedAt":
diff --git a/kernel/conf/settings_normalize_test.go b/kernel/conf/settings_normalize_test.go
index 760b54c6cd..1f42817321 100644
--- a/kernel/conf/settings_normalize_test.go
+++ b/kernel/conf/settings_normalize_test.go
@@ -34,7 +34,7 @@ func TestNormalizeSettingsFileTreeAndSearch(t *testing.T) {
fileTree := NormalizeFileTree(&FileTree{CloseTabsOnStart: true, ShorthandSavePath: "notes", MaxOpenTabCount: 99})
if *fileTree.TabStartupMode != 2 || !fileTree.CloseTabsOnStart || fileTree.ShorthandSavePath != "/notes" ||
fileTree.MaxOpenTabCount != 32 || fileTree.MaxListCount != 512 || fileTree.LargeFileWarningSize != 8 ||
- !*fileTree.CreateDocAtTop || *fileTree.BoxDocEnabled {
+ !*fileTree.CreateDocAtTop || fileTree.BoxDocEnabled {
t.Fatalf("unexpected file tree normalization: %+v", fileTree)
}
for _, entry := range []struct{ input, want int }{{-1, 64}, {0, 64}, {1, 32}, {31, 32}, {32, 32}, {128, 128}} {
diff --git a/kernel/model/box_doc.go b/kernel/model/box_doc.go
index 7b8bdc0737..79a1a0d5f3 100644
--- a/kernel/model/box_doc.go
+++ b/kernel/model/box_doc.go
@@ -94,7 +94,7 @@ func writeBoxDocID(boxID string) error {
func IsBoxDocEnabled() bool {
confFileTreeSnapshot := Conf.LoadFileTree()
- return nil != Conf && nil != confFileTreeSnapshot && nil != confFileTreeSnapshot.BoxDocEnabled && *confFileTreeSnapshot.BoxDocEnabled
+ return nil != Conf && nil != confFileTreeSnapshot && confFileTreeSnapshot.BoxDocEnabled
}
func hiddenBoxDocRootIDs() (ret []string) {
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 17486f7446..781fcf3525 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -476,7 +476,6 @@ func InitConf() {
}
if boxes, listErr := ListNotebooks(); listErr == nil {
prepareOnboardingForEmptyWorkspace(Conf.Onboarding.Load(), util.ReadOnly, len(boxes))
- initMenuMouseScrollPreference(Conf.Appearance.Load(), confFileExists, len(boxes))
}
if 0 < len(Conf.Repo.Load().Key) {
@@ -593,13 +592,8 @@ func normalizeFileTreeDefaultIcon(fileTree *conf.FileTree, confFileExists bool)
if nil == fileTree {
fileTree = conf.NewFileTree()
if confFileExists {
- fileTree.UseSVGDefaultIcon = new(bool)
+ fileTree.UseSVGDefaultIcon = false
}
- return fileTree
- }
- if nil == fileTree.UseSVGDefaultIcon {
- useSVGDefaultIcon := !confFileExists
- fileTree.UseSVGDefaultIcon = &useSVGDefaultIcon
}
return fileTree
}
diff --git a/kernel/model/conf_defaults_test.go b/kernel/model/conf_defaults_test.go
new file mode 100644
index 0000000000..3c1e9844d6
--- /dev/null
+++ b/kernel/model/conf_defaults_test.go
@@ -0,0 +1,105 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "reflect"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/conf"
+)
+
+// 以下默认值取决于 conf.json 是否存在:新工作空间使用构造函数默认值,已有工作空间缺少字段时沿用旧默认值。
+func TestNormalizeFileTreeDefaultIcon(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ fileTree *conf.FileTree
+ confFileExists bool
+ want bool
+ }{
+ {name: "new workspace", want: true},
+ {name: "existing workspace without file tree", confFileExists: true},
+ {name: "existing setting enabled", fileTree: &conf.FileTree{UseSVGDefaultIcon: true}, confFileExists: true, want: true},
+ {name: "existing setting disabled", fileTree: &conf.FileTree{UseSVGDefaultIcon: false}, confFileExists: true},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ fileTree := normalizeFileTreeDefaultIcon(test.fileTree, test.confFileExists)
+ if test.want != fileTree.UseSVGDefaultIcon {
+ t.Fatalf("unexpected default icon setting: got %t, want %t", fileTree.UseSVGDefaultIcon, test.want)
+ }
+ })
+ }
+}
+
+func TestNormalizeOCRConfig(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ value *conf.OCR
+ confFileExists bool
+ want conf.OCR
+ }{
+ {name: "new device", want: conf.OCR{Provider: "paddleocr", Model: "tiny", Auto: false}},
+ {name: "legacy device", confFileExists: true, want: conf.OCR{Provider: "tesseract", Model: "tiny", Auto: true}},
+ {
+ name: "existing workspace without provider",
+ value: &conf.OCR{},
+ confFileExists: true,
+ want: conf.OCR{Provider: "tesseract", Model: "tiny", Auto: true},
+ },
+ {
+ name: "existing workspace with provider but without model",
+ value: &conf.OCR{Provider: "paddleocr", Auto: true},
+ confFileExists: true,
+ want: conf.OCR{Provider: "paddleocr", Model: "tiny", Auto: true},
+ },
+ {
+ name: "new device without model",
+ value: &conf.OCR{Provider: "paddleocr"},
+ confFileExists: false,
+ want: conf.OCR{Provider: "paddleocr", Model: "tiny", Auto: false},
+ },
+ {
+ name: "saved PaddleOCR with auto enabled",
+ value: &conf.OCR{Provider: "paddleocr", Model: "small", Auto: true},
+ confFileExists: true,
+ want: conf.OCR{Provider: "paddleocr", Model: "tiny", Auto: true},
+ },
+ {
+ name: "saved Tesseract with auto disabled",
+ value: &conf.OCR{Provider: "tesseract", Model: "tiny", Auto: false},
+ confFileExists: true,
+ want: conf.OCR{Provider: "tesseract", Model: "tiny", Auto: false},
+ },
+ {
+ name: "saved PaddleOCR with imported model",
+ value: &conf.OCR{Provider: "paddleocr", Model: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", Auto: false},
+ confFileExists: true,
+ want: conf.OCR{Provider: "paddleocr", Model: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", Auto: false},
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ test.want.ShowText = new(true)
+ got := normalizeOCRConfig(test.value, test.confFileExists)
+ if !reflect.DeepEqual(*got, test.want) {
+ t.Fatalf("OCR config: got %+v, want %+v", *got, test.want)
+ }
+ if test.value != nil && got != test.value {
+ t.Fatal("saved OCR config should be retained")
+ }
+ })
+ }
+}
diff --git a/kernel/model/conf_filetree_test.go b/kernel/model/conf_filetree_test.go
deleted file mode 100644
index 31fcb5ca07..0000000000
--- a/kernel/model/conf_filetree_test.go
+++ /dev/null
@@ -1,57 +0,0 @@
-// SiYuan - From thought to insight, with agents
-// Copyright (c) 2020-present, b3log.org
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program. If not, see <https://www.gnu.org/licenses/>.
-
-package model
-
-import (
- "testing"
-
- "github.com/siyuan-note/siyuan/kernel/conf"
-)
-
-func TestNormalizeFileTreeDefaultIcon(t *testing.T) {
- for _, test := range []struct {
- name string
- fileTree *conf.FileTree
- confFileExists bool
- want bool
- }{
- {name: "new user", want: true},
- {name: "existing configuration without file tree", confFileExists: true},
- {name: "existing configuration without setting", fileTree: &conf.FileTree{}, confFileExists: true},
- {
- name: "existing configuration with setting enabled",
- fileTree: &conf.FileTree{UseSVGDefaultIcon: new(true)},
- confFileExists: true,
- want: true,
- },
- {
- name: "existing configuration with setting disabled",
- fileTree: &conf.FileTree{UseSVGDefaultIcon: new(false)},
- confFileExists: true,
- },
- } {
- t.Run(test.name, func(t *testing.T) {
- fileTree := normalizeFileTreeDefaultIcon(test.fileTree, test.confFileExists)
- if nil == fileTree.UseSVGDefaultIcon {
- t.Fatal("default icon setting should be initialized")
- }
- if test.want != *fileTree.UseSVGDefaultIcon {
- t.Fatalf("unexpected default icon setting: got %t, want %t", *fileTree.UseSVGDefaultIcon, test.want)
- }
- })
- }
-}
diff --git a/kernel/model/conf_preferences.go b/kernel/model/conf_preferences.go
deleted file mode 100644
index cfdffb22a0..0000000000
--- a/kernel/model/conf_preferences.go
+++ /dev/null
@@ -1,13 +0,0 @@
-package model
-
-import (
- "github.com/siyuan-note/siyuan/kernel/conf"
- "github.com/siyuan-note/siyuan/kernel/util"
-)
-
-// 新建空工作空间启用菜单鼠标滚动;已有工作空间缺省保持关闭,不读取旧 KV 偏好。
-func initMenuMouseScrollPreference(appearance *conf.Appearance, confFileExists bool, notebookCount int) {
- if !confFileExists && notebookCount == 0 && !util.ReadOnly {
- appearance.MenuMouseScroll = true
- }
-}
diff --git a/kernel/model/conf_preferences_test.go b/kernel/model/conf_preferences_test.go
deleted file mode 100644
index 6bd05fb3e4..0000000000
--- a/kernel/model/conf_preferences_test.go
+++ /dev/null
@@ -1,69 +0,0 @@
-package model
-
-import (
- "encoding/json"
- "os"
- "path/filepath"
- "reflect"
- "testing"
-
- "github.com/siyuan-note/siyuan/kernel/util"
-)
-
-func TestConfPreferencesPersistPatchAndReset(t *testing.T) {
- prepareConfConsistencyTest(t)
- settingsResetTestEnvironment(t)
- Conf.AI.Load().Agent.StreamingMarkdown = true
- oldStorage := []byte(`{"local-compact-mode":true,"local-menu-mouse-scroll":true,"local-outline":{"keepCurrentExpand":true},"local-pdftheme":{"light":"dark"},"local-mobile-bars":{"autoHide":false},"local-recent-docs":{"type":"updated"},"siyuan-agent-streaming-markdown":true}`)
- if err := os.WriteFile(settingsResetPaths()[1], oldStorage, 0600); err != nil {
- t.Fatal(err)
- }
- for _, item := range []struct{ namespace, patch string }{
- {"appearance", `{"compactMode":true,"menuMouseScroll":true,"outlineKeepCurrentExpand":true,"pdfTheme":{"light":"dark","annoColor":"red"},"mobileBars":{"autoHide":false,"sidebarSwipe":false}}`},
- {"fileTree", `{"recentDocsSort":"updated"}`},
- {"ai", `{"agent":{"streamingMarkdown":true}}`},
- {"appearance", `{"pdfTheme":{"dark":"light"},"mobileBars":{"sidebarButtons":false}}`},
- } {
- if _, err := ApplyConfPatch(item.namespace, []byte(item.patch)); err != nil {
- t.Fatal(err)
- }
- }
- data, err := os.ReadFile(filepath.Join(util.ConfDir, "conf.json"))
- if err != nil {
- t.Fatal(err)
- }
- reloaded := NewAppConf()
- if err = json.Unmarshal(data, reloaded); err != nil {
- t.Fatal(err)
- }
- appearance := reloaded.Appearance.Load()
- if !appearance.CompactMode || !appearance.MenuMouseScroll || !appearance.OutlineKeepCurrentExpand ||
- appearance.PDFTheme.Light != "dark" || appearance.PDFTheme.Dark != "light" || appearance.PDFTheme.AnnoColor != "red" ||
- *appearance.MobileBars.AutoHide || *appearance.MobileBars.SidebarSwipe || !*appearance.MobileBars.SidebarButtons ||
- reloaded.FileTree.Load().RecentDocsSort != "updated" || !reloaded.AI.Load().Agent.StreamingMarkdown {
- t.Fatal("persisted preferences or partial update preservation changed")
- }
- if err = ResetSettings(); err != nil {
- t.Fatal(err)
- }
- appearance = Conf.Appearance.Load()
- if appearance.CompactMode || appearance.MenuMouseScroll || appearance.OutlineKeepCurrentExpand ||
- appearance.PDFTheme.Light != "light" || appearance.PDFTheme.Dark != "dark" ||
- !*appearance.MobileBars.AutoHide || !*appearance.MobileBars.SidebarSwipe || !*appearance.MobileBars.SidebarButtons ||
- Conf.FileTree.Load().RecentDocsSort != "viewedAt" || !Conf.AI.Load().Agent.StreamingMarkdown {
- t.Fatal("reset must restore appearance and file tree preferences while preserving AI")
- }
- if data, err = os.ReadFile(settingsResetPaths()[1]); err != nil {
- t.Fatal(err)
- }
- var old, stored map[string]any
- if err = json.Unmarshal(oldStorage, &old); err != nil {
- t.Fatal(err)
- }
- if err = json.Unmarshal(data, &stored); err != nil {
- t.Fatal(err)
- }
- if !reflect.DeepEqual(old, stored) {
- t.Fatal("retired KV keys must not be maintained by settings reset")
- }
-}
diff --git a/kernel/model/conf_specs.go b/kernel/model/conf_specs.go
index 8905e569bb..f2a8c63f8e 100644
--- a/kernel/model/conf_specs.go
+++ b/kernel/model/conf_specs.go
@@ -206,7 +206,7 @@ func fileTreeReactions() []Reaction[*conf.FileTree] {
return []Reaction[*conf.FileTree]{
{Name: "sort", Changed: func(old, next *conf.FileTree) bool { return old.Sort != next.Sort }, Apply: func(old, next *conf.FileTree) { PushDocSortModeChanged("global", "", "", "/", &next.Sort) }},
{Name: "box-document", Changed: func(old, next *conf.FileTree) bool {
- return (old.BoxDocEnabled != nil && *old.BoxDocEnabled) != (next.BoxDocEnabled != nil && *next.BoxDocEnabled)
+ return old.BoxDocEnabled != next.BoxDocEnabled
}, Apply: func(old, next *conf.FileTree) { RefreshBoxDocFeature() }},
}
}
diff --git a/kernel/model/crypto_create_sqlcipher_test.go b/kernel/model/crypto_create_sqlcipher_test.go
index b73500d476..d582d99e87 100644
--- a/kernel/model/crypto_create_sqlcipher_test.go
+++ b/kernel/model/crypto_create_sqlcipher_test.go
@@ -74,7 +74,7 @@ func testCreateEncryptedBoxInitializesBoxDocument(t *testing.T) {
Conf = NewAppConf()
Conf.FileTree.Store(conf.NewFileTree())
- *Conf.FileTree.Load().BoxDocEnabled = true
+ Conf.FileTree.Load().BoxDocEnabled = true
Conf.NotebookCrypto = conf.NewNotebookCrypto()
Conf.Sync.Store(conf.NewSync())
sql.InitDatabase(true)
diff --git a/kernel/model/menu_mouse_scroll_test.go b/kernel/model/menu_mouse_scroll_test.go
deleted file mode 100644
index ea14b86258..0000000000
--- a/kernel/model/menu_mouse_scroll_test.go
+++ /dev/null
@@ -1,35 +0,0 @@
-package model
-
-import (
- "github.com/siyuan-note/siyuan/kernel/conf"
- "github.com/siyuan-note/siyuan/kernel/util"
- "testing"
-)
-
-func TestInitMenuMouseScrollPreference(t *testing.T) {
- for _, test := range []struct {
- name string
- exists, readonly, enabled bool
- notebooks int
- want bool
- }{
- {name: "new empty workspace", want: true},
- {name: "existing empty workspace", exists: true},
- {name: "existing notebooks", notebooks: 1},
- {name: "readonly workspace", readonly: true},
- {name: "explicitly disabled", exists: true},
- {name: "explicitly enabled", exists: true, enabled: true, want: true},
- } {
- t.Run(test.name, func(t *testing.T) {
- previous := util.ReadOnly
- util.ReadOnly = test.readonly
- t.Cleanup(func() { util.ReadOnly = previous })
- appearance := conf.NewAppearance()
- appearance.MenuMouseScroll = test.enabled
- initMenuMouseScrollPreference(appearance, test.exists, test.notebooks)
- if appearance.MenuMouseScroll != test.want {
- t.Fatalf("enabled = %v, want %v", appearance.MenuMouseScroll, test.want)
- }
- })
- }
-}
diff --git a/kernel/model/ocr.go b/kernel/model/ocr.go
index c9899f8f2f..0c3bf96566 100644
--- a/kernel/model/ocr.go
+++ b/kernel/model/ocr.go
@@ -43,15 +43,18 @@ func ocrModelFilesChanged(changes ...[]string) bool {
return false
}
-// 新设备使用内置 PaddleOCR 并关闭自动识别,已有设备缺少 OCR 配置时保留 Tesseract 自动识别。
+// normalizeOCRConfig 按工作空间新旧补全默认值:新工作空间使用内置 PaddleOCR,已有工作空间沿用 Tesseract 自动识别。
func normalizeOCRConfig(value *conf.OCR, confFileExists bool) *conf.OCR {
- if value == nil {
- // 已有工作空间缺失 OCR 配置时沿用升级前的 Tesseract 自动识别行为。
+ missing := nil == value
+ if missing {
value = conf.NewOCR()
- if confFileExists {
- value.Provider = string(ocr.Tesseract)
- value.Auto = true
- }
+ }
+ if confFileExists && (missing || "" == value.Provider) {
+ value.Provider = string(ocr.Tesseract)
+ value.Auto = true
+ }
+ if "" == value.Model {
+ value.Model = "tiny"
}
return conf.NormalizeOCR(value)
}
diff --git a/kernel/model/ocr_conf_test.go b/kernel/model/ocr_conf_test.go
index cee45343c3..0e80f21c3b 100644
--- a/kernel/model/ocr_conf_test.go
+++ b/kernel/model/ocr_conf_test.go
@@ -31,47 +31,6 @@ func TestOCRSmallMigrationPreservesPreferences(t *testing.T) {
}
}
-func TestNormalizeOCRConfig(t *testing.T) {
- for _, test := range []struct {
- name string
- value *conf.OCR
- confFileExists bool
- want conf.OCR
- }{
- {name: "new device", want: conf.OCR{Provider: "paddleocr", Model: "tiny", Auto: false}},
- {name: "legacy device", confFileExists: true, want: conf.OCR{Provider: "tesseract", Model: "tiny", Auto: true}},
- {
- name: "saved PaddleOCR with auto enabled",
- value: &conf.OCR{Provider: "paddleocr", Model: "small", Auto: true},
- confFileExists: true,
- want: conf.OCR{Provider: "paddleocr", Model: "tiny", Auto: true},
- },
- {
- name: "saved Tesseract with auto disabled",
- value: &conf.OCR{Provider: "tesseract", Model: "tiny", Auto: false},
- confFileExists: true,
- want: conf.OCR{Provider: "tesseract", Model: "tiny", Auto: false},
- },
- {
- name: "saved PaddleOCR with imported model",
- value: &conf.OCR{Provider: "paddleocr", Model: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", Auto: false},
- confFileExists: true,
- want: conf.OCR{Provider: "paddleocr", Model: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", Auto: false},
- },
- } {
- t.Run(test.name, func(t *testing.T) {
- test.want.ShowText = new(true)
- got := normalizeOCRConfig(test.value, test.confFileExists)
- if !reflect.DeepEqual(*got, test.want) {
- t.Fatalf("OCR config: got %+v, want %+v", *got, test.want)
- }
- if test.value != nil && got != test.value {
- t.Fatal("saved OCR config should be retained")
- }
- })
- }
-}
-
func TestOCRModelFilesChanged(t *testing.T) {
for _, test := range []struct {
name string
diff --git a/kernel/model/settings_reset_test.go b/kernel/model/settings_reset_test.go
index f18a26da2b..a879ccd031 100644
--- a/kernel/model/settings_reset_test.go
+++ b/kernel/model/settings_reset_test.go
@@ -348,3 +348,61 @@ func TestSettingsResetRollbackFailureStopsWrites(t *testing.T) {
})
}
}
+
+func TestConfPreferencesPersistPatchAndReset(t *testing.T) {
+ prepareConfConsistencyTest(t)
+ settingsResetTestEnvironment(t)
+ Conf.AI.Load().Agent.StreamingMarkdown = true
+ oldStorage := []byte(`{"local-compact-mode":true,"local-menu-mouse-scroll":true,"local-outline":{"keepCurrentExpand":true},"local-pdftheme":{"light":"dark"},"local-mobile-bars":{"autoHide":false},"local-recent-docs":{"type":"updated"},"siyuan-agent-streaming-markdown":true}`)
+ if err := os.WriteFile(settingsResetPaths()[1], oldStorage, 0600); err != nil {
+ t.Fatal(err)
+ }
+ for _, item := range []struct{ namespace, patch string }{
+ {"appearance", `{"compactMode":true,"menuMouseScroll":true,"outlineKeepCurrentExpand":true,"pdfTheme":{"light":"dark","annoColor":"red"},"mobileBars":{"autoHide":false,"sidebarSwipe":false}}`},
+ {"fileTree", `{"recentDocsSort":"updated"}`},
+ {"ai", `{"agent":{"streamingMarkdown":true}}`},
+ {"appearance", `{"pdfTheme":{"dark":"light"},"mobileBars":{"sidebarButtons":false}}`},
+ } {
+ if _, err := ApplyConfPatch(item.namespace, []byte(item.patch)); err != nil {
+ t.Fatal(err)
+ }
+ }
+ data, err := os.ReadFile(filepath.Join(util.ConfDir, "conf.json"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ reloaded := NewAppConf()
+ if err = json.Unmarshal(data, reloaded); err != nil {
+ t.Fatal(err)
+ }
+ appearance := reloaded.Appearance.Load()
+ if !appearance.CompactMode || !appearance.MenuMouseScroll || !appearance.OutlineKeepCurrentExpand ||
+ appearance.PDFTheme.Light != "dark" || appearance.PDFTheme.Dark != "light" || appearance.PDFTheme.AnnoColor != "red" ||
+ *appearance.MobileBars.AutoHide || *appearance.MobileBars.SidebarSwipe || !*appearance.MobileBars.SidebarButtons ||
+ reloaded.FileTree.Load().RecentDocsSort != "updated" || !reloaded.AI.Load().Agent.StreamingMarkdown {
+ t.Fatal("persisted preferences or partial update preservation changed")
+ }
+ if err = ResetSettings(); err != nil {
+ t.Fatal(err)
+ }
+ appearance = Conf.Appearance.Load()
+ if appearance.CompactMode || !appearance.MenuMouseScroll || appearance.OutlineKeepCurrentExpand ||
+ appearance.PDFTheme.Light != "light" || appearance.PDFTheme.Dark != "dark" ||
+ !*appearance.MobileBars.AutoHide || !*appearance.MobileBars.SidebarSwipe || !*appearance.MobileBars.SidebarButtons ||
+ Conf.FileTree.Load().RecentDocsSort != "viewedAt" || !Conf.AI.Load().Agent.StreamingMarkdown {
+ t.Fatal("reset must restore appearance and file tree preferences while preserving AI")
+ }
+ if data, err = os.ReadFile(settingsResetPaths()[1]); err != nil {
+ t.Fatal(err)
+ }
+ var old, stored map[string]any
+ if err = json.Unmarshal(oldStorage, &old); err != nil {
+ t.Fatal(err)
+ }
+ if err = json.Unmarshal(data, &stored); err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(old, stored) {
+ t.Fatal("retired KV keys must not be maintained by settings reset")
+ }
+}