58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
import chatbot
|
|
|
|
|
|
def test_read_file_tool_returns_file_contents(tmp_path):
|
|
sample = tmp_path / "note.txt"
|
|
sample.write_text("hello\nworld\n", encoding="utf-8")
|
|
|
|
assert "read_file" in chatbot.TOOLS
|
|
assert chatbot.read_file(str(sample)) == "hello\nworld\n"
|
|
|
|
|
|
def test_ask_uses_tool_name_in_callback(tmp_path):
|
|
sample = tmp_path / "note.txt"
|
|
sample.write_text("hello\nworld\n", encoding="utf-8")
|
|
|
|
class DummyLLM:
|
|
n_tokens = 0
|
|
|
|
def __init__(self):
|
|
self.calls = 0
|
|
|
|
def create_chat_completion(self, *args, **kwargs):
|
|
self.calls += 1
|
|
if self.calls == 1:
|
|
yield {
|
|
"choices": [{
|
|
"delta": {
|
|
"content": (
|
|
f"<tool_call><function=read_file>"
|
|
f"<parameter=path>{sample}</parameter></function></tool_call>"
|
|
)
|
|
}
|
|
}]
|
|
}
|
|
return
|
|
yield {"choices": [{"delta": {"content": "Final answer"}}]}
|
|
|
|
seen = []
|
|
llm = DummyLLM()
|
|
result = chatbot.ask(
|
|
llm,
|
|
[{"role": "user", "content": "read this file"}],
|
|
on_tool_call=lambda tool_name, target: seen.append((tool_name, target)),
|
|
)
|
|
|
|
assert result == "Final answer"
|
|
assert seen == [("read_file", str(sample))]
|
|
|
|
|
|
def test_gui_tool_signal_accepts_name_and_target():
|
|
import gui
|
|
from PyQt6.QtWidgets import QApplication
|
|
|
|
app = QApplication.instance() or QApplication([])
|
|
thread = gui.AskThread(None, [])
|
|
thread.tool_call.emit("read_file", "C:/temp/example.py")
|
|
assert app is not None
|