"""Tests for chat endpoints: GET/POST /api/projects/{project_id}/chat (KIN-UI-005).""" import pytest from unittest.mock import patch, MagicMock from fastapi.testclient import TestClient import web.api as api_module @pytest.fixture def client(tmp_path): db_path = tmp_path / "test.db" api_module.DB_PATH = db_path from web.api import app c = TestClient(app) c.post("/api/projects", json={"id": "p1", "name": "P1", "path": "/p1"}) return c def test_get_chat_history_empty_for_new_project(client): r = client.get("/api/projects/p1/chat") assert r.status_code == 200 assert r.json() == [] def test_post_chat_task_request_creates_task_stub(client): with patch("core.chat_intent.classify_intent", return_value="task_request"), \ patch("web.api.subprocess.Popen") as mock_popen: mock_popen.return_value = MagicMock() r = client.post("/api/projects/p1/chat", json={"content": "Добавь кнопку выхода"}) assert r.status_code == 200 data = r.json() assert data["user_message"]["role"] == "user" assert data["assistant_message"]["message_type"] == "task_created" assert "task_stub" in data["assistant_message"] assert data["assistant_message"]["task_stub"]["status"] == "pending" assert data["task"] is not None assert mock_popen.called def test_post_chat_status_query_returns_text_response(client): with patch("core.chat_intent.classify_intent", return_value="status_query"): r = client.post("/api/projects/p1/chat", json={"content": "что сейчас в работе?"}) assert r.status_code == 200 data = r.json() assert data["user_message"]["role"] == "user" assert data["assistant_message"]["role"] == "assistant" assert data["task"] is None assert "Нет активных задач" in data["assistant_message"]["content"] def test_post_chat_empty_content_returns_400(client): r = client.post("/api/projects/p1/chat", json={"content": " "}) assert r.status_code == 400