Files
yunke/yunke-server/tests/test_api_messages.py
T
浅念 b822e87d4d feat: remaining frontend views (groups/calls/sms/sync) + messageout media fields
Backend (Part A):
- MessageOut: add file_th_url, file_hd_url, duration, transcription_text, md5
  (room_id + sender_head_url already existed) so the chat timeline can render
  image thumbnails, voice duration/transcription, and avatars from a single
  response.
- PhoneCallOut: add record_file so the Calls view can offer recording playback.
- SmsOut: add content (full body) so the SMS detail dialog shows the whole text.
- Tests: red->green for each new field (media fields default None + populated,
  record_file round-trip, content round-trip). Suite 267 -> 271.

Frontend (Part B):
- api/index.ts: wrappers getGroups/getGroupMembers/getCalls/getSms/getSyncStatus
  + Group/GroupMember/PhoneCall/Sms/SyncState/SyncRun/SyncStatus types in
  types.ts (mirror the *Out pydantic schemas).
- views/GroupList.vue (/groups): table + channel filter + pagination; row click
  opens the group timeline.
- views/CallList.vue (/calls): table + call-type filter (phone_call/voice_call/
  all); "播放录音" action opens an el-dialog with <audio controls>.
- views/SmsList.vue (/sms): table; row click opens a detail dialog with the full
  content.
- views/SyncStatus.vue (/sync): two tables (states + recent_runs) with a manual
  refresh; run status color-coded.
- ChatTimeline.vue: reused for both 1:1 DM (talkerId) and group (roomId) by
  branching the query; group title + per-member sender already handled by
  MessageBubble.
- router: add /groups /calls /sms /sync + /group/:channel/:ownerId/:roomId.
- layouts/MainLayout.vue: replace the single 客户列表 button with an el-menu
  (客户 / 群 / 通话 / 短信 / 同步); org tree kept on the left.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-01 16:38:47 +08:00

287 lines
8.8 KiB
Python

"""Tests for GET /api/messages — chat timeline.
Seeds 3 messages (2 for talker t_a across times 1000/3000, 1 for talker t_b at
2000) and asserts filters (channel, owner, talker, time window) + ordering
(``timestamp asc``) + pagination.
"""
from __future__ import annotations
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.models import Message
async def _seed(session: AsyncSession) -> None:
session.add_all(
[
Message(
msg_id="m1",
channel="wechat",
owner_account_id="wx_owner_1",
talker_id="t_a",
room_id=None,
text="hello",
timestamp=1000,
is_send=True,
type="text",
type_raw=1,
),
Message(
msg_id="m2",
channel="wechat",
owner_account_id="wx_owner_1",
talker_id="t_b",
room_id=None,
text="hi",
timestamp=2000,
is_send=False,
type="text",
type_raw=1,
),
Message(
msg_id="m3",
channel="wechat",
owner_account_id="wx_owner_1",
talker_id="t_a",
room_id=None,
text="bye",
timestamp=3000,
is_send=True,
type="text",
type_raw=1,
),
]
)
await session.commit()
async def test_default_page_returns_all_ordered_asc(make_client):
"""No filters -> 3 rows ordered by ``timestamp asc`` (timeline order)."""
client = await make_client(_seed)
r = await client.get("/api/messages")
assert r.status_code == 200
body = r.json()
assert body["total"] == 3
assert len(body["items"]) == 3
assert [it["msg_id"] for it in body["items"]] == ["m1", "m2", "m3"]
for key in (
"msg_id",
"channel",
"owner_account_id",
"talker_id",
"room_id",
"is_send",
"type",
"type_raw",
"text",
"file_url",
"sender_name",
"sender_head_url",
"timestamp",
):
assert key in body["items"][0]
async def test_filter_by_talker_id(make_client):
"""``?talker_id=t_a`` -> 2 messages for that 1:1 conversation."""
client = await make_client(_seed)
r = await client.get("/api/messages", params={"talker_id": "t_a"})
assert r.status_code == 200
body = r.json()
assert body["total"] == 2
assert {it["msg_id"] for it in body["items"]} == {"m1", "m3"}
async def test_filter_by_owner_account_id(make_client):
"""``?owner_account_id=wx_owner_1`` -> all 3 (all owned by this account)."""
client = await make_client(_seed)
r = await client.get("/api/messages", params={"owner_account_id": "wx_owner_1"})
assert r.status_code == 200
assert r.json()["total"] == 3
async def test_filter_by_owner_account_id_no_match(make_client):
"""A non-existent owner account -> 0 rows."""
client = await make_client(_seed)
r = await client.get("/api/messages", params={"owner_account_id": "nobody"})
assert r.status_code == 200
assert r.json()["total"] == 0
async def test_time_window_filter(make_client):
"""``?start=1500&end=2500`` -> only m2 (timestamp 2000)."""
client = await make_client(_seed)
r = await client.get(
"/api/messages", params={"start": 1500, "end": 2500}
)
assert r.status_code == 200
body = r.json()
assert body["total"] == 1
assert body["items"][0]["msg_id"] == "m2"
async def test_time_window_combined_with_talker(make_client):
"""``talker_id=t_a & start=1500`` -> only m3 (t_a's later message)."""
client = await make_client(_seed)
r = await client.get(
"/api/messages", params={"talker_id": "t_a", "start": 1500}
)
assert r.status_code == 200
body = r.json()
assert body["total"] == 1
assert body["items"][0]["msg_id"] == "m3"
async def test_channel_filter(make_client):
"""``?channel=whatsapp`` -> 0 (all seeded are wechat)."""
client = await make_client(_seed)
r = await client.get("/api/messages", params={"channel": "whatsapp"})
assert r.status_code == 200
assert r.json()["total"] == 0
async def test_pagination_page_size_one(make_client):
"""``page_size=1&page=1`` -> 1 item, total still 3; page 2 -> next item."""
client = await make_client(_seed)
r1 = await client.get(
"/api/messages", params={"page": 1, "page_size": 1}
)
b1 = r1.json()
assert b1["total"] == 3
assert len(b1["items"]) == 1
assert b1["items"][0]["msg_id"] == "m1"
r2 = await client.get(
"/api/messages", params={"page": 2, "page_size": 1}
)
b2 = r2.json()
assert len(b2["items"]) == 1
assert b2["items"][0]["msg_id"] == "m2"
async def test_group_chat_filter_by_room_id(make_client):
"""``?room_id=room_x`` -> only the group message."""
async def seed(session: AsyncSession) -> None:
await _seed(session)
session.add(
Message(
msg_id="m4",
channel="wechat",
owner_account_id="wx_owner_1",
talker_id=None,
room_id="room_x",
text="group hi",
timestamp=4000,
type="text",
type_raw=1,
)
)
await session.commit()
client = await make_client(seed)
r = await client.get("/api/messages", params={"room_id": "room_x"})
assert r.status_code == 200
body = r.json()
assert body["total"] == 1
assert body["items"][0]["msg_id"] == "m4"
async def test_media_fields_serialize(make_client):
"""Image thumbnail/HD URLs, voice duration/transcription, md5, room_id and
sender avatar round-trip through MessageOut so the timeline can render
media + group messages without a second call.
"""
async def seed(session: AsyncSession) -> None:
session.add_all(
[
Message(
msg_id="img1",
channel="wechat",
owner_account_id="wx_owner_1",
talker_id="t_a",
room_id=None,
type="image",
type_raw=3,
timestamp=1000,
file_url="https://oss/full.jpg",
file_th_url="https://oss/th.jpg",
file_hd_url="https://oss/hd.jpg",
md5="abc123",
sender_name="Alice",
sender_head_url="https://oss/alice.jpg",
),
Message(
msg_id="voice1",
channel="wechat",
owner_account_id="wx_owner_1",
talker_id=None,
room_id="room_x",
type="voice",
type_raw=34,
timestamp=2000,
duration=12,
transcription_text="hello there",
sender_name="Bob",
sender_head_url="https://oss/bob.jpg",
),
]
)
await session.commit()
client = await make_client(seed)
r = await client.get("/api/messages", params={"owner_account_id": "wx_owner_1"})
assert r.status_code == 200
body = r.json()
assert body["total"] == 2
by_id = {it["msg_id"]: it for it in body["items"]}
img = by_id["img1"]
assert img["file_th_url"] == "https://oss/th.jpg"
assert img["file_hd_url"] == "https://oss/hd.jpg"
assert img["md5"] == "abc123"
assert img["room_id"] is None
assert img["sender_head_url"] == "https://oss/alice.jpg"
voice = by_id["voice1"]
assert voice["duration"] == 12
assert voice["transcription_text"] == "hello there"
assert voice["room_id"] == "room_x"
assert voice["sender_head_url"] == "https://oss/bob.jpg"
async def test_media_fields_default_none_when_unset(make_client):
"""A text message with no media columns set serializes the new keys as None."""
async def seed(session: AsyncSession) -> None:
session.add(
Message(
msg_id="t1",
channel="wechat",
owner_account_id="wx_owner_1",
talker_id="t_a",
type="text",
type_raw=1,
timestamp=1000,
text="plain",
)
)
await session.commit()
client = await make_client(seed)
r = await client.get("/api/messages")
assert r.status_code == 200
it = r.json()["items"][0]
for key in (
"file_th_url",
"file_hd_url",
"duration",
"transcription_text",
"md5",
"room_id",
"sender_head_url",
):
assert key in it
assert it[key] is None