Skip to content

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

32.2 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package model

import (
	"bytes"
	"context"
	"encoding/base64"
	"image"
	"image/png"
	"net/http"
	"net/http/httptest"
	"os"
	"path"
	"path/filepath"
	"reflect"
	"strings"
	"testing"
	"time"

	"github.com/88250/lute/ast"
	"github.com/siyuan-note/siyuan/kernel/av"
	"github.com/siyuan-note/siyuan/kernel/cache"
	"github.com/siyuan-note/siyuan/kernel/conf"
	"github.com/siyuan-note/siyuan/kernel/util"
)

func TestRemoveAssetThumbnail(t *testing.T) {
	originalDataDir, originalTempDir := util.DataDir, util.TempDir
	t.Cleanup(func() { util.DataDir, util.TempDir = originalDataDir, originalTempDir })
	root := filepath.Join(t.TempDir(), "assets", "workspace")
	util.DataDir = filepath.Join(root, "data")
	util.TempDir = filepath.Join(root, "temp")
	for _, test := range []struct {
		name      string
		source    string
		thumbnail string
		remove    bool
	}{
		{"global", "assets/photo.png", "assets/photo.png", true},
		{"uppercase", "assets/Photo.JPG", "assets/Photo.JPG", true},
		{"nested", "assets/nested/assets/photo.jpeg", "assets/nested/assets/photo.jpeg", true},
		{"notebook", "20260101120000-abc1234/assets/photo.png", "assets/photo.png", true},
		{"document", "20260101120000-abc1234/20260101120001-def5678/assets/photo.png", "assets/photo.png", true},
		{"unsupported", "assets/photo.txt", "assets/photo.txt", false},
		{"outside", "../assets/photo.png", "assets/photo.png", false},
		{"nonasset", "storage/assets/photo.png", "assets/photo.png", false},
		{"similar directory", "otherassets/photo.png", "assets/photo.png", false},
	} {
		t.Run(test.name, func(t *testing.T) {
			thumbnailPath := filepath.Join(util.TempDir, "thumbnails", filepath.FromSlash(test.thumbnail))
			if err := os.MkdirAll(filepath.Dir(thumbnailPath), 0755); err != nil {
				t.Fatal(err)
			}
			if err := os.WriteFile(thumbnailPath, []byte("cached image"), 0644); err != nil {
				t.Fatal(err)
			}
			assetAbsPath := filepath.Join(util.DataDir, filepath.FromSlash(test.source))
			removeAssetThumbnail(assetAbsPath)
			_, err := os.Stat(thumbnailPath)
			if test.remove {
				if !os.IsNotExist(err) {
					t.Fatalf("thumbnail should be removed: %v", err)
				}
				removeAssetThumbnail(assetAbsPath)
			} else if err != nil {
				t.Fatalf("unrelated thumbnail should be preserved: %v", err)
			}
		})
	}
}

func TestGenerateImageDoesNotRequireDocument(t *testing.T) {
	var source bytes.Buffer
	if err := png.Encode(&source, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {
		t.Fatal(err)
	}
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write([]byte(`{"data":[{"b64_json":"` + base64.StdEncoding.EncodeToString(source.Bytes()) + `","revised_prompt":"refined"}]}`))
	}))
	defer server.Close()

	originalConf := Conf
	t.Cleanup(func() { Conf = originalConf })
	ai := conf.NewAI()
	modelID := "20260715130000-hijklmn"
	ai.Providers = []*conf.Provider{{
		ID: "provider", Enabled: true, APIKey: "test", BaseURL: server.URL + "/v1", Protocol: "openai", RequestTimeout: 5,
		Models: []*conf.Model{{ID: modelID, Enabled: true, Name: "image-model"}},
	}}
	ai.ImageGeneration.ModelID = modelID
	Conf = NewAppConf()
	Conf.AI.Store(ai)

	result, err := GenerateImage(context.Background(), GenerateImageRequest{Prompt: "draw", OutputFormat: "png"})
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(result.Data, source.Bytes()) || result.MIMEType != "image/png" || result.Extension != ".png" || result.RevisedPrompt != "refined" {
		t.Fatalf("unexpected generated image result: %#v", result)
	}
}

func TestImageGenerationProviderErrorsPreventAutomaticRetry(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		http.Error(w, "provider failed", http.StatusBadGateway)
	}))
	defer server.Close()

	originalConf := Conf
	t.Cleanup(func() { Conf = originalConf })
	ai := conf.NewAI()
	generationModelID := "20260715130001-provider"
	ai.Providers = []*conf.Provider{{
		ID: "provider", Enabled: true, APIKey: "test", BaseURL: server.URL + "/v1", Protocol: "openai", RequestTimeout: 5,
		Models: []*conf.Model{{ID: generationModelID, Enabled: true, Name: "image-model"}},
	}}
	ai.ImageGeneration.ModelID = generationModelID
	Conf = NewAppConf()
	Conf.AI.Store(ai)

	if _, err := GenerateImage(context.Background(), GenerateImageRequest{Prompt: "draw", OutputFormat: "png"}); !IsImageExecutionUnknown(err) {
		t.Fatalf("image provider error should prevent automatic retry: %v", err)
	}
}

func TestClearWorkspaceTempRemovesImageOperations(t *testing.T) {
	originalDataDir, originalTempDir, originalWorkspaceDir := util.DataDir, util.TempDir, util.WorkspaceDir
	t.Cleanup(func() {
		util.DataDir, util.TempDir, util.WorkspaceDir = originalDataDir, originalTempDir, originalWorkspaceDir
	})
	root := t.TempDir()
	util.DataDir = filepath.Join(root, "data")
	util.TempDir = filepath.Join(root, "temp")
	util.WorkspaceDir = root
	operationDir := filepath.Join(util.DataDir, "storage", "ai", "agent", "operations", "image")
	if err := os.MkdirAll(operationDir, 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(operationDir, "operation.json"), []byte("{}"), 0644); err != nil {
		t.Fatal(err)
	}

	clearWorkspaceTemp("")
	if _, err := os.Stat(operationDir); !os.IsNotExist(err) {
		t.Fatalf("image operation directory was not removed: %v", err)
	}
}

func TestClearWorkspaceTempPreservesInstallPackages(t *testing.T) {
	originalDataDir, originalTempDir, originalWorkspaceDir := util.DataDir, util.TempDir, util.WorkspaceDir
	t.Cleanup(func() {
		util.DataDir, util.TempDir, util.WorkspaceDir = originalDataDir, originalTempDir, originalWorkspaceDir
	})
	root := t.TempDir()
	util.DataDir = filepath.Join(root, "data")
	util.TempDir = filepath.Join(root, "temp")
	util.WorkspaceDir = root
	installPkgPath := filepath.Join(util.TempDir, "install", "siyuan-0.0.1-win.exe")
	if err := os.MkdirAll(filepath.Dir(installPkgPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(installPkgPath, []byte("test"), 0644); err != nil {
		t.Fatal(err)
	}
	oldTime := time.Now().Add(-8 * 24 * time.Hour)
	if err := os.Chtimes(installPkgPath, oldTime, oldTime); err != nil {
		t.Fatal(err)
	}

	clearWorkspaceTemp(installPkgPath)
	if _, err := os.Stat(installPkgPath); err != nil {
		t.Fatalf("install package should be preserved during update: %v", err)
	}
	clearWorkspaceTemp("")
	if _, err := os.Stat(installPkgPath); !os.IsNotExist(err) {
		t.Fatalf("old install package should be removed during normal exit: %v", err)
	}
}

func TestPrepareDocumentImageRejectsNetworkImage(t *testing.T) {
	_, err := PrepareDocumentImage("20260715130000-abcdefg", "https://example.com/image.png")
	if err == nil || err.Error() != "only local assets/... images are supported" {
		t.Fatalf("unexpected network image error: %v", err)
	}
}

func TestPrepareAgentMessageImageReadsGlobalAsset(t *testing.T) {
	originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir
	t.Cleanup(func() {
		util.DataDir, util.WorkspaceDir = originalDataDir, originalWorkspaceDir
	})
	workspaceDir := t.TempDir()
	util.WorkspaceDir = workspaceDir
	util.DataDir = filepath.Join(workspaceDir, "data")
	assetPath := filepath.Join(util.DataDir, "assets", "chat.png")
	if err := os.MkdirAll(filepath.Dir(assetPath), 0755); err != nil {
		t.Fatal(err)
	}
	var imageData bytes.Buffer
	if err := png.Encode(&imageData, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(assetPath, imageData.Bytes(), 0644); err != nil {
		t.Fatal(err)
	}

	prepared, err := PrepareAgentMessageImage("assets/chat.png?style=thumb")
	if err != nil {
		t.Fatal(err)
	}
	if prepared.Artifact.Path != "assets/chat.png" || prepared.MIMEType != "image/png" ||
		prepared.Prepared.Width != 2 || prepared.Prepared.Height != 2 {
		t.Fatalf("unexpected prepared agent image: %#v", prepared)
	}
	if _, err = PrepareAgentMessageImage("assets/../storage/secret.png"); err == nil {
		t.Fatal("agent image path traversal should be rejected")
	}
}

func TestAgentMessageImageAssets(t *testing.T) {
	markdown := "![first](assets/first.png)\n\n[asset link](assets/file.png)\n\n" +
		"![duplicate](assets/first.png)\n\n![network](https://example.com/image.png)"
	want := []string{"assets/first.png"}
	if got := AgentMessageImageAssets(markdown); !reflect.DeepEqual(got, want) {
		t.Fatalf("agent message image assets: got %v, want %v", got, want)
	}
}

func TestUnusedAssetsExcludesAgentSessionImages(t *testing.T) {
	originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir
	originalConf := Conf
	t.Cleanup(func() {
		util.DataDir, util.WorkspaceDir = originalDataDir, originalWorkspaceDir
		Conf = originalConf
	})
	workspaceDir := t.TempDir()
	util.WorkspaceDir = workspaceDir
	util.DataDir = filepath.Join(workspaceDir, "data")
	Conf = NewAppConf()
	Conf.FileTree.Store(conf.NewFileTree())
	assetsDir := filepath.Join(util.DataDir, "assets")
	if err := os.MkdirAll(assetsDir, 0755); err != nil {
		t.Fatal(err)
	}
	for _, name := range []string{"session.png", "runtime.png", "linked.png", "unused.png"} {
		if err := os.WriteFile(filepath.Join(assetsDir, name), []byte("image"), 0644); err != nil {
			t.Fatal(err)
		}
	}
	sessionDir := filepath.Join(util.DataDir, "storage", "ai", "agent", "sessions", "20260825120000-abcdefg")
	if err := os.MkdirAll(sessionDir, 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(sessionDir, "session.json"), []byte(
		`{"entries":[{"type":"user","content":"![session](assets/session.png?style=thumb)\n\n[link](assets/linked.png)"}]}`), 0644); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(sessionDir, "runtime.json"), []byte(
		`{"activeTurn":{"userContent":"![runtime](assets/runtime.png)"}}`), 0644); err != nil {
		t.Fatal(err)
	}

	items, err := UnusedAssets(false)
	if err != nil {
		t.Fatal(err)
	}
	unused := map[string]bool{}
	for _, item := range items {
		unused[item.Item] = true
	}
	if unused["assets/session.png"] || unused["assets/runtime.png"] {
		t.Fatalf("agent session images were classified as unused: %#v", unused)
	}
	if !unused["assets/linked.png"] || !unused["assets/unused.png"] {
		t.Fatalf("unreferenced assets were not classified as unused: %#v", unused)
	}
}

func TestNormalizeMissingAssetLinkDest(t *testing.T) {
	tests := []struct {
		name string
		dest string
		want string
	}{
		{name: "asset", dest: "assets/image.png", want: "assets/image.png"},
		{name: "query", dest: "assets/document.pdf?page=2", want: "assets/document.pdf"},
		{name: "folder", dest: "assets/images/", want: ""},
		{name: "rtfd", dest: "assets/document.rtfd", want: ""},
		{name: "pdf annotation", dest: "assets/document.pdf/20200101000000-abcdefg", want: ""},
		{name: "external", dest: "https://example.com/image.png", want: ""},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			if got := normalizeMissingAssetLinkDest(test.dest); got != test.want {
				t.Fatalf("normalize missing asset link destination: got %q, want %q", got, test.want)
			}
		})
	}
}

func TestLookupAssetPath(t *testing.T) {
	assetsPathMap := map[string]string{
		"assets/file":    "file",
		"assets/folder/": "folder",
		"assets/shared":  "shared-file",
		"assets/shared/": "shared-folder",
	}
	tests := []struct {
		name        string
		dest        string
		wantDest    string
		wantAbsPath string
		wantFound   bool
	}{
		{name: "file", dest: "assets/file", wantDest: "assets/file", wantAbsPath: "file", wantFound: true},
		{name: "folder alias", dest: "assets/folder", wantDest: "assets/folder/", wantAbsPath: "folder", wantFound: true},
		{name: "folder", dest: "assets/folder/", wantDest: "assets/folder/", wantAbsPath: "folder", wantFound: true},
		{name: "exact file before folder alias", dest: "assets/shared", wantDest: "assets/shared", wantAbsPath: "shared-file", wantFound: true},
		{name: "folder does not match file", dest: "assets/file/"},
		{name: "missing", dest: "assets/missing"},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			gotDest, gotAbsPath, gotFound := lookupAssetPath(assetsPathMap, test.dest)
			if gotDest != test.wantDest || gotAbsPath != test.wantAbsPath || gotFound != test.wantFound {
				t.Fatalf("lookup asset path: got [%q, %q, %v], want [%q, %q, %v]",
					gotDest, gotAbsPath, gotFound, test.wantDest, test.wantAbsPath, test.wantFound)
			}
		})
	}
}

func TestRemoveReferencedAssetPathsWithFolderAlias(t *testing.T) {
	assetsPathMap := map[string]string{
		"assets/ch4/":         "ch4",
		"assets/ch4/notes.md": "notes.md",
		"assets/ch4/demo.js":  "demo.js",
		"assets/ch40/":        "ch40",
	}
	linkDestFilePaths := removeReferencedAssetPaths(assetsPathMap, map[string]bool{"assets/ch4": true})
	if 0 != len(linkDestFilePaths) {
		t.Fatalf("folder alias should not be classified as a file: %v", linkDestFilePaths)
	}
	expected := map[string]string{"assets/ch40/": "ch40"}
	if !reflect.DeepEqual(assetsPathMap, expected) {
		t.Fatalf("unexpected assets after removing referenced folder: got %#v, want %#v", assetsPathMap, expected)
	}
}

func TestMissingAssetItemsWithFolderAlias(t *testing.T) {
	referenceBlockIDs := map[missingAssetReference]map[string]bool{}
	addAssetLinkDestBlockID(referenceBlockIDs, "20260806120000-abcdefg", false, "assets/existing-folder", "20260806120001-hijklmn")
	addAssetLinkDestBlockID(referenceBlockIDs, "20260806120000-abcdefg", false, "assets/missing-folder", "20260806120002-opqrstu")

	items := missingAssetItems(referenceBlockIDs, map[string]string{"assets/existing-folder/": "existing-folder"})
	expected := []*UnusedItem{{
		Item:     "assets/missing-folder",
		Name:     "missing-folder",
		BlockIDs: []string{"20260806120002-opqrstu"},
	}}
	if !reflect.DeepEqual(items, expected) {
		t.Fatalf("unexpected missing folder assets: got %#v, want %#v", items, expected)
	}
}

func TestMissingAssetItemsAreScopedToEncryptedNotebook(t *testing.T) {
	originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir
	originalConf, originalLangs := Conf, util.Langs
	t.Cleanup(func() {
		util.DataDir, util.WorkspaceDir = originalDataDir, originalWorkspaceDir
		Conf, util.Langs = originalConf, originalLangs
	})

	workspaceDir := t.TempDir()
	util.WorkspaceDir = workspaceDir
	util.DataDir = filepath.Join(workspaceDir, "data")
	Conf = NewAppConf()
	Conf.Lang = "en"
	util.Langs = map[string]map[int]string{"en": {12: "Asset [%s] not found"}}
	boxA := "20260728160000-abcdefg"
	boxB := "20260728160001-hijklmn"
	normalBox := "20260728160002-opqrstu"
	for _, boxID := range []string{boxA, boxB} {
		confDir := filepath.Join(util.DataDir, boxID, ".siyuan")
		if err := os.MkdirAll(confDir, 0755); err != nil {
			t.Fatal(err)
		}
		if err := os.WriteFile(filepath.Join(confDir, "conf.json"), []byte(`{"encrypted":true}`), 0644); err != nil {
			t.Fatal(err)
		}
	}

	sharedDest := "assets/shared-20260728160003-vwxyzab.bin"
	for _, assetPath := range []string{
		filepath.Join(util.DataDir, boxA, filepath.FromSlash(sharedDest)),
		filepath.Join(util.DataDir, boxA, "assets", "mismatch.bin"),
		filepath.Join(util.DataDir, boxB, "assets", "mismatch.bin"),
	} {
		if err := os.MkdirAll(filepath.Dir(assetPath), 0755); err != nil {
			t.Fatal(err)
		}
		if err := os.WriteFile(assetPath, []byte("asset"), 0644); err != nil {
			t.Fatal(err)
		}
	}

	referenceBlockIDs := map[missingAssetReference]map[string]bool{}
	addAssetLinkDestBlockID(referenceBlockIDs, boxA, true, sharedDest+"?box="+boxA, "20260728160100-aaaaaaa")
	addAssetLinkDestBlockID(referenceBlockIDs, boxB, true, sharedDest+"?box="+boxB, "20260728160101-bbbbbbb")
	addAssetLinkDestBlockID(referenceBlockIDs, boxA, true, "assets/mismatch.bin?box="+boxB, "20260728160102-ccccccc")
	addAssetLinkDestBlockID(referenceBlockIDs, normalBox, false, "assets/global.pdf?page=2", "20260728160103-ddddddd")
	addAssetLinkDestBlockID(referenceBlockIDs, normalBox, false, "assets/normal-missing.txt", "20260728160104-eeeeeee")

	items := missingAssetItems(referenceBlockIDs, map[string]string{"assets/global.pdf": "global.pdf"})
	expected := []*UnusedItem{
		{Item: "assets/mismatch.bin", Name: "mismatch.bin", BlockIDs: []string{"20260728160102-ccccccc"}},
		{Item: "assets/normal-missing.txt", Name: "normal-missing.txt", BlockIDs: []string{"20260728160104-eeeeeee"}},
		{Item: sharedDest, Name: path.Base(sharedDest), BlockIDs: []string{"20260728160101-bbbbbbb"}},
	}
	if !reflect.DeepEqual(items, expected) {
		t.Fatalf("unexpected missing assets: got %#v, want %#v", items, expected)
	}

	items = missingAssetItems(map[missingAssetReference]map[string]bool{}, map[string]string{})
	if nil == items || 0 != len(items) {
		t.Fatalf("empty missing assets should be a non-nil empty slice: %#v", items)
	}
}

func TestGetAssetAbsPathWithSymlinkedWorkspaceAncestor(t *testing.T) {
	originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir
	t.Cleanup(func() {
		util.DataDir, util.WorkspaceDir = originalDataDir, originalWorkspaceDir
	})

	realWorkspaceDir := t.TempDir()
	aliasBaseDir := t.TempDir()
	aliasWorkspaceDir := filepath.Join(aliasBaseDir, "workspace")
	if err := os.Symlink(realWorkspaceDir, aliasWorkspaceDir); err != nil {
		t.Skipf("create workspace symlink failed: %s", err)
	}
	util.WorkspaceDir = aliasWorkspaceDir
	util.DataDir = filepath.Join(aliasWorkspaceDir, "data")

	assetPath := filepath.Join(util.DataDir, "assets", "image.png")
	if err := os.MkdirAll(filepath.Dir(assetPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(assetPath, []byte("image"), 0644); err != nil {
		t.Fatal(err)
	}

	got, err := GetAssetAbsPath("assets/image.png")
	if err != nil {
		t.Fatal(err)
	}
	if got != assetPath {
		t.Fatalf("get global asset path: got %q, want %q", got, assetPath)
	}

	outsideDir := t.TempDir()
	outsidePath := filepath.Join(outsideDir, "outside.png")
	if err = os.WriteFile(outsidePath, []byte("outside"), 0644); err != nil {
		t.Fatal(err)
	}
	linkedAssetPath := filepath.Join(util.DataDir, "assets", "outside.png")
	if err = os.Symlink(outsidePath, linkedAssetPath); err != nil {
		t.Skipf("create asset symlink failed: %s", err)
	}
	if _, err = GetAssetAbsPath("assets/outside.png"); err == nil {
		t.Fatal("asset symlink outside data/assets should be rejected")
	}
}

func TestResolveDataAssetPath(t *testing.T) {
	originalDataDir := util.DataDir
	t.Cleanup(func() {
		util.DataDir = originalDataDir
	})

	util.DataDir = t.TempDir()
	globalAssetPath := filepath.Join(util.DataDir, "assets", "image.png")
	if err := os.MkdirAll(filepath.Dir(globalAssetPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(globalAssetPath, []byte("image"), 0644); err != nil {
		t.Fatal(err)
	}

	relativePath, absPath, err := ResolveDataAssetPath("assets/image.png")
	if err != nil {
		t.Fatal(err)
	}
	if relativePath != "assets/image.png" || absPath != globalAssetPath {
		t.Fatalf("resolve global asset: got [%q, %q], want [%q, %q]", relativePath, absPath, "assets/image.png", globalAssetPath)
	}

	const boxID = "20260723000000-abcdefg"
	notebookAssetPath := filepath.Join(util.DataDir, boxID, "20260723000001-abcdefg", "assets", "document.pdf")
	boxConfPath := filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json")
	if err = os.MkdirAll(filepath.Dir(boxConfPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err = os.WriteFile(boxConfPath, []byte(`{"name":"Notebook"}`), 0644); err != nil {
		t.Fatal(err)
	}
	if err = os.MkdirAll(filepath.Dir(notebookAssetPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err = os.WriteFile(notebookAssetPath, []byte("pdf"), 0644); err != nil {
		t.Fatal(err)
	}
	notebookRelativePath := filepath.ToSlash(strings.TrimPrefix(notebookAssetPath, util.DataDir+string(filepath.Separator)))
	relativePath, absPath, err = ResolveDataAssetPath(notebookRelativePath)
	if err != nil {
		t.Fatal(err)
	}
	if relativePath != notebookRelativePath || absPath != notebookAssetPath {
		t.Fatalf("resolve notebook asset: got [%q, %q], want [%q, %q]",
			relativePath, absPath, notebookRelativePath, notebookAssetPath)
	}
	unusedRelativePath, ok := unusedAssetRelativePath(filepath.Join(util.DataDir, "assets"), notebookAssetPath)
	if !ok || unusedRelativePath != notebookRelativePath {
		t.Fatalf("build notebook unused asset path: got [%q, %v], want [%q, true]",
			unusedRelativePath, ok, notebookRelativePath)
	}

	outsideNotebookDir := t.TempDir()
	outsideNotebookAssetPath := filepath.Join(outsideNotebookDir, "assets", "outside.png")
	if err = os.MkdirAll(filepath.Dir(outsideNotebookAssetPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err = os.WriteFile(outsideNotebookAssetPath, []byte("outside"), 0644); err != nil {
		t.Fatal(err)
	}
	notebookLinkPath := filepath.Join(util.DataDir, boxID, "linked")
	if err = os.Symlink(outsideNotebookDir, notebookLinkPath); err == nil {
		linkedAssetPath := path.Join(boxID, "linked", "assets", "outside.png")
		if _, _, resolveErr := ResolveDataAssetPath(linkedAssetPath); resolveErr == nil {
			t.Error("notebook asset path through symlink outside notebook should be rejected")
		}
	} else {
		t.Logf("skip notebook symlink assertion: %s", err)
	}

	outsidePath := filepath.Join(t.TempDir(), "outside.txt")
	if err = os.WriteFile(outsidePath, []byte("outside"), 0644); err != nil {
		t.Fatal(err)
	}
	invalidPaths := []string{
		"",
		".",
		"..",
		string(filepath.Separator) + filepath.Join("assets", "image.png"),
		"assets",
		"assets/..",
		"assets/../storage/file.txt",
		"storage/file.txt",
		outsidePath,
	}
	for _, invalidPath := range invalidPaths {
		if _, _, resolveErr := ResolveDataAssetPath(invalidPath); resolveErr == nil {
			t.Errorf("path [%s] should be rejected", invalidPath)
		}
	}

	outsideDir := t.TempDir()
	outsideAssetPath := filepath.Join(outsideDir, "outside.png")
	if err = os.WriteFile(outsideAssetPath, []byte("outside"), 0644); err != nil {
		t.Fatal(err)
	}
	linkedDir := filepath.Join(util.DataDir, "assets", "linked")
	if err = os.Symlink(outsideDir, linkedDir); err == nil {
		if _, _, resolveErr := ResolveDataAssetPath("assets/linked/outside.png"); resolveErr == nil {
			t.Error("asset path through symlink outside assets directory should be rejected")
		}
	} else {
		t.Logf("skip child symlink assertion: %s", err)
	}
}

func TestResolveDataAssetPathWithSymlinkedAssetsRoot(t *testing.T) {
	originalDataDir := util.DataDir
	t.Cleanup(func() {
		util.DataDir = originalDataDir
	})

	util.DataDir = t.TempDir()
	realAssetsDir := t.TempDir()
	assetPath := filepath.Join(realAssetsDir, "image.png")
	if err := os.WriteFile(assetPath, []byte("image"), 0644); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(realAssetsDir, filepath.Join(util.DataDir, "assets")); err != nil {
		t.Skipf("create assets directory symlink failed: %s", err)
	}

	relativePath, absPath, err := ResolveDataAssetPath("assets/image.png")
	if err != nil {
		t.Fatal(err)
	}
	wantAbsPath := filepath.Join(util.DataDir, "assets", "image.png")
	if relativePath != "assets/image.png" || absPath != wantAbsPath {
		t.Fatalf("resolve asset under symlinked root: got [%q, %q], want [%q, %q]",
			relativePath, absPath, "assets/image.png", wantAbsPath)
	}
	if !unusedAssetsContainPath(relativePath, absPath, []*UnusedItem{{Item: "physical/path.png", AbsPath: assetPath}}) {
		t.Fatal("asset under symlinked root should match unused item by resolved path")
	}
	unusedRelativePath, ok := unusedAssetRelativePath(realAssetsDir, assetPath)
	if !ok || unusedRelativePath != "assets/image.png" {
		t.Fatalf("build unused asset path under symlinked root: got [%q, %v], want [%q, true]",
			unusedRelativePath, ok, "assets/image.png")
	}
}

func TestUnusedAssetsContainPath(t *testing.T) {
	assetPath := filepath.Join(t.TempDir(), "image.png")
	if err := os.WriteFile(assetPath, []byte("image"), 0644); err != nil {
		t.Fatal(err)
	}
	items := []*UnusedItem{
		{Item: "unexpected/path.png", AbsPath: assetPath},
		{Item: "20260723000000-abcdefg/assets/folder/"},
	}
	if !unusedAssetsContainPath("assets/image.png", assetPath, items) {
		t.Fatal("global unused asset should be found by its absolute path")
	}
	if !unusedAssetsContainPath("20260723000000-abcdefg/assets/folder", "", items) {
		t.Fatal("notebook unused asset directory should be found after normalization")
	}
	if unusedAssetsContainPath("assets/referenced.png", "", items) {
		t.Fatal("referenced asset should not be found in unused assets")
	}
}

func TestAllAssetAbsPathsWithAssetsInWorkspacePath(t *testing.T) {
	originalDataDir := util.DataDir
	originalConf := Conf
	t.Cleanup(func() {
		util.DataDir = originalDataDir
		Conf = originalConf
	})

	util.DataDir = filepath.Join(t.TempDir(), "assets", "workspace", "data")
	Conf = NewAppConf()
	Conf.FileTree.Store(conf.NewFileTree())
	globalAssetPath := filepath.Join(util.DataDir, "assets", "nested", "assets", "image.png")
	if err := os.MkdirAll(filepath.Dir(globalAssetPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(globalAssetPath, []byte("image"), 0644); err != nil {
		t.Fatal(err)
	}

	const (
		boxID = "20260814140000-abcdefg"
		docID = "20260814140001-hijklmn"
	)
	boxConfPath := filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json")
	if err := os.MkdirAll(filepath.Dir(boxConfPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(boxConfPath, []byte(`{"name":"Notebook"}`), 0644); err != nil {
		t.Fatal(err)
	}
	notebookAssetPath := filepath.Join(util.DataDir, boxID, docID, "assets", "document.png")
	if err := os.MkdirAll(filepath.Dir(notebookAssetPath), 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(notebookAssetPath, []byte("image"), 0644); err != nil {
		t.Fatal(err)
	}

	assets, err := allAssetAbsPaths()
	if err != nil {
		t.Fatal(err)
	}
	expected := map[string]string{
		"assets/nested/assets/image.png": globalAssetPath,
		"assets/document.png":            notebookAssetPath,
	}
	for assetPath, expectedAbsPath := range expected {
		if actualAbsPath := assets[assetPath]; actualAbsPath != expectedAbsPath {
			t.Fatalf("asset path [%s]: got %q, want %q", assetPath, actualAbsPath, expectedAbsPath)
		}
	}
}

func TestGetAssetLinkDestsByNode(t *testing.T) {
	const blockID = "20200101000000-abcdefg"
	root := &ast.Node{Type: ast.NodeDocument}
	paragraph := &ast.Node{Type: ast.NodeParagraph, ID: blockID}
	paragraph.SetIALAttr("custom-data-assets", "assets/custom.png")
	linkDest := &ast.Node{Type: ast.NodeLinkDest, Tokens: []byte("assets/image.png")}
	annotationID := &ast.Node{Type: ast.NodeFileAnnotationRefID,
		Tokens: []byte("assets/document-20200101000001-bcdefgh.pdf/20200101000002-cdefghi")}
	root.AppendChild(paragraph)
	paragraph.AppendChild(linkDest)
	paragraph.AppendChild(annotationID)

	want := []string{"assets/custom.png", "assets/image.png", "assets/document-20200101000001-bcdefgh.pdf"}
	if got := getAssetsLinkDests(root, false); !reflect.DeepEqual(got, want) {
		t.Fatalf("get asset link destinations: got %v, want %v", got, want)
	}
	if got := getAssetLinkDestsByNode(paragraph, false); !reflect.DeepEqual(got, []string{"assets/custom.png"}) {
		t.Fatalf("get block asset link destinations: got %v, want %v", got, []string{"assets/custom.png"})
	}
	if got := getAssetLinkDestsByNode(linkDest, false); !reflect.DeepEqual(got, []string{"assets/image.png"}) {
		t.Fatalf("get inline asset link destinations: got %v, want %v", got, []string{"assets/image.png"})
	}
	if got := getAssetLinkDestsByNode(annotationID, false); !reflect.DeepEqual(got,
		[]string{"assets/document-20200101000001-bcdefgh.pdf"}) {
		t.Fatalf("get PDF annotation asset link destination: got %v", got)
	}
	if got := assetLinkDestBlockID(linkDest); got != blockID {
		t.Fatalf("get asset link destination block ID: got %q, want %q", got, blockID)
	}
}

func TestGetAttributeViewAssetsLinkDestsFiltersItems(t *testing.T) {
	attrView := &av.AttributeView{
		KeyValues: []*av.KeyValues{
			{
				Key: &av.Key{Type: av.KeyTypeMAsset},
				Values: []*av.Value{
					{
						BlockID: "public-item",
						MAsset: []*av.ValueAsset{
							{Type: av.AssetTypeImage, Content: "assets/public.png"},
						},
					},
					{
						BlockID: "private-item",
						MAsset: []*av.ValueAsset{
							{Type: av.AssetTypeImage, Content: "assets/private.png"},
						},
					},
				},
			},
			{
				Key: &av.Key{Type: av.KeyTypeURL},
				Values: []*av.Value{
					{BlockID: "public-item", URL: &av.ValueURL{Content: "assets/public-url.png"}},
					{BlockID: "private-item", URL: &av.ValueURL{Content: "assets/private-url.png"}},
				},
			},
			{
				Key: &av.Key{Type: av.KeyTypeText},
				Values: []*av.Value{
					{
						BlockID: "public-item",
						Text: newAttributeViewRichText(t, strings.Join([]string{
							"[Markdown](assets/public-rich.png)",
							`<span data-type="a" data-href="assets/public-text-mark.png">Text mark</span>`,
							`<<assets/public-document-20200101000001-bcdefgh.pdf/20200101000002-cdefghi "PDF shorthand">>`,
							`<span data-type="file-annotation-ref" data-id="assets/public-span-20200101000003-defghij.pdf/20200101000004-efghijk">PDF text mark</span>`,
							"[Network](https://example.com/remote.png)",
						}, "\n\n")),
						Relation: &av.ValueRelation{Contents: []*av.Value{{
							Text: newAttributeViewRichText(t, "[Relation](assets/public-relation.png)"),
						}}},
						Rollup: &av.ValueRollup{Contents: []*av.Value{{
							Text: newAttributeViewRichText(t, "[Rollup](assets/public-rollup.png)"),
						}}},
					},
					{
						BlockID: "private-item",
						Text:    newAttributeViewRichText(t, "[Private](assets/private-rich.png)"),
					},
				},
			},
		},
		NewItemTemplates: []*av.NewItemTemplate{{
			FieldValues: map[string]*av.NewItemFieldValue{
				"text": {Value: &av.Value{Text: newAttributeViewRichText(t, "[Template](assets/template-rich.png)")}},
			},
		}},
	}

	filter := func(_ *av.AttributeView, itemID string) bool {
		return "public-item" == itemID
	}
	want := []string{
		"assets/public.png",
		"assets/public-url.png",
		"assets/public-rich.png",
		"assets/public-text-mark.png",
		"assets/public-document-20200101000001-bcdefgh.pdf",
		"assets/public-span-20200101000003-defghij.pdf",
		"assets/public-relation.png",
		"assets/public-rollup.png",
	}
	if got := getAttributeViewAssetsLinkDests(attrView, false, filter); !reflect.DeepEqual(got, want) {
		t.Fatalf("get filtered attribute view asset links: got %v, want %v", got, want)
	}

	want = []string{
		"assets/public.png",
		"assets/private.png",
		"assets/public-url.png",
		"assets/private-url.png",
		"assets/public-rich.png",
		"assets/public-text-mark.png",
		"assets/public-document-20200101000001-bcdefgh.pdf",
		"assets/public-span-20200101000003-defghij.pdf",
		"assets/public-relation.png",
		"assets/public-rollup.png",
		"assets/private-rich.png",
		"assets/template-rich.png",
	}
	if got := getAttributeViewAssetsLinkDests(attrView, false, nil); !reflect.DeepEqual(got, want) {
		t.Fatalf("get unfiltered attribute view asset links: got %v, want %v", got, want)
	}
}

func newAttributeViewRichText(t *testing.T, content string) *av.ValueText {
	t.Helper()
	ret := &av.ValueText{
		Content: "stale projection",
		Rich: &av.ValueTextRich{
			Spec: av.ValueTextRichSpec, Format: av.ValueTextRichFormatKramdown, Content: content,
		},
	}
	if err := ret.NormalizeRichContent(); nil != err {
		t.Fatalf("normalize attribute view rich text: %v", err)
	}
	return ret
}

func TestRenameAssetClearsAttributeViewCache(t *testing.T) {
	const avID = "20200101000000-abcdefg"
	oldPath := "assets/old-20200101000000-abcdefg.png"
	newPath := "assets/new-20200101000000-hijklmn.png"
	data := []byte(`{"keyValues":[{"values":[{"mAsset":[{"type":"image","name":"","content":"` + oldPath + `"}]}]}]}`)
	avJSONPath := filepath.Join(t.TempDir(), avID+".json")
	if err := os.WriteFile(avJSONPath, data, 0644); err != nil {
		t.Fatal(err)
	}

	cache.SetAVData(avID, data)
	deadline := time.Now().Add(time.Second)
	for {
		if cached, ok := cache.GetAVData(avID); ok && bytes.Equal(cached, data) {
			break
		}
		if time.Now().After(deadline) {
			t.Fatal("wait for attribute view cache timed out")
		}
		time.Sleep(time.Millisecond)
	}
	t.Cleanup(func() {
		cache.RemoveAVData(avID)
	})

	updated, err := replaceAttributeViewAssetPath(avJSONPath, avID, oldPath, newPath)
	if err != nil {
		t.Fatal(err)
	}
	if !updated {
		t.Fatal("attribute view asset path should be updated")
	}
	if _, ok := cache.GetAVData(avID); ok {
		t.Fatal("renaming an asset should clear the attribute view cache")
	}
	updatedData, err := os.ReadFile(avJSONPath)
	if err != nil {
		t.Fatal(err)
	}
	if bytes.Contains(updatedData, []byte(oldPath)) || !bytes.Contains(updatedData, []byte(newPath)) {
		t.Fatalf("replace attribute view asset path: got %s", updatedData)
	}
}