优化代码

This commit is contained in:
hero920103 committed 2026-10-28 08:47:09 +08:00
1 parent 3553790475
commit eda7e8bc96
23 files changed
+3113 -342

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
package agent
import (
"testing"
"viewsagent/internal/capture"
"viewsagent/internal/config"
"viewsagent/internal/llm"
)
// readyCfg 造一份只差一个接入点的配置。
func readyCfg(p config.Profile) *config.Config {
cfg := &config.Config{Models: []config.Profile{p}}
cfg.Active = p.Name
cfg.Normalize()
return cfg
}
func TestEnsureReadyRejectsIncompleteModel(t *testing.T) {
e := New(&config.Config{Models: []config.Profile{{Name: "空"}}}, nil)
if err := e.EnsureReady(); err == nil {
t.Fatal("模型未配置时应拒绝监听")
}
}
func TestEnsureReadyRequiresAPIKey(t *testing.T) {
e := New(readyCfg(config.Profile{
Name: "远程", BaseURL: "https://api.example.com/v1", Model: "vl-model",
}), nil)
if err := e.EnsureReady(); err == nil {
t.Fatal("远程服务缺少 API Key 时应拒绝监听")
}
}
func TestEnsureReadyAllowsLocalWithoutKey(t *testing.T) {
e := New(readyCfg(config.Profile{
Name: "本地", BaseURL: "http://localhost:11434/v1", Model: "qwen2.5vl:7b",
}), nil)
if err := e.EnsureReady(); err != nil {
t.Fatalf("本地服务不应要求 API Key: %v", err)
}
}
func TestEnsureReadyPassesWithKey(t *testing.T) {
e := New(readyCfg(config.Profile{
Name: "远程", BaseURL: "https://api.example.com/v1", Model: "vl-model", APIKey: "sk-test",
}), nil)
if err := e.EnsureReady(); err != nil {
t.Fatalf("配置完整时应通过检测: %v", err)
}
}
func TestStartWatchRefusesWhenModelNotReady(t *testing.T) {
e := New(readyCfg(config.Profile{
Name: "远程", BaseURL: "https://api.example.com/v1", Model: "vl-model",
}), nil)
e.region = capture.Region{X: 0, Y: 0, W: 100, H: 100}
if err := e.StartWatch(); err == nil {
t.Fatal("模型未接好时不应开始监听")
}
if e.State().Watching {
t.Fatal("监听不应被启动")
}
}
func TestActiveProfileIsUsed(t *testing.T) {
cfg := &config.Config{Models: []config.Profile{
{Name: "甲", Format: llm.FormatOpenAI, BaseURL: "https://a.example/v1", Model: "a-vl", APIKey: "sk-a"},
{Name: "乙", Format: llm.FormatAnthropic, BaseURL: "https://b.example", Model: "b-vl", APIKey: "sk-b"},
}}
cfg.Active = "乙"
cfg.Normalize()
e := New(cfg, nil)
st := e.State()
if st.Profile != "乙" || st.Model != "b-vl" {
t.Fatalf("状态应反映生效接入点: %+v", st)
}
if st.Format != llm.FormatAnthropic {
t.Fatalf("状态应带出接口格式: %s", st.Format)
}
}