test: skip live token comparison on network errors

This commit is contained in:
wucm667
2026-09-03 15:16:18 +08:00
parent 5e4958c889
commit 78fc7b171e
@@ -4,8 +4,10 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
@@ -350,9 +352,13 @@ func TestEstimateOpenAIInputTokens_CompareWithOpenAIAPI(t *testing.T) {
actual, err := callOpenAIInputTokensAPIForTest(client, apiKey, prepared.Request)
if err != nil {
// Live-API comparison only; invalid/expired local keys should skip, not fail CI.
if strings.Contains(err.Error(), "status=401") || strings.Contains(err.Error(), "invalid_api_key") {
t.Skipf("OPENAI_API_KEY rejected by OpenAI: %v", err)
// This is an optional live-API comparison. Credential and transient
// network failures must not make the deterministic unit suite fail.
var netErr net.Error
if strings.Contains(err.Error(), "status=401") ||
strings.Contains(err.Error(), "invalid_api_key") ||
errors.As(err, &netErr) {
t.Skipf("OpenAI live comparison unavailable: %v", err)
}
require.NoError(t, err)
}