diff --git a/CHANGELOG.md b/CHANGELOG.md index 93854d4..ad982ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # CHANGELOG +## 2026-10-23 · 代码体检:修 7 个测试失败 + 测试隔离(2.9.13 → 2.9.14) + +老板:「检查 ai 网关项目,看下有什么问题或者可以优化的点」→ 体检中发现测试套件跑不起来/有回归。 + +### 发现并修复 +| 问题 | 处理 | +|---|---| +| **pytest 未安装**,`tests/` 里 6197 行 / 225 个用例根本跑不了 | 装到 venv,新增 `requirements-dev.txt` | +| **测试直接读写生产库**(`data/calllog.db`、`data/users.json`)| 新增 `tests/conftest.py`:导出 `CALLLOG_DB` 到临时目录 + 自动打开库(TestClient 不走 lifespan)| +| **4 个用例断言内存版 `/logs`**(2.9.6 改读库后失效)→ 我引入的回归 | 改为断言前 `calllog.store.flush()`;「只看自己」用例改为往库里塞记录,并改用 `/logs/calls` 断言管理员视角 | +| **3 处夹具引用 `grok-4.6`**(模型清单同步后已下线)| 改为 `grok-4.7` | +| `/logs` 的库映射**缺 `video_id`** | `api_calls` 增 `video_id` 列(自动迁移),读写与查询都带上 | +| 测试不幂等(失败会残留 `bob2` 账号)| 用例开头先删 | + +**结果:225/225 全部通过**(此前 218/225)。 + ## 2026-10-23 · 消灭最后两处内存态:视频回调任务落库 + /logs 改读库(2.9.12 → 2.9.13) 老板:「看下项目的数据还有什么数据是放在内存重启就没了的吗」→ 盘点后「都做」。 diff --git a/app/calllog.py b/app/calllog.py index f785acf..f1738d1 100644 --- a/app/calllog.py +++ b/app/calllog.py @@ -61,6 +61,7 @@ CREATE TABLE IF NOT EXISTS api_calls ( served TEXT, provider TEXT, caller TEXT, + video_id TEXT, stream INTEGER DEFAULT 0, ok INTEGER DEFAULT 1, status INTEGER, @@ -112,7 +113,7 @@ CREATE INDEX IF NOT EXISTS idx_vj_status ON video_jobs(status, created); """ _COLUMNS = ("ts", "rid", "account", "key_id", "kind", "method", "path", "model", - "served", "provider", "caller", "stream", "ok", "status", "latency_ms", + "served", "provider", "caller", "video_id", "stream", "ok", "status", "latency_ms", "ip", "ua", "prompt", "completion", "tok_prompt", "tok_completion", "tok_cached", "tok_reasoning", "ver") @@ -231,7 +232,7 @@ class CallLogStore: def _migrate(db: sqlite3.Connection) -> None: """轻量迁移:老库缺 token 列时补上(2026-10-23 加)。""" have = {r[1] for r in db.execute("PRAGMA table_info(api_calls)")} - for col in ("tok_prompt", "tok_completion", "tok_cached", "tok_reasoning"): + for col in ("tok_prompt", "tok_completion", "tok_cached", "tok_reasoning", "video_id"): if col not in have: db.execute(f"ALTER TABLE api_calls ADD COLUMN {col} INTEGER DEFAULT 0") log.info("调用日志表已补列: %s", col) @@ -376,7 +377,7 @@ class CallLogStore: # 列表页也要「内容」列(2026-10-23 老板要求):始终取 prompt 生成单行预览, # 但不把动辄几千字的原文整包吐给列表(with_content=1 才给完整 prompt/completion)。 cols = ("id, ts, rid, account, key_id, kind, method, path, model, served, provider, " - "caller, stream, ok, status, latency_ms, ip, ua, ver, prompt, " + "caller, video_id, stream, ok, status, latency_ms, ip, ua, ver, prompt, " "tok_prompt, tok_completion, tok_cached, tok_reasoning") if with_content: cols += ", completion" @@ -697,6 +698,7 @@ def _row(entry: dict[str, Any], meta: dict[str, Any]) -> dict[str, Any]: "served": entry.get("served"), "provider": entry.get("provider"), "caller": entry.get("caller"), + "video_id": entry.get("video_id"), "stream": 1 if entry.get("stream") else 0, "ok": 1 if entry.get("ok") else 0, "status": meta.get("status"), diff --git a/app/main.py b/app/main.py index 64848bb..04b6c7e 100644 --- a/app/main.py +++ b/app/main.py @@ -383,7 +383,7 @@ _docs_on = config.server.docs_enabled # 安全默认关(report: /docs、/ope # 2026-09-30: 2.6.1 → 2.7.0(按老板要求整个删除「火山方舟 Coding Plan」供应商:配置/模型/代码/文档/前端下拉/环境变量全清)。 # 2026-09-30: 2.7.0 → 2.7.1(界面提示白话化:熔断器→供应商自动保护、引擎→运行环境、slug→标识;删 120 个未引用死键)。 # ★ 版本号纪律:每次改动都要往上加,小改动加小版本号、大改动加大版本号(README 有专章)。 -VERSION = "2.9.13" +VERSION = "2.9.14" app = FastAPI( title="API Gateway", @@ -3242,6 +3242,9 @@ def _recent_entries(account: str, n: int = 200) -> list[dict]: "model": it.get("model"), "served": it.get("served"), "provider": it.get("provider"), "caller": it.get("caller"), "ok": bool(it.get("ok")), "stream": bool(it.get("stream")), "status": it.get("status"), "ms": it.get("latency_ms"), + # 视频条目:库里没有独立的 video_id 列,video_id 存在 served 上(与旧内存条目保持同名) + **({"video_id": it.get("video_id") or it.get("served")} + if it.get("kind") == "video" else {}), "body": (it.get("prompt") or "")[:8000], "response": (it.get("completion") or "")[:8000], "body_len": len(it.get("prompt") or ""), "resp_len": len(it.get("completion") or ""), "tokens": {"prompt_tokens": it.get("tok_prompt") or 0, diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..85790d8 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,3 @@ +# 开发/测试依赖(生产不需要) +-r requirements.txt +pytest>=8.0 # tests/ 有 225 个用例:/root/.venvs/api-gateway/bin/python -m pytest tests/ -q diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..751e552 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,31 @@ +"""测试隔离(2026-10-23)。 + +问题:`app.calllog` 在**导入时**按 `CALLLOG_DB` 决定库路径,默认落到 `data/calllog.db` +—— 也就是**生产库**。测试一跑就会往里写测试数据(还会污染 /metrics 的统计口径)。 + +conftest 在测试模块导入**之前**执行,所以在这里改环境变量最合适: +让所有测试写到临时目录,跑完即弃,生产库一个字节都不动。 +""" + +import os +import tempfile + +_TMP = tempfile.mkdtemp(prefix="gw-test-calllog-") +os.environ.setdefault("CALLLOG_DB", os.path.join(_TMP, "calllog-test.db")) +# 内容上限调小,避免测试里的大 prompt 把临时库撑大 +os.environ.setdefault("CALLLOG_CONTENT_MAX", "200000") + +import pytest + + +@pytest.fixture(scope="session", autouse=True) +def _calllog_ready(): + """测试会话内把调用日志库打开(TestClient 不走 lifespan,否则 flush() 写不进临时库)。""" + from app import calllog + + calllog.store.open() + yield + try: + calllog.store.flush() + except Exception: # noqa: BLE001 + pass diff --git a/tests/test_gateway.py b/tests/test_gateway.py index 35a5163..1dc0e4c 100644 --- a/tests/test_gateway.py +++ b/tests/test_gateway.py @@ -288,7 +288,7 @@ def test_app_endpoints(): # 每个模型都带 protocols(三线直传的能力声明) byid2 = {x["id"]: x for x in r2.json()["data"]} assert byid2["hy3"]["protocols"] == ["chat/completions"] - assert byid2["grok-4.6"]["protocols"] == ["responses"] + assert byid2["grok-4.7"]["protocols"] == ["responses"] # ── 根路径导航 ── @@ -724,6 +724,7 @@ def test_logs_include_request_body(): "messages": [{"role": "user", "content": "把这句话记进日志"}], }) assert r.status_code == 200, r.text + m.calllog.store.flush() # 日志异步批量写库,断言前先刷盘 j = client.get("/logs?n=20").json() chats = [e for e in j["entries"] if e.get("path") == "/v1/chat/completions"] assert chats, "应有 chat 日志条目" @@ -758,6 +759,7 @@ def test_logs_include_streaming_response(): }) assert r.status_code == 200, r.text assert "[DONE]" in r.text, r.text + m.calllog.store.flush() # 日志异步批量写库,断言前先刷盘 j = client.get("/logs?n=20").json() hit = [e for e in j["entries"] if e.get("path") == "/v1/chat/completions" @@ -2286,6 +2288,7 @@ def test_video_polls_merged_into_one_log_row_with_response(): vid = r.json()["video_id"] client.get(f"/v1/videos/{vid}", headers=ah) # 第 1 次轮询 processing client.get(f"/v1/videos/{vid}", headers=ah) # 第 2 次轮询 completed + m.calllog.store.flush() logs = client.get("/logs?n=200", headers=ah).json()["entries"] rows = [e for e in logs if e.get("video_id") == vid] assert len(rows) == 1, f"轮询应合成到创建那条里(实际 {len(rows)} 行): {[r['path'] for r in logs]}" @@ -2303,6 +2306,7 @@ def test_video_polls_merged_into_one_log_row_with_response(): r2 = client.post("/v1/images/generations", json={"model": "agnes-image-2.5-flash", "prompt": "hi", "n": 1}, headers=ah) assert r2.status_code == 200, r2.text + m.calllog.store.flush() # 日志异步批量写库,断言前先刷盘 imgs = [e for e in client.get("/logs?n=200", headers=ah).json()["entries"] if e.get("path") == "/v1/images/generations"] assert imgs and "n" in (imgs[-1].get("response") or ""), imgs[-1].get("response") @@ -2310,32 +2314,42 @@ def test_video_polls_merged_into_one_log_row_with_response(): def test_logs_own_only_and_admin_per_user_view(): - """第十一轮 #2:日志页对所有人只显示自己;admin 在用户管理端点看别人的日志与资产。""" + """第十一轮 #2 + 2026-10-23:/logs 只看自己的(现读库);全站明细走 /logs/calls(仅管理员)。""" from fastapi.testclient import TestClient + import time as _t import app.main as m client = TestClient(m.app) ah = {"Authorization": f"Bearer {_login_admin_session(client)}"} + client.delete("/auth/users/bob2", headers=ah) # 幂等:清掉上次失败可能残留的账号 assert client.post("/auth/users", json={"username": "bob2"}, headers=ah).status_code == 200 bob = client.post("/auth/login", json={"username": "bob2", "password": "bob2"}).json()["access_token"] bh = {"Authorization": f"Bearer {bob}"} - # 直接往访问日志塞一条属于 bob2 的记录(省去真打上游),并让 bob2 存一条图像资产 - m._access.add({"method": "POST", "path": "/v1/chat/completions", "rid": "r-bob2", - "account": "bob2", "caller": "bob2", "ok": True}) + # 2026-10-23:/logs 改读库 —— 直接往调用日志里塞一条属于 bob2 的记录(省去真打上游) + m.calllog.store.stage({"ts": _t.time(), "rid": "r-bob2", "account": "bob2", + "caller": "bob2", "kind": "chat", "method": "POST", + "path": "/v1/chat/completions", "ok": 1, "stream": 0, + "status": 200, "latency_ms": 1, "key_id": "", "ver": "test"}) + m.calllog.store.flush() client.post("/history/images", json={"items": [{"id": "img1", "url": "http://x/1.png", "prompt": "p"}]}, headers=bh) - # ① admin 的 /logs 不再包含 bob2 的行(以前 admin 看全部) + # ① 成员自己的 /logs 能看到这条(口径:只看自己的) + bob_logs = client.get("/logs?n=500", headers=bh).json()["entries"] + assert any(e.get("rid") == "r-bob2" for e in bob_logs), "成员应看到自己的日志" + # ② admin 的 /logs 不包含 bob2 的行(admin 也只看自己的) admin_logs = client.get("/logs?n=500", headers=ah).json()["entries"] assert all(e.get("account") != "bob2" for e in admin_logs), "日志页 admin 不该看到别人" - # ② admin 专属:按用户查日志 - r = client.get("/auth/users/bob2/logs?n=500", headers=ah) - assert r.status_code == 200 and any(e.get("rid") == "r-bob2" for e in r.json()["entries"]) - # ③ admin 专属:按用户查图像资产 + # ③ 全站明细:/logs/calls 仅管理员(成员 403) + r = client.get("/logs/calls?limit=500", headers=ah) + assert r.status_code == 200 and any(e.get("rid") == "r-bob2" for e in r.json()["items"]) + assert client.get("/logs/calls", headers=bh).status_code == 403 + # ④ 按用户查图像资产(admin 专属;成员 403;不存在账号 404;未知 kind 400) r2 = client.get("/auth/users/bob2/history/images", headers=ah) assert r2.status_code == 200 and any(i.get("id") == "img1" for i in r2.json()["items"]) - # ④ 非 admin 不能用这些端点;⑤ 不存在账号 404 - assert client.get("/auth/users/admin/logs", headers=bh).status_code == 403 assert client.get("/auth/users/admin/history/videos", headers=bh).status_code == 403 - assert client.get("/auth/users/ghost/logs", headers=ah).status_code == 404 + assert client.get("/auth/users/ghost/history/images", headers=ah).status_code == 404 assert client.get("/auth/users/bob2/history/bogus", headers=ah).status_code == 400 + # ⑤ 已删除的按用户查日志端点:一律 404 + assert client.get("/auth/users/admin/logs", headers=ah).status_code == 404 + assert client.get("/auth/users/ghost/logs", headers=ah).status_code == 404 client.delete("/auth/users/bob2", headers=ah) @@ -2544,7 +2558,7 @@ def test_models_expose_vision_capability(): if d.get("alias_of"): assert d["protocols"] == ["chat/completions"], d assert byid["hy3"]["protocols"] == ["chat/completions"] - assert byid["grok-4.6"]["protocols"] == ["responses"] + assert byid["grok-4.7"]["protocols"] == ["responses"] assert byid["deepseek-v4.1-flash"]["protocols"] == ["chat/completions", "messages", "responses"] @@ -4300,8 +4314,8 @@ def test_passthrough_responses_line_and_cross_line_gate(): with _Isolated() as mm: _bind_passthrough(mm, fake) c = TestClient(mm.app) - # grok-4.6 实测只认 responses 线 - r = c.post("/v1/responses", json={"model": "grok-4.6", "input": "hi", "max_tokens": 16}) + # grok-4.7 实测只认 responses 线 + r = c.post("/v1/responses", json={"model": "grok-4.7", "input": "hi", "max_tokens": 16}) assert r.status_code == 200, r.text assert fake.json_calls[-1][0] == m.PROTOCOL_RESPONSES assert r.json()["protocol"] == "responses", r.json()