Files
Cyrene_v2/internal/config/runtimeconfig/channels_test.go
T
Aska b614633432
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
fix: KOOK 连不上根因——空token裸账号不注册/占名 + open指向带token账号 + 前端token不覆盖真值
agent-119 后端:
- buildAccountAdapter: token平台(kook/telegram/weixin)BotToken空→跳过build(不注册不占名), channelAccountsFromEnv 无tokenenv不合成账号(杜绝裸kook id="")
- runtimeconfig cleanAccounts: LoadChannels/SaveChannels 丢弃token平台空token账号(清理channels.json现有裸kook, 下次保存抹掉)
- findChannel: 裸平台名(kook)回退到该平台第一个带token账号, open"kook"不再命中裸的missing token

我补 前端token掩码不就覆真值:
- openConfig 不再把'(已配置,仅掩码显示)'当token回填(setToken空)→save token空不写bot_token保留真值
- 加 tokenHint(显示'已配置留空不改'/重填换); 修之前channels.json kook:3fc8 token变掩码串的bug
全仓 build/vet/test绿+前端build绿(463行)
2026-09-07 16:01:47 +08:00

259 lines
8.7 KiB
Go

package runtimeconfig
import (
"os"
"path/filepath"
"strings"
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
)
func TestLoadChannelsFileWins(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "channels.json")
if err := os.WriteFile(path, []byte(`{"kook":[{"id":"a","config":{"bot_token":"tk"}}]}`), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
accounts, warn := LoadChannels(path, map[string][]config.ChannelAccount{
"telegram": {{ID: "env", Config: map[string]any{"bot_token": "env"}}},
})
if warn != nil {
t.Fatalf("warn = %v, want nil", warn)
}
if len(accounts["kook"]) != 1 || accounts["kook"][0].ID != "a" || accounts["kook"][0].Config["bot_token"] != "tk" {
t.Fatalf("file accounts not used: %+v", accounts)
}
if _, ok := accounts["telegram"]; ok {
t.Fatalf("file should win over env fallback, telegram leaked in: %+v", accounts)
}
}
func TestLoadChannelsMissingFallsBackToEnv(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "nope.json")
fallback := map[string][]config.ChannelAccount{"telegram": {{ID: "env", Config: map[string]any{"bot_token": "tk"}}}}
accounts, warn := LoadChannels(path, fallback)
if warn != nil {
t.Fatalf("warn = %v, want nil for missing file", warn)
}
if len(accounts["telegram"]) != 1 || accounts["telegram"][0].ID != "env" {
t.Fatalf("fallback not used: %+v", accounts)
}
}
func TestLoadChannelsInvalidFallsBackWithWarn(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "channels.json")
if err := os.WriteFile(path, []byte(`{not-json`), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
fallback := map[string][]config.ChannelAccount{"kook": {{Config: map[string]any{}}}}
accounts, warn := LoadChannels(path, fallback)
if warn == nil {
t.Fatal("warn = nil, want a warning for an invalid file")
}
if len(accounts["kook"]) != 1 {
t.Fatalf("invalid file did not fall back to env: %+v", accounts)
}
}
func TestLoadChannelsEmptyFileFallsBackWithWarn(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "channels.json")
if err := os.WriteFile(path, []byte(" \n"), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
fallback := map[string][]config.ChannelAccount{"kook": {{Config: map[string]any{}}}}
accounts, warn := LoadChannels(path, fallback)
if warn == nil {
t.Fatal("warn = nil, want a warning for an empty file")
}
if len(accounts["kook"]) != 1 {
t.Fatalf("empty file did not fall back to env: %+v", accounts)
}
}
func TestLoadChannelsEmptyMapFallsBackWithWarn(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "channels.json")
if err := os.WriteFile(path, []byte(`{}`), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
fallback := map[string][]config.ChannelAccount{"kook": {{Config: map[string]any{}}}}
accounts, warn := LoadChannels(path, fallback)
if warn == nil {
t.Fatal("warn = nil, want a warning for an empty map")
}
if len(accounts["kook"]) != 1 {
t.Fatalf("empty map did not fall back to env: %+v", accounts)
}
}
func TestLoadChannelsCleansEmptyTokenKook(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "channels.json")
// A stale channels.json holding a bare empty-token kook plus a real account.
raw := `{"kook":[
{"id":"","config":{"base_url":"","bot_token":""}},
{"id":"3fc8ef9434bddfbbe4f4daa8da21c093","config":{"bot_token":"real-token"}}
]}`
if err := os.WriteFile(path, []byte(raw), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
accounts, warn := LoadChannels(path, nil)
if warn != nil {
t.Fatalf("warn = %v, want nil", warn)
}
kook := accounts["kook"]
if len(kook) != 1 {
t.Fatalf("kook = %+v, want only the token-bearing account", kook)
}
if kook[0].ID != "3fc8ef9434bddfbbe4f4daa8da21c093" || kook[0].Config["bot_token"] != "real-token" {
t.Fatalf("kook = %+v, want the token-bearing account", kook[0])
}
}
func TestLoadChannelsKeepsNonTokenPlatforms(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "channels.json")
// qq-obv11/webhook carry no bot token and must be preserved.
raw := `{"qq-obv11":[{"id":"","config":{"mode":"server"}}],"webhook":[{"id":"","config":{"endpointPath":""}}]}`
if err := os.WriteFile(path, []byte(raw), 0o600); err != nil {
t.Fatalf("write: %v", err)
}
accounts, warn := LoadChannels(path, nil)
if warn != nil {
t.Fatalf("warn = %v", warn)
}
if len(accounts["qq-obv11"]) != 1 || len(accounts["webhook"]) != 1 {
t.Fatalf("non-token platforms should be preserved: %+v", accounts)
}
}
func TestSaveChannelsDropsEmptyTokenBareAccount(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "channels.json")
accts := map[string][]config.ChannelAccount{
"kook": {
{ID: "", Config: map[string]any{"bot_token": ""}},
{ID: "a", Config: map[string]any{"bot_token": "tk"}},
},
"webhook": {{ID: "", Config: map[string]any{"endpointPath": ""}}},
}
if err := SaveChannels(path, accts); err != nil {
t.Fatalf("save: %v", err)
}
got, warn := LoadChannels(path, nil)
if warn != nil {
t.Fatalf("load warn = %v", warn)
}
if len(got["kook"]) != 1 || got["kook"][0].ID != "a" {
t.Fatalf("kook = %+v, want only the token-bearing account", got["kook"])
}
if _, ok := got["webhook"]; !ok || len(got["webhook"]) != 1 {
t.Fatalf("webhook should be preserved untouched: %+v", got["webhook"])
}
}
func TestSaveChannelsRoundTrip(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "channels.json")
accts := map[string][]config.ChannelAccount{
"kook": {{ID: "a", Config: map[string]any{"bot_token": "tk"}}},
}
if err := SaveChannels(path, accts); err != nil {
t.Fatalf("save: %v", err)
}
got, warn := LoadChannels(path, nil)
if warn != nil {
t.Fatalf("load warn = %v", warn)
}
if len(got["kook"]) != 1 || got["kook"][0].Config["bot_token"] != "tk" {
t.Fatalf("round trip mismatch: %+v", got)
}
info, err := os.Stat(path)
if err != nil {
t.Fatalf("stat: %v", err)
}
if info.Mode().Perm()&0o077 != 0 {
t.Fatalf("channels.json mode = %v, want owner-only (0600)", info.Mode().Perm())
}
}
func TestUpsertChannelAssignsIDAndReplaces(t *testing.T) {
t.Parallel()
accounts := map[string][]config.ChannelAccount{
"kook": {{ID: "a", Config: map[string]any{"bot_token": "old"}}},
}
// Blank ID → generated.
added := UpsertChannel(accounts, "kook", config.ChannelAccount{Config: map[string]any{"bot_token": "new"}})
if len(added["kook"]) != 2 {
t.Fatalf("want 2 accounts after add, got %d", len(added["kook"]))
}
if added["kook"][1].ID == "" {
t.Fatal("new account should get a generated id")
}
// Same ID → replaced in place.
replaced := UpsertChannel(accounts, "kook", config.ChannelAccount{ID: "a", Config: map[string]any{"bot_token": "updated"}})
if len(replaced["kook"]) != 2 {
t.Fatalf("want 2 accounts after replace, got %d", len(replaced["kook"]))
}
if replaced["kook"][0].Config["bot_token"] != "updated" {
t.Fatalf("replace did not update in place: %+v", replaced["kook"][0])
}
}
func TestDeleteChannelRemovesAccountAndDropsPlatform(t *testing.T) {
t.Parallel()
accounts := map[string][]config.ChannelAccount{
"kook": {{ID: "a", Config: map[string]any{}}, {ID: "b", Config: map[string]any{}}},
"telegram": {{ID: "t", Config: map[string]any{}}},
}
out := DeleteChannel(accounts, "kook", "a")
if len(out["kook"]) != 1 || out["kook"][0].ID != "b" {
t.Fatalf("kook after delete: %+v", out["kook"])
}
// Deleting the last account drops the platform key entirely.
out2 := DeleteChannel(out, "kook", "b")
if _, ok := out2["kook"]; ok {
t.Fatalf("kook should be dropped when empty: %+v", out2)
}
// Other platforms untouched.
if len(out2["telegram"]) != 1 {
t.Fatalf("telegram affected: %+v", out2)
}
}
func TestMaskedRedactsTokensKeepsOriginal(t *testing.T) {
t.Parallel()
acct := config.ChannelAccount{
ID: "a",
Config: map[string]any{"bot_token": "super-secret-token", "base_url": "https://x", "access_token": "abc", "secret": "s"},
DenyUsers: []string{"u1"},
}
masked := Masked(acct)
if got := masked.Config["bot_token"]; got != "****oken" {
t.Fatalf("bot_token = %v, want masked", got)
}
if got := masked.Config["access_token"]; got != "****" {
t.Fatalf("access_token = %v, want fully masked", got)
}
if got := masked.Config["secret"]; got != "****" {
t.Fatalf("secret = %v, want fully masked", got)
}
if got := masked.Config["base_url"]; got != "https://x" {
t.Fatalf("base_url = %v, want unchanged", got)
}
if masked.DenyUsers[0] != "u1" {
t.Fatalf("deny user list lost: %+v", masked)
}
// The original account still carries the raw token.
if acct.Config["bot_token"] != "super-secret-token" {
t.Fatalf("Masked mutated the original account")
}
if strings.Contains(masked.Config["bot_token"].(string), "super-secret") {
t.Fatalf("masked leaked the full token: %v", masked.Config["bot_token"])
}
}