Files
Cyrene_v2/internal/services/gateway/admin_rag_delete_test.go
T
AskaEth 2dee372fb2
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(backthink/admin): ③b — rag/delete 真实现 + D1 推送出口钩子(自建优先→个推兜底) + api-reference 补 D2/D3/D5 补丁
- 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 即可
2026-09-09 00:08:29 +08:00

97 lines
3.1 KiB
Go

package gateway
import (
"context"
"encoding/json"
"net/http"
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/auth"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/knowledge"
)
// TestAdminRagDeleteRemovesDocument proves the delete write path is real: the
// indexed document (project entry + its chapters) is gone from the list and the
// search, and deleting it again answers 404 rather than a fabricated success.
func TestAdminRagDeleteRemovesDocument(t *testing.T) {
t.Parallel()
ctx := context.Background()
rag := newRagStoreForGateway(t)
if err := rag.IndexDocument(ctx, knowledge.Doc{
ID: "doc-1",
Name: "Cyrene 设计",
Desc: "v2 设计文档",
Content: "第一段:项目描述。\n\n第二段:内部实现要点。",
}); err != nil {
t.Fatalf("IndexDocument: %v", err)
}
srv, ts, jwt := buildEnv(t, cfgDefault(), Deps{RAG: rag})
_ = srv
tok := signAny(t, jwt, "acct", auth.RoleOwner)
resp, body := doReq(t, http.MethodPost, ts.URL+"/api/v1/admin/rag/delete", map[string]any{"id": "doc-1"}, tok)
if resp.StatusCode != http.StatusOK {
t.Fatalf("delete status = %d, want 200 (body=%s)", resp.StatusCode, body)
}
var res struct {
ID string `json:"id"`
Deleted bool `json:"deleted"`
}
if err := json.Unmarshal(decodeBody(t, body)["data"], &res); err != nil {
t.Fatalf("decode: %v (body=%s)", err, body)
}
if res.ID != "doc-1" || !res.Deleted {
t.Fatalf("response = %+v, want id=doc-1 deleted=true", res)
}
docs, err := rag.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 := rag.Search(ctx, "内部实现")
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(hits) != 0 {
t.Fatalf("hits = %+v, want none after delete", hits)
}
resp2, body2 := doReq(t, http.MethodPost, ts.URL+"/api/v1/admin/rag/delete", map[string]any{"id": "doc-1"}, tok)
if resp2.StatusCode != http.StatusNotFound {
t.Fatalf("second delete status = %d, want 404 (body=%s)", resp2.StatusCode, body2)
}
wantErrorShape(t, body2)
}
// TestAdminRagDeleteUnwired501 asserts an unconfigured RAG service still answers
// honestly instead of pretending the document was removed.
func TestAdminRagDeleteUnwired501(t *testing.T) {
t.Parallel()
srv, ts, jwt := buildEnv(t, cfgDefault(), Deps{})
_ = srv
tok := signAny(t, jwt, "acct", auth.RoleOwner)
resp, body := doReq(t, http.MethodPost, ts.URL+"/api/v1/admin/rag/delete", map[string]any{"id": "doc-1"}, tok)
if resp.StatusCode != http.StatusNotImplemented {
t.Fatalf("status = %d, want 501 (body=%s)", resp.StatusCode, body)
}
wantErrorShape(t, body)
}
// TestAdminRagDeleteRequiresOwner asserts the write route stays owner-gated.
func TestAdminRagDeleteRequiresOwner(t *testing.T) {
t.Parallel()
srv, ts, jwt := buildEnv(t, cfgDefault(), Deps{RAG: newRagStoreForGateway(t)})
_ = srv
tok := signAny(t, jwt, "acct", auth.RoleAdmin)
resp, _ := doReq(t, http.MethodPost, ts.URL+"/api/v1/admin/rag/delete", map[string]any{"id": "doc-1"}, tok)
if resp.StatusCode != http.StatusForbidden {
t.Fatalf("status = %d, want 403", resp.StatusCode)
}
}