All checks were successful
Build & Deploy / deploy (push) Successful in 1m24s
Auslesen im Hintergrund: /api/scan antwortet, sobald die Karten freigestellt und gespeichert sind, und stößt die Extraktion als Task an. Ein Zwanzigerstapel blockierte vorher den Upload für die ganze Dauer aller Modellaufrufe. Die Oberfläche zeigt "wird gelesen" und lädt nach, solange etwas offen ist. Zwei Folgen davon sind mitbehandelt: Beim Schreiben der Ergebnisse steht COALESCE, damit ein Handeintrag während des Lesens nicht überschrieben wird, und offene Karten werden beim Start nachgeholt, statt dauerhaft in der Warteschlange zu hängen. Bildaufbereitung des Zuschnitts: - Der Einzug zieht die erkannten Ecken um 1,5 % zur Mitte, damit kein Untergrund im Zuschnitt bleibt. - Die Beleuchtung wird ausgeglichen (Division durch eine weichgezeichnete Fassung), damit das Papier weiß wird statt grau. Dunkle Karten bleiben unangetastet - bei ihnen ist das Dunkle das Papier, kein Schatten. - Das Modell meldet die nötige Drehung im Schema; das gespeicherte Bild wird entsprechend gedreht. Geometrisch ist die Lage nicht bestimmbar. Sammelexport entfernt: Der Button "Alle als vCard" ist weg, mit ihm der Endpunkt /vcf sowie vcard.build_many und db.execute_many, die dadurch keinen Aufrufer mehr hatten. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
265 lines
8.9 KiB
Python
265 lines
8.9 KiB
Python
"""Durchstich durch die HTTP-Schnittstelle.
|
|
|
|
Ohne ANTHROPIC_API_KEY laeuft die Extraktion in den Leerlauf - Freistellen,
|
|
Speichern, Suchen, Bearbeiten und Export werden trotzdem vollstaendig gepruft.
|
|
"""
|
|
import asyncio
|
|
import time
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app import db, extract, mailer, reminders
|
|
from app.main import app
|
|
from tests.helpers import photo_bytes
|
|
|
|
|
|
def warte_bis_gelesen(client, card_ids, timeout=15.0):
|
|
"""Das Auslesen laeuft im Hintergrund - hier auf das Ende warten."""
|
|
frist = time.monotonic() + timeout
|
|
while time.monotonic() < frist:
|
|
stati = [
|
|
client.get(f"/api/cards/{i}").json()["extract_status"] for i in card_ids
|
|
]
|
|
if all(status != "offen" for status in stati):
|
|
return stati
|
|
time.sleep(0.1)
|
|
raise AssertionError("Karten wurden nicht fertig gelesen")
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def client():
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def scanned(client):
|
|
response = client.post(
|
|
"/api/scan", files={"file": ("tisch.jpg", photo_bytes(4), "image/jpeg")}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
warte_bis_gelesen(client, [c["id"] for c in data["cards"]])
|
|
return data
|
|
|
|
|
|
def test_healthz(client):
|
|
assert client.get("/healthz").json() == {"status": "ok"}
|
|
|
|
|
|
def test_scan_legt_je_karte_einen_datensatz_an(scanned):
|
|
assert scanned["count"] == 4
|
|
assert scanned["fallback"] is False
|
|
assert len({card["id"] for card in scanned["cards"]}) == 4
|
|
|
|
|
|
def test_jede_karte_hat_ein_gespeichertes_bild(client, scanned):
|
|
for card in scanned["cards"]:
|
|
image = client.get(card["image_url"])
|
|
assert image.status_code == 200
|
|
assert image.headers["content-type"] == "image/jpeg"
|
|
assert len(image.content) > 1000
|
|
|
|
|
|
def test_liste_zaehlt_und_liefert_die_karten(client, scanned):
|
|
data = client.get("/api/cards").json()
|
|
|
|
assert data["stats"]["total"] >= 4
|
|
assert data["stats"]["offen"] >= 4
|
|
|
|
|
|
def test_bearbeiten_und_suchen(client, scanned):
|
|
card_id = scanned["cards"][0]["id"]
|
|
|
|
patched = client.patch(
|
|
f"/api/cards/{card_id}",
|
|
json={"full_name": "Anna Berg", "company": "Berg & Partner", "notes": "Messe"},
|
|
)
|
|
assert patched.status_code == 200
|
|
assert patched.json()["full_name"] == "Anna Berg"
|
|
|
|
hits = client.get("/api/cards", params={"q": "berg"}).json()["cards"]
|
|
assert [c["id"] for c in hits] == [card_id]
|
|
|
|
by_note = client.get("/api/cards", params={"q": "Messe"}).json()["cards"]
|
|
assert card_id in [c["id"] for c in by_note]
|
|
|
|
|
|
def test_unbekannte_felder_werden_abgewiesen(client, scanned):
|
|
response = client.patch(
|
|
f"/api/cards/{scanned['cards'][0]['id']}", json={"extract_status": "ok"}
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
def test_vcard_download_markiert_die_karte_als_exportiert(client, scanned):
|
|
card_id = scanned["cards"][1]["id"]
|
|
client.patch(f"/api/cards/{card_id}", json={"full_name": "Bea Kurz"})
|
|
|
|
response = client.get(f"/vcf/{card_id}")
|
|
|
|
assert response.status_code == 200
|
|
assert response.headers["content-type"].startswith("text/vcard")
|
|
assert "Bea-Kurz.vcf" in response.headers["content-disposition"]
|
|
assert "FN:Bea Kurz" in response.text
|
|
assert client.get(f"/api/cards/{card_id}").json()["exported_at"] is not None
|
|
|
|
|
|
def test_filter_offen_blendet_exportierte_karten_aus(client, scanned):
|
|
offen = client.get("/api/cards", params={"filter": "offen"}).json()["cards"]
|
|
|
|
assert all(card["exported_at"] is None for card in offen)
|
|
|
|
|
|
def test_erinnerung_anlegen_und_filtern(client, scanned):
|
|
card_id = scanned["cards"][2]["id"]
|
|
due = datetime.now(timezone.utc) + timedelta(days=3)
|
|
|
|
created = client.post(
|
|
f"/api/cards/{card_id}/reminders",
|
|
json={"text": "Angebot nachfassen", "due_at": due.isoformat()},
|
|
)
|
|
assert created.status_code == 200
|
|
|
|
detail = client.get(f"/api/cards/{card_id}").json()
|
|
assert len(detail["reminders"]) == 1
|
|
|
|
mit_erinnerung = client.get("/api/cards", params={"filter": "erinnerung"}).json()
|
|
assert card_id in [c["id"] for c in mit_erinnerung["cards"]]
|
|
assert mit_erinnerung["stats"]["erinnerungen"] >= 1
|
|
|
|
|
|
def test_erinnerung_ohne_datum_wird_abgewiesen(client, scanned):
|
|
response = client.post(
|
|
f"/api/cards/{scanned['cards'][2]['id']}/reminders",
|
|
json={"text": "Ohne Termin", "due_at": "morgen"},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
def test_faellige_erinnerung_wird_verschickt(client, scanned, monkeypatch):
|
|
card_id = scanned["cards"][3]["id"]
|
|
client.patch(f"/api/cards/{card_id}", json={"full_name": "Carl Spät"})
|
|
past = datetime.now(timezone.utc) - timedelta(minutes=5)
|
|
client.post(
|
|
f"/api/cards/{card_id}/reminders",
|
|
json={"text": "Rueckruf", "due_at": past.isoformat()},
|
|
)
|
|
|
|
verschickt = []
|
|
monkeypatch.setattr(mailer, "configured", lambda: True)
|
|
monkeypatch.setattr(mailer, "send", lambda subject, body: verschickt.append((subject, body)))
|
|
|
|
assert reminders.process_due() == 1
|
|
assert "Carl Spät" in verschickt[0][0]
|
|
assert "Rueckruf" in verschickt[0][1]
|
|
|
|
# Zweiter Durchlauf darf nicht erneut verschicken.
|
|
assert reminders.process_due() == 0
|
|
|
|
|
|
def test_loeschen_entfernt_karte_und_bild(client):
|
|
created = client.post(
|
|
"/api/scan", files={"file": ("einzel.jpg", photo_bytes(1), "image/jpeg")}
|
|
).json()
|
|
card_id = created["cards"][0]["id"]
|
|
image_file = db.query_one("SELECT image_file FROM cards WHERE id = ?", (card_id,))["image_file"]
|
|
|
|
from app import config
|
|
|
|
assert (config.IMAGE_DIR / image_file).exists()
|
|
assert client.delete(f"/api/cards/{card_id}").status_code == 200
|
|
assert client.get(f"/api/cards/{card_id}").status_code == 404
|
|
assert not (config.IMAGE_DIR / image_file).exists()
|
|
|
|
|
|
def test_kaputter_upload_gibt_400(client):
|
|
response = client.post(
|
|
"/api/scan", files={"file": ("kaputt.jpg", b"kein bild", "image/jpeg")}
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
def test_pwa_dateien_werden_ausgeliefert(client):
|
|
assert client.get("/").status_code == 200
|
|
assert client.get("/sw.js").status_code == 200
|
|
assert client.get("/manifest.webmanifest").status_code == 200
|
|
assert client.get("/static/app.js").status_code == 200
|
|
|
|
|
|
def test_scan_antwortet_bevor_das_auslesen_fertig_ist(client, monkeypatch):
|
|
"""Der Kern der Hintergrundverarbeitung: die Antwort wartet nicht auf das
|
|
Modell. Ohne das haengt ein Zwanzigerstapel eine Minute im Upload."""
|
|
original = extract.extract_all
|
|
|
|
async def langsam(images):
|
|
await asyncio.sleep(1.5)
|
|
return await original(images)
|
|
|
|
monkeypatch.setattr(extract, "extract_all", langsam)
|
|
|
|
start = time.monotonic()
|
|
response = client.post(
|
|
"/api/scan", files={"file": ("einzel.jpg", photo_bytes(1), "image/jpeg")}
|
|
)
|
|
dauer = time.monotonic() - start
|
|
card_id = response.json()["cards"][0]["id"]
|
|
|
|
assert dauer < 1.2, f"Antwort brauchte {dauer:.2f}s, hat also gewartet"
|
|
assert client.get(f"/api/cards/{card_id}").json()["extract_status"] == "offen"
|
|
|
|
monkeypatch.undo()
|
|
warte_bis_gelesen(client, [card_id])
|
|
|
|
|
|
def test_offene_karten_zaehlen_nicht_als_unvollstaendig(client):
|
|
"""Waehrend des Lesens sind Felder naturgemaess leer - das ist kein Mangel."""
|
|
created = client.post(
|
|
"/api/scan", files={"file": ("einzel.jpg", photo_bytes(1), "image/jpeg")}
|
|
).json()
|
|
card_id = created["cards"][0]["id"]
|
|
warte_bis_gelesen(client, [card_id]) # sonst ueberschreibt der Hintergrundlauf
|
|
db.execute("UPDATE cards SET extract_status = 'offen' WHERE id = ?", (card_id,))
|
|
|
|
unvollstaendig = client.get(
|
|
"/api/cards", params={"filter": "unvollstaendig"}
|
|
).json()["cards"]
|
|
|
|
assert card_id not in [c["id"] for c in unvollstaendig]
|
|
assert client.get("/api/cards").json()["stats"]["laeuft"] >= 1
|
|
|
|
db.execute("UPDATE cards SET extract_status = 'ok' WHERE id = ?", (card_id,))
|
|
|
|
|
|
def test_sammelexport_ist_entfernt(client):
|
|
assert client.get("/vcf", params={"ids": "egal"}).status_code == 404
|
|
|
|
|
|
def test_handeintrag_ueberlebt_den_hintergrundlauf(client, monkeypatch):
|
|
"""Die Karte ist sichtbar, bevor sie gelesen ist. Wer in der Zwischenzeit
|
|
etwas eintraegt, darf es nicht wieder verlieren."""
|
|
original = extract.extract_all
|
|
|
|
async def langsam(images):
|
|
await asyncio.sleep(1.0)
|
|
return await original(images)
|
|
|
|
monkeypatch.setattr(extract, "extract_all", langsam)
|
|
card_id = client.post(
|
|
"/api/scan", files={"file": ("einzel.jpg", photo_bytes(1), "image/jpeg")}
|
|
).json()["cards"][0]["id"]
|
|
|
|
client.patch(f"/api/cards/{card_id}", json={"full_name": "Von Hand", "notes": "Messe"})
|
|
|
|
monkeypatch.undo()
|
|
warte_bis_gelesen(client, [card_id])
|
|
|
|
card = client.get(f"/api/cards/{card_id}").json()
|
|
assert card["full_name"] == "Von Hand"
|
|
assert card["notes"] == "Messe"
|