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,"maxMessages":80,"msgPerMin":30} ] }` got, err := ParseChannelAccounts(raw) if err != nil { t.Fatalf("ParseChannelAccounts: %v", err) } a := got["telegram"][0] if a.GroupMsgPerMin != 10 || a.MsgPerMin != 30 || a.MaxMessages != 80 { t.Fatalf("flow fields = group:%d msg:%d max:%d, want 10/30/80", a.GroupMsgPerMin, 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, 50 max), so old accounts are backward compatible. if a.GroupMsgPerMin != 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") } }