From 9abae0478c6a945dab1e0daf1abdc9334f85b932 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Tue, 3 Mar 2026 15:00:59 -0800 Subject: [PATCH] feat(avocet): GET /api/queue endpoint --- app/api.py | 8 +++++++- tests/test_api.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/api.py b/app/api.py index bebbe68..5e9f97d 100644 --- a/app/api.py +++ b/app/api.py @@ -8,7 +8,7 @@ from __future__ import annotations import json from pathlib import Path -from fastapi import FastAPI +from fastapi import FastAPI, Query _ROOT = Path(__file__).parent.parent _DATA_DIR: Path = _ROOT / "data" # overridable in tests via set_data_dir() @@ -61,3 +61,9 @@ app = FastAPI(title="Avocet API") # In-memory last-action store (single user, local tool — in-memory is fine) _last_action: dict | None = None + + +@app.get("/api/queue") +def get_queue(limit: int = Query(default=10, ge=1, le=50)): + items = _read_jsonl(_queue_file()) + return {"items": items[:limit], "total": len(items)} diff --git a/tests/test_api.py b/tests/test_api.py index 50d316e..637a7ca 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,3 +1,5 @@ +import json + import pytest from app import api as api_module # noqa: F401 @@ -13,3 +15,40 @@ def reset_globals(tmp_path): def test_import(): from app import api # noqa: F401 + + +from fastapi.testclient import TestClient + + +@pytest.fixture +def client(): + from app.api import app + return TestClient(app) + + +@pytest.fixture +def queue_with_items(tmp_path): + """Write 3 test emails to the queue file.""" + from app import api as api_module + items = [ + {"id": f"id{i}", "subject": f"Subject {i}", "body": f"Body {i}", + "from": "test@example.com", "date": "2026-03-01", "source": "imap:test"} + for i in range(3) + ] + queue_path = tmp_path / "email_label_queue.jsonl" + queue_path.write_text("\n".join(json.dumps(x) for x in items) + "\n") + return items + + +def test_queue_returns_items(client, queue_with_items): + r = client.get("/api/queue?limit=2") + assert r.status_code == 200 + data = r.json() + assert len(data["items"]) == 2 + assert data["total"] == 3 + + +def test_queue_empty_when_no_file(client): + r = client.get("/api/queue") + assert r.status_code == 200 + assert r.json() == {"items": [], "total": 0}