ff7e9cb1d9
Adds the read-only API surface the upcoming Vue frontend needs to browse all
mirrored entities. Follows the existing customer-router pattern: Annotated
deps, Page[T] envelope, select+where+order_by, count() for total.
Endpoints (all GET):
* /api/departments -> list[DepartmentOut] (path-ordered, plain list)
* /api/staff -> Page[StaffOut] (department_id, is_on_job filters)
* /api/accounts -> Page[AccountOut] (channel filter; unified
wechat+whatsapp projection built in memory)
* /api/messages -> Page[MessageOut] (channel/owner/talker/room/
[start,end] filters; ordered timestamp asc)
* /api/groups -> Page[GroupOut] (channel/owner filters)
* /api/groups/{id}/members -> list[GroupMemberOut] (404 on unknown room)
* /api/calls -> Page[PhoneCallOut] (channel/user/time window;
ordered start_call_time desc)
* /api/sms -> Page[SmsOut] (user/time window; create_time desc)
* /api/sync/status -> {states, recent_runs} monitor payload
Schemas (Pydantic v2, from_attributes=True): MessageOut, GroupOut,
GroupMemberOut, PhoneCallOut, SmsOut, DepartmentOut, StaffOut, AccountOut
(unified across two tables), SyncStateOut, SyncRunOut, SyncStatusOut.
Only human-relevant fields per entity are exposed.
Tests: per-router TDD via httpx ASGITransport against an in-memory SQLite
override of get_session. A shared make_client factory in conftest.py builds
the seeded client, deduping the ~40-line fixture across the six new files.
41 new tests cover shape + every documented filter + pagination + ordering
(nulls-last verified on all desc/asc time columns).
Suite: 226 -> 267 passing. ruff check clean.
Co-Authored-By: Claude <noreply@anthropic.com>
137 lines
4.1 KiB
Python
137 lines
4.1 KiB
Python
"""Tests for GET /api/sync/status — sync monitor.
|
|
|
|
Seeds 2 SyncState cursors and 3 SyncRun audit rows and asserts the aggregate
|
|
shape ``{states, recent_runs}``, ordering of runs (newest first), and the
|
|
``recent`` cap.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime as dt
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.db.models import SyncRun, SyncState
|
|
|
|
|
|
async def _seed(session: AsyncSession) -> None:
|
|
session.add_all(
|
|
[
|
|
SyncState(
|
|
state_id=1,
|
|
entity_type="message",
|
|
channel="wechat",
|
|
scope_id="wx_owner_1",
|
|
last_version="v100",
|
|
last_timestamp=1000,
|
|
last_synced_at=dt.datetime(2026, 8, 1, 10, 0, 0, tzinfo=dt.UTC),
|
|
),
|
|
SyncState(
|
|
state_id=2,
|
|
entity_type="customer",
|
|
channel="whatsapp",
|
|
scope_id=None,
|
|
last_version="v9",
|
|
last_timestamp=2000,
|
|
),
|
|
SyncRun(
|
|
run_id=1,
|
|
entity_type="message",
|
|
channel="wechat",
|
|
scope_id="wx_owner_1",
|
|
status="success",
|
|
started_at=dt.datetime(2026, 8, 1, 9, 0, 0, tzinfo=dt.UTC),
|
|
finished_at=dt.datetime(2026, 8, 1, 9, 0, 5, tzinfo=dt.UTC),
|
|
records_fetched=100,
|
|
records_upserted=95,
|
|
),
|
|
SyncRun(
|
|
run_id=2,
|
|
entity_type="customer",
|
|
channel="whatsapp",
|
|
status="failed",
|
|
started_at=dt.datetime(2026, 8, 1, 10, 0, 0, tzinfo=dt.UTC),
|
|
records_fetched=0,
|
|
records_upserted=0,
|
|
error="upstream 500",
|
|
),
|
|
SyncRun(
|
|
run_id=3,
|
|
entity_type="message",
|
|
channel="wechat",
|
|
status="running",
|
|
started_at=dt.datetime(2026, 8, 1, 11, 0, 0, tzinfo=dt.UTC),
|
|
records_fetched=0,
|
|
records_upserted=0,
|
|
),
|
|
]
|
|
)
|
|
await session.commit()
|
|
|
|
|
|
async def test_status_returns_states_and_recent_runs(make_client):
|
|
"""``GET /api/sync/status`` -> both keys present with correct shapes."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/sync/status")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert set(body.keys()) == {"states", "recent_runs"}
|
|
|
|
assert len(body["states"]) == 2
|
|
for key in (
|
|
"state_id",
|
|
"entity_type",
|
|
"channel",
|
|
"scope_id",
|
|
"last_version",
|
|
"last_timestamp",
|
|
"last_synced_at",
|
|
):
|
|
assert key in body["states"][0]
|
|
|
|
assert len(body["recent_runs"]) == 3
|
|
for key in (
|
|
"run_id",
|
|
"entity_type",
|
|
"channel",
|
|
"scope_id",
|
|
"status",
|
|
"started_at",
|
|
"finished_at",
|
|
"records_fetched",
|
|
"records_upserted",
|
|
"error",
|
|
):
|
|
assert key in body["recent_runs"][0]
|
|
|
|
|
|
async def test_recent_runs_ordered_newest_first(make_client):
|
|
"""Runs ordered by ``started_at desc`` -> run 3, 2, 1."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/sync/status")
|
|
body = r.json()
|
|
ids = [run["run_id"] for run in body["recent_runs"]]
|
|
assert ids == [3, 2, 1]
|
|
|
|
|
|
async def test_recent_limit_caps_results(make_client):
|
|
"""``?recent=2`` -> only the 2 most recent runs."""
|
|
client = await make_client(_seed)
|
|
r = await client.get("/api/sync/status", params={"recent": 2})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert len(body["recent_runs"]) == 2
|
|
assert [run["run_id"] for run in body["recent_runs"]] == [3, 2]
|
|
|
|
|
|
async def test_empty_db_returns_empty_lists(make_client):
|
|
"""No seed -> 200 with empty states + recent_runs (no crash)."""
|
|
async def empty_seed(session: AsyncSession) -> None:
|
|
await session.commit()
|
|
|
|
client = await make_client(empty_seed)
|
|
r = await client.get("/api/sync/status")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body == {"states": [], "recent_runs": []}
|