diff --git a/cmd/picoclaw/internal/model/add_test.go b/cmd/picoclaw/internal/model/add_test.go index 5da4d5e7..51a61124 100644 --- a/cmd/picoclaw/internal/model/add_test.go +++ b/cmd/picoclaw/internal/model/add_test.go @@ -4,6 +4,7 @@ import ( "bytes" "net/http" "net/http/httptest" + "strconv" "strings" "testing" @@ -68,6 +69,21 @@ func TestFetchOpenAIModels_HTTPError(t *testing.T) { assert.Contains(t, err.Error(), "HTTP 401") } +func TestFetchOpenAIModels_HTTPErrorReadFailure(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + body := "short" + w.Header().Set("Content-Length", strconv.Itoa(len(body)+1)) + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(body)) + })) + defer srv.Close() + + _, err := fetchOpenAIModels(srv.URL, "bad") + require.Error(t, err) + assert.Contains(t, err.Error(), "read error response") + assert.Contains(t, err.Error(), "unexpected EOF") +} + func TestFetchOpenAIModels_EmptyDataEnvelope(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte(`{"data":[]}`)) diff --git a/cmd/picoclaw/internal/model/online.go b/cmd/picoclaw/internal/model/online.go index 9b8f7811..d27a261f 100644 --- a/cmd/picoclaw/internal/model/online.go +++ b/cmd/picoclaw/internal/model/online.go @@ -45,7 +45,10 @@ func fetchOpenAIModels(baseURL, apiKey string) ([]modelEntry, error) { defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) + body, readErr := io.ReadAll(io.LimitReader(resp.Body, 512)) + if readErr != nil { + return nil, fmt.Errorf("read error response: %w", readErr) + } return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(body))) } diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index c12a8348..317a4930 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -160,9 +160,12 @@ func (p *Provider) buildRequestBody( // treat it as false — web_search_preview must not be injected // when the caller cannot express a well-typed intent. if _, present := options["native_search"]; present { - log.Printf( - "[openai_compat] native_search option has unexpected type %T, ignoring", - options["native_search"], + logger.WarnCF( + "provider.openai_compat", + "native_search option has unexpected type, ignoring", + map[string]any{ + "type": fmt.Sprintf("%T", options["native_search"]), + }, ) } }