e2e_helpers_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //go:build e2e
  2. package integration
  3. import (
  4. "os"
  5. "strings"
  6. "testing"
  7. )
  8. // =============================================================================
  9. // E2E Mock 模式支持
  10. // =============================================================================
  11. // 当 E2E_MOCK=true 时,使用本地 Mock 响应替代真实 API 调用。
  12. // 这允许在没有真实 API Key 的环境(如 CI)中验证基本的请求/响应流程。
  13. // isMockMode 检查是否启用 Mock 模式
  14. func isMockMode() bool {
  15. return strings.EqualFold(os.Getenv("E2E_MOCK"), "true")
  16. }
  17. // skipIfNoRealAPI 如果未配置真实 API Key 且不在 Mock 模式,则跳过测试
  18. func skipIfNoRealAPI(t *testing.T) {
  19. t.Helper()
  20. if isMockMode() {
  21. return // Mock 模式下不跳过
  22. }
  23. claudeKey := strings.TrimSpace(os.Getenv(claudeAPIKeyEnv))
  24. geminiKey := strings.TrimSpace(os.Getenv(geminiAPIKeyEnv))
  25. if claudeKey == "" && geminiKey == "" {
  26. t.Skip("未设置 API Key 且未启用 Mock 模式,跳过测试")
  27. }
  28. }
  29. // =============================================================================
  30. // API Key 脱敏(Task 6.10)
  31. // =============================================================================
  32. // safeLogKey 安全地记录 API Key(仅显示前 8 位)
  33. func safeLogKey(t *testing.T, prefix string, key string) {
  34. t.Helper()
  35. key = strings.TrimSpace(key)
  36. if len(key) <= 8 {
  37. t.Logf("%s: ***(长度: %d)", prefix, len(key))
  38. return
  39. }
  40. t.Logf("%s: %s...(长度: %d)", prefix, key[:8], len(key))
  41. }