|
| 1 | +"""Tests for POST /api/v1/input/text and GET /api/v1/input/{input_id}. |
| 2 | +
|
| 3 | +Uses the shared in-memory SQLite engine from conftest.py. No Ollama or |
| 4 | +Whisper involvement — text input is fully synchronous. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +TEXT_URL = "/api/v1/input/text" |
| 10 | +INPUT_URL = "/api/v1/input" |
| 11 | + |
| 12 | +# Narrative that passes all validation: > 20 chars, >= 10 words, << 50,000 chars. |
| 13 | +VALID_NARRATIVE = ( |
| 14 | + "Responded to a wildfire at Bear Creek Trailhead around 1:45 PM on July 10th. " |
| 15 | + "Lightning strike ignited timber litter in mixed conifer and chaparral." |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# POST /api/v1/input/text |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | + |
| 23 | +class TestSubmitTextInput: |
| 24 | + |
| 25 | + def test_201_returns_required_fields(self, client): |
| 26 | + resp = client.post(TEXT_URL, json={"narrative": VALID_NARRATIVE}) |
| 27 | + assert resp.status_code == 201 |
| 28 | + body = resp.json() |
| 29 | + assert body["status"] == "ready" |
| 30 | + assert body["input_type"] == "text" |
| 31 | + assert "input_id" in body |
| 32 | + assert "created_at" in body |
| 33 | + |
| 34 | + def test_201_character_and_word_counts_match_narrative(self, client): |
| 35 | + resp = client.post(TEXT_URL, json={"narrative": VALID_NARRATIVE}) |
| 36 | + assert resp.status_code == 201 |
| 37 | + body = resp.json() |
| 38 | + assert body["character_count"] == len(VALID_NARRATIVE) |
| 39 | + assert body["word_count"] == len(VALID_NARRATIVE.split()) |
| 40 | + |
| 41 | + def test_201_optional_fields_accepted_and_visible_via_get(self, client): |
| 42 | + resp = client.post(TEXT_URL, json={ |
| 43 | + "narrative": VALID_NARRATIVE, |
| 44 | + "station_id": "STA-045", |
| 45 | + "responder_badge": "FD-7842", |
| 46 | + "incident_date_hint": "2024-07-10", |
| 47 | + }) |
| 48 | + assert resp.status_code == 201 |
| 49 | + input_id = resp.json()["input_id"] |
| 50 | + |
| 51 | + get_resp = client.get(f"{INPUT_URL}/{input_id}") |
| 52 | + assert get_resp.status_code == 200 |
| 53 | + body = get_resp.json() |
| 54 | + assert body["station_id"] == "STA-045" |
| 55 | + assert body["responder_badge"] == "FD-7842" |
| 56 | + assert body["incident_date_hint"] == "2024-07-10" |
| 57 | + |
| 58 | + def test_narrative_stored_in_transcript_field(self, client): |
| 59 | + resp = client.post(TEXT_URL, json={"narrative": VALID_NARRATIVE}) |
| 60 | + input_id = resp.json()["input_id"] |
| 61 | + get_resp = client.get(f"{INPUT_URL}/{input_id}") |
| 62 | + assert get_resp.json()["transcript"] == VALID_NARRATIVE |
| 63 | + |
| 64 | + def test_413_narrative_over_50000_chars(self, client): |
| 65 | + too_long = "x" * 50_001 |
| 66 | + resp = client.post(TEXT_URL, json={"narrative": too_long}) |
| 67 | + assert resp.status_code == 413 |
| 68 | + body = resp.json() |
| 69 | + assert body["error_code"] == "NARRATIVE_TOO_LONG" |
| 70 | + assert body["detail"]["max_characters"] == 50_000 |
| 71 | + assert body["detail"]["received_characters"] == 50_001 |
| 72 | + |
| 73 | + def test_413_boundary_exactly_50000_chars_is_accepted(self, client): |
| 74 | + # Exactly at the limit: should succeed (> 50_000 is rejected, not >=). |
| 75 | + # Build a narrative at exactly 50,000 chars that has >= 10 words. |
| 76 | + phrase = "fire incident response " # 23 chars |
| 77 | + narrative = (phrase * 2175)[:50_000] # 2175*23=50025, sliced to 50000 |
| 78 | + resp = client.post(TEXT_URL, json={"narrative": narrative}) |
| 79 | + assert resp.status_code == 201 |
| 80 | + |
| 81 | + def test_422_fewer_than_10_words_matches_request_validation_shape(self, client): |
| 82 | + # >= 20 chars so Pydantic minLength passes, but only 6 words. |
| 83 | + short = "Fire happened at the scene today here" # 7 words, > 20 chars |
| 84 | + resp = client.post(TEXT_URL, json={"narrative": short}) |
| 85 | + assert resp.status_code == 422 |
| 86 | + body = resp.json() |
| 87 | + assert body["error_code"] == "VALIDATION_ERROR" |
| 88 | + assert body["message"] == "Request validation failed" |
| 89 | + errs = body["validation_errors"] |
| 90 | + assert len(errs) == 1 |
| 91 | + assert errs[0]["field"] == "narrative" |
| 92 | + assert errs[0]["issue"] == "Must contain at least 10 words" |
| 93 | + assert errs[0]["value"] == short |
| 94 | + |
| 95 | + def test_422_narrative_under_20_chars_triggers_pydantic_validation(self, client): |
| 96 | + resp = client.post(TEXT_URL, json={"narrative": "Too short"}) |
| 97 | + assert resp.status_code == 422 |
| 98 | + body = resp.json() |
| 99 | + assert body["error_code"] == "VALIDATION_ERROR" |
| 100 | + assert "validation_errors" in body |
| 101 | + |
| 102 | + def test_422_missing_narrative_field(self, client): |
| 103 | + resp = client.post(TEXT_URL, json={}) |
| 104 | + assert resp.status_code == 422 |
| 105 | + body = resp.json() |
| 106 | + assert body["error_code"] == "VALIDATION_ERROR" |
| 107 | + |
| 108 | + def test_422_empty_string_narrative(self, client): |
| 109 | + resp = client.post(TEXT_URL, json={"narrative": ""}) |
| 110 | + assert resp.status_code == 422 |
| 111 | + body = resp.json() |
| 112 | + assert body["error_code"] == "VALIDATION_ERROR" |
| 113 | + |
| 114 | + |
| 115 | +# --------------------------------------------------------------------------- |
| 116 | +# GET /api/v1/input/{input_id} |
| 117 | +# --------------------------------------------------------------------------- |
| 118 | + |
| 119 | +class TestGetInput: |
| 120 | + |
| 121 | + def _create(self, client, **kwargs): |
| 122 | + payload = {"narrative": VALID_NARRATIVE, **kwargs} |
| 123 | + resp = client.post(TEXT_URL, json=payload) |
| 124 | + assert resp.status_code == 201 |
| 125 | + return resp.json()["input_id"] |
| 126 | + |
| 127 | + def test_200_returns_full_record(self, client): |
| 128 | + input_id = self._create(client) |
| 129 | + resp = client.get(f"{INPUT_URL}/{input_id}") |
| 130 | + assert resp.status_code == 200 |
| 131 | + body = resp.json() |
| 132 | + assert body["input_id"] == input_id |
| 133 | + assert body["input_type"] == "text" |
| 134 | + assert body["status"] == "ready" |
| 135 | + assert body["transcript"] == VALID_NARRATIVE |
| 136 | + assert body["character_count"] == len(VALID_NARRATIVE) |
| 137 | + assert body["word_count"] == len(VALID_NARRATIVE.split()) |
| 138 | + assert body["created_at"] is not None |
| 139 | + assert body["updated_at"] is not None |
| 140 | + |
| 141 | + def test_200_voice_only_fields_are_null_for_text_input(self, client): |
| 142 | + input_id = self._create(client) |
| 143 | + body = client.get(f"{INPUT_URL}/{input_id}").json() |
| 144 | + assert body["original_filename"] is None |
| 145 | + assert body["audio_duration_seconds"] is None |
| 146 | + assert body["error_detail"] is None |
| 147 | + |
| 148 | + def test_200_retry_after_is_null_for_ready_input(self, client): |
| 149 | + input_id = self._create(client) |
| 150 | + body = client.get(f"{INPUT_URL}/{input_id}").json() |
| 151 | + assert body.get("retry_after_seconds") is None |
| 152 | + |
| 153 | + def test_404_unknown_uuid_returns_app_error_envelope(self, client): |
| 154 | + fake_id = "00000000-0000-0000-0000-000000000000" |
| 155 | + resp = client.get(f"{INPUT_URL}/{fake_id}") |
| 156 | + assert resp.status_code == 404 |
| 157 | + body = resp.json() |
| 158 | + assert body["error_code"] == "INPUT_NOT_FOUND" |
| 159 | + assert fake_id in body["message"] |
| 160 | + |
| 161 | + def test_404_message_contains_the_requested_id(self, client): |
| 162 | + target = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" |
| 163 | + resp = client.get(f"{INPUT_URL}/{target}") |
| 164 | + assert resp.status_code == 404 |
| 165 | + assert target in resp.json()["message"] |
0 commit comments