Skip to content

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

:bug: Restore first-run startup and document API error responses

Daniel7c2bb7736cb7
diff --git a/app/src/config/keymapPayload.test.ts b/app/src/config/keymapPayload.test.ts
new file mode 100644
index 0000000000..8f0e9742aa
--- /dev/null
+++ b/app/src/config/keymapPayload.test.ts
@@ -0,0 +1,28 @@
+import * as assert from "node:assert/strict";
+import {test} from "node:test";
+import {keymapPayload} from "./keymapPayload";
+import {systemConfigCollections} from "./systemConfig";
+
+test("shortcut payload accepts missing plugin configuration after first boot defaults are merged", () => {
+ for (const plugin of [undefined, null, {}]) {
+ const {keymap} = systemConfigCollections({keymap: {
+ general: {command: {custom: "", default: "key"}},
+ editor: {general: {}, heading: {}, insert: {}, list: {}, table: {}},
+ plugin,
+ }, uiLayout: {}});
+ const before = JSON.stringify(keymap);
+ assert.deepEqual(keymapPayload(keymap), {...keymap, plugin: {}});
+ assert.equal(JSON.stringify(keymap), before);
+ }
+});
+
+test("shortcut payload preserves plugin bindings and extension fields", () => {
+ const {keymap} = systemConfigCollections({keymap: {
+ general: {},
+ editor: {general: {}, heading: {}, insert: {}, list: {}, table: {}},
+ plugin: {example: {command: {custom: "key", default: "key", bindings: {
+ version: 1, keys: ["key"], priority: {key: 2},
+ }, extension: {enabled: true}}}},
+ }, uiLayout: {}});
+ assert.deepEqual(keymapPayload(keymap), keymap);
+});
diff --git a/app/src/config/keymapPayload.ts b/app/src/config/keymapPayload.ts
index 166e0351b0..e951dc3c6a 100644
--- a/app/src/config/keymapPayload.ts
+++ b/app/src/config/keymapPayload.ts
@@ -10,7 +10,7 @@ export const keymapPayload = (keymap: Config.IKeymap): {[key: string]: JSONValue
return result;
};
const plugin: {[key: string]: JSONValue} = {};
- for (const [name, items] of Object.entries(keymap.plugin)) {
+ for (const [name, items] of Object.entries(keymap.plugin || {})) {
plugin[name] = keys(items);
}
return {
diff --git a/docs/API-CONTRACTS.md b/docs/API-CONTRACTS.md
index fb6a50dc47..b6a653416e 100644
--- a/docs/API-CONTRACTS.md
+++ b/docs/API-CONTRACTS.md
@@ -140,6 +140,8 @@ Dynamic multipart endpoints use `MultipartFields` to retain every text value and
## Generation and verification
+`TestAPIContractFileTreeMissingDocuments` exercises actual HTTP responses for missing documents during path lookup, removal, renaming, duplication, and moving. It checks business errors and preserved `closeTimeout` values against the response contracts and runs in the full kernel command below. Run it separately with `go test -tags "fts5 sqlcipher" ./api -run TestAPIContractFileTreeMissingDocuments -count=1` from `kernel/`.
+
`swapBlockRef` accepts the optional boolean `originalToEmbed`. Omission or `false` keeps the reference at the original definition position; `true` replaces it with an embed querying the moved definition ID. `includeChildren` retains its heading and list behavior. `TestSwapBlockRefContractCompatibility` and `TestSwapBlockRefNodes` cover request defaults, invalid options, and both replacement modes across document, heading, and list cases; they run in the full kernel command below.
Asset-reference scans report missing attribute-view definitions and invalid definition IDs in the optional `unavailableAttributeViews` list without blocking queries, previews, or replacement. Each entry identifies the notebook ID/name, document ID/path/human-readable path, block ID, view ID, and reason. Existing unreadable or corrupt definitions remain errors. Missing-file snapshots are revalidated before writing so a definition restored during scanning cannot be silently omitted. Attribute-view definitions are not deferred assets under the current download-path policy; the regression suite checks this assumption. The unavailable-definition regressions run through the model and actual HTTP contracts, including closed notebooks and shared definitions.
diff --git a/docs/API-CONTRACTS.zh-CN.md b/docs/API-CONTRACTS.zh-CN.md
index 62c698e804..2071d4e767 100644
--- a/docs/API-CONTRACTS.zh-CN.md
+++ b/docs/API-CONTRACTS.zh-CN.md
@@ -140,6 +140,8 @@ JSON SSE 接口通过 `SSEOptions` 和 `SSEEvent` 声明各事件名称及载荷
## 生成与验证
+`TestAPIContractFileTreeMissingDocuments` 通过实际 HTTP 响应覆盖查询路径、删除、重命名、复制和移动不存在的文档,按响应契约检查业务错误及保留的 `closeTimeout`,已包含在下方的内核全量测试命令中。可在 `kernel/` 中执行 `go test -tags "fts5 sqlcipher" ./api -run TestAPIContractFileTreeMissingDocuments -count=1` 单独验证。
+
资源引用扫描将定义文件缺失或定义 ID 无效的数据库记录到可选的 `unavailableAttributeViews` 列表,不阻断查询、预演或替换。每项包含笔记本 ID 与名称、文档 ID 与路径及人类可读路径、块 ID、数据库 ID 和原因。已存在但无法读取或内容损坏的定义仍返回错误。保存前重新校验缺失文件快照,避免漏掉扫描期间恢复的定义。当前按需下载规则不包含数据库定义,回归测试会检查这一前提。缺失定义回归覆盖模型和实际 HTTP 契约,包括关闭的笔记本和共享定义。
资源引用查询和替换契约保留单资源请求,同时支持批量参数。批量结果按输入顺序返回每项状态、原因、引用和改动文件数;顶层计数对共享文件去重。空批次、重复源路径、链式和循环映射会被拒绝。独立映射可以在其他项失败时完成,共享文件写入失败归属所有受影响映射。扫描期间允许编辑,保存前校验工作区快照;取消和无变化重试保留源数据。`TestAssetRelink` 回归覆盖单资源兼容、批量校验、共享文档、数据库及 OCR 保存、历史和并发编辑,已包含在下方完整内核测试命令中。
diff --git a/kernel/api/contract_filetree_test.go b/kernel/api/contract_filetree_test.go
index 1b854ad731..d6a1f306d6 100644
--- a/kernel/api/contract_filetree_test.go
+++ b/kernel/api/contract_filetree_test.go
@@ -13,9 +13,66 @@ import (
"github.com/siyuan-note/siyuan/kernel/apicontract"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
+func TestAPIContractFileTreeMissingDocuments(t *testing.T) {
+ previousConf, previousPath := model.Conf, util.BlockTreeDBPath
+ model.Conf = model.NewAppConf()
+ util.BlockTreeDBPath = filepath.Join(t.TempDir(), "blocktree.db")
+ treenode.InitBlockTree(true)
+ t.Cleanup(func() {
+ treenode.CloseDatabase()
+ model.Conf, util.BlockTreeDBPath = previousConf, previousPath
+ if previousPath != "" {
+ treenode.InitBlockTree(false)
+ }
+ })
+ const id = "29991231235959-zzzzzzz"
+ for _, entry := range []struct {
+ name string
+ handler gin.HandlerFunc
+ body string
+ timeout int
+ }{
+ {"removeDocByID", removeDocByID, `{"id":"` + id + `"}`, 7000},
+ {"renameDocByID", renameDocByID, `{"id":"` + id + `","title":"missing"}`, 7000},
+ {"duplicateDoc", duplicateDoc, `{"id":"` + id + `"}`, 7000},
+ {"getPathByID", getPathByID, `{"id":"` + id + `"}`, 0},
+ {"moveDocsByID", moveDocsByID, `{"fromIDs":["` + id + `"],"toID":"` + id + `"}`, 7000},
+ } {
+ t.Run(entry.name, func(t *testing.T) {
+ engine := gin.New()
+ path := "/api/filetree/" + entry.name
+ engine.POST(path, entry.handler)
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", path, strings.NewReader(entry.body)))
+ requireAPIContract(t, "POST", path, recorder)
+ var response struct {
+ Code int `json:"code"`
+ Msg string `json:"msg"`
+ Data *struct {
+ CloseTimeout int `json:"closeTimeout"`
+ } `json:"data"`
+ }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
+ t.Fatal(err)
+ }
+ if response.Code != -1 || response.Msg != "tree not found" {
+ t.Fatalf("missing document response changed: %s", recorder.Body.String())
+ }
+ if entry.timeout == 0 {
+ if response.Data != nil {
+ t.Fatalf("unexpected error data: %s", recorder.Body.String())
+ }
+ } else if response.Data == nil || response.Data.CloseTimeout != entry.timeout {
+ t.Fatalf("error display duration changed: %s", recorder.Body.String())
+ }
+ })
+ }
+}
+
func TestAPIContractFileTreePayloadConversions(t *testing.T) {
compare := func(before, after []byte) {
t.Helper()
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go
index 5e3c366077..88ccc9884d 100644
--- a/kernel/api/filetree.go
+++ b/kernel/api/filetree.go
@@ -222,8 +222,7 @@ func doc2HeadingContract(c *gin.Context, request apicontract.FileTreeDocHeadingR
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 5000}
- return contractFailure[apicontract.FileTreeDocHeadingData](ret)
+ return apicontract.FailureWithTimeout[apicontract.FileTreeDocHeadingData](ret.Code, ret.Msg, 5000)
}
return apicontract.Success(apicontract.FileTreeDocHeadingData{SrcTreeBox: srcTreeBox, SrcTreePath: srcTreePath})
@@ -241,8 +240,7 @@ func heading2DocContract(c *gin.Context, request apicontract.FileTreeHeadingDocR
if bt := treenode.GetBlockTree(srcHeadingID); bt != nil && model.IsEncryptedBox(bt.BoxID) && bt.BoxID != targetNotebook {
ret.Code = -1
ret.Msg = model.Conf.Language(391)
- ret.Data = map[string]any{"closeTimeout": 5000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 5000)
}
options, err := request.Options()
if err != nil {
@@ -253,8 +251,7 @@ func heading2DocContract(c *gin.Context, request apicontract.FileTreeHeadingDocR
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 5000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 5000)
}
model.FlushTxQueue()
@@ -282,8 +279,7 @@ func li2DocContract(c *gin.Context, request apicontract.FileTreeListItemDocReque
if bt := treenode.GetBlockTree(srcListItemID); bt != nil && model.IsEncryptedBox(bt.BoxID) && bt.BoxID != targetNotebook {
ret.Code = -1
ret.Msg = model.Conf.Language(391)
- ret.Data = map[string]any{"closeTimeout": 5000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 5000)
}
options, err := request.Options()
@@ -295,8 +291,7 @@ func li2DocContract(c *gin.Context, request apicontract.FileTreeListItemDocReque
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 5000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 5000)
}
model.FlushTxQueue()
@@ -530,8 +525,7 @@ func moveDocsContract(c *gin.Context, request apicontract.FileTreeMoveRequest) a
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 7000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 7000)
}
return contractFailure[apicontract.Null](ret)
}
@@ -561,8 +555,7 @@ func moveDocsByIDContract(c *gin.Context, request apicontract.FileTreeMoveIDsReq
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 7000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 7000)
}
fromPaths = append(fromPaths, tree.Path)
}
@@ -575,8 +568,7 @@ func moveDocsByIDContract(c *gin.Context, request apicontract.FileTreeMoveIDsReq
if nil == box {
ret.Code = -1
ret.Msg = "can't found box or tree by id [" + toID + "]"
- ret.Data = map[string]any{"closeTimeout": 7000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 7000)
}
}
@@ -593,8 +585,7 @@ func moveDocsByIDContract(c *gin.Context, request apicontract.FileTreeMoveIDsReq
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 7000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 7000)
}
return contractFailure[apicontract.Null](ret)
}
@@ -631,8 +622,7 @@ func removeDocByIDContract(c *gin.Context, request apicontract.FileTreeTrimIDReq
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 7000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 7000)
}
if err = model.RemoveDoc(notebook, p); err != nil {
@@ -704,8 +694,7 @@ func renameDocByIDContract(c *gin.Context, request apicontract.FileTreeRenameIDR
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 7000}
- return contractFailure[apicontract.Null](ret)
+ return apicontract.FailureWithTimeout[apicontract.Null](ret.Code, ret.Msg, 7000)
}
err = model.RenameDoc(tree.Box, tree.Path, title)
@@ -727,8 +716,7 @@ func duplicateDocContract(c *gin.Context, request apicontract.FileTreeIDRequest)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 7000}
- return contractFailure[apicontract.FileTreeDuplicateData](ret)
+ return apicontract.FailureWithTimeout[apicontract.FileTreeDuplicateData](ret.Code, ret.Msg, 7000)
}
notebook := tree.Box
@@ -751,8 +739,7 @@ func createDocContract(c *gin.Context, request apicontract.FileTreeCreateRequest
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
- ret.Data = map[string]any{"closeTimeout": 7000}
- return contractFailure[apicontract.FileTreeCreateData](ret)
+ return apicontract.FailureWithTimeout[apicontract.FileTreeCreateData](ret.Code, ret.Msg, 7000)
}
return apicontract.Success(apicontract.FileTreeCreateData{ID: tree.Root.ID})