跳到主要内容

测试与质量

项目没有配置外部 linter,质量门槛是 go test 与 go vet。

命令​

# 全部测试
go test ./...

# 单个包
go test ./internal/router -v

# 覆盖率
go test -cover ./internal/...

# 竞态检测(耗时较长)
go test -race ./...

测试组织​

  • 测试文件与生产代码同目录,*_test.go
  • 白盒测试放同包;需要黑盒时用 _test 后缀包
  • 表驱动测试配合 t.Run(name, func(t *testing.T))
  • 临时数据库用 t.TempDir()
  • 上游模拟用 httptest.NewServer()
  • 纯 testing 包,不引入 testify / ginkgo

示例:

func TestWeightedPick(t *testing.T) {
tests := []struct {
name string
weights []int
want int
}{
{"single", []int{10}, 0},
{"zero", []int{0, 10}, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := weightedPick(tt.weights)
if got != tt.want {
t.Errorf("got %d, want %d", got, tt.want)
}
})
}
}

重点覆盖​

  • 路由选择:权重、轮询、亲和、直达
  • 熔断 FSM:阶梯冷却、永久禁用、解禁
  • 协议适配器:OpenAI ↔ Anthropic ↔ Responses ↔ Gemini
  • 代理:重试转移、SSE 流式、上游错误
  • 门户:按密钥取数、白名单、脱敏
  • 数据库方言:upsert 占位符与参数对齐(离线 DryRun)

提交前检查​

go test ./...
go vet ./...
go test -race ./...
go build -o omnigate ./cmd/omnigate
cd web && npm run build

所有测试必须通过才提交。前端目前没有自动化测试,依赖手工 QA。

不要提交​

  • internal/webui/dist/*(由构建生成;.gitkeep 除外)
  • 本地 omnigate.db、config.yaml、logs/
  • 二进制 ./omnigate