Files
MrhongxingandCopilot ec594286f3 文档:逐个源文件的说明 + 架构设计文档
- docs/files/:仓库中每个被跟踪文件一份说明,逐函数/逐常量解释「做什么」与「为什么这么做」
  - dhtchat/ 12 份模块文档(node、chat、gui、app、crypto、protocol、routing、store、identity、util、__main__、__init__)
  - tests/ 3 份测试文档(test_crypto 官方向量、test_dht 真实 UDP 集成、test_chat 聊天语义)
  - run.cmd、start.bat、.gitignore、根 README.md 各一份
- docs/ARCHITECTURE.md:架构设计
  - 六层架构与单向依赖、DHT 设计、键空间与 TTL 策略、报文与分片
  - 聊天室消息双通路与迟到者历史回放的端到端时序
  - 私聊三级投递(直连 → 中继 → 离线信箱)与 AAD 绑定
  - 安全模型(提供什么、明确不提供什么)、线程模型、局域网发现
  - 已知局限清单与扩展指南
- docs/README.md:文档总索引与「想了解 X 就看 Y」导航表
- 根 README.md 的「开发与测试」末尾补一段文档入口
- 26 张 Mermaid 图(分层、时序、流程),267 个相对链接全部校验通过

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-09-18 18:52:08 +08:00

93 lines
5.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# `tests/test_chat.py` — 聊天层的集成测试
> 本文件的作用:在真实网络上验证**业务语义**——资料发布与解析、房间成员与历史、消息伪造的拒绝、私聊加密与三级投递、去重与持久化。
> 与 [test_dht.md](./test_dht.md) 的分工:那边测「网络能不能送到」,这边测「聊天逻辑对不对、安不安全」。
- **路径**:`tests/test_chat.py`
- **行数**:368
- **依赖**:[chat.py](../../../dhtchat/chat.py)、[node.py](../../../dhtchat/node.py)
- **类型**:`unittest.IsolatedAsyncioTestCase`
---
## 1. `class Peer` — 「一个用户 + 它产生的所有事件」
| 成员 | 作用 |
| --- | --- |
| `__init__(self, node, chat, events)` | 保存节点、聊天服务与事件收集列表 |
| `nickname()` | 返回该 peer 的昵称(便捷访问) |
| `of_type(kind)` | 从事件流里筛出指定 `type` 的事件 |
| `texts(kind)` | 从指定类型的事件里抽取 `text` 字段列表(断言消息内容的常用方式) |
**为什么这样设计**:聊天是事件驱动的,测试的唯一可观测量就是 `emit` 出来的事件序列。把「收集 + 筛选」封装起来,让每个用例只写断言。
---
## 2. `class ChatServiceTests(unittest.IsolatedAsyncioTestCase)`
### 脚手架
| 方法 | 作用 |
| --- | --- |
| `asyncSetUp(self)` | 初始化 peer 列表与事件容器 |
| `asyncTearDown(self)` | 逆序关闭所有 peer(`chat.stop()` 再 `node.stop()`),保证端口释放 |
| `async make_peer(self, nickname, start_chat=True)` | 建节点 + `ChatService` + 事件队列;`start_chat=False` 用于测试「未启动」的状态 |
| `async link(self, follower, anchor)` | 让 `follower` 引导到 `anchor`(建立叠加网连接) |
| `async join_and_sync(self, *peers, room)` | 让多个 peer 加入同一房间并完成一轮同步(保证后续断言不受时序影响) |
### 用例清单(16 个)
#### 资料与昵称解析
| 用例 | 验证内容 |
| --- | --- |
| `test_profile_is_published_and_verified` | 资料发布后能被别的节点从 DHT 读回,且**签名有效** |
| `test_resolve_nickname_returns_none_for_stranger` | 查不存在的昵称返回 `None`(而不是异常或坏数据) |
| `test_tampered_profile_is_rejected` | 篡改资料任一字段后,签名校验失败、该记录被忽略 |
| `test_sending_to_unknown_nickname_reports_failure` | 给未知昵称发私聊时明确返回**失败**(而不是静默丢弃) |
#### 房间
| 用例 | 验证内容 |
| --- | --- |
| `test_room_message_reaches_member` | 房间消息到达在线的另一成员(点对点直推主路径) |
| `test_late_joiner_replays_history_from_dht` | **迟到加入者**能从 DHT 日志补到历史消息 |
| `test_room_rejects_forged_record` | 未签名 / 签名不匹配的房间消息被拒绝 |
| `test_room_log_is_merged_across_replicas` | 不同副本上的日志经过合并后收敛为同一份(GROW 并集) |
| `test_room_sync_picks_up_messages_missed_by_broadcast` | 发送时未被发送方认识的成员,仍能通过日志同步拿到消息(直推之外的补偿路径) |
| `test_room_history_backlog_is_not_replayed_again` | 加入时被跳过的老消息不会在后续同步里**再次当作新消息弹出** |
| `test_room_history_is_capped_and_deduplicated` | 日志有条数上限且按消息 ID 去重 |
#### 私聊
| 用例 | 验证内容 |
| --- | --- |
| `test_private_message_over_direct_connection` | 直连路径:双方路由表已知彼此,密文直接送达并成功解密 |
| `test_private_message_falls_back_to_overlay_routing` | 目标地址未知时**退回叠加网中继**(`send_routed`)送达 |
| `test_ciphertext_for_another_recipient_is_rejected` | 把同一段密文发给**另一个**接收者,因 AAD 不匹配而解密失败、被拒(验证 `_direct_aad` 的绑定作用) |
| `test_duplicate_private_message_is_ignored` | 同一条私聊(相同消息 ID)只上屏一次 |
| `test_private_message_reaches_offline_peer_through_inbox` | 对方离线时消息被写入 DHT 信箱;对方上线后轮询取出并解密 |
---
## 3. 三条最容易被忽视的测试意图
1. **`test_room_sync_picks_up_messages_missed_by_broadcast`**
房间消息有两条通路(直推 + DHT 日志)。这个用例固定了「直推漏掉时日志能补上」这一行为——否则「新成员看不到刚发的消息」会成为一个难查的偶发 bug。
2. **`test_room_history_backlog_is_not_replayed_again`**
加入房间时会用 `seen` 把窗口外的老消息标记为「已知但不上屏」。这个用例防止后续同步把它们当新消息再弹一遍。
3. **`test_ciphertext_for_another_recipient_is_rejected`**
它验证的不是「加密能解开」,而是「**只能被正确的那个人解开**」。私聊的 AAD 把密文绑定到唯一一对节点 ID,这是重放/转发攻击的防线。
---
## 4. 运行
```bat
py -3 -m unittest discover -s tests -v
```
全部 48 个用例(本文件 16 个 + [test_dht.md](./test_dht.md) 22 个 + [test_crypto.md](./test_crypto.md) 10 个)在数秒内完成。