Testing¶
Guide: Testing.
FakeModel
¶
FakeModel(replies: Iterable[Any], *, name: str = 'fake', context_window: int = 200000, price: Price | None = None)
Bases: Model
A Model that returns prepared replies in order, with no API key or network.
Each request (think, one attempt of ask, compact) uses the next item:
str: a text reply with no tool callsToolCall: a reply with just that call (seetool_call)listortupleofstr,ToolCallandRawBlock: a reply with those blocks in orderReply: used as is- an exception instance: raised, for testing error paths
- a callable: called with the
Request, and its result is used by the rules above
Token usage is estimated from the text, so state.usage fills in as it would with a real model.
Safe to use from several threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replies
|
Iterable[Any]
|
The reply items, in order. |
required |
name
|
str
|
Model name. |
'fake'
|
context_window
|
int
|
Context window size in tokens. A request estimated above it raises
|
200000
|
price
|
Price | None
|
Token prices for |
None
|
Example
fake = FakeModel([tool_call("read_file", path="main.py"), "The bug is on line 3"])
agent.copy(model=fake, reporter=None).run(State("Find the bug"))
assert fake.remaining == 0
requests
instance-attribute
¶
requests: list[Request] = []
Every request received, in order. Use it to assert what the agent sent.
context_window
property
¶
context_window: int
Context window size in tokens, as passed to context_window=.
respond
¶
Records request in requests and returns the next prepared reply.
Each text block is passed to on_text once.
Raises:
| Type | Description |
|---|---|
ContextTooLongError
|
The estimated request size is above |
RuntimeError
|
Every prepared reply has been used. |
FakeHuman
¶
FakeHuman(answers: Iterable[Any])
Bases: Human
A Human that returns prepared answers in order, for testing code that calls ask_human.
Each answer is converted to the requested returns type the same way a typed answer is. An answer that
does not fit is skipped and the next one is used, as if the person answered again. An empty answer does not
fit returns=str. Safe to use from several threads; questions are answered one at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
answers
|
Iterable[Any]
|
The answers, in order. |
required |
Example
human = FakeHuman(["yes", "always"])
agent.copy(model=fake, human=human, reporter=None).run(state)
assert human.remaining == 0
tool_call
¶
tool_call(name: str, /, **args: Any) -> ToolCall
Builds a tool call to put in FakeModel replies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tool name. |
required |
**args
|
Any
|
The tool arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
ToolCall
|
A |
Example
FakeModel([tool_call("read_file", path="main.py"), "Done"])