- 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 即可
262 lines
8.5 KiB
Go
262 lines
8.5 KiB
Go
package backthink
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
|
)
|
|
|
|
// mockExporter records exported messages and can block or fail on demand so the
|
|
// pusher hook's non-blocking / failure-tolerant behaviour is testable.
|
|
type mockExporter struct {
|
|
mu sync.Mutex
|
|
msgs []adapter.UnifiedMessage
|
|
delivered bool
|
|
err error
|
|
started chan struct{}
|
|
startOnce sync.Once
|
|
release chan struct{}
|
|
}
|
|
|
|
func (m *mockExporter) Export(ctx context.Context, msg adapter.UnifiedMessage) (bool, error) {
|
|
m.startOnce.Do(func() {
|
|
if m.started != nil {
|
|
close(m.started)
|
|
}
|
|
})
|
|
if m.release != nil {
|
|
<-m.release
|
|
}
|
|
m.mu.Lock()
|
|
m.msgs = append(m.msgs, msg)
|
|
m.mu.Unlock()
|
|
return m.delivered, m.err
|
|
}
|
|
|
|
func (m *mockExporter) messages() []adapter.UnifiedMessage {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return append([]adapter.UnifiedMessage(nil), m.msgs...)
|
|
}
|
|
|
|
// TestPushOutletHTTPPrimaryDelivers proves the self-hosted service is the
|
|
// primary tier: the payload is POSTed with the bearer token and a 2xx response
|
|
// is reported as delivered.
|
|
func TestPushOutletHTTPPrimaryDelivers(t *testing.T) {
|
|
t.Parallel()
|
|
type payload struct {
|
|
Source string `json:"source"`
|
|
Title string `json:"title"`
|
|
Content string `json:"content"`
|
|
Platform string `json:"platform"`
|
|
Timestamp int64 `json:"timestamp"`
|
|
}
|
|
var (
|
|
got payload
|
|
auth string
|
|
ct string
|
|
)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
auth = r.Header.Get("Authorization")
|
|
ct = r.Header.Get("Content-Type")
|
|
if err := json.NewDecoder(r.Body).Decode(&got); err != nil {
|
|
t.Errorf("decode payload: %v", err)
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
exp := NewPushOutlet(PushOutletConfig{
|
|
Enabled: true,
|
|
URL: srv.URL,
|
|
Token: "s3cret-token",
|
|
Timeout: 2 * time.Second,
|
|
Log: discardLog(),
|
|
})
|
|
if exp == nil {
|
|
t.Fatal("NewPushOutlet returned nil for an enabled outlet")
|
|
}
|
|
delivered, err := exp.Export(context.Background(), adapter.UnifiedMessage{
|
|
Platform: "cyrene-backthink",
|
|
Content: "晚安,早点睡。",
|
|
Sender: adapter.Actor{DisplayName: "Cyrene"},
|
|
Timestamp: time.UnixMilli(1700000000000),
|
|
})
|
|
if err != nil || !delivered {
|
|
t.Fatalf("Export = (%v, %v), want (true, nil)", delivered, err)
|
|
}
|
|
if got.Source != "cyrene/backthink" || got.Title != "Cyrene" || got.Content != "晚安,早点睡。" || got.Platform != "cyrene-backthink" {
|
|
t.Fatalf("payload = %+v, want source/title/content/platform set", got)
|
|
}
|
|
if got.Timestamp != 1700000000000 {
|
|
t.Fatalf("timestamp = %d, want 1700000000000", got.Timestamp)
|
|
}
|
|
if auth != "Bearer s3cret-token" {
|
|
t.Fatalf("Authorization = %q, want the bearer token", auth)
|
|
}
|
|
if ct != "application/json" {
|
|
t.Fatalf("Content-Type = %q, want application/json", ct)
|
|
}
|
|
}
|
|
|
|
// TestPushOutletUnreachableLogsGetuiFallback asserts an unreachable primary is a
|
|
// logged miss (not an error), names the pending Getui fallback, and never logs
|
|
// the token.
|
|
func TestPushOutletUnreachableLogsGetuiFallback(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
|
url := srv.URL
|
|
srv.Close() // now unreachable
|
|
|
|
var buf bytes.Buffer
|
|
log := slog.New(slog.NewTextHandler(&buf, nil))
|
|
exp := NewPushOutlet(PushOutletConfig{Enabled: true, URL: url, Token: "s3cret-token", Timeout: 300 * time.Millisecond, Log: log})
|
|
|
|
delivered, err := exp.Export(context.Background(), adapter.UnifiedMessage{Content: "hi"})
|
|
if err != nil {
|
|
t.Fatalf("Export err = %v, want nil (logged miss)", err)
|
|
}
|
|
if delivered {
|
|
t.Fatal("Export delivered = true, want false for an unreachable primary")
|
|
}
|
|
if !strings.Contains(buf.String(), "getui") {
|
|
t.Fatalf("log = %q, want the pending Getui fallback note", buf.String())
|
|
}
|
|
if strings.Contains(buf.String(), "s3cret-token") {
|
|
t.Fatalf("log leaked the token: %q", buf.String())
|
|
}
|
|
}
|
|
|
|
// TestPushOutletHTTPErrorReportsMiss asserts a non-2xx primary response is a miss.
|
|
func TestPushOutletHTTPErrorReportsMiss(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
http.Error(w, "nope", http.StatusInternalServerError)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
exp := NewPushOutlet(PushOutletConfig{Enabled: true, URL: srv.URL, Timeout: 2 * time.Second, Log: discardLog()})
|
|
delivered, err := exp.Export(context.Background(), adapter.UnifiedMessage{Content: "hi"})
|
|
if err != nil {
|
|
t.Fatalf("Export err = %v, want nil (logged miss)", err)
|
|
}
|
|
if delivered {
|
|
t.Fatal("Export delivered = true, want false for a 500 response")
|
|
}
|
|
}
|
|
|
|
// TestNewPushOutletDisabledOrUnconfigured covers the two degraded shapes: the
|
|
// hook off (nil) and enabled without a primary URL (miss + Getui note).
|
|
func TestNewPushOutletDisabledOrUnconfigured(t *testing.T) {
|
|
t.Parallel()
|
|
if exp := NewPushOutlet(PushOutletConfig{Enabled: false, URL: "http://example.invalid"}); exp != nil {
|
|
t.Fatalf("NewPushOutlet disabled = %v, want nil", exp)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
log := slog.New(slog.NewTextHandler(&buf, nil))
|
|
exp := NewPushOutlet(PushOutletConfig{Enabled: true, Log: log})
|
|
if exp == nil {
|
|
t.Fatal("NewPushOutlet enabled without a URL = nil, want a fallback-only outlet")
|
|
}
|
|
delivered, err := exp.Export(context.Background(), adapter.UnifiedMessage{Content: "hi"})
|
|
if err != nil || delivered {
|
|
t.Fatalf("Export = (%v, %v), want (false, nil)", delivered, err)
|
|
}
|
|
if !strings.Contains(buf.String(), "getui") {
|
|
t.Fatalf("log = %q, want the Getui fallback note", buf.String())
|
|
}
|
|
}
|
|
|
|
// TestPusherExportsProactiveToOutlet asserts the hook mirrors the delivered
|
|
// proactive message to the outlet after the IM delivery.
|
|
func TestPusherExportsProactiveToOutlet(t *testing.T) {
|
|
t.Parallel()
|
|
exp := &mockExporter{delivered: true}
|
|
im := &mockIMAdapter{}
|
|
p := newPusher(nil, im, &mockGate{allow: true}, discardLog()).withExporter(exp)
|
|
p.exportDone = make(chan struct{}, 1)
|
|
|
|
res := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{Content: "在吗"}})
|
|
if !res.Pushed || res.Route != RouteIMBridge {
|
|
t.Fatalf("push = %+v, want pushed via IM bridge", res)
|
|
}
|
|
if im.count() != 1 {
|
|
t.Fatalf("im calls = %d, want 1", im.count())
|
|
}
|
|
select {
|
|
case <-p.exportDone:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("export hook did not run")
|
|
}
|
|
msgs := exp.messages()
|
|
if len(msgs) != 1 || msgs[0].Content != "在吗" {
|
|
t.Fatalf("exported = %+v, want the proactive content", msgs)
|
|
}
|
|
}
|
|
|
|
// TestPusherExportHookNonBlockingAndFailureTolerant asserts a hung/failing outlet
|
|
// never delays the push and never turns a delivered proactive message into a
|
|
// failure.
|
|
func TestPusherExportHookNonBlockingAndFailureTolerant(t *testing.T) {
|
|
t.Parallel()
|
|
started := make(chan struct{})
|
|
release := make(chan struct{})
|
|
exp := &mockExporter{delivered: false, err: errors.New("outlet down"), started: started, release: release}
|
|
im := &mockIMAdapter{}
|
|
p := newPusher(nil, im, &mockGate{allow: true}, discardLog()).withExporter(exp)
|
|
p.exportDone = make(chan struct{}, 1)
|
|
|
|
begin := time.Now()
|
|
res := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{Content: "hi"}})
|
|
if elapsed := time.Since(begin); elapsed > 100*time.Millisecond {
|
|
t.Fatalf("Push blocked on the outlet for %v, want non-blocking", elapsed)
|
|
}
|
|
if !res.Pushed || res.Route != RouteIMBridge {
|
|
t.Fatalf("push = %+v, want pushed via IM bridge despite the outlet failure", res)
|
|
}
|
|
select {
|
|
case <-started:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("exporter was never invoked")
|
|
}
|
|
close(release)
|
|
select {
|
|
case <-p.exportDone:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("export hook did not finish")
|
|
}
|
|
}
|
|
|
|
// TestPusherRateLimitedPushIsNotExported asserts a rate-limited push never
|
|
// reaches the outlet: the hook mirrors real proactive sends only.
|
|
func TestPusherRateLimitedPushIsNotExported(t *testing.T) {
|
|
t.Parallel()
|
|
exp := &mockExporter{delivered: true}
|
|
p := newPusher(nil, &mockIMAdapter{}, &mockGate{allow: false}, discardLog()).withExporter(exp)
|
|
p.exportDone = make(chan struct{}, 1)
|
|
|
|
res := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{Content: "hi"}})
|
|
if res.Route != RouteRateLimited {
|
|
t.Fatalf("route = %q, want rate_limited", res.Route)
|
|
}
|
|
select {
|
|
case <-p.exportDone:
|
|
t.Fatal("rate-limited push should not be exported")
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
if len(exp.messages()) != 0 {
|
|
t.Fatalf("exported = %+v, want none", exp.messages())
|
|
}
|
|
}
|