2026-05-15 01:55:34 +08:00
|
|
|
package docDB
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"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)
|
|
|
|
|
|
2026-05-15 14:07:01 +08:00
|
|
|
docDBInst := GetDB(unauthorizedDB, "./test_docs")
|
2026-05-15 01:55:34 +08:00
|
|
|
app := docDBInst.Auth("user1")
|
|
|
|
|
|
2026-05-15 14:51:36 +08:00
|
|
|
var createdCalled, updatedCalled, removedCalled, movedCalled bool
|
2026-05-15 14:35:00 +08:00
|
|
|
docDBInst.Hooks.OnCreatedDoc = func(doc *Document) { createdCalled = true }
|
|
|
|
|
docDBInst.Hooks.OnUpdatedDoc = func(doc *Document) { updatedCalled = true }
|
2026-05-15 14:51:36 +08:00
|
|
|
docDBInst.Hooks.OnRemoved = func(path string) { removedCalled = true }
|
|
|
|
|
docDBInst.Hooks.OnMoved = func(oldPath, newPath string) { movedCalled = true }
|
2026-05-15 01:55:34 +08:00
|
|
|
|
|
|
|
|
// 1. 测试 SetDoc (创建)
|
2026-05-15 14:51:36 +08:00
|
|
|
docPath := "/test/doc1.md"
|
2026-05-15 01:55:34 +08:00
|
|
|
doc1 := &Document{
|
2026-05-15 14:51:36 +08:00
|
|
|
Path: docPath,
|
2026-05-15 01:55:34 +08:00
|
|
|
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
|
2026-05-15 14:51:36 +08:00
|
|
|
d, err := app.Get(docPath)
|
2026-05-15 01:55:34 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 14:51:36 +08:00
|
|
|
// 3. 测试 SetDoc (更新内容)
|
2026-05-15 01:55:34 +08:00
|
|
|
doc1.TextContent = "# Header 1\nUpdated Content"
|
|
|
|
|
err = app.SetDoc(doc1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Update SetDoc failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if doc1.Version != 2 {
|
2026-05-15 14:51:36 +08:00
|
|
|
t.Fatalf("Expected version 2, got %d", doc1.Version)
|
2026-05-15 01:55:34 +08:00
|
|
|
}
|
|
|
|
|
if !updatedCalled {
|
|
|
|
|
t.Error("OnUpdatedDoc event not triggered")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 14:07:01 +08:00
|
|
|
// 4. 测试 GetByVersion
|
2026-05-15 14:51:36 +08:00
|
|
|
hist, err := app.GetByVersion(docPath, 1)
|
2026-05-15 14:07:01 +08:00
|
|
|
if err != nil || hist == nil {
|
|
|
|
|
t.Fatalf("GetByVersion(1) failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(hist.TextContent, "Content 1") {
|
2026-05-15 14:51:36 +08:00
|
|
|
t.Fatalf("Expected 'Content 1' in history, got %s", hist.TextContent)
|
2026-05-15 14:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-15 14:51:36 +08:00
|
|
|
// 5. 测试 Move (重命名)
|
|
|
|
|
newPath := "/test/renamed.md"
|
|
|
|
|
err = app.Move(docPath, newPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Move failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !movedCalled {
|
|
|
|
|
t.Error("OnMoved event not triggered")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查旧路径失效,新路径生效
|
|
|
|
|
oldDoc, _ := app.Get(docPath)
|
|
|
|
|
if oldDoc != nil {
|
|
|
|
|
t.Error("Old path should be gone")
|
|
|
|
|
}
|
|
|
|
|
newDoc, err := app.Get(newPath)
|
|
|
|
|
if err != nil || newDoc == nil {
|
|
|
|
|
t.Fatalf("New path should work: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if newDoc.Title != "Test Doc" {
|
|
|
|
|
t.Error("Title should remain same after move")
|
2026-05-15 14:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-15 14:51:36 +08:00
|
|
|
// 6. 测试 Remove
|
|
|
|
|
err = app.Remove(newPath)
|
2026-05-15 01:55:34 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Remove failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !removedCalled {
|
|
|
|
|
t.Error("OnRemoved event not triggered")
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 14:51:36 +08:00
|
|
|
removedDoc, _ := app.Get(newPath)
|
2026-05-15 14:35:00 +08:00
|
|
|
if removedDoc != nil {
|
2026-05-15 14:51:36 +08:00
|
|
|
t.Error("Document should be removed")
|
2026-05-15 14:35:00 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-15 14:51:36 +08:00
|
|
|
// 7. 测试删除后依然能获取历史
|
|
|
|
|
lastHist, err := app.GetByVersion(newPath, 2)
|
|
|
|
|
if err != nil || lastHist == nil {
|
|
|
|
|
t.Fatalf("Should get history after removal: %v", err)
|
2026-05-15 14:35:00 +08:00
|
|
|
}
|
2026-05-15 14:51:36 +08:00
|
|
|
if lastHist.Version != 2 {
|
|
|
|
|
t.Fatalf("Expected version 2, got %d", lastHist.Version)
|
2026-05-15 01:55:34 +08:00
|
|
|
}
|
|
|
|
|
}
|