package runtimeconfig // 本文件覆盖裁决 06.4.2 的配置侧形状:channels.json / CHANNELS_JSON 的账号级 // `adminIds[]`(现有 allowUsers 之外)能被解析,并被 AdminIDs 汇总成 // platform → 管理员 id 列表。 import ( "encoding/json" "os" "path/filepath" "testing" "git.yeij.top/AskaEth/Cyrene_v2/internal/config" ) // TestAdminIDsAggregatesPerPlatform 覆盖多账号、重复与空白条目的合并去重。 func TestAdminIDsAggregatesPerPlatform(t *testing.T) { t.Parallel() accounts := map[string][]config.ChannelAccount{ "qq-obv11": { {ID: "conf0", AdminIDs: []string{" 10001 ", "10002"}}, {ID: "conf1", AdminIDs: []string{"10002", "", "10003"}}, }, "telegram": {{ID: "conf0"}}, } got := AdminIDs(accounts) ids := got["qq-obv11"] if len(ids) != 3 { t.Fatalf("qq-obv11 admin ids = %v, want 3 个去重后的 id", ids) } for i, want := range []string{"10001", "10002", "10003"} { if ids[i] != want { t.Errorf("ids[%d] = %q, want %q", i, ids[i], want) } } // 未声明 adminIds 的平台仍出现在结果里(空切片),这样调用方的一次循环就是整体替换: // 该平台上一轮的管理员层会被 SetPlatformAdmins(p, nil) 清掉,热重载不留悬挂管理员。 tg, ok := got["telegram"] if !ok { t.Error("未声明 adminIds 的平台也应出现在结果里(空切片),否则热重载无法清掉旧管理员层") } if len(tg) != 0 { t.Errorf("telegram admin ids = %v, want 空", tg) } if len(AdminIDs(nil)) != 0 { t.Error("nil accounts 应返回空映射") } } // TestLoadChannelsParsesAdminIDs 覆盖 channels.json 的账号级 adminIds[] 形状:文件优先 // 加载后能被 AdminIDs 读到(证明 schema 真的落到了配置结构上,而不是被静默丢弃)。 func TestLoadChannelsParsesAdminIDs(t *testing.T) { t.Parallel() path := filepath.Join(t.TempDir(), "channels.json") raw := map[string][]config.ChannelAccount{ "qq-obv11": {{ ID: "conf0", Config: map[string]any{"remote_url": "ws://127.0.0.1:3001"}, AdminIDs: []string{"10001", "10002"}, }}, } data, err := json.Marshal(raw) if err != nil { t.Fatalf("marshal: %v", err) } if err := os.WriteFile(path, data, 0o600); err != nil { t.Fatalf("write: %v", err) } accounts, warn := LoadChannels(path, nil) if warn != nil { t.Fatalf("LoadChannels warn = %v, want nil", warn) } ids := AdminIDs(accounts)["qq-obv11"] if len(ids) != 2 || ids[0] != "10001" || ids[1] != "10002" { t.Fatalf("channels.json 的 adminIds[] 未落到配置结构: %v", ids) } }