|
| 1 | +"""Integration-shaped smoke tests that verify the frontend chat module is |
| 2 | +wired to the backend correctly. |
| 3 | +
|
| 4 | +These tests do not require a running Ollama — they assert on: |
| 5 | + * Static frontend mount when TUTOR_SERVE_FRONTEND=1. |
| 6 | + * The chat JS/CSS assets exist and are referenced from index.html. |
| 7 | + * The chat JS calls POST /api/chat with a JSON body matching the |
| 8 | + ChatRequest schema. |
| 9 | + * The service worker bypasses /api/* requests. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import re |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import httpx |
| 18 | +import pytest |
| 19 | +import respx |
| 20 | +from fastapi.testclient import TestClient |
| 21 | + |
| 22 | +from app.config import REPO_ROOT, Settings |
| 23 | +from app.main import create_app |
| 24 | + |
| 25 | + |
| 26 | +FRONTEND_DIR = REPO_ROOT / "frontend" |
| 27 | + |
| 28 | + |
| 29 | +def _settings(**overrides) -> Settings: |
| 30 | + base = dict( |
| 31 | + ollama_url="http://ollama.test", |
| 32 | + model="gemma3:4b", |
| 33 | + request_timeout=5.0, |
| 34 | + allow_origins=("http://localhost:8000",), |
| 35 | + serve_frontend=True, |
| 36 | + frontend_dir=FRONTEND_DIR, |
| 37 | + system_prompt="You are a Python tutor.", |
| 38 | + ) |
| 39 | + base.update(overrides) |
| 40 | + return Settings(**base) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def client_with_frontend() -> TestClient: |
| 45 | + return TestClient(create_app(_settings())) |
| 46 | + |
| 47 | + |
| 48 | +def test_chat_assets_exist() -> None: |
| 49 | + assert (FRONTEND_DIR / "tutor-chat.js").is_file() |
| 50 | + assert (FRONTEND_DIR / "tutor-chat.css").is_file() |
| 51 | + |
| 52 | + |
| 53 | +def test_index_html_references_chat_assets() -> None: |
| 54 | + index = (FRONTEND_DIR / "index.html").read_text(encoding="utf-8") |
| 55 | + assert 'href="tutor-chat.css' in index |
| 56 | + assert 'src="tutor-chat.js' in index |
| 57 | + assert 'meta name="tutor-backend"' in index |
| 58 | + |
| 59 | + |
| 60 | +def test_chat_js_posts_to_api_chat() -> None: |
| 61 | + js = (FRONTEND_DIR / "tutor-chat.js").read_text(encoding="utf-8") |
| 62 | + # The module must POST to /api/chat with a JSON body containing `messages`. |
| 63 | + assert "/api/chat" in js |
| 64 | + assert "method: 'POST'" in js or 'method: "POST"' in js |
| 65 | + assert "application/json" in js |
| 66 | + assert "messages" in js |
| 67 | + # And it should also probe /api/health. |
| 68 | + assert "/api/health" in js |
| 69 | + |
| 70 | + |
| 71 | +def test_service_worker_bypasses_api_calls() -> None: |
| 72 | + sw = (FRONTEND_DIR / "sw.js").read_text(encoding="utf-8") |
| 73 | + # The bypass must short-circuit before the cache logic. |
| 74 | + assert "/api/" in sw |
| 75 | + assert re.search(r"pathname\.startsWith\(['\"]/api/['\"]\)", sw) |
| 76 | + |
| 77 | + |
| 78 | +def test_frontend_index_served_when_serve_frontend(client_with_frontend: TestClient) -> None: |
| 79 | + resp = client_with_frontend.get("/") |
| 80 | + assert resp.status_code == 200 |
| 81 | + assert "Offline Python Tutor" in resp.text |
| 82 | + assert "tutor-chat.js" in resp.text |
| 83 | + |
| 84 | + |
| 85 | +def test_frontend_static_assets_served(client_with_frontend: TestClient) -> None: |
| 86 | + for path in ("/tutor-chat.js", "/tutor-chat.css", "/app.js", "/sw.js"): |
| 87 | + resp = client_with_frontend.get(path) |
| 88 | + assert resp.status_code == 200, path |
| 89 | + assert resp.content, path |
| 90 | + |
| 91 | + |
| 92 | +def test_api_routes_still_work_alongside_frontend(client_with_frontend: TestClient) -> None: |
| 93 | + resp = client_with_frontend.get("/api/config") |
| 94 | + assert resp.status_code == 200 |
| 95 | + assert resp.json()["default_model"] == "gemma3:4b" |
| 96 | + |
| 97 | + |
| 98 | +@respx.mock |
| 99 | +def test_chat_endpoint_accepts_frontend_payload_shape(client_with_frontend: TestClient) -> None: |
| 100 | + """Round-trip the exact JSON shape tutor-chat.js sends.""" |
| 101 | + respx.post("http://ollama.test/api/chat").mock( |
| 102 | + return_value=httpx.Response( |
| 103 | + 200, |
| 104 | + json={ |
| 105 | + "model": "gemma3:4b", |
| 106 | + "message": {"role": "assistant", "content": "Let's start by printing the list."}, |
| 107 | + "done": True, |
| 108 | + }, |
| 109 | + ) |
| 110 | + ) |
| 111 | + payload = { |
| 112 | + "messages": [ |
| 113 | + {"role": "user", "content": 'Context: I am currently reading "Section 03 — Lists".\n\nWhy is my list empty?'}, |
| 114 | + ] |
| 115 | + } |
| 116 | + resp = client_with_frontend.post("/api/chat", json=payload) |
| 117 | + assert resp.status_code == 200 |
| 118 | + body = resp.json() |
| 119 | + assert body["message"]["role"] == "assistant" |
| 120 | + assert "print" in body["message"]["content"] |
| 121 | + |
| 122 | + |
| 123 | +def test_cors_allows_configured_origin() -> None: |
| 124 | + settings = _settings(allow_origins=("http://localhost:8000",), serve_frontend=False) |
| 125 | + client = TestClient(create_app(settings)) |
| 126 | + resp = client.options( |
| 127 | + "/api/chat", |
| 128 | + headers={ |
| 129 | + "origin": "http://localhost:8000", |
| 130 | + "access-control-request-method": "POST", |
| 131 | + "access-control-request-headers": "content-type", |
| 132 | + }, |
| 133 | + ) |
| 134 | + assert resp.status_code in (200, 204) |
| 135 | + assert resp.headers.get("access-control-allow-origin") == "http://localhost:8000" |
0 commit comments