fix(tests): Resolve pytest session binding issues and update test cases

- Updated test files to import `AgentSessionLocal` from `advisor_db` instead of directly, preventing session binding to the real database during tests.
- Fixed 6 test cases to use the new login token utility, ensuring consistency across authentication methods.
- Adjusted customer risk codes in `test_convert_confirm.py` to reflect changes in customer classification (C3 to C4).
- Verified that changes resulted in zero database pollution during test runs, maintaining integrity of the testing environment.
- Documented findings and updates in the relevant test logs and memory files, ensuring clarity on the current state of tests and defects.
This commit is contained in:
2026-09-13 23:21:34 +08:00
parent 188bf6a438
commit f856ab4aa6
20 changed files with 86 additions and 92 deletions
+1 -1
View File
@@ -30,7 +30,7 @@
- ✅ **Phase 6 还原已校验**:`core` 5 表按快照还原,**逐表 COUNT + CHECKSUM 与快照完全一致**(`95/59/73/19/0`)· `script_template` 页面可见恢复到 **20**
- 🟢 **已拍板 2 项(用户 2026-09-13 定)**:**F-14 维持现状**(「目前够用就行」—— 接受 LLM 自撰风险提示措辞,**不扩** `compliance_guard.py:109` 名单;代价是措辞与是否出现由 LLM 随机决定,`06-chat.mjs` 中该条已降级为 2 条 INFO)· **RK 发现-1 关闭**(「这个不用管」—— 未知客户语义保持 officer 200 / advisor 403 / customer 403 **无一 404**,现状不泄漏客户存在性,仅作实测事实保留在风控包与横切包)
- 🟢 **已拍板 · F-β = 选 A(用户 2026-09-13 定)**:**维持现状语义不动,下一轮只改测试**(`test_convert_confirm.py` 客户档位 C3→**C4** 或产品 R4→**R3**),**零业务代码改动**。**规则固化:`C3×R4`/`C4×R5` = 匹配但需揭示 ⇒ 自助渠道阻断并引导线下,这是设计意图不是缺陷**;**禁止给阻断响应体补 `forced_full_transfer` 让测试变绿**。上一轮 P1 修复使 `test_convert_confirm.py` **28 条失败**,**根因是测试前提过期,非接口缺陷**。依据:① L0 矩阵**刻意设三态**(`core_suitability_rule` 25 行实测)—— `C3×R4`/`C4×R5` = `allowed_with_disclosure`,服务层 `matched=True`/`blocked=False`/`mismatch_type="none"`/`block_code="SUIT_NEED_DISCLOSURE"`,**不是"不匹配"**,而是"匹配但须特别风险警示+签揭示书"(适当性办法 §19);② "需揭示→自助端阻断"是**已定设计**,三处同指:答辩稿 `答辩知识点清单.md:59`「**C3×R4 揭示 block**」· `CustomerTradePage.tsx:116`「标有『风险不匹配/需揭示』的请换产品或**到网点办理**」· `tradeEligibility.ts` 两类均 `canSelfServe:false`+`tone:'warn'`+标签「需揭示/**网点办理**」(揭示需双录/柜面,**线上无法完成"签署"动作**)⇒ P1 修复是**后端追平前端**;③ 28 条失败根因 = **测试前提过期**:`test_convert_confirm.py:1013` 的 `forced_full_transfer` 是**受理成功路径**字段,`CUST-CC`(C3) 被阻断后受理未发生 ⇒ `KeyError`;**阻断体里本就不该有该键**。**选项 B(已否决)**=把 PRD 二期(放行+提示)提前做,需新建前端揭示书 Modal + 留痕 + 后端受理分支;**只改后端不建前端更糟**。**🟠 新增记账(真缺口,只记录不修)**:文案引导的「网点办理/请联系持证投顾」**无实现通路** —— 理财师线**零交易 API**,全仓唯一入口 `POST /api/simulate/trade` 不区分渠道身份 ⇒ `C3×R4` 当前**任何渠道都买不成**,**文案承诺 > 实现能力**。处置二选一待定:① 答辩口径统一为"线下受理一期未实现";② 改前端文案(`CustomerTradePage.tsx:116`)
- 🔴 **F-ε(影响最广)**:**`pytest` 会写真实的 `jinrong_agent` 与 `jinrong_core`** —— 根因 `scripts/seed/import_script_templates.py:14` **模块级**绑定 `AgentSessionLocal`,而 `tests/test_sprint2_template_import.py` 未请求 sqlite fixture。实测:`script_template` 47→65 行、**20 条演示模板被翻成未审核(页面可见 20→6)**、`jinrong_core` 亦被写入。**本轮只还原,根因未修 ⇒ 再跑一次 pytest 污染即复现**
- 🟢 **F-ε(已修 · 2026-09-13 修复轮)**:`pytest` 写真库根因 = 测试文件**模块级** `from app.advisor_db import AgentSessionLocal` 在收集期绑定真库 sessionmaker,绕过 fixture 对 `app.advisor_db.AgentSessionLocal` 的 monkeypatch(`test_sprint1_compliance_rule_import.py` 早已用正确模式 `import app.advisor_db as advisor_db`)。**修法**:① 10 个 `test_sprint*.py` 改 `from app import advisor_db` + 调用点 `advisor_db.AgentSessionLocal`/`advisor_db.agent_engine`;② `import_script_templates.py` 同改;③ 唯一写真库的 subprocess 用例(seed 脚本独立跑)改 `@pytest.mark.skip`(另两处 subprocess `sync_template_vectors --dry-run` 只计数、`evaluate_compliance` 不带 trace_id/advisor_id 不写日志,经查只读)。**验证**:跑后 `script_template` 仍 65 / seed 20 / `test:*` 36,零污染。**同轮另发现并修**:6 个 `test_sprint2_*.py` 的 `token_for(username,password)` 打 `/api/v1/auth/login` + `{username,password}`,与现行 `POST /api/auth/login`(`actor_id`+`token_type`+`roles`)不符 ⇒ 迁移到 `tests/advisor_test_utils.login_token`。**副作用(如实)**:转 sqlite 后暴露 F-α 假 DDL 缺列(`agent_session` 无 `metadata` 等),sprint 组 45 条失败为陈旧 DDL/期望,非本轮引入
- 🔴 **F-ζ**:`--ignore-glob=test_sprint*.py` **是空操作**(pytest 匹配完整路径,缺 `*/`)⇒ 文档门槛"排除 sprint 后的 896"**从未按设计生效**。修正写法 `--ignore-glob='*/test_sprint*.py'`(1368→1274)
- 🔴 **F-η**:测试套件**非幂等** —— 同库同命令**连跑两次 FAIL 集合不同**(129 vs 127 行)⇒ **单次 `pytest` 的 passed 数不可作"不退步"基线**,引用须附跑批序号
- ⚠️ **F-γ 更正**:Phase 0 记的 `core_share_lot=74` 与同次快照(**73 行**)**不自洽** ⇒ 还原判据改为「**与快照逐表 CHECKSUM 一致**」,不再用某个读数值