Files
chat/tests/test_settings.py
T
2026-04-26 12:22:00 -04:00

113 lines
3.1 KiB
Python

from __future__ import annotations
import pytest
from fastapi.testclient import TestClient
from chat.app import app
@pytest.fixture
def client(tmp_path, monkeypatch):
config_path = tmp_path / "config.toml"
config_path.write_text('featherless_api_key = "test"\n')
monkeypatch.setenv("CHAT_CONFIG_PATH", str(config_path))
monkeypatch.setenv("CHAT_DB_PATH", str(tmp_path / "test.db"))
with TestClient(app) as c:
yield c
def test_get_settings_renders_empty(client):
response = client.get("/settings")
assert response.status_code == 200
body = response.text.lower()
assert "<form" in body
assert "name" in body
assert "pronouns" in body
assert "persona" in body
def test_post_settings_appends_event_and_renders_saved(client, tmp_path):
response = client.post(
"/settings",
data={
"name": "Me",
"pronouns": "they/them",
"persona": "engineer",
},
)
assert response.status_code == 200
body = response.text.lower()
assert "saved" in body
from chat.db.connection import open_db
from chat.state.entities import get_you
with open_db(tmp_path / "test.db") as conn:
you = get_you(conn)
assert you is not None
assert you["name"] == "Me"
assert you["pronouns"] == "they/them"
assert you["persona"] == "engineer"
cur = conn.execute(
"SELECT kind, payload_json FROM event_log WHERE kind = 'you_authored'"
)
rows = cur.fetchall()
assert len(rows) == 1
def test_get_settings_pre_populates_existing(client):
post_response = client.post(
"/settings",
data={
"name": "Joseph",
"pronouns": "he/him",
"persona": "writes code",
},
)
assert post_response.status_code == 200
response = client.get("/settings")
assert response.status_code == 200
body = response.text
assert "Joseph" in body
assert "he/him" in body
assert "writes code" in body
def test_post_settings_rejects_missing_name(client):
response = client.post(
"/settings",
data={"name": "", "pronouns": "they/them", "persona": "anything"},
)
assert response.status_code == 400
def test_post_settings_overwrites_existing(client, tmp_path):
first = client.post(
"/settings",
data={"name": "First", "pronouns": "she/her", "persona": "p1"},
)
assert first.status_code == 200
second = client.post(
"/settings",
data={"name": "Second", "pronouns": "they/them", "persona": "p2"},
)
assert second.status_code == 200
from chat.db.connection import open_db
from chat.state.entities import get_you
with open_db(tmp_path / "test.db") as conn:
you = get_you(conn)
assert you is not None
assert you["name"] == "Second"
assert you["pronouns"] == "they/them"
assert you["persona"] == "p2"
cur = conn.execute(
"SELECT COUNT(*) FROM event_log WHERE kind = 'you_authored'"
)
assert cur.fetchone()[0] == 2