-module(anthropic@testing). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/anthropic/testing.gleam"). -export([mock_text_response_body/2, mock_text_response/1, mock_tool_use_response_body/4, mock_tool_use_response/3, mock_error_body/2, mock_error_response/3, mock_auth_error/0, mock_rate_limit_error/0, mock_overloaded_error/0, mock_invalid_request_error/1, fixture_simple_response/0, fixture_conversation_response/0, fixture_tool_use_response/0, fixture_max_tokens_response/0, fixture_stop_sequence_response/0, has_api_key/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( " Testing utilities for Anthropic API client\n" "\n" " This module provides mock responses, test fixtures, and helpers\n" " for testing code that uses the anthropic_gleam library.\n" ). -file("src/anthropic/testing.gleam", 73). ?DOC(" Build a mock text response body JSON\n"). -spec mock_text_response_body(binary(), binary()) -> binary(). mock_text_response_body(Id, Text) -> gleam@json:to_string( gleam@json:object( [{<<"id"/utf8>>, gleam@json:string(Id)}, {<<"type"/utf8>>, gleam@json:string(<<"message"/utf8>>)}, {<<"role"/utf8>>, gleam@json:string(<<"assistant"/utf8>>)}, {<<"content"/utf8>>, gleam@json:array( [gleam@json:object( [{<<"type"/utf8>>, gleam@json:string(<<"text"/utf8>>)}, {<<"text"/utf8>>, gleam@json:string(Text)}] )], fun(X) -> X end )}, {<<"model"/utf8>>, gleam@json:string(<<"claude-sonnet-4-20250514"/utf8>>)}, {<<"stop_reason"/utf8>>, gleam@json:string(<<"end_turn"/utf8>>)}, {<<"usage"/utf8>>, gleam@json:object( [{<<"input_tokens"/utf8>>, gleam@json:int(10)}, {<<"output_tokens"/utf8>>, gleam@json:int(20)}] )}] ) ). -file("src/anthropic/testing.gleam", 18). ?DOC(" Create a mock successful text response\n"). -spec mock_text_response(binary()) -> gleam@http@response:response(binary()). mock_text_response(Text) -> _pipe = gleam@http@response:new(200), gleam@http@response:set_body( _pipe, mock_text_response_body(<<"msg_mock_123"/utf8>>, Text) ). -file("src/anthropic/testing.gleam", 105). ?DOC(" Build a mock tool use response body JSON\n"). -spec mock_tool_use_response_body(binary(), binary(), binary(), binary()) -> binary(). mock_tool_use_response_body(Id, Tool_id, Tool_name, _) -> gleam@json:to_string( gleam@json:object( [{<<"id"/utf8>>, gleam@json:string(Id)}, {<<"type"/utf8>>, gleam@json:string(<<"message"/utf8>>)}, {<<"role"/utf8>>, gleam@json:string(<<"assistant"/utf8>>)}, {<<"content"/utf8>>, gleam@json:array( [gleam@json:object( [{<<"type"/utf8>>, gleam@json:string(<<"tool_use"/utf8>>)}, {<<"id"/utf8>>, gleam@json:string(Tool_id)}, {<<"name"/utf8>>, gleam@json:string(Tool_name)}, {<<"input"/utf8>>, gleam@json:object([])}] )], fun(X) -> X end )}, {<<"model"/utf8>>, gleam@json:string(<<"claude-sonnet-4-20250514"/utf8>>)}, {<<"stop_reason"/utf8>>, gleam@json:string(<<"tool_use"/utf8>>)}, {<<"usage"/utf8>>, gleam@json:object( [{<<"input_tokens"/utf8>>, gleam@json:int(15)}, {<<"output_tokens"/utf8>>, gleam@json:int(25)}] )}] ) ). -file("src/anthropic/testing.gleam", 24). ?DOC(" Create a mock successful tool use response\n"). -spec mock_tool_use_response(binary(), binary(), binary()) -> gleam@http@response:response(binary()). mock_tool_use_response(Tool_id, Tool_name, Tool_input) -> _pipe = gleam@http@response:new(200), gleam@http@response:set_body( _pipe, mock_tool_use_response_body( <<"msg_mock_456"/utf8>>, Tool_id, Tool_name, Tool_input ) ). -file("src/anthropic/testing.gleam", 146). ?DOC(" Build a mock error body JSON\n"). -spec mock_error_body(binary(), binary()) -> binary(). mock_error_body(Error_type, Message) -> gleam@json:to_string( gleam@json:object( [{<<"type"/utf8>>, gleam@json:string(<<"error"/utf8>>)}, {<<"error"/utf8>>, gleam@json:object( [{<<"type"/utf8>>, gleam@json:string(Error_type)}, {<<"message"/utf8>>, gleam@json:string(Message)}] )}] ) ). -file("src/anthropic/testing.gleam", 39). ?DOC(" Create a mock error response\n"). -spec mock_error_response(integer(), binary(), binary()) -> gleam@http@response:response(binary()). mock_error_response(Status_code, Error_type, Message) -> _pipe = gleam@http@response:new(Status_code), gleam@http@response:set_body(_pipe, mock_error_body(Error_type, Message)). -file("src/anthropic/testing.gleam", 49). ?DOC(" Create a mock authentication error response\n"). -spec mock_auth_error() -> gleam@http@response:response(binary()). mock_auth_error() -> mock_error_response( 401, <<"authentication_error"/utf8>>, <<"Invalid API key"/utf8>> ). -file("src/anthropic/testing.gleam", 54). ?DOC(" Create a mock rate limit error response\n"). -spec mock_rate_limit_error() -> gleam@http@response:response(binary()). mock_rate_limit_error() -> mock_error_response( 429, <<"rate_limit_error"/utf8>>, <<"Rate limit exceeded"/utf8>> ). -file("src/anthropic/testing.gleam", 59). ?DOC(" Create a mock overloaded error response\n"). -spec mock_overloaded_error() -> gleam@http@response:response(binary()). mock_overloaded_error() -> mock_error_response( 529, <<"overloaded_error"/utf8>>, <<"API is temporarily overloaded"/utf8>> ). -file("src/anthropic/testing.gleam", 64). ?DOC(" Create a mock invalid request error response\n"). -spec mock_invalid_request_error(binary()) -> gleam@http@response:response(binary()). mock_invalid_request_error(Message) -> mock_error_response(400, <<"invalid_request_error"/utf8>>, Message). -file("src/anthropic/testing.gleam", 166). ?DOC(" A simple text response fixture\n"). -spec fixture_simple_response() -> anthropic@types@request:create_message_response(). fixture_simple_response() -> {create_message_response, <<"msg_fixture_001"/utf8>>, <<"message"/utf8>>, assistant, [{text_block, <<"Hello! How can I help you today?"/utf8>>}], <<"claude-sonnet-4-20250514"/utf8>>, {some, end_turn}, none, {usage, 12, 8}}. -file("src/anthropic/testing.gleam", 180). ?DOC(" A multi-turn conversation response fixture\n"). -spec fixture_conversation_response() -> anthropic@types@request:create_message_response(). fixture_conversation_response() -> {create_message_response, <<"msg_fixture_002"/utf8>>, <<"message"/utf8>>, assistant, [{text_block, <<"Based on our previous conversation, I understand you're asking about Gleam programming."/utf8>>}], <<"claude-sonnet-4-20250514"/utf8>>, {some, end_turn}, none, {usage, 150, 45}}. -file("src/anthropic/testing.gleam", 198). ?DOC(" A tool use response fixture\n"). -spec fixture_tool_use_response() -> anthropic@types@request:create_message_response(). fixture_tool_use_response() -> {create_message_response, <<"msg_fixture_003"/utf8>>, <<"message"/utf8>>, assistant, [{text_block, <<"Let me check the weather for you."/utf8>>}, {tool_use_block, <<"toolu_fixture_001"/utf8>>, <<"get_weather"/utf8>>, <<"{\"location\":\"San Francisco\",\"unit\":\"celsius\"}"/utf8>>}], <<"claude-sonnet-4-20250514"/utf8>>, {some, tool_use}, none, {usage, 25, 35}}. -file("src/anthropic/testing.gleam", 219). ?DOC(" A max tokens response fixture\n"). -spec fixture_max_tokens_response() -> anthropic@types@request:create_message_response(). fixture_max_tokens_response() -> {create_message_response, <<"msg_fixture_004"/utf8>>, <<"message"/utf8>>, assistant, [{text_block, <<"This response was truncated because it reached the maximum token limit..."/utf8>>}], <<"claude-sonnet-4-20250514"/utf8>>, {some, max_tokens}, none, {usage, 20, 100}}. -file("src/anthropic/testing.gleam", 237). ?DOC(" A stop sequence response fixture\n"). -spec fixture_stop_sequence_response() -> anthropic@types@request:create_message_response(). fixture_stop_sequence_response() -> {create_message_response, <<"msg_fixture_005"/utf8>>, <<"message"/utf8>>, assistant, [{text_block, <<"The answer is 42"/utf8>>}], <<"claude-sonnet-4-20250514"/utf8>>, {some, stop_sequence}, {some, <<"END"/utf8>>}, {usage, 15, 5}}. -file("src/anthropic/testing.gleam", 269). -spec get_env(binary()) -> {ok, binary()} | {error, nil}. get_env(Name) -> Value = begin _pipe = os:getenv( unicode:characters_to_list(Name), unicode:characters_to_list(<<""/utf8>>) ), unicode:characters_to_binary(_pipe) end, case Value of <<""/utf8>> -> {error, nil}; V -> {ok, V} end. -file("src/anthropic/testing.gleam", 255). ?DOC(" Check if an API key is available for integration tests\n"). -spec has_api_key() -> boolean(). has_api_key() -> case get_env(<<"ANTHROPIC_API_KEY"/utf8>>) of {ok, Key} -> Key /= <<""/utf8>>; {error, _} -> false end.