Initial test for 'llm prompt' command, closes #18

This commit is contained in:
Simon Willison 2023-06-14 18:32:48 +01:00
parent 1afb5e83f6
commit 12d6cf1049
2 changed files with 25 additions and 1 deletions

View file

@ -32,6 +32,6 @@ setup(
llm=llm.cli:cli
""",
install_requires=["click", "openai", "click-default-group-wheel", "sqlite-utils"],
extras_require={"test": ["pytest"]},
extras_require={"test": ["pytest", "requests-mock"]},
python_requires=">=3.7",
)

View file

@ -1,8 +1,10 @@
from click.testing import CliRunner
from llm.cli import cli
import json
import os
import pytest
import sqlite_utils
from unittest import mock
def test_version():
@ -46,3 +48,25 @@ def test_logs(n, log_path):
else:
expected_length = n
assert len(logs) == expected_length
@mock.patch.dict(os.environ, {"OPENAI_API_KEY": "X"})
@pytest.mark.parametrize("use_stdin", (True, False))
def test_llm_default_prompt(requests_mock, use_stdin):
mocked = requests_mock.post(
"https://api.openai.com/v1/chat/completions",
json={"choices": [{"message": {"content": "Bob, Alice, Eve"}}]},
headers={"Content-Type": "application/json"},
)
runner = CliRunner()
prompt = "three names for a pet pelican"
input = None
args = []
if use_stdin:
input = prompt
else:
args.append(prompt)
result = runner.invoke(cli, args, input=input, catch_exceptions=False)
assert result.exit_code == 0
assert result.output == "Bob, Alice, Eve\n"
assert mocked.last_request.headers["Authorization"] == "Bearer X"