feat(channels): 流控可配 —— 陌生人私聊额度走账号配置 directMsgPerMin(裁决 13)
- 后端:账号顶层 directMsgPerMin(缺失/0 = 保护性默认 6 条/分;正值 = 该值;负值 = 关闭并留带 source 的 WARN)
- 日志如实报出**实际生效**额度(adapter.(*FlowGate).DirectPerMin()),告警文案不再写死 6
- 面板:渠道配置表单加字段(含帮助文案)+ 回显/写回链路(LIMIT_KEYS 四键)+ 账号卡片摘要
- 文案纠错:AdapterConfigSummary 的 msgPerMin 由「私聊每分钟上限」改为「非主人消息每分钟总上限」(原描述是误导),并列补 directMsgPerMin 行
- 变异实测:缺键丢默认 / 负值当 1 条每分 / 前端吞负值 —— 三个都精确打红并还原
- ⚠️ 已知缺口(下一片补,gateway 面):GET/PUT /admin/channels 尚未下发/解码该键 ⇒ 面板暂时回显空、保存会把 -1 重置为 0
This commit is contained in:
1 parent
ba17820040
commit
a057a0f61f
18 files changed
+339
-70
No files matched your search
@@ -1,8 +1,12 @@
|
||||
package channelbridge
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/session"
|
||||
@@ -104,8 +108,9 @@ func TestFlowGate_OwnerPrivateBypassesGate(t *testing.T) {
|
||||
|
||||
// TestFlowGate_GateOptionsDefaults 钉住「配置→限制」的解析规则,
|
||||
// 它保证旧的 CHANNELS_JSON 账号向后兼容:键缺失 → 关闭,0 → 保护性
|
||||
// 默认值,负值 → 关闭,正值 → 固定为该值。**私聊(陌生人)限流是例外**:
|
||||
// 它不读配置键,恒取保护性默认(裁决③:陌生人必须有流控)。
|
||||
// 默认值,负值 → 关闭,正值 → 固定为该值。**私聊(陌生人)限流同一口径,
|
||||
// 但缺省侧相反**:键缺失/非整数/0 一律取保护性默认 6(裁决③「陌生人必须有流控」
|
||||
// +裁决 13「流控时间要可配置」)——绝不因为「没配」而静默关掉。
|
||||
func TestFlowGate_GateOptionsDefaults(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -114,18 +119,25 @@ func TestFlowGate_GateOptionsDefaults(t *testing.T) {
|
||||
t.Fatalf("New: %v", err)
|
||||
}
|
||||
|
||||
// direct 列用**字面量 6**(该常量由 adapter/flowgate_direct_test.go 的
|
||||
// TestFlowDirectDefaultIsPinned 钉住):缺键时若解析成 0(= 关掉流控)本用例必须红。
|
||||
cases := []struct {
|
||||
name string
|
||||
cfg map[string]any
|
||||
group, max int
|
||||
direct int
|
||||
}{
|
||||
{"absent keys off", map[string]any{"bot_token": "x"}, 0, 0},
|
||||
{"group 0 defaults 6", map[string]any{"bot_token": "x", "groupMsgPerMin": 0}, 6, 0},
|
||||
{"group -1 off", map[string]any{"bot_token": "x", "groupMsgPerMin": -1}, 0, 0},
|
||||
{"group 10 pinned", map[string]any{"bot_token": "x", "groupMsgPerMin": 10}, 10, 0},
|
||||
{"max 0 defaults 50", map[string]any{"bot_token": "x", "maxMessages": 0}, 0, 50},
|
||||
{"max -1 off", map[string]any{"bot_token": "x", "maxMessages": -1}, 0, 0},
|
||||
{"max 80 pinned", map[string]any{"bot_token": "x", "maxMessages": 80}, 0, 80},
|
||||
{"absent keys off", map[string]any{"bot_token": "x"}, 0, 0, 6},
|
||||
{"group 0 defaults 6", map[string]any{"bot_token": "x", "groupMsgPerMin": 0}, 6, 0, 6},
|
||||
{"group -1 off", map[string]any{"bot_token": "x", "groupMsgPerMin": -1}, 0, 0, 6},
|
||||
{"group 10 pinned", map[string]any{"bot_token": "x", "groupMsgPerMin": 10}, 10, 0, 6},
|
||||
{"max 0 defaults 50", map[string]any{"bot_token": "x", "maxMessages": 0}, 0, 50, 6},
|
||||
{"max -1 off", map[string]any{"bot_token": "x", "maxMessages": -1}, 0, 0, 6},
|
||||
{"max 80 pinned", map[string]any{"bot_token": "x", "maxMessages": 80}, 0, 80, 6},
|
||||
{"direct 0 defaults 6", map[string]any{"bot_token": "x", "directMsgPerMin": 0}, 0, 0, 6},
|
||||
{"direct -1 off", map[string]any{"bot_token": "x", "directMsgPerMin": -1}, 0, 0, 0},
|
||||
{"direct 12 pinned", map[string]any{"bot_token": "x", "directMsgPerMin": 12}, 0, 0, 12},
|
||||
{"direct non-numeric defaults 6", map[string]any{"bot_token": "x", "directMsgPerMin": "abc"}, 0, 0, 6},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
@@ -136,16 +148,116 @@ func TestFlowGate_GateOptionsDefaults(t *testing.T) {
|
||||
if opts.MaxMessages != c.max {
|
||||
t.Fatalf("MaxMessages = %d, want %d", opts.MaxMessages, c.max)
|
||||
}
|
||||
// 陌生人私聊的强制流控不随账号配置开关:无论群聊/上限怎么配,
|
||||
// 私聊限流恒为保护性默认(主人 2026-10-05 裁决③)。
|
||||
if opts.DirectMsgPerMin != adapter.FlowDirectDefault {
|
||||
t.Fatalf("DirectMsgPerMin = %d, want the protective default %d",
|
||||
opts.DirectMsgPerMin, adapter.FlowDirectDefault)
|
||||
if opts.DirectMsgPerMin != c.direct {
|
||||
t.Fatalf("DirectMsgPerMin = %d, want %d(缺失/0 走保护性默认 6,负值=关闭,正值=该值)",
|
||||
opts.DirectMsgPerMin, c.direct)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFlowGate_DirectLimitFromAccountConfig 验证 directMsgPerMin 从账号配置到闸门的
|
||||
// **整条链**:键缺失 ⇒ 保护性默认 6 条/分真的拦下第 7 条;正值 ⇒ 该值;负值 ⇒ 关闭
|
||||
// (全部放行)。计数按发送者独立(AllowDirect 的会话键含 sender)。
|
||||
func TestFlowGate_DirectLimitFromAccountConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
cfg map[string]any
|
||||
// allowed 是限制内会被放行的条数;off = 限制被显式关闭(第 allowed+1 条不会被拒)。
|
||||
allowed int
|
||||
off bool
|
||||
}{
|
||||
{"absent key keeps the protective default 6", map[string]any{"bot_token": "x"}, 6, false},
|
||||
{"pinned 2", map[string]any{"bot_token": "x", "directMsgPerMin": 2}, 2, false},
|
||||
{"negative disables the limit", map[string]any{"bot_token": "x", "directMsgPerMin": -1}, 12, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
c := c
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
b, ad := newGateBridge(t, c.cfg)
|
||||
gate := b.gateFor(ad)
|
||||
if gate == nil {
|
||||
t.Fatal("gateFor = nil, want a gate for the configured adapter")
|
||||
}
|
||||
for i := 0; i < c.allowed; i++ {
|
||||
if !gate.AllowDirect(strangerDirectMsg("777", "第 "+itoa(i)+" 句")) {
|
||||
t.Fatalf("额度内第 %d 条被拒,want allowed", i+1)
|
||||
}
|
||||
}
|
||||
// 限制生效时第 allowed+1 条必须被拒(off=false);关闭时(off=true)必须放行。
|
||||
if over := gate.AllowDirect(strangerDirectMsg("777", "第 N+1 句")); over != c.off {
|
||||
t.Fatalf("第 %d 条的放行结果 = %v(off=%v),want %v",
|
||||
c.allowed+1, over, c.off, c.off)
|
||||
}
|
||||
// 另一个发送者有独立额度:不被前一个发送者挤占。
|
||||
if !gate.AllowDirect(strangerDirectMsg("888", "我是另一个人")) {
|
||||
t.Fatal("另一个发送者的额度被挤占")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// newGateBridge 构建一条只用于流控解析的桥接器(telegram 适配器 + 给定账号配置),
|
||||
// 使 directMsgPerMin 走与生产完全相同的「Config() → gateOptions」链。
|
||||
func newGateBridge(t *testing.T, cfg map[string]any) (*Bridge, *fakeAdapter) {
|
||||
t.Helper()
|
||||
b, err := New(Options{Loop: &stubChatter{}, Sessions: &stubSessions{}})
|
||||
if err != nil {
|
||||
t.Fatalf("New: %v", err)
|
||||
}
|
||||
ad := &fakeAdapter{name: "telegram", cfg: cfg}
|
||||
b.Add(ad)
|
||||
return b, ad
|
||||
}
|
||||
|
||||
// TestFlowGate_DirectLimitOffIsLogged 钉住「负值关闭必须留痕」:主人显式关掉熟人/
|
||||
// 陌生人私聊流控时,日志里必须出现一条带 source 的 WARN(不许静默关闭);而正值或
|
||||
// 缺省(保护性默认)绝不产生这条告警(否则主人会以为流控被关了)。
|
||||
func TestFlowGate_DirectLimitOffIsLogged(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var buf bytes.Buffer
|
||||
log := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
||||
at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
|
||||
b, _, ad := newStrangerBridge(t, &stubChatter{}, &captureHistory{}, at, log)
|
||||
|
||||
ad.cfg = map[string]any{"bot_token": "x", "directMsgPerMin": -1}
|
||||
if opts := b.gateOptions(ad); opts.DirectMsgPerMin != 0 {
|
||||
t.Fatalf("DirectMsgPerMin = %d, want 0(负值 = 关闭)", opts.DirectMsgPerMin)
|
||||
}
|
||||
logged := buf.String()
|
||||
if !strings.Contains(logged, "non-owner private rate limit turned off") ||
|
||||
!strings.Contains(logged, "source=services/channelbridge/flowgate") ||
|
||||
!strings.Contains(logged, "directMsgPerMin") {
|
||||
t.Fatalf("负值关闭流控没有留下带 source 的可见告警:%q", logged)
|
||||
}
|
||||
|
||||
// 正值:不告警,且额度就是该值。
|
||||
buf.Reset()
|
||||
ad.cfg = map[string]any{"bot_token": "x", "directMsgPerMin": 12}
|
||||
if opts := b.gateOptions(ad); opts.DirectMsgPerMin != 12 {
|
||||
t.Fatalf("DirectMsgPerMin = %d, want 12", opts.DirectMsgPerMin)
|
||||
}
|
||||
if got := buf.String(); got != "" {
|
||||
t.Fatalf("正值不该产生「流控被关闭」告警:%q", got)
|
||||
}
|
||||
|
||||
// 缺省(保护性默认):同样不告警 —— 默认不是「主人关掉了它」。
|
||||
buf.Reset()
|
||||
ad.cfg = map[string]any{"bot_token": "x"}
|
||||
if opts := b.gateOptions(ad); opts.DirectMsgPerMin != adapter.FlowDirectDefault {
|
||||
t.Fatalf("DirectMsgPerMin = %d, want the protective default %d",
|
||||
opts.DirectMsgPerMin, adapter.FlowDirectDefault)
|
||||
}
|
||||
if got := buf.String(); got != "" {
|
||||
t.Fatalf("缺省不该产生「流控被关闭」告警:%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// mockLLM 是返回固定补全的 model.Adapter 桩。
|
||||
type mockLLM struct {
|
||||
content string
|
||||
|
||||
Reference in new issue
Block a user