An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"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/util"
)
func TestPublishReaderSearchAndBacklinkEncryptedNotebook(t *testing.T) {
gin.SetMode(gin.TestMode)
const boxID = "20260726000003-encrypt"
oldConf, oldDataDir := model.Conf, util.DataDir
util.DataDir = t.TempDir()
model.Conf = model.NewAppConf()
model.Conf.Editor.Store(conf.NewEditor())
t.Cleanup(func() {
model.Conf, util.DataDir = oldConf, oldDataDir
})
boxConf := conf.NewBoxConf()
boxConf.Encrypted = true
if err := (&model.Box{ID: boxID}).SaveConf(boxConf); err != nil {
t.Fatal(err)
}
engine := gin.New()
engine.Use(func(c *gin.Context) {
c.Set(model.RoleContextKey, model.RoleReader)
c.Next()
})
engine.POST("/api/search/fullTextSearchBlock", fullTextSearchBlock)
engine.POST("/api/ref/getBacklink2", getBacklink2)
engine.POST("/api/search/searchRefBlock", searchRefBlock)
t.Run("exact reference title", func(t *testing.T) {
recorder := httptest.NewRecorder()
engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/search/searchRefBlock",
strings.NewReader(`{"id":"block","k":"secret","notebook":"`+boxID+`","exactDocTitle":true}`)))
requireAPIContract(t, http.MethodPost, "/api/search/searchRefBlock", recorder)
var response struct {
Code int `json:"code"`
Data struct {
Blocks []*apicontract.SearchBlock `json:"blocks"`
NewDoc bool `json:"newDoc"`
} `json:"data"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil || response.Code != 0 ||
len(response.Data.Blocks) != 0 || response.Data.NewDoc {
t.Fatalf("published exact title search exposed encrypted data: %s, %v", recorder.Body.String(), err)
}
})
t.Run("search", func(t *testing.T) {
recorder := httptest.NewRecorder()
request := httptest.NewRequest(
http.MethodPost,
"/api/search/fullTextSearchBlock",
strings.NewReader(`{"query":"secret","notebook":"`+boxID+`"}`),
)
request.Header.Set("Content-Type", "application/json")
engine.ServeHTTP(recorder, request)
response := &struct {
Code int `json:"code"`
Data struct {
Blocks []any `json:"blocks"`
MatchedBlockCount int `json:"matchedBlockCount"`
MatchedRootCount int `json:"matchedRootCount"`
PageCount int `json:"pageCount"`
DocMode bool `json:"docMode"`
} `json:"data"`
}{}
if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {
t.Fatalf("unmarshal search response failed: %v", err)
}
if response.Code != 0 || len(response.Data.Blocks) != 0 || response.Data.MatchedBlockCount != 0 ||
response.Data.MatchedRootCount != 0 || response.Data.PageCount != 0 || response.Data.DocMode {
t.Fatalf("publish search exposed encrypted notebook metadata: %s", recorder.Body.String())
}
})
t.Run("backlink", func(t *testing.T) {
recorder := httptest.NewRecorder()
request := httptest.NewRequest(
http.MethodPost,
"/api/ref/getBacklink2",
strings.NewReader(`{"id":"20260726000004-encrypt","k":"","mk":"","notebook":"`+boxID+`"}`),
)
request.Header.Set("Content-Type", "application/json")
engine.ServeHTTP(recorder, request)
response := &struct {
Code int `json:"code"`
Data struct {
Backlinks []any `json:"backlinks"`
LinkRefsCount int `json:"linkRefsCount"`
Backmentions []any `json:"backmentions"`
MentionsCount int `json:"mentionsCount"`
Box string `json:"box"`
} `json:"data"`
}{}
if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {
t.Fatalf("unmarshal backlink response failed: %v", err)
}
if response.Code != 0 || len(response.Data.Backlinks) != 0 || response.Data.LinkRefsCount != 0 ||
len(response.Data.Backmentions) != 0 || response.Data.MentionsCount != 0 || response.Data.Box != "" {
t.Fatalf("publish backlinks exposed encrypted notebook metadata: %s", recorder.Body.String())
}
})
}