ClaudeCodeSDK.Mock (claude_code_sdk v0.0.1)
View SourceMock implementation for the Claude Code CLI for testing purposes.
This module provides a GenServer-based mock system that allows testing and development without making actual API calls to the Claude service. It can be configured with predefined responses for different prompt patterns.
Features
- Pattern-based responses: Configure responses for specific prompt patterns
- Default fallback: Provides realistic default responses for unmatched prompts
- Integration testing: Seamlessly integrates with the main SDK for testing
- Cost-free development: Enables development without incurring API costs
Usage
Start the mock server and configure responses:
{:ok, _pid} = ClaudeCodeSDK.Mock.start_link()
# Set up a specific response
ClaudeCodeSDK.Mock.set_response("hello", [
%{"type" => "system", "subtype" => "init", "session_id" => "mock-123"},
%{"type" => "assistant", "message" => %{"content" => "Hello from mock!"}},
%{"type" => "result", "subtype" => "success", "total_cost_usd" => 0.001}
])
Enable mocking in your application configuration:
Application.put_env(:claude_code_sdk, :use_mock, true)
Response Format
Mock responses should follow the same format as the actual Claude CLI output:
- Each response is a list of message maps
- Each message has a "type" field (system, assistant, user, result)
- Messages may include optional "subtype" fields for categorization
- Result messages should include cost and timing information for realistic testing
Testing Integration
The mock system is designed to work seamlessly with testing frameworks:
test "queries return expected responses" do
ClaudeCodeSDK.Mock.set_response("test prompt", expected_messages)
result = ClaudeCodeSDK.query("test prompt", options)
assert length(Enum.to_list(result)) == length(expected_messages)
end
Summary
Functions
Returns a specification to start this module under a supervisor.
Clears all mock responses.
Gets the response for a prompt.
Sets the default response for any unmatched prompt.
Sets a mock response for a given prompt pattern.
Starts the mock server.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec clear_responses() :: :ok
Clears all mock responses.
Gets the response for a prompt.
@spec set_default_response([map()]) :: :ok
Sets the default response for any unmatched prompt.
Sets a mock response for a given prompt pattern.
Examples
ClaudeCodeSDK.Mock.set_response("Hello", [
%{type: "system", subtype: "init", session_id: "mock-123"},
%{type: "assistant", message: %{"content" => "Hello from mock!"}},
%{type: "result", subtype: "success", total_cost_usd: 0.001}
])
@spec start_link(keyword()) :: GenServer.on_start()
Starts the mock server.