diff --git a/llm/cli.py b/llm/cli.py index 2563351f..02b61062 100644 --- a/llm/cli.py +++ b/llm/cli.py @@ -270,6 +270,7 @@ def read_prompt(): # Log to the database if (logs_on() or log) and not no_log: log_path = logs_db_path() + (log_path.parent).mkdir(parents=True, exist_ok=True) db = sqlite_utils.Database(log_path) migrate(db) response.log_to_db(db) diff --git a/tests/test_llm.py b/tests/test_llm.py index df164367..969a7138 100644 --- a/tests/test_llm.py +++ b/tests/test_llm.py @@ -167,6 +167,22 @@ def _insert(id, text): assert [record["id"] for record in records] == expected +def test_llm_prompt_creates_log_database(mocked_openai, tmpdir, monkeypatch): + user_path = tmpdir / "user" + monkeypatch.setenv("LLM_USER_PATH", str(user_path)) + runner = CliRunner() + result = runner.invoke( + cli, + ["three names \nfor a pet pelican", "--no-stream", "--key", "x"], + catch_exceptions=False, + ) + assert result.exit_code == 0 + assert result.output == "Bob, Alice, Eve\n" + # Should have created user_path and put a logs.db in it + assert (user_path / "logs.db").exists() + assert sqlite_utils.Database(str(user_path / "logs.db"))["responses"].count == 1 + + @mock.patch.dict(os.environ, {"OPENAI_API_KEY": "X"}) @pytest.mark.parametrize("use_stdin", (True, False, "split")) @pytest.mark.parametrize(