Skip to content

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

:bug: Report failed and incomplete missing asset scans https://github.com/siyuan-note/siyuan/issues/20767

Daniel263fb30ab24b
diff --git a/app/src/config/assets.test.ts b/app/src/config/assets.test.ts
index 7dd2a1bceb..711b937f92 100644
--- a/app/src/config/assets.test.ts
+++ b/app/src/config/assets.test.ts
@@ -86,8 +86,8 @@ test("a failed assets dependency reports an error and permits another mount", as
assert.equal(assets.mounted(), 1);
});
-test("initial and repeated unused asset queries identify their own desktop or mobile frontend", () => {
- for (const mobile of [false, true]) {
+test("desktop and mobile asset tabs identify unused queries and retry failed missing scans", async () => {
+ for (const [mobile, type] of [[false, "remove"], [false, "missing"], [true, "remove"], [true, "missing"]] as const) {
const code = transpileModule(readFileSync("src/config/assets.ts", "utf8"), {
compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
}).outputText;
@@ -96,7 +96,14 @@ test("initial and repeated unused asset queries identify their own desktop or mo
const classList = {contains: (value: string) => value === "item", add: () => {}, remove: () => {}};
const preview = {classList, removeAttribute: () => {}};
const list = {nextElementSibling: preview, addEventListener: () => {}, querySelector: (): null => null};
- const panel = {classList, getAttribute: () => "remove"};
+ const attributes = new Map<string, string>();
+ const panel = {
+ classList,
+ getAttribute: (key: string) => key === "data-type" ? type : attributes.get(key),
+ setAttribute: (key: string, value: string) => attributes.set(key, value),
+ removeAttribute: (key: string) => attributes.delete(key),
+ querySelector: () => list,
+ };
const root = {
classList,
querySelector: (selector: string) => selector.includes("config-assets__list") ? list : {classList},
@@ -111,7 +118,12 @@ test("initial and repeated unused asset queries identify their own desktop or mo
fetchPost: (url: string, _data: any, _callback: any, headers: Record<string, string>) => {
requests.push({url, headers});
},
+ fetchSyncPost: async (url: string): Promise<{code: number; data: null; msg: string}> => {
+ requests.push({url, headers: {}});
+ return {code: -1, data: null, msg: "Scan incomplete"};
+ },
};
+ if (name === "../util/escape") return {escapeHtml: (text: string) => text};
if (name === "../util/fetchAppId") return {SIYUAN_APP_ID_HEADER: "X-SiYuan-App-ID"};
if (name === "../constants") return {Constants: {SIYUAN_APPID: "this-window"}};
if (name === "../util/functions") return {isMobile: () => mobile};
@@ -126,14 +138,90 @@ test("initial and repeated unused asset queries identify their own desktop or mo
runInNewContext(code + "\nexports.assets = assets;", context);
context.exports.assets.element = root;
context.exports.assets.bindEvent({appId: "settings-host-window"});
- click({
- target: {classList, getAttribute: () => "remove", isEqualNode: () => false},
+ const event = {
+ target: {classList, getAttribute: () => type, isEqualNode: () => false},
preventDefault: () => {}, stopPropagation: () => {},
- });
+ };
+ click(event);
+ await new Promise(resolve => setImmediate(resolve));
assert.equal(requests.length, 2);
- requests.forEach(request => {
- assert.equal(request.url, "/api/asset/getUnusedAssets");
- assert.equal(request.headers["X-SiYuan-App-ID"], "this-window");
- });
+ assert.equal(requests[0].url, "/api/asset/getUnusedAssets");
+ assert.equal(requests[0].headers["X-SiYuan-App-ID"], "this-window");
+ if (type === "remove") {
+ assert.equal(requests[1].url, "/api/asset/getUnusedAssets");
+ assert.equal(requests[1].headers["X-SiYuan-App-ID"], "this-window");
+ } else {
+ assert.equal(requests[1].url, "/api/asset/getMissingAssets");
+ assert.equal(attributes.has("data-init"), false);
+ assert.equal(attributes.has("data-loading"), false);
+ click(event);
+ await new Promise(resolve => setImmediate(resolve));
+ assert.equal(requests.length, 3);
+ assert.equal(requests[2].url, "/api/asset/getMissingAssets");
+ }
+ }
+});
+
+test("missing asset failures replace loading, remain retryable and never render an empty success", async () => {
+ const code = transpileModule(readFileSync("src/config/assets.ts", "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+ }).outputText;
+ const rendered: unknown[] = [];
+ let complete: (response: any) => void;
+ let reject: (error: Error) => void;
+ let requests = 0;
+ let messages = 0;
+ const context = {
+ exports: {} as {loadMissingAssets: (panel: any) => Promise<void>; assets: {_renderList: (data: unknown) => void}},
+ window: {siyuan: {languages: {_kernel: {258: "Operation failed"}}}},
+ console: {error: () => {}},
+ require: (name: string) => {
+ if (name === "../util/fetch") return {
+ fetchSyncPost: (url: string) => {
+ assert.equal(url, "/api/asset/getMissingAssets");
+ requests++;
+ return new Promise((resolve, fail) => { complete = resolve; reject = fail; });
+ },
+ };
+ if (name === "../util/escape") return {escapeHtml: (text: string) => text.replaceAll("<", "&lt;")};
+ if (name === "../dialog/message") return {showMessage: () => messages++};
+ return {};
+ },
+ };
+ runInNewContext(code + "\nexports.loadMissingAssets = loadMissingAssets; exports.assets = assets;", context);
+ context.exports.assets._renderList = data => rendered.push(data);
+ const attributes = new Map<string, string>();
+ const list = {innerHTML: ""};
+ const panel = {
+ getAttribute: (key: string) => attributes.get(key),
+ setAttribute: (key: string, value: string) => attributes.set(key, value),
+ removeAttribute: (key: string) => attributes.delete(key),
+ querySelector: () => list,
+ };
+ const failed = context.exports.loadMissingAssets(panel);
+ await context.exports.loadMissingAssets(panel);
+ assert.equal(requests, 1);
+ complete({code: -1, data: null, msg: "Scan incomplete: <broken.sy>"});
+ await failed;
+ assert.match(list.innerHTML, /Scan incomplete: &lt;broken.sy>/);
+ assert.doesNotMatch(list.innerHTML, /fn__loading/);
+ assert.equal(attributes.has("data-init"), false);
+ assert.equal(attributes.has("data-loading"), false);
+ assert.equal(rendered.length, 0);
+
+ const networkFailure = context.exports.loadMissingAssets(panel);
+ reject(new Error("offline"));
+ await networkFailure;
+ assert.match(list.innerHTML, /Operation failed/);
+ assert.equal(messages, 1);
+ assert.equal(attributes.has("data-init"), false);
+
+ for (const data of [[], [{item: "assets/missing.png"}]]) {
+ const retried = context.exports.loadMissingAssets(panel);
+ complete({code: 0, data, msg: ""});
+ await retried;
+ assert.equal(attributes.get("data-init"), "true");
+ assert.equal(attributes.has("data-loading"), false);
+ assert.equal(rendered.at(-1), data);
}
});
diff --git a/app/src/config/assets.ts b/app/src/config/assets.ts
index 80b25f0268..7c202556cb 100644
--- a/app/src/config/assets.ts
+++ b/app/src/config/assets.ts
@@ -3,7 +3,7 @@ import {runSettingsMaintenance} from "./setting/maintenance";
import {confirmDialog} from "../dialog/confirmDialog";
import {isBrowser, isMobile} from "../util/functions";
import {hasClosestByClassName} from "../protyle/util/hasClosest";
-import {fetchPost} from "../util/fetch";
+import {fetchPost, fetchSyncPost} from "../util/fetch";
import {SIYUAN_APP_ID_HEADER} from "../util/fetchAppId";
/// #if !MOBILE
import {getAllModels} from "../layout/getAll";
@@ -37,6 +37,31 @@ export const collectAssetsTabSearchStrings = (): string[] => [
const pendingAssetsMounts = new WeakMap<Element, object>();
+const loadMissingAssets = async (panel: Element) => {
+ if (panel.getAttribute("data-loading")) {
+ return;
+ }
+ panel.setAttribute("data-loading", "true");
+ const list = panel.querySelector(".config-assets__list");
+ list.innerHTML = '<li class="fn__loading"><img src="/stage/loading-pure.svg"></li>';
+ try {
+ const response = await fetchSyncPost("/api/asset/getMissingAssets", {});
+ if (response.code !== 0) {
+ list.innerHTML = `<li class="b3-list--empty">${escapeHtml(response.msg)}</li>`;
+ return;
+ }
+ assets._renderList(response.data, list, "lostAssets");
+ panel.setAttribute("data-init", "true");
+ } catch (error) {
+ const message = window.siyuan.languages._kernel[258];
+ list.innerHTML = `<li class="b3-list--empty">${escapeHtml(message)}</li>`;
+ showMessage(message, 0, "error");
+ console.error(error);
+ } finally {
+ panel.removeAttribute("data-loading");
+ }
+};
+
/** 资源 Tab 挂载(面板页,不走注册表渲染) */
export const mountAssetsTab = async (root: HTMLElement, keywords?: string, app?: App) => {
if (assets.element && assets.element !== root) {
@@ -251,12 +276,10 @@ const assets = {
fetchPost("/api/av/getUnusedAttributeViews", {}, response => {
assets._renderList(response.data, avListElement, "unRefAV");
});
+ item.setAttribute("data-init", "true");
} else {
- fetchPost("/api/asset/getMissingAssets", {}, response => {
- assets._renderList(response.data, item.querySelector(".config-assets__list"), "lostAssets");
- });
+ void loadMissingAssets(item);
}
- item.setAttribute("data-init", "true");
}
} else {
item.classList.add("fn__none");
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index 5c50ba703c..5de3e66c8f 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -3387,6 +3387,12 @@ export interface APIPOSTRoutes {
authorization?: readonly ["authenticated"];
output: "binary";
};
+ /**
+ * 扫描已打开笔记本中的丢失资源,成功时返回列表(无丢失资源时为空数组)。
+ * 资源清单读取、笔记本枚举、目录遍历失败或 panic 时返回 code -1、data null。
+ * 单篇文档无法读取时继续扫描其他文档,最终也返回 code -1,msg 包含被跳过的路径,不发布不完整的列表。
+ * 保留管理员鉴权和加密笔记本请求租约,不改变资源存在性判定或任何持久化格式。
+ */
"/api/asset/getMissingAssets": {
request: EmptyRequestInput;
response: { "code": 0; "data": Array<AssetUnusedItem | null> | null; "msg": string; } | { "code": -1; "data": { "closeTimeout": number; } | null; "msg": string; };
diff --git a/kernel/api/asset.go b/kernel/api/asset.go
index d0600bed9e..b3f50ed8ae 100644
--- a/kernel/api/asset.go
+++ b/kernel/api/asset.go
@@ -380,7 +380,10 @@ var removeUnusedEncryptedBoxAssets = contractHandler(apicontract.RemoveUnusedEnc
var getMissingAssets = contractHandler(apicontract.GetMissingAssets, func(c *gin.Context, request apicontract.EmptyRequest) apicontract.Response[[]*apicontract.AssetUnusedItem] {
- missingAssets := model.MissingAssets()
+ missingAssets, err := model.MissingAssets()
+ if err != nil {
+ return apicontract.Failure[[]*apicontract.AssetUnusedItem](-1, err.Error())
+ }
return apicontract.Success(assetUnusedItems(missingAssets))
})
diff --git a/kernel/api/contract_asset_test.go b/kernel/api/contract_asset_test.go
index 13ddd83a51..0d36dcc2f2 100644
--- a/kernel/api/contract_asset_test.go
+++ b/kernel/api/contract_asset_test.go
@@ -37,6 +37,64 @@ func setupAssetContractWorkspace(t *testing.T) string {
return dir
}
+func TestMissingAssetsContract(t *testing.T) {
+ for _, mode := range []string{"empty", "missing", "unreadable document", "source failure", "panic"} {
+ t.Run(mode, func(t *testing.T) {
+ setupAssetContractWorkspace(t)
+ model.Conf.FileTree.Store(conf.NewFileTree())
+ if mode == "source failure" {
+ util.DataDir = filepath.Join(util.WorkspaceDir, "missing")
+ } else if mode == "panic" {
+ model.Conf = nil
+ } else if mode != "empty" {
+ boxID := "20260918000000-abcdefg"
+ boxConf := conf.NewBoxConf()
+ boxConf.Closed = false
+ if err := (&model.Box{ID: boxID}).SaveConf(boxConf); err != nil {
+ t.Fatal(err)
+ }
+ data := `{"Type":"NodeDocument","ID":"20260918000001-abcdefg","Properties":{"title-img":"background-image: url(assets/missing.png)"}}`
+ if mode == "unreadable document" {
+ if err := os.WriteFile(filepath.Join(util.DataDir, boxID, "20260918000002-abcdefg.sy"), []byte(data), 0644); err != nil {
+ t.Fatal(err)
+ }
+ data = `{"Type":`
+ }
+ if err := os.WriteFile(filepath.Join(util.DataDir, boxID, "20260918000001-abcdefg.sy"), []byte(data), 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ engine := gin.New()
+ engine.POST("/api/asset/getMissingAssets", getMissingAssets)
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", "/api/asset/getMissingAssets", strings.NewReader(`{}`)))
+ requireAPIContract(t, "POST", "/api/asset/getMissingAssets", recorder)
+ var response struct {
+ Code int
+ Msg string
+ Data []*apicontract.AssetUnusedItem
+ }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
+ t.Fatal(err)
+ }
+ if mode == "empty" || mode == "missing" {
+ want := 0
+ if mode == "missing" {
+ want = 1
+ }
+ if response.Code != 0 || response.Data == nil || len(response.Data) != want {
+ t.Fatalf("invalid successful list: %s", recorder.Body.String())
+ }
+ } else if response.Code != -1 || response.Msg == "" || response.Data != nil {
+ t.Fatalf("failed scan must not publish a successful list: %s", recorder.Body.String())
+ }
+ if mode == "unreadable document" && !strings.Contains(response.Msg, "incomplete") {
+ t.Fatalf("skipped documents must be identified as an incomplete scan: %s", recorder.Body.String())
+ }
+ })
+ }
+}
+
func TestAssetUploadContractAdapter(t *testing.T) {
assets := setupAssetContractWorkspace(t)
content := []byte("upload fixture")
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index f2e80fa9d0..645b56dd84 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -1171,6 +1171,11 @@ var GetUnusedEncryptedBoxAssets = define[EncryptedBoxAssetsRequest, []*AssetUnus
// 失败补偿恢复源文件,未提交的中断批次在启动同步前恢复;清理历史只索引脱敏元数据。
// 不改变现有全局清理行为,不修改加密格式,不生成全局资源索引或明文缓存。
var RemoveUnusedEncryptedBoxAssets = define[RemoveEncryptedBoxAssetsRequest, AssetPathsData]("removeUnusedEncryptedBoxAssets", "/api/asset/removeUnusedEncryptedBoxAssets", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")
+
+// GetMissingAssets 扫描已打开笔记本中的丢失资源,成功时返回列表(无丢失资源时为空数组)。
+// 资源清单读取、笔记本枚举、目录遍历失败或 panic 时返回 code -1、data null。
+// 单篇文档无法读取时继续扫描其他文档,最终也返回 code -1,msg 包含被跳过的路径,不发布不完整的列表。
+// 保留管理员鉴权和加密笔记本请求租约,不改变资源存在性判定或任何持久化格式。
var GetMissingAssets = define[EmptyRequest, []*AssetUnusedItem]("getMissingAssets", "/api/asset/getMissingAssets", AuthenticatedAccess|AdminAccess, NoBody, ResponseOptions{}, "POST")
// ResolveAssetPath 返回普通资源路径;已解锁的加密资源返回保留原始名称的受管临时明文副本路径。
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index c75d5103a7..5389bbd57f 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -2318,32 +2318,48 @@ func unusedAssetRelativePath(dataAssetsAbsPath, assetAbsPath string) (string, bo
return "", false
}
-func MissingAssets() (ret []*UnusedItem) {
- defer logging.Recover()
+func MissingAssets() (ret []*UnusedItem, err error) {
+ return missingAssets(allAssetAbsPaths, ListNotebooks)
+}
+
+func missingAssets(assetPaths func() (map[string]string, error), notebooks func() ([]*Box, error)) (ret []*UnusedItem, err error) {
+ defer func() {
+ if recovered := recover(); recovered != nil {
+ ret = nil
+ err = fmt.Errorf("scan missing asset references failed: %v", recovered)
+ logging.LogErrorf("%s", err)
+ }
+ }()
ret = []*UnusedItem{}
- assetsPathMap, err := allAssetAbsPaths()
+ assetsPathMap, err := assetPaths()
if err != nil {
return
}
- notebooks, err := ListNotebooks()
+ boxes, err := notebooks()
if err != nil {
return
}
luteEngine := util.NewLute()
referenceBlockIDs := map[missingAssetReference]map[string]bool{}
- for _, notebook := range notebooks {
+ var skippedErr error
+ for _, notebook := range boxes {
if notebook.Closed {
continue
}
encrypted := IsEncryptedBox(notebook.ID)
- pages := pagedPaths(filepath.Join(util.DataDir, notebook.ID), 32)
+ pages, walkErr := pagedPathsWithError(filepath.Join(util.DataDir, notebook.ID), 32)
+ if walkErr != nil {
+ return nil, fmt.Errorf("scan missing asset references failed: %w", walkErr)
+ }
for _, paths := range pages {
var trees []*parse.Tree
for _, localPath := range paths {
tree, loadTreeErr := loadTree(localPath, luteEngine)
if nil != loadTreeErr {
+ // 保留逐篇跳过的行为,但不能将不完整的扫描报告为成功。
+ skippedErr = errors.Join(skippedErr, fmt.Errorf("read asset references [%s] failed: %w", localPath, loadTreeErr))
continue
}
trees = append(trees, tree)
@@ -2373,7 +2389,10 @@ func MissingAssets() (ret []*UnusedItem) {
}
}
- return missingAssetItems(referenceBlockIDs, assetsPathMap)
+ if skippedErr != nil {
+ return missingAssetItems(referenceBlockIDs, assetsPathMap), fmt.Errorf("missing asset scan incomplete: %w", skippedErr)
+ }
+ return missingAssetItems(referenceBlockIDs, assetsPathMap), nil
}
func missingAssetItems(referenceBlockIDs map[missingAssetReference]map[string]bool, assetsPathMap map[string]string) (ret []*UnusedItem) {
diff --git a/kernel/model/assets_cleanup_test.go b/kernel/model/assets_cleanup_test.go
index 5894830d7f..85b39212c1 100644
--- a/kernel/model/assets_cleanup_test.go
+++ b/kernel/model/assets_cleanup_test.go
@@ -53,15 +53,15 @@ func TestUnusedAssetsPathPrefixes(t *testing.T) {
if err != nil || len(items) != 1 || items[0].Item != "assets/unused.png" {
t.Fatalf("unused: %+v, %v", items, err)
}
- if missing := MissingAssets(); len(missing) != 0 {
- t.Fatalf("existing assets reported missing: %+v", missing)
+ if missing, err := MissingAssets(); err != nil || len(missing) != 0 {
+ t.Fatalf("existing assets reported missing: %+v, %v", missing, err)
}
if err := os.Remove(filepath.Join(util.DataDir, "assets", "sub", "image.png")); err != nil {
t.Fatal(err)
}
- missing := MissingAssets()
- if len(missing) != 1 || missing[0].Item != "assets/sub/image.png" || !reflect.DeepEqual(missing[0].BlockIDs, []string{"20260918000002-abcdefg"}) {
- t.Fatalf("missing: %+v", missing)
+ missing, err := MissingAssets()
+ if err != nil || len(missing) != 1 || missing[0].Item != "assets/sub/image.png" || !reflect.DeepEqual(missing[0].BlockIDs, []string{"20260918000002-abcdefg"}) {
+ t.Fatalf("missing: %+v, %v", missing, err)
}
})
}
diff --git a/kernel/model/assets_missing_test.go b/kernel/model/assets_missing_test.go
new file mode 100644
index 0000000000..5eee7938c4
--- /dev/null
+++ b/kernel/model/assets_missing_test.go
@@ -0,0 +1,130 @@
+package model
+
+import (
+ "errors"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/88250/lute/render"
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestMissingAssetsSourceFailures(t *testing.T) {
+ want := errors.New("scan source unavailable")
+ for _, source := range []string{"asset paths", "notebooks", "panic", "directory walk"} {
+ t.Run(source, func(t *testing.T) {
+ assetPaths := func() (map[string]string, error) {
+ if source == "asset paths" {
+ return nil, want
+ }
+ return map[string]string{}, nil
+ }
+ notebooks := func() ([]*Box, error) {
+ switch source {
+ case "notebooks":
+ return nil, want
+ case "panic":
+ panic("scan panic")
+ case "directory walk":
+ return []*Box{{ID: filepath.Join(t.TempDir(), "missing")}}, nil
+ }
+ t.Fatal("asset path failure must stop notebook enumeration")
+ return nil, nil
+ }
+ items, err := missingAssets(assetPaths, notebooks)
+ if err == nil || len(items) != 0 {
+ t.Fatalf("failed scan returned success: %+v, %v", items, err)
+ }
+ if (source == "asset paths" || source == "notebooks") && !errors.Is(err, want) {
+ t.Fatalf("lost source error: %v", err)
+ }
+ if source == "panic" && !strings.Contains(err.Error(), "scan panic") {
+ t.Fatalf("lost panic diagnostic: %v", err)
+ }
+ })
+ }
+}
+
+func writeMissingAssetDocument(t *testing.T, boxID, filename string) {
+ t.Helper()
+ root := &ast.Node{Type: ast.NodeDocument, ID: "20260918000001-abcdefg", Spec: treenode.CurrentSpec}
+ root.SetIALAttr("title-img", "background-image: url(assets/missing.png)")
+ engine := util.NewLute()
+ data := render.NewJSONRenderer(&parse.Tree{Root: root}, engine.RenderOptions, engine.ParseOptions).Render()
+ if err := os.WriteFile(filename, data, 0644); err != nil {
+ t.Fatal(err)
+ }
+ boxConf := conf.NewBoxConf()
+ boxConf.Closed = false
+ if err := (&Box{ID: boxID}).SaveConf(boxConf); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestMissingAssetsCompleteScan(t *testing.T) {
+ boxID, docPath := setupUnusedAssetWorkspace(t)
+ writeMissingAssetDocument(t, boxID, docPath)
+ items, err := MissingAssets()
+ if err != nil || len(items) != 1 || items[0].Item != "assets/missing.png" ||
+ len(items[0].BlockIDs) != 1 || items[0].BlockIDs[0] != "20260918000001-abcdefg" {
+ t.Fatalf("missing asset scan: %+v, %v", items, err)
+ }
+ if err = os.WriteFile(filepath.Join(util.DataDir, "assets", "missing.png"), []byte("image"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ items, err = MissingAssets()
+ if err != nil || items == nil || len(items) != 0 {
+ t.Fatalf("complete scan must return an empty array: %+v, %v", items, err)
+ }
+}
+
+func TestMissingAssetsSkipUnreadableDocument(t *testing.T) {
+ for _, test := range []struct{ name, data string }{
+ {"new spec", `{"Type":"NodeDocument","Spec":"99"}`},
+ {"invalid spec", `{"Type":"NodeDocument","Spec":"invalid"}`},
+ {"corrupt JSON", `{"Type":`},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ boxID, docPath := setupUnusedAssetWorkspace(t)
+ writeMissingAssetDocument(t, boxID, docPath)
+ brokenPath := filepath.Join(filepath.Dir(docPath), "20260918000000-abcdefg.sy")
+ if err := os.WriteFile(brokenPath, []byte(test.data), 0644); err != nil {
+ t.Fatal(err)
+ }
+ items, err := MissingAssets()
+ if err == nil || !strings.Contains(err.Error(), brokenPath) || !strings.Contains(err.Error(), "incomplete") {
+ t.Fatalf("skipped document must be reported: %+v, %v", items, err)
+ }
+ if len(items) != 1 || items[0].Item != "assets/missing.png" {
+ t.Fatalf("readable documents must still be scanned: %+v, %v", items, err)
+ }
+ if test.name == "new spec" && !errors.Is(err, treenode.ErrSpecTooNew) {
+ t.Fatalf("lost spec error: %v", err)
+ }
+ if data, readErr := os.ReadFile(brokenPath); readErr != nil || string(data) != test.data {
+ t.Fatalf("scan modified unreadable document: %q, %v", data, readErr)
+ }
+ })
+ }
+}
+
+func TestMissingAssetsSkipClosedNotebook(t *testing.T) {
+ boxID, docPath := setupUnusedAssetWorkspace(t)
+ if err := os.WriteFile(docPath, []byte(`{"Type":`), 0644); err != nil {
+ t.Fatal(err)
+ }
+ boxConf := conf.NewBoxConf()
+ boxConf.Closed = true
+ if err := (&Box{ID: boxID}).SaveConf(boxConf); err != nil {
+ t.Fatal(err)
+ }
+ if items, err := MissingAssets(); err != nil || len(items) != 0 {
+ t.Fatalf("closed notebooks must remain excluded: %+v, %v", items, err)
+ }
+}