- B5: POST /admin/rag/delete 真做(DeleteDocument→DeleteProject 级联删章节+索引清理, owner+审计, 200/404/500, 500降级兜底); users/iot scenarios+rules 诚实501(无用户表/IoT场景规则模型, 消息写明缺什么, 不伪造), 锁住 501+not_implemented
- D1: backthink/push_outlet.go PushExporter{Export}; pushOutlet 自建HTTP(2xx=送达, token仅Authorization头+error脱敏)优先→Getui兜底(未接入, 诚实日志'getui not integrated'+(false,nil)); backthink.Deps.Exporter → pusher.withExporter, Push 在WS/IM后 exportAsync(detached+5s超时, 失败仅日志, 不破坏现有投递, 限流不上报); config PushOutletConfig(PUSH_EXPORTER_* + GETUI_*占位)
- 前端 Rag.tsx/api.ts 删除后 reload + 类型; Docs/api-client v0.31 + api-reference(#23 owner/200-404, §8.4, 端点表PATCH/DELETE messages + pinned + messages?q=)
- 测试(push_outlet/admin_rag_delete/admin_honest501/knowledge store), 后端+前端build全绿+3平台交叉编译
- 刻意: Getui SDK 未接入(无SDK不硬造), 主人自建服务填 PUSH_EXPORTER_URL/TOKEN 即可
155 lines
4.4 KiB
Go
155 lines
4.4 KiB
Go
package knowledge
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/db"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/memory/projectindex"
|
|
)
|
|
|
|
func testLogger() *slog.Logger {
|
|
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
}
|
|
|
|
// newTestStore builds a Store backed by an isolated in-memory SQLite project index.
|
|
func newTestStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
d, err := db.Open(ctx, ":memory:", testLogger())
|
|
if err != nil {
|
|
t.Fatalf("open in-memory db: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = d.Close() })
|
|
ps, err := projectindex.New(ctx, d, testLogger())
|
|
if err != nil {
|
|
t.Fatalf("projectindex.New: %v", err)
|
|
}
|
|
return New(Deps{Projects: ps, Log: testLogger()})
|
|
}
|
|
|
|
func TestIndexDocumentPersistsProjectAndChapters(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
doc := Doc{Name: "Cyrene 设计", Desc: "v2 设计文档", Content: "这是项目描述段落。\n\n第二段:内部实现要点。\n第三行补充说明。"}
|
|
if err := s.IndexDocument(ctx, doc); err != nil {
|
|
t.Fatalf("IndexDocument: %v", err)
|
|
}
|
|
|
|
docs, err := s.ListDocuments(ctx)
|
|
if err != nil {
|
|
t.Fatalf("ListDocuments: %v", err)
|
|
}
|
|
if len(docs) != 1 {
|
|
t.Fatalf("documents = %d, want 1", len(docs))
|
|
}
|
|
if docs[0].Name != "Cyrene 设计" || docs[0].Desc != "v2 设计文档" {
|
|
t.Fatalf("doc = %+v, want name/desc set", docs[0])
|
|
}
|
|
if docs[0].Chunks != 2 {
|
|
t.Fatalf("chunks = %d, want 2 (two paragraphs)", docs[0].Chunks)
|
|
}
|
|
|
|
// The imported content is keyword-searchable via the project index.
|
|
hits, err := s.Search(ctx, "内部实现")
|
|
if err != nil {
|
|
t.Fatalf("Search: %v", err)
|
|
}
|
|
if len(hits) == 0 {
|
|
t.Fatalf("search returned no hits for imported content")
|
|
}
|
|
found := false
|
|
for _, h := range hits {
|
|
if h.DocName == "Cyrene 设计" && h.Chapter == "第 2 节" {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("hits = %+v, want chapter 第 2 节 under Cyrene 设计", hits)
|
|
}
|
|
}
|
|
|
|
func TestIndexDocumentRejectsBlankNameOrContent(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
|
|
if err := s.IndexDocument(ctx, Doc{Content: "有内容没名字"}); err == nil {
|
|
t.Fatal("IndexDocument with blank name should error")
|
|
}
|
|
if err := s.IndexDocument(ctx, Doc{Name: "有名字没内容"}); err == nil {
|
|
t.Fatal("IndexDocument with blank content should error")
|
|
}
|
|
}
|
|
|
|
func TestIndexDocumentRequiresProjectStore(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := New(Deps{Log: testLogger()}) // projects == nil
|
|
|
|
err := s.IndexDocument(ctx, Doc{Name: "x", Content: "y"})
|
|
if err == nil {
|
|
t.Fatal("IndexDocument without a project index should error")
|
|
}
|
|
}
|
|
|
|
// TestDeleteDocumentRemovesProjectAndChapters proves delete is a real round-trip:
|
|
// the document disappears from the list and the search, and deleting it again is
|
|
// a not-found rather than a silent success.
|
|
func TestDeleteDocumentRemovesProjectAndChapters(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
s := newTestStore(t)
|
|
if err := s.IndexDocument(ctx, Doc{
|
|
ID: "doc-1",
|
|
Name: "Cyrene 设计",
|
|
Content: "第一段:项目描述。\n\n第二段:内部实现要点。",
|
|
}); err != nil {
|
|
t.Fatalf("IndexDocument: %v", err)
|
|
}
|
|
|
|
if err := s.DeleteDocument(ctx, "doc-1"); err != nil {
|
|
t.Fatalf("DeleteDocument: %v", err)
|
|
}
|
|
docs, err := s.ListDocuments(ctx)
|
|
if err != nil {
|
|
t.Fatalf("ListDocuments: %v", err)
|
|
}
|
|
if len(docs) != 0 {
|
|
t.Fatalf("documents = %+v, want empty after delete", docs)
|
|
}
|
|
hits, err := s.Search(ctx, "内部实现")
|
|
if err != nil {
|
|
t.Fatalf("Search: %v", err)
|
|
}
|
|
if len(hits) != 0 {
|
|
t.Fatalf("hits = %+v, want none after delete", hits)
|
|
}
|
|
|
|
if err := s.DeleteDocument(ctx, "doc-1"); !errors.Is(err, ErrDocumentNotFound) {
|
|
t.Fatalf("second DeleteDocument err = %v, want ErrDocumentNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteDocumentBlankIDNotFound(t *testing.T) {
|
|
t.Parallel()
|
|
s := newTestStore(t)
|
|
if err := s.DeleteDocument(context.Background(), " "); !errors.Is(err, ErrDocumentNotFound) {
|
|
t.Fatalf("DeleteDocument blank id err = %v, want ErrDocumentNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteDocumentRequiresProjectStore(t *testing.T) {
|
|
t.Parallel()
|
|
s := New(Deps{Log: testLogger()}) // projects == nil
|
|
if err := s.DeleteDocument(context.Background(), "doc-1"); err == nil {
|
|
t.Fatal("DeleteDocument without a project index should error")
|
|
}
|
|
}
|