Files
AskaEth 074ffc4ebf
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
docs(gateway): gateway 剩余文件注释改中文(逐行 1:1,代码零字节改动)
主人 2026-10-03 拍板「注释一律中文」;gateway 至此整包注释中文化完成(本会话共 3 批:
已推库 16 个文件 + 本批剩余文件)。
校验:机械校验(非注释改动行)= 空;剥注释后代码文本逐行比对全等(不一致文件数 0);
全树 go test ./... 58 包 ok / 0 FAIL;交叉编译绿。
2026-10-03 22:28:25 +08:00

65 lines
2.6 KiB
Go

package gateway
import (
"context"
"testing"
capreg "git.yeij.top/AskaEth/Cyrene_v2/internal/capability/registry"
)
// testChannel 是一个裸 sdk.Channel,用于演练 findChannel 的解析。
type testChannel struct{ name string }
func (t testChannel) Name() string { return t.name }
func (t testChannel) Connect(context.Context) error {
return nil
}
func (t testChannel) Disconnect(context.Context) error { return nil }
func (t testChannel) IsConnected() bool { return false }
func (t testChannel) HealthCheck(context.Context) error { return nil }
// TestFindChannelExactMatch 优先精确适配器名而非裸名匹配。
func TestFindChannelExactMatch(t *testing.T) {
reg := capreg.New()
_ = reg.Register(capreg.Capability{Name: "kook:conf0", Kind: capreg.KindChannel, Value: testChannel{name: "kook:conf0"}})
_ = reg.Register(capreg.Capability{Name: "telegram:conf0", Kind: capreg.KindChannel, Value: testChannel{name: "telegram:conf0"}})
ch, ok := findChannel(reg, "kook:conf0")
if !ok || ch.Name() != "kook:conf0" {
t.Fatalf("exact kook:conf0 not found, got %v/%v", ch, ok)
}
ch, ok = findChannel(reg, "telegram:conf0")
if !ok || ch.Name() != "telegram:conf0" {
t.Fatalf("exact telegram:conf0 not found, got %v/%v", ch, ok)
}
}
// TestFindChannelBarePlatformResolvesToTokenAccount 验证非精确适配器名的
// 裸平台名("kook")会解析到该平台首个已注册账号
// (即携带 token 的那个,因为空 token 的裸账号
// 从不注册)。
func TestFindChannelBarePlatformResolvesToTokenAccount(t *testing.T) {
reg := capreg.New()
_ = reg.Register(capreg.Capability{Name: "kook:3fc8", Kind: capreg.KindChannel, Value: testChannel{name: "kook:3fc8"}})
ch, ok := findChannel(reg, "kook")
if !ok || ch.Name() != "kook:3fc8" {
t.Fatalf("bare kook should resolve to kook:3fc8, got %v/%v", ch, ok)
}
}
// TestFindChannelUnknownOrBlank 拒绝不匹配任何渠道的名字。
func TestFindChannelUnknownOrBlank(t *testing.T) {
reg := capreg.New()
_ = reg.Register(capreg.Capability{Name: "kook:conf0", Kind: capreg.KindChannel, Value: testChannel{name: "kook:conf0"}})
if _, ok := findChannel(reg, "unknown"); ok {
t.Fatal("unknown name should not be found")
}
if _, ok := findChannel(reg, ""); ok {
t.Fatal("blank name should not be found")
}
// 已跳过:非渠道能力不得匹配。
_ = reg.Register(capreg.Capability{Name: "some-tool", Kind: capreg.KindTool, Value: testChannel{name: "some-tool"}})
if _, ok := findChannel(reg, "some-tool"); ok {
t.Fatal("non-channel capability must not match findChannel")
}
}