package openai import ( "net/http" "testing" "time" ) // TestNew_DefaultTimeout verifies that a Config with no HTTPClient and no Timeout // produces a Provider whose http.Transport has ResponseHeaderTimeout set to the // package defaultTimeout (60s, cloud-appropriate). func TestNew_DefaultTimeout(t *testing.T) { p := New(Config{APIKey: "test-key"}) got := responseHeaderTimeout(t, p) if got != defaultTimeout { t.Errorf("ResponseHeaderTimeout = %v, want %v (defaultTimeout)", got, defaultTimeout) } } // TestNew_ConfigTimeout verifies that an explicit Config.Timeout propagates // through to http.Transport.ResponseHeaderTimeout. func TestNew_ConfigTimeout(t *testing.T) { want := 42 * time.Second p := New(Config{APIKey: "test-key", Timeout: want}) got := responseHeaderTimeout(t, p) if got != want { t.Errorf("ResponseHeaderTimeout = %v, want %v", got, want) } } // TestNew_HTTPClientOverridesTimeout verifies that providing a custom HTTPClient // takes full control: Config.Timeout is ignored, and the consumer's http.Client // is used as-is. func TestNew_HTTPClientOverridesTimeout(t *testing.T) { consumerTimeout := 7 * time.Second consumerClient := &http.Client{ Transport: &http.Transport{ResponseHeaderTimeout: consumerTimeout}, } p := New(Config{ APIKey: "test-key", HTTPClient: consumerClient, Timeout: 999 * time.Second, // deliberately wrong; must be ignored }) got := responseHeaderTimeout(t, p) if got != consumerTimeout { t.Errorf("ResponseHeaderTimeout = %v, want %v (consumer's value)", got, consumerTimeout) } if p.client.HTTPClient() != consumerClient { t.Errorf("Provider is not using the consumer's HTTPClient") } } func responseHeaderTimeout(t *testing.T, p *Provider) time.Duration { t.Helper() hc := p.client.HTTPClient() if hc == nil { t.Fatal("Provider has nil http.Client") } transport, ok := hc.Transport.(*http.Transport) if !ok { t.Fatalf("Provider http.Client.Transport is %T, want *http.Transport", hc.Transport) } return transport.ResponseHeaderTimeout }