-module(examples@reliability_integration_test). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/examples/reliability_integration_test.gleam"). -export([main/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " Integration test for Reliability Features (A4.1-A4.3)\n" "\n" " Tests retry logic, request validation, and logging hooks with actual API\n" " Run with: gleam run -m examples/reliability_integration_test\n" ). -file("src/examples/reliability_integration_test.gleam", 131). ?DOC(" Test 2: Validate invalid requests to ensure validation catches errors\n"). -spec test_validation_invalid() -> nil. test_validation_invalid() -> gleam_stdlib:println(<<"Testing validation error detection..."/utf8>>), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"2a. Empty messages list:"/utf8>>), Empty_req = anthropic@types@request:create_request( <<"claude-3-5-haiku-20241022"/utf8>>, [], 100 ), case anthropic@validation:validate_request(Empty_req) of {ok, nil} -> gleam_stdlib:println( <<" ✗ FAIL: Should have caught empty messages"/utf8>> ); {error, Errors} -> gleam_stdlib:println(<<" ✓ PASS: Caught validation errors:"/utf8>>), gleam@list:each( Errors, fun(E) -> gleam_stdlib:println( <<" - "/utf8, (anthropic@validation:error_to_string(E))/binary>> ) end ) end, gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"2b. Empty model name:"/utf8>>), Empty_model_req = anthropic@types@request:create_request( <<""/utf8>>, [anthropic@types@message:user_message(<<"Hello"/utf8>>)], 100 ), case anthropic@validation:validate_request(Empty_model_req) of {ok, nil} -> gleam_stdlib:println( <<" ✗ FAIL: Should have caught empty model"/utf8>> ); {error, Errors@1} -> gleam_stdlib:println(<<" ✓ PASS: Caught validation errors:"/utf8>>), gleam@list:each( Errors@1, fun(E@1) -> gleam_stdlib:println( <<" - "/utf8, (anthropic@validation:error_to_string(E@1))/binary>> ) end ) end, gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"2c. Invalid temperature (1.5):"/utf8>>), case anthropic@validation:validate_temperature({some, 1.5}) of {ok, nil} -> gleam_stdlib:println( <<" ✗ FAIL: Should have caught invalid temperature"/utf8>> ); {error, Errors@2} -> gleam_stdlib:println(<<" ✓ PASS: Caught validation errors:"/utf8>>), gleam@list:each( Errors@2, fun(E@2) -> gleam_stdlib:println( <<" - "/utf8, (anthropic@validation:error_to_string(E@2))/binary>> ) end ) end, gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"2d. Zero max_tokens:"/utf8>>), Zero_tokens_req = anthropic@types@request:create_request( <<"claude-3-5-haiku-20241022"/utf8>>, [anthropic@types@message:user_message(<<"Hello"/utf8>>)], 0 ), case anthropic@validation:validate_request(Zero_tokens_req) of {ok, nil} -> gleam_stdlib:println( <<" ✗ FAIL: Should have caught zero max_tokens"/utf8>> ); {error, Errors@3} -> gleam_stdlib:println(<<" ✓ PASS: Caught validation errors:"/utf8>>), gleam@list:each( Errors@3, fun(E@3) -> gleam_stdlib:println( <<" - "/utf8, (anthropic@validation:error_to_string(E@3))/binary>> ) end ) end, gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"2e. validate_or_error returns AnthropicError:"/utf8>> ), case anthropic@validation:validate_or_error(Empty_req) of {ok, _} -> gleam_stdlib:println( <<" ✗ FAIL: Should have returned error"/utf8>> ); {error, Err} -> gleam_stdlib:println(<<" ✓ PASS: Returned AnthropicError:"/utf8>>), gleam_stdlib:println( <<" "/utf8, (anthropic@types@error:error_to_string(Err))/binary>> ) end. -file("src/examples/reliability_integration_test.gleam", 542). -spec get_env(binary()) -> {ok, binary()} | {error, nil}. get_env(Name) -> Result = os:getenv(unicode:characters_to_list(Name)), Str = unicode:characters_to_binary(Result), case Str of <<"false"/utf8>> -> {error, nil}; <<""/utf8>> -> {error, nil}; Value -> {ok, Value} end. -file("src/examples/reliability_integration_test.gleam", 528). -spec get_api_key() -> {ok, binary()} | {error, nil}. get_api_key() -> case get_env(<<"ANTHROPIC_API_KEY"/utf8>>) of {ok, Key} -> case gleam@string:is_empty(gleam@string:trim(Key)) of true -> {error, nil}; false -> {ok, Key} end; {error, _} -> {error, nil} end. -file("src/examples/reliability_integration_test.gleam", 552). -spec bool_to_string(boolean()) -> binary(). bool_to_string(B) -> case B of true -> <<"true"/utf8>>; false -> <<"false"/utf8>> end. -file("src/examples/reliability_integration_test.gleam", 99). ?DOC(" Test 1: Validate a properly formed request\n"). -spec test_validation_valid(binary()) -> nil. test_validation_valid(Api_key) -> gleam_stdlib:println(<<"Creating a valid request..."/utf8>>), Messages = [anthropic@types@message:user_message( <<"What is 2 + 2? Answer briefly."/utf8>> )], Req = begin _pipe = anthropic@types@request:create_request( <<"claude-3-5-haiku-20241022"/utf8>>, Messages, 100 ), anthropic@types@request:with_temperature(_pipe, 0.7) end, gleam_stdlib:println(<<"Request details:"/utf8>>), gleam_stdlib:println(<<" Model: claude-3-5-haiku-20241022"/utf8>>), gleam_stdlib:println(<<" Messages: 1 user message"/utf8>>), gleam_stdlib:println(<<" Max tokens: 100"/utf8>>), gleam_stdlib:println(<<" Temperature: 0.7"/utf8>>), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"Running validation..."/utf8>>), case anthropic@validation:validate_request(Req) of {ok, nil} -> gleam_stdlib:println(<<"✓ PASS: Request validation passed"/utf8>>), gleam_stdlib:println( <<" is_valid() returns: "/utf8, (bool_to_string(anthropic@validation:is_valid(Req)))/binary>> ); {error, Errors} -> gleam_stdlib:println(<<"✗ FAIL: Request validation failed"/utf8>>), gleam@list:each( Errors, fun(E) -> gleam_stdlib:println( <<" - "/utf8, (anthropic@validation:error_to_string(E))/binary>> ) end ) end. -file("src/examples/reliability_integration_test.gleam", 559). -spec float_to_string(float()) -> binary(). float_to_string(F) -> erlang:float_to_binary(F). -file("src/examples/reliability_integration_test.gleam", 328). ?DOC(" Test 4: Test retry configuration (without triggering actual retries)\n"). -spec test_retry_config() -> nil. test_retry_config() -> gleam_stdlib:println(<<"Testing retry configuration..."/utf8>>), gleam_stdlib:println(<<""/utf8>>), Default_config = anthropic@retry:default_retry_config(), gleam_stdlib:println(<<"Default retry config:"/utf8>>), gleam_stdlib:println( <<" Max retries: "/utf8, (erlang:integer_to_binary(erlang:element(2, Default_config)))/binary>> ), gleam_stdlib:println( <<<<" Base delay: "/utf8, (erlang:integer_to_binary(erlang:element(3, Default_config)))/binary>>/binary, "ms"/utf8>> ), gleam_stdlib:println( <<<<" Max delay: "/utf8, (erlang:integer_to_binary(erlang:element(4, Default_config)))/binary>>/binary, "ms"/utf8>> ), gleam_stdlib:println( <<" Backoff multiplier: "/utf8, (float_to_string(erlang:element(6, Default_config)))/binary>> ), gleam_stdlib:println( <<" Jitter factor: "/utf8, (float_to_string(erlang:element(5, Default_config)))/binary>> ), gleam_stdlib:println(<<""/utf8>>), Custom_config = begin _pipe = anthropic@retry:default_retry_config(), anthropic@retry:with_max_retries(_pipe, 5) end, gleam_stdlib:println(<<"Custom config (5 retries):"/utf8>>), gleam_stdlib:println( <<" Max retries: "/utf8, (erlang:integer_to_binary(erlang:element(2, Custom_config)))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"Expected delays (without jitter):"/utf8>>), gleam_stdlib:println( <<<<" Attempt 1: "/utf8, (erlang:integer_to_binary( anthropic@retry:calculate_delay(Default_config, 0) ))/binary>>/binary, "ms"/utf8>> ), gleam_stdlib:println( <<<<" Attempt 2: "/utf8, (erlang:integer_to_binary( anthropic@retry:calculate_delay(Default_config, 1) ))/binary>>/binary, "ms"/utf8>> ), gleam_stdlib:println( <<<<" Attempt 3: "/utf8, (erlang:integer_to_binary( anthropic@retry:calculate_delay(Default_config, 2) ))/binary>>/binary, "ms"/utf8>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"Retryable error detection:"/utf8>>), Rate_limit_err = anthropic@types@error:rate_limit_error( <<"Rate limited"/utf8>> ), Auth_err = anthropic@types@error:authentication_error( <<"Invalid key"/utf8>> ), gleam_stdlib:println( <<" Rate limit error retryable: "/utf8, (bool_to_string(anthropic@types@error:is_retryable(Rate_limit_err)))/binary>> ), gleam_stdlib:println( <<" Auth error retryable: "/utf8, (bool_to_string(anthropic@types@error:is_retryable(Auth_err)))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"✓ PASS: Retry configuration test completed"/utf8>>). -file("src/examples/reliability_integration_test.gleam", 569). -spec get_timestamp_ms() -> integer(). get_timestamp_ms() -> os:system_time() div 1000. -file("src/examples/reliability_integration_test.gleam", 212). ?DOC(" Test 3: Test logging hooks with actual API call\n"). -spec test_hooks_with_api(binary()) -> nil. test_hooks_with_api(Api_key) -> gleam_stdlib:println(<<"Setting up logging hooks..."/utf8>>), Hooks = begin _pipe = anthropic@hooks:default_hooks(), _pipe@1 = anthropic@hooks:with_on_request_start( _pipe, fun(Event) -> gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"[HOOK] Request Started:"/utf8>>), gleam_stdlib:println( <<" Endpoint: "/utf8, (erlang:element(2, Event))/binary>> ), gleam_stdlib:println( <<" Request ID: "/utf8, (erlang:element(5, Event))/binary>> ), gleam_stdlib:println( <<" Model: "/utf8, (erlang:element(2, erlang:element(3, Event)))/binary>> ), gleam_stdlib:println( <<" Message count: "/utf8, (erlang:integer_to_binary( erlang:element(3, erlang:element(3, Event)) ))/binary>> ), gleam_stdlib:println( <<" Max tokens: "/utf8, (erlang:integer_to_binary( erlang:element(4, erlang:element(3, Event)) ))/binary>> ), gleam_stdlib:println( <<" Streaming: "/utf8, (bool_to_string( erlang:element(5, erlang:element(3, Event)) ))/binary>> ), nil end ), anthropic@hooks:with_on_request_end( _pipe@1, fun(Event@1) -> gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"[HOOK] Request Ended:"/utf8>>), gleam_stdlib:println( <<" Endpoint: "/utf8, (erlang:element(2, Event@1))/binary>> ), gleam_stdlib:println( <<" Request ID: "/utf8, (erlang:element(7, Event@1))/binary>> ), gleam_stdlib:println( <<<<" Duration: "/utf8, (erlang:integer_to_binary( erlang:element(3, Event@1) ))/binary>>/binary, "ms"/utf8>> ), gleam_stdlib:println( <<" Success: "/utf8, (bool_to_string(erlang:element(4, Event@1)))/binary>> ), gleam_stdlib:println( <<" Retry count: "/utf8, (erlang:integer_to_binary(erlang:element(8, Event@1)))/binary>> ), case erlang:element(5, Event@1) of {some, Resp} -> gleam_stdlib:println( <<" Response ID: "/utf8, (erlang:element(2, Resp))/binary>> ), gleam_stdlib:println( <<" Input tokens: "/utf8, (erlang:integer_to_binary( erlang:element(5, Resp) ))/binary>> ), gleam_stdlib:println( <<" Output tokens: "/utf8, (erlang:integer_to_binary( erlang:element(6, Resp) ))/binary>> ); none -> nil end, nil end ) end, gleam_stdlib:println(<<"Hooks configured. Making API request..."/utf8>>), gleam_stdlib:println(<<""/utf8>>), Cfg@1 = case begin _pipe@2 = anthropic@config:config_options(), _pipe@3 = anthropic@config:with_api_key(_pipe@2, Api_key), anthropic@config:load_config(_pipe@3) end of {ok, Cfg} -> Cfg; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"examples/reliability_integration_test"/utf8>>, function => <<"test_hooks_with_api"/utf8>>, line => 254, value => _assert_fail, start => 8183, 'end' => 8295, pattern_start => 8194, pattern_end => 8201}) end, Api_client = anthropic@client:new(Cfg@1), Messages = [anthropic@types@message:user_message( <<"Say 'Hello from hooks test!' and nothing else."/utf8>> )], Req = anthropic@types@request:create_request( <<"claude-3-5-haiku-20241022"/utf8>>, Messages, 50 ), Request_id = anthropic@hooks:generate_request_id(), Start_time = get_timestamp_ms(), anthropic@hooks:emit_request_start( Hooks, {request_start_event, <<"/v1/messages"/utf8>>, anthropic@hooks:summarize_request(Req), Start_time, Request_id} ), Result = anthropic@api:create_message(Api_client, Req), End_time = get_timestamp_ms(), Duration = End_time - Start_time, case Result of {ok, Response} -> anthropic@hooks:emit_request_end( Hooks, {request_end_event, <<"/v1/messages"/utf8>>, Duration, true, {some, anthropic@hooks:summarize_response(Response)}, none, Request_id, 0} ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"API Response:"/utf8>>), gleam_stdlib:println( <<" "/utf8, (anthropic@types@request:response_text(Response))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"✓ PASS: Hooks test completed successfully"/utf8>> ); {error, Err} -> anthropic@hooks:emit_request_end( Hooks, {request_end_event, <<"/v1/messages"/utf8>>, Duration, false, none, {some, Err}, Request_id, 0} ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"✗ FAIL: API call failed: "/utf8, (anthropic@types@error:error_to_string(Err))/binary>> ) end. -file("src/examples/reliability_integration_test.gleam", 399). ?DOC(" Test 5: Full integration test with validation + hooks + API\n"). -spec test_full_integration(binary()) -> nil. test_full_integration(Api_key) -> gleam_stdlib:println(<<"Running full integration test..."/utf8>>), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"Step 1: Creating request..."/utf8>>), Messages = [anthropic@types@message:user_message( <<"What is the capital of France? Answer in one word."/utf8>> )], Req = begin _pipe = anthropic@types@request:create_request( <<"claude-3-5-haiku-20241022"/utf8>>, Messages, 50 ), anthropic@types@request:with_temperature(_pipe, 0.5) end, gleam_stdlib:println(<<"Step 2: Validating request..."/utf8>>), case anthropic@validation:validate_or_error(Req) of {error, Err} -> gleam_stdlib:println( <<"✗ FAIL: Validation failed: "/utf8, (anthropic@types@error:error_to_string(Err))/binary>> ); {ok, Validated_req} -> gleam_stdlib:println(<<"✓ Request validated successfully"/utf8>>), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"Step 3: Setting up hooks..."/utf8>>), Hooks = begin _pipe@1 = anthropic@hooks:default_hooks(), _pipe@2 = anthropic@hooks:with_on_request_start( _pipe@1, fun(Event) -> gleam_stdlib:println( <<<<<<<<"[LOG] Starting request to "/utf8, (erlang:element(2, Event))/binary>>/binary, " (ID: "/utf8>>/binary, (erlang:element(5, Event))/binary>>/binary, ")"/utf8>> ), nil end ), anthropic@hooks:with_on_request_end( _pipe@2, fun(Event@1) -> gleam_stdlib:println( <<<<<<"[LOG] Request completed in "/utf8, (erlang:integer_to_binary( erlang:element(3, Event@1) ))/binary>>/binary, "ms, success="/utf8>>/binary, (bool_to_string(erlang:element(4, Event@1)))/binary>> ), nil end ) end, gleam_stdlib:println(<<"Step 4: Creating API client..."/utf8>>), Cfg@1 = case begin _pipe@3 = anthropic@config:config_options(), _pipe@4 = anthropic@config:with_api_key(_pipe@3, Api_key), anthropic@config:load_config(_pipe@4) end of {ok, Cfg} -> Cfg; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"examples/reliability_integration_test"/utf8>>, function => <<"test_full_integration"/utf8>>, line => 447, value => _assert_fail, start => 13557, 'end' => 13681, pattern_start => 13568, pattern_end => 13575}) end, Api_client = anthropic@client:new(Cfg@1), gleam_stdlib:println(<<"Step 5: Making API call..."/utf8>>), gleam_stdlib:println(<<""/utf8>>), Request_id = anthropic@hooks:generate_request_id(), Start_time = get_timestamp_ms(), anthropic@hooks:emit_request_start( Hooks, {request_start_event, <<"/v1/messages"/utf8>>, anthropic@hooks:summarize_request(Validated_req), Start_time, Request_id} ), case anthropic@api:create_message(Api_client, Validated_req) of {ok, Response} -> End_time = get_timestamp_ms(), anthropic@hooks:emit_request_end( Hooks, {request_end_event, <<"/v1/messages"/utf8>>, End_time - Start_time, true, {some, anthropic@hooks:summarize_response(Response)}, none, Request_id, 0} ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"Step 6: Processing response..."/utf8>> ), gleam_stdlib:println( <<" Response ID: "/utf8, (erlang:element(2, Response))/binary>> ), gleam_stdlib:println( <<" Model: "/utf8, (erlang:element(6, Response))/binary>> ), gleam_stdlib:println( <<" Input tokens: "/utf8, (erlang:integer_to_binary( erlang:element(2, erlang:element(9, Response)) ))/binary>> ), gleam_stdlib:println( <<" Output tokens: "/utf8, (erlang:integer_to_binary( erlang:element(3, erlang:element(9, Response)) ))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<" Claude says: "/utf8, (anthropic@types@request:response_text(Response))/binary>> ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"✓ PASS: Full integration test completed successfully"/utf8>> ); {error, Err@1} -> End_time@1 = get_timestamp_ms(), anthropic@hooks:emit_request_end( Hooks, {request_end_event, <<"/v1/messages"/utf8>>, End_time@1 - Start_time, false, none, {some, Err@1}, Request_id, 0} ), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println( <<"✗ FAIL: API call failed: "/utf8, (anthropic@types@error:error_to_string(Err@1))/binary>> ) end end. -file("src/examples/reliability_integration_test.gleam", 53). -spec run_tests(binary()) -> nil. run_tests(Api_key) -> gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println(<<"TEST 1: Request Validation - Valid Request"/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_validation_valid(Api_key), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println( <<"TEST 2: Request Validation - Invalid Requests"/utf8>> ), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_validation_invalid(), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println(<<"TEST 3: Logging Hooks with API Call"/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_hooks_with_api(Api_key), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println(<<"TEST 4: Retry Logic Configuration"/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_retry_config(), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), gleam_stdlib:println(<<"TEST 5: Full Integration Test"/utf8>>), gleam_stdlib:println(<<"-------------------------------------------"/utf8>>), test_full_integration(Api_key), gleam_stdlib:println(<<""/utf8>>), gleam_stdlib:println(<<"==========================================="/utf8>>), gleam_stdlib:println(<<"All tests completed!"/utf8>>), gleam_stdlib:println(<<"==========================================="/utf8>>). -file("src/examples/reliability_integration_test.gleam", 29). ?DOC(" Main entry point\n"). -spec main() -> nil. main() -> gleam_stdlib:println(<<"==========================================="/utf8>>), gleam_stdlib:println(<<"Reliability Features Integration Test"/utf8>>), gleam_stdlib:println(<<"(A4.1 Retry, A4.2 Validation, A4.3 Hooks)"/utf8>>), gleam_stdlib:println(<<"==========================================="/utf8>>), gleam_stdlib:println(<<""/utf8>>), case get_api_key() of {error, nil} -> gleam_stdlib:println( <<"ERROR: ANTHROPIC_API_KEY environment variable not set"/utf8>> ), gleam_stdlib:println(<<"Please set it and try again:"/utf8>>), gleam_stdlib:println( <<" export ANTHROPIC_API_KEY=your-api-key"/utf8>> ); {ok, Api_key} -> gleam_stdlib:println( <<<<"API Key: [REDACTED - "/utf8, (gleam@string:slice(Api_key, 0, 8))/binary>>/binary, "...]"/utf8>> ), gleam_stdlib:println(<<""/utf8>>), run_tests(Api_key) end.