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 treenode
import (
"database/sql"
"errors"
"strings"
"testing"
"time"
)
func TestHPathRefreshIndexMigration(t *testing.T) {
for _, init := range []struct {
name string
run func(*sql.DB) error
}{{"existing database", ensureHPathIndexes}, {"encrypted database schema", initEncryptedBlockTreeTables}} {
t.Run(init.name, func(t *testing.T) {
database, err := sql.Open("sqlite3_extended", ":memory:")
if err != nil {
t.Fatal(err)
}
defer database.Close()
database.SetMaxOpenConns(1)
for _, stmt := range []string{
"CREATE TABLE blocktrees (id, root_id, parent_id, box_id, path, hpath, updated, type)",
"CREATE INDEX idx_blocktrees_id ON blocktrees(id)",
"CREATE INDEX idx_blocktrees_root_id ON blocktrees(root_id)",
"CREATE INDEX idx_blocktrees_box_id ON blocktrees(box_id)",
"CREATE INDEX idx_blocktrees_doc_path ON blocktrees(box_id, path) WHERE type = 'd'",
"INSERT INTO blocktrees VALUES ('block', 'doc', 'doc', 'box', '/doc.sy', '/Document', '20260915000000', 'p')",
} {
if _, err = database.Exec(stmt); err != nil {
t.Fatal(err)
}
}
for range 2 {
if err = init.run(database); err != nil {
t.Fatal(err)
}
}
var hpath string
if err = database.QueryRow("SELECT hpath FROM blocktrees WHERE id = 'block'").Scan(&hpath); err != nil || hpath != "/Document" {
t.Fatalf("index migration changed existing data: %q, %v", hpath, err)
}
rows, err := database.Query("EXPLAIN QUERY PLAN "+blockHPathBatchQuery, "doc", "box", "/doc.sy", 0, 257)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
var plan strings.Builder
for rows.Next() {
var id, parent, unused int
var detail string
if err = rows.Scan(&id, &parent, &unused, &detail); err != nil {
t.Fatal(err)
}
plan.WriteString(detail)
plan.WriteString("\n")
}
if err = rows.Err(); err != nil {
t.Fatal(err)
}
if !strings.Contains(plan.String(), "root_id=? AND box_id=? AND rowid>?") || strings.Contains(plan.String(), "TEMP B-TREE") {
t.Fatalf("batch scans beyond the document or sorts all rows: %s", plan.String())
}
})
}
}
func TestHPathRefreshYieldsToBlockTreeWriter(t *testing.T) {
indexBlockTreeLock.Lock()
defer indexBlockTreeLock.Unlock()
result := make(chan error, 1)
go func() {
_, _, err := RefreshBlockHPathsBatch(&BlockTree{}, 0, 32, func() error {
return errors.New("content write attempted while blocktree writer was busy")
})
result <- err
}()
select {
case err := <-result:
if !errors.Is(err, ErrHPathRefreshBusy) {
t.Fatalf("background did not yield to writer: %v", err)
}
case <-time.After(time.Second):
t.Fatal("background waited for the blocktree writer")
}
}
func TestDocumentUpdatesUseIdentityIndexes(t *testing.T) {
for _, encrypted := range []bool{false, true} {
t.Run(map[bool]string{false: "plain", true: "encrypted"}[encrypted], func(t *testing.T) {
box := withDocumentMetadataDB(t, encrypted)
for _, update := range []struct {
name, query, index, lookup string
args []any
}{
{"display path", documentHPathUpdateQuery, "idx_blocktrees_doc_path", "box_id=? AND path=?",
[]any{"/Renamed", "doc", box, "/doc.sy", "/Renamed"}},
{"document metadata", documentBlockTreeUpdateQuery, "idx_blocktrees_id", "id=?",
[]any{"doc", "", "/doc.sy", "/Renamed", "20261023000000", box, "doc"}},
} {
t.Run(update.name, func(t *testing.T) {
rows, err := queryForBox(box, "EXPLAIN QUERY PLAN "+update.query, update.args...)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
var plan strings.Builder
for rows.Next() {
var id, parent, unused int
var detail string
if err = rows.Scan(&id, &parent, &unused, &detail); err != nil {
t.Fatal(err)
}
plan.WriteString(detail + "\n")
}
if err = rows.Err(); err != nil {
t.Fatal(err)
}
if !strings.Contains(plan.String(), "SEARCH blocktrees USING INDEX "+update.index) ||
!strings.Contains(plan.String(), "("+update.lookup+")") {
t.Fatalf("document update does not locate its identity: %s", plan.String())
}
})
}
})
}
}