131 lines
3.2 KiB
Go
131 lines
3.2 KiB
Go
package docDB
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"apigo.cc/go/log"
|
|
"apigo.cc/go/tableDB"
|
|
)
|
|
|
|
func TestDocDB(t *testing.T) {
|
|
logger := log.DefaultLogger
|
|
dbFile := "test_doc.db"
|
|
os.Remove(dbFile)
|
|
defer os.Remove(dbFile)
|
|
os.RemoveAll("./test_docs")
|
|
defer os.RemoveAll("./test_docs")
|
|
|
|
unauthorizedDB := tableDB.GetDB("sqlite://"+dbFile, logger)
|
|
|
|
systemApp := unauthorizedDB.Auth("_system")
|
|
// Bootstrap system tables via the special :Schema table
|
|
_ = systemApp.Table("_Table:Schema").Set(map[string]any{"dsl": ""})
|
|
|
|
docDBInst := New(unauthorizedDB, "./test_docs")
|
|
app := docDBInst.Auth("user1")
|
|
|
|
var createdCalled, updatedCalled, removedCalled bool
|
|
docDBInst.OnCreatedDoc(func(doc *Document) { createdCalled = true })
|
|
docDBInst.OnUpdatedDoc(func(doc *Document) { updatedCalled = true })
|
|
docDBInst.OnRemoved(func(id string) { removedCalled = true })
|
|
|
|
// 1. 测试 SetDoc (创建)
|
|
doc1 := &Document{
|
|
Path: "/test/doc1.md",
|
|
Title: "Test Doc",
|
|
TextContent: "# Header 1\nContent 1",
|
|
Type: "markdown",
|
|
}
|
|
|
|
err := app.SetDoc(doc1)
|
|
if err != nil {
|
|
t.Fatalf("SetDoc failed: %v", err)
|
|
}
|
|
|
|
if doc1.Version != 1 {
|
|
t.Fatalf("Expected version 1, got %d", doc1.Version)
|
|
}
|
|
if !createdCalled {
|
|
t.Error("OnCreatedDoc event not triggered")
|
|
}
|
|
|
|
// 2. 测试 Get
|
|
d, err := app.Get(doc1.ID)
|
|
if err != nil || d == nil {
|
|
t.Fatalf("Get failed: %v", err)
|
|
}
|
|
if d.Title != "Test Doc" {
|
|
t.Fatalf("Expected title 'Test Doc', got %s", d.Title)
|
|
}
|
|
if len(d.ToC) == 0 {
|
|
t.Error("ToC should be extracted")
|
|
}
|
|
|
|
// 3. 测试 SetDoc (更新内容, 提升版本)
|
|
doc1.TextContent = "# Header 1\nUpdated Content"
|
|
err = app.SetDoc(doc1)
|
|
if err != nil {
|
|
t.Fatalf("Update SetDoc failed: %v", err)
|
|
}
|
|
if doc1.Version != 2 {
|
|
t.Fatalf("Expected version 2 after content update, got %d", doc1.Version)
|
|
}
|
|
if !updatedCalled {
|
|
t.Error("OnUpdatedDoc event not triggered")
|
|
}
|
|
|
|
// 4. 测试 SetMeta (更新元数据, 不提升版本)
|
|
err = app.SetMeta(doc1.ID, map[string]any{"title": "New Title"})
|
|
if err != nil {
|
|
t.Fatalf("SetMeta failed: %v", err)
|
|
}
|
|
|
|
d2, _ := app.Get(doc1.ID)
|
|
if d2.Title != "New Title" {
|
|
t.Fatalf("Expected title 'New Title', got %s", d2.Title)
|
|
}
|
|
if d2.Version != 2 {
|
|
t.Fatalf("Expected version 2 after meta update, got %d", d2.Version)
|
|
}
|
|
|
|
// 5. 测试 带二进制内容的 SetDoc
|
|
doc2 := &Document{
|
|
Path: "/test/binary.txt",
|
|
BinaryContent: bytes.NewReader([]byte("Binary Content")),
|
|
}
|
|
err = app.SetDoc(doc2)
|
|
if err != nil {
|
|
t.Fatalf("SetDoc with binary failed: %v", err)
|
|
}
|
|
if doc2.FilePath == "" {
|
|
t.Error("FilePath should be set for binary content")
|
|
}
|
|
if _, err := os.Stat(doc2.FilePath); os.IsNotExist(err) {
|
|
t.Error("File should be written to disk")
|
|
}
|
|
|
|
// 6. 测试 Remove
|
|
err = app.Remove(doc1.ID)
|
|
if err != nil {
|
|
t.Fatalf("Remove failed: %v", err)
|
|
}
|
|
if !removedCalled {
|
|
t.Error("OnRemoved event not triggered")
|
|
}
|
|
|
|
d3, _ := app.Get(doc1.ID)
|
|
if d3 != nil {
|
|
t.Error("Document should be removed")
|
|
}
|
|
}
|
|
|
|
func BenchmarkExtractToC(b *testing.B) {
|
|
content := "# H1\n## H1.1\n### H1.1.1\n" + strings.Repeat("Some content\n", 100) + "# H2\n## H2.1\n"
|
|
for i := 0; i < b.N; i++ {
|
|
_ = ExtractToC(content)
|
|
}
|
|
}
|