test_case_id: "proxy_auth_oauth2_401_sse_001"
description: "Tests that 401 errors causing SSE Content-Type errors show helpful generic message"
tags: ["all", "proxy", "auth", "oauth"]
skip_intermediate_events: true

# Configure OAuth mock to return token
mock_oauth_server:
  token_url: "https://test-oauth.example.com/token"
  response_sequence:
    - access_token: "test_token"
      expires_in: 3600

# Configure downstream agent to reject with 401 (causes SSE error)
downstream_agent_auth:
  enabled: true
  type: "bearer"
  expected_value: "impossible_token_that_never_matches"

# Configure proxy authentication
proxy_auth_config:
  agent_name: "TestAgent_Proxied"
  authentication:
    type: "oauth2_client_credentials"
    token_url: "https://test-oauth.example.com/token"
    client_id: "test_oauth_client_id"
    client_secret: "test_oauth_client_secret"
    token_cache_duration_seconds: 3300

gateway_input:
  target_agent_name: "TestAgent_Proxied"
  user_identity: "oauth_401_sse_tester@example.com"
  parts:
    - type: "text"
      text: "Test 401 causing SSE Content-Type error"
  external_context:
    a2a_session_id: "session_oauth_401_sse_001"

# No successful responses - 401 causes SSE error which is caught on first attempt
expected_gateway_output:
  - type: "error"
    error_code: -32603  # InternalError
    error_message_contains: "did not return a valid SSE streaming response"
    error_data_contains:
      agent_name: "TestAgent_Proxied"

# Assert one token request was made (no retry since SSE error caught first)
assert_oauth_token_requests:
  - token_url: "https://test-oauth.example.com/token"
    call_count: 1

# Assert one downstream request was made (no retry)
assert_downstream_auth:
  - request_index: 0
    authorization_header:
      exact: "Bearer test_token"
