mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-13 18:15:34 +00:00
The dashboard persists per-group/per-topic overrides (enabled, require_mention, skills, tools, system_prompt, ...) into the channel instance config, but telegramInstanceConfig had no groups field, so the payload was dropped on unmarshal. config.TelegramConfig.Groups stayed nil, resolveTopicConfig fell back to global defaults, and the bot replied in every forum topic regardless of the configured per-topic gating. Map the groups field through the DB-instance factory so per-topic config takes effect for dashboard-created channels, matching the static file-config path.
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package telegram
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
const validBotToken = "123456:abcdefghijklmnopqrstuvwxyzABCDE1234"
|
|
|
|
func TestFactory_PropagatesGroupsAndTopics(t *testing.T) {
|
|
creds := json.RawMessage(`{"token":"` + validBotToken + `"}`)
|
|
cfg := json.RawMessage(`{
|
|
"groups": {
|
|
"-1003957927661": {
|
|
"enabled": false,
|
|
"topics": {
|
|
"13": { "enabled": true }
|
|
}
|
|
}
|
|
}
|
|
}`)
|
|
|
|
ch, err := Factory("telegram-test", creds, cfg, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Factory: %v", err)
|
|
}
|
|
tg, ok := ch.(*Channel)
|
|
if !ok {
|
|
t.Fatalf("Factory returned %T, want *Channel", ch)
|
|
}
|
|
|
|
if tg.config.Groups == nil {
|
|
t.Fatal("config.Groups is nil — dashboard groups/topics payload was dropped on unmarshal")
|
|
}
|
|
group, ok := tg.config.Groups["-1003957927661"]
|
|
if !ok || group == nil {
|
|
t.Fatal("group -1003957927661 missing from resolved config")
|
|
}
|
|
if group.Enabled == nil || *group.Enabled != false {
|
|
t.Errorf("group.Enabled = %v, want false", group.Enabled)
|
|
}
|
|
|
|
allowed := resolveTopicConfig(tg.config, "-1003957927661", 13)
|
|
if !allowed.isEnabled() {
|
|
t.Error("topic 13: isEnabled() = false, want true")
|
|
}
|
|
other := resolveTopicConfig(tg.config, "-1003957927661", 99)
|
|
if other.isEnabled() {
|
|
t.Error("topic 99: isEnabled() = true, want false")
|
|
}
|
|
general := resolveTopicConfig(tg.config, "-1003957927661", telegramGeneralTopicID)
|
|
if general.isEnabled() {
|
|
t.Error("General topic: isEnabled() = true, want false")
|
|
}
|
|
}
|
|
|
|
func TestFactory_NilGroupsWhenAbsent(t *testing.T) {
|
|
creds := json.RawMessage(`{"token":"` + validBotToken + `"}`)
|
|
cfg := json.RawMessage(`{"group_policy":"open"}`)
|
|
|
|
ch, err := Factory("telegram-test", creds, cfg, nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("Factory: %v", err)
|
|
}
|
|
tg := ch.(*Channel)
|
|
if tg.config.Groups != nil {
|
|
t.Errorf("config.Groups = %v, want nil when groups absent", tg.config.Groups)
|
|
}
|
|
def := resolveTopicConfig(tg.config, "-100123", 0)
|
|
if !def.isEnabled() {
|
|
t.Error("isEnabled() = false, want true by default when no overrides set")
|
|
}
|
|
}
|