Archived
refactor!: uv 管理环境 + 彻底删除技术债(Flask 与 requests 全部移除)
## uv 环境管理 - pyproject.toml 声明 dependencies + [dependency-groups].dev,uv.lock 精确锁定 - 删除 requirements.txt / requirements-fastapi.txt / requirements-dev.txt - .python-version = 3.12;Dockerfile 改用 uv sync --frozen(可复现构建) - 新增 jinja2(首页渲染) ## 删除 Flask 技术债 - 从 main.py 用 ast 精确剥离 56 个 Flask 节点(路由/中间件/错误处理/启动), 框架无关的服务层落为 services/runtime.py(MusicAPIService、下载/打包 worker、 任务监控、缓存清扫),零 Flask 依赖 - 删除 main.py(4527 行)与 auth_routes.py(1050 行) - auth_system.py 移除会话/请求上下文层(login_session/current_user/login_required/ admin_required/_client_ip 等),保留数据与业务层 - 新增 core/net.py(IP 解析)、web/auth_state.py(登录锁定/2FA 票据/发码限流) - FastAPI 侧改直接依赖 services.runtime,不再 import main - web/app.py 去掉 Flask fallback;入口默认根路径模式(API_PREFIX 可选) ## 删除 requests 技术债 - 新增 core/sync_http.py:requests 风格的同步门面(底层 aiohttp 连接池, 流式读取经有界队列背压),含 Session/headers/(connect,read) 元组超时兼容 - core/utils、auth_system、podcast_api、music_downloader、services 全部切换 - 实测真实下载 5.2MB 音频完整成功 ## 质量门禁归零 - ruff:0 错误(B904 全部修复,不再忽略) - pyright:0 错误(类型收窄修复) - pytest:140/140(含 golden 契约回归、root 模式 66 项路径覆盖) - 真实 uvicorn 启动:/health、/、/static、受保护端鉴权全部正确 新增 tests/golden/responses.json:删除 Flask 前捕获的 32 例响应快照, FastAPI 逐字段复现——删除巨石后行为回归仍有硬证据。
This commit is contained in:
1 parent
f5d1e3c3a9
commit
8446166120
41 files changed
+4836
-6060
No files matched your search
@@ -48,7 +48,7 @@ def _login(env, username):
|
||||
base64 padding('=') 的值加双引号,导致服务端签名解析失败(真实浏览器不会),
|
||||
故测试避开 jar。
|
||||
"""
|
||||
r = env.post('/api/v1/auth/login', json={'username': username, 'password': PW})
|
||||
r = env.post('/auth/login', json={'username': username, 'password': PW})
|
||||
assert r.status_code == 200, r.text
|
||||
raw = r.headers.get('set-cookie', '')
|
||||
cookie = ''
|
||||
@@ -73,7 +73,7 @@ def _open_ledger_rows():
|
||||
|
||||
|
||||
def test_download_requires_login(env):
|
||||
r = env.post('/api/v1/download', json={'id': '347230'})
|
||||
r = env.post('/download', json={'id': '347230'})
|
||||
assert r.status_code == 401
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ def test_download_bad_id_refunds(env):
|
||||
ck, _ = _login(env, 'payer')
|
||||
H = {'Cookie': f'session={ck}'}
|
||||
before = _balance('payer')
|
||||
r = env.post('/api/v1/download', json={'id': 'not-a-number'}, headers=H)
|
||||
r = env.post('/download', json={'id': 'not-a-number'}, headers=H)
|
||||
assert r.status_code == 400
|
||||
assert '无效' in r.json()['message']
|
||||
# 预扣已退回,且无 open 账本行残留
|
||||
@@ -93,7 +93,7 @@ def test_download_bad_quality_refunds(env):
|
||||
ck, _ = _login(env, 'payer')
|
||||
H = {'Cookie': f'session={ck}'}
|
||||
before = _balance('payer')
|
||||
r = env.post('/api/v1/download', json={'id': '347230', 'quality': 'nope'}, headers=H)
|
||||
r = env.post('/download', json={'id': '347230', 'quality': 'nope'}, headers=H)
|
||||
assert r.status_code == 400
|
||||
assert _balance('payer') == before
|
||||
assert _open_ledger_rows() == []
|
||||
@@ -103,7 +103,7 @@ def test_download_bad_format_refunds(env):
|
||||
ck, _ = _login(env, 'payer')
|
||||
H = {'Cookie': f'session={ck}'}
|
||||
before = _balance('payer')
|
||||
r = env.post('/api/v1/download', json={'id': '347230', 'format': 'xml'}, headers=H)
|
||||
r = env.post('/download', json={'id': '347230', 'format': 'xml'}, headers=H)
|
||||
assert r.status_code == 400
|
||||
assert _balance('payer') == before
|
||||
|
||||
@@ -111,7 +111,7 @@ def test_download_bad_format_refunds(env):
|
||||
def test_download_insufficient_credits(env):
|
||||
ck, _ = _login(env, 'broke')
|
||||
H = {'Cookie': f'session={ck}'}
|
||||
r = env.post('/api/v1/download', json={'id': '347230'}, headers=H)
|
||||
r = env.post('/download', json={'id': '347230'}, headers=H)
|
||||
assert r.status_code == 401
|
||||
assert r.json().get('error_code') == 'expired'
|
||||
# 未开账
|
||||
@@ -124,7 +124,7 @@ def test_batch_zip_too_many_refunds(env):
|
||||
H = {'Cookie': f'session={ck}'}
|
||||
before = _balance('rich')
|
||||
ids = ','.join(str(1000 + i) for i in range(501))
|
||||
r = env.post('/api/v1/download/batch_zip', json={'ids': ids}, headers=H)
|
||||
r = env.post('/download/batch_zip', json={'ids': ids}, headers=H)
|
||||
assert r.status_code == 400
|
||||
assert '500' in r.json()['message']
|
||||
# 预扣(封顶 500)已全额退回,余额回到 600
|
||||
@@ -136,6 +136,6 @@ def test_batch_zip_missing_ids_refunds(env):
|
||||
ck, _ = _login(env, 'payer')
|
||||
H = {'Cookie': f'session={ck}'}
|
||||
before = _balance('payer')
|
||||
r = env.post('/api/v1/download/batch_zip', json={}, headers=H)
|
||||
r = env.post('/download/batch_zip', json={}, headers=H)
|
||||
assert r.status_code == 400
|
||||
assert _balance('payer') == before
|
||||
Reference in new issue
Block a user