Files
AskaEth a057a0f61f
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
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
2026-10-05 13:50:29 +08:00

97 lines
3.1 KiB
Go

package config
import (
"reflect"
"testing"
)
func TestParseChannelAccounts_Empty(t *testing.T) {
t.Parallel()
got, err := ParseChannelAccounts("")
if err != nil {
t.Fatalf("ParseChannelAccounts(empty): %v", err)
}
if len(got) != 0 {
t.Fatalf("empty input = %d platforms, want 0", len(got))
}
}
func TestParseChannelAccounts_MultiAccountWithAllowDeny(t *testing.T) {
t.Parallel()
raw := `{
"telegram": [
{"id":"conf0","config":{"bot_token":"t0"},"denyUsers":["12345"],"denyGroups":["-100"]},
{"id":"conf1","config":{"bot_token":"t1"},"allowUsers":["u1","u2"],"allowGroups":["g1"]}
],
"qq-obv11": [
{"id":"bot0","config":{"remote_url":"http://x","mode":"client"}}
]
}`
got, err := ParseChannelAccounts(raw)
if err != nil {
t.Fatalf("ParseChannelAccounts: %v", err)
}
if len(got) != 2 {
t.Fatalf("platforms = %d, want 2", len(got))
}
tg := got["telegram"]
if len(tg) != 2 {
t.Fatalf("telegram accounts = %d, want 2", len(tg))
}
if tg[0].ID != "conf0" || tg[0].Config["bot_token"] != "t0" {
t.Fatalf("account[0] = %+v", tg[0])
}
if !reflect.DeepEqual(tg[0].DenyUsers, []string{"12345"}) || !reflect.DeepEqual(tg[0].DenyGroups, []string{"-100"}) {
t.Fatalf("account[0] deny lists = %+v", tg[0])
}
if !reflect.DeepEqual(tg[1].AllowUsers, []string{"u1", "u2"}) || !reflect.DeepEqual(tg[1].AllowGroups, []string{"g1"}) {
t.Fatalf("account[1] allow lists = %+v", tg[1])
}
qq := got["qq-obv11"]
if len(qq) != 1 || qq[0].ID != "bot0" || qq[0].Config["mode"] != "client" {
t.Fatalf("qq accounts = %+v", qq)
}
}
func TestParseChannelAccounts_FlowLimitFields(t *testing.T) {
t.Parallel()
raw := `{
"telegram": [
{"id":"conf0","config":{"bot_token":"t0"},"groupMsgPerMin":10,"directMsgPerMin":-1,"maxMessages":80,"msgPerMin":30}
]
}`
got, err := ParseChannelAccounts(raw)
if err != nil {
t.Fatalf("ParseChannelAccounts: %v", err)
}
a := got["telegram"][0]
// directMsgPerMin 是账号顶层字段(与 groupMsgPerMin 同一口径):负值 = 关闭该限制,
// 必须是 -1,绝不能被解析层吞成 0(0 的语义是「用保护性默认 6」)。
if a.GroupMsgPerMin != 10 || a.MsgPerMin != 30 || a.MaxMessages != 80 || a.DirectMsgPerMin != -1 {
t.Fatalf("flow fields = group:%d direct:%d msg:%d max:%d, want 10/-1/30/80",
a.GroupMsgPerMin, a.DirectMsgPerMin, a.MsgPerMin, a.MaxMessages)
}
}
func TestParseChannelAccounts_NoFlowFieldsDefaultsToZero(t *testing.T) {
t.Parallel()
raw := `{"telegram":[{"id":"conf0","config":{"bot_token":"t0"}}]}`
got, err := ParseChannelAccounts(raw)
if err != nil {
t.Fatalf("ParseChannelAccounts: %v", err)
}
a := got["telegram"][0]
// Absent fields stay zero; the bridge maps zero to the protective defaults
// (6 group/min, 6 direct/min, 50 max), so old accounts are backward compatible.
if a.GroupMsgPerMin != 0 || a.DirectMsgPerMin != 0 || a.MsgPerMin != 0 || a.MaxMessages != 0 {
t.Fatalf("absent flow fields should be zero, got %+v", a)
}
}
func TestParseChannelAccounts_Malformed(t *testing.T) {
t.Parallel()
if _, err := ParseChannelAccounts(`{`); err == nil {
t.Fatal("expected error for malformed JSON")
}
}