|
|
@@ -65,8 +65,9 @@ def cfg(tmp_path):
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
def _reset_state(cfg):
|
|
|
- """每个用例前:重置 key 轮换游标 + 把 db 单例切到 tmp 库;用例后还原。"""
|
|
|
+ """每个用例前:重置 key 轮换游标/冷却表 + 把 db 单例切到 tmp 库;用例后还原。"""
|
|
|
media._KEY_CURSOR = None
|
|
|
+ media._KEY_COOLDOWN = {}
|
|
|
old_conn = db._CONN
|
|
|
db._CONN = None
|
|
|
db.conn(cfg)
|
|
|
@@ -137,7 +138,7 @@ def test_translate_ok(cfg, monkeypatch):
|
|
|
|
|
|
monkeypatch.setattr(media.requests, 'post', fake_post)
|
|
|
assert media.translate(cfg, '好物分享') == 'good stuff'
|
|
|
- assert 'fanyi-api.baidu.com/api/trans/vip/translate' in cap['url']
|
|
|
+ assert 'api.fanyi.baidu.com/api/trans/vip/translate' in cap['url']
|
|
|
d = cap['data']
|
|
|
assert d['from'] == 'zh' and d['to'] == 'en'
|
|
|
assert d['appid'] == 'testappid'
|
|
|
@@ -264,6 +265,97 @@ def test_ai_image_all_fail_raises(cfg, tmp_path, monkeypatch):
|
|
|
media.ai_image(cfg, 'p', out_path=tmp_path / 'x.jpeg')
|
|
|
|
|
|
|
|
|
+def test_ai_image_keys_array_rotation(cfg, tmp_path, monkeypatch):
|
|
|
+ """zhipu_keys 数组形式(推荐填法):顺序轮换 ak_1→ak_2→ak_1。"""
|
|
|
+ cfg['ai']['zhipu_keys'] = ['ak_1', 'ak_2']
|
|
|
+ cfg['ai']['zhipu_api_key'] = ''
|
|
|
+ bears = []
|
|
|
+
|
|
|
+ def fake_post(url, json=None, headers=None, **kw):
|
|
|
+ bears.append(headers['Authorization'])
|
|
|
+ return FakeResp(js={'data': [{'b64_json': base64.b64encode(b'x').decode()}]})
|
|
|
+
|
|
|
+ monkeypatch.setattr(media.requests, 'post', fake_post)
|
|
|
+ for _ in range(3):
|
|
|
+ media.ai_image(cfg, 'p', out_path=tmp_path / 'a.jpeg')
|
|
|
+ assert bears == ['Bearer ak_1', 'Bearer ak_2', 'Bearer ak_1']
|
|
|
+
|
|
|
+
|
|
|
+def test_ai_image_keys_merge_dedup(cfg, tmp_path, monkeypatch):
|
|
|
+ """数组 + 竖线串并存:合并去重保序(数组在前,字符串位补充)。"""
|
|
|
+ cfg['ai']['zhipu_keys'] = ['ak_1', 'sk_k1'] # sk_k1 与字符串位重复
|
|
|
+ bears = []
|
|
|
+
|
|
|
+ def fake_post(url, json=None, headers=None, **kw):
|
|
|
+ bears.append(headers['Authorization'])
|
|
|
+ return FakeResp(js={'data': [{'b64_json': base64.b64encode(b'x').decode()}]})
|
|
|
+
|
|
|
+ monkeypatch.setattr(media.requests, 'post', fake_post)
|
|
|
+ for _ in range(5):
|
|
|
+ media.ai_image(cfg, 'p', out_path=tmp_path / 'm.jpeg')
|
|
|
+ assert bears == ['Bearer ak_1', 'Bearer sk_k1', 'Bearer sk_k2',
|
|
|
+ 'Bearer sk_k3', 'Bearer ak_1']
|
|
|
+
|
|
|
+
|
|
|
+def test_ai_image_failed_key_cooldown(cfg, tmp_path, monkeypatch):
|
|
|
+ """失败的 key 进入冷却:冷却期内跳过,不重撞刚失败的 key;成功即解冷。"""
|
|
|
+ calls = []
|
|
|
+
|
|
|
+ def fake_post(url, json=None, headers=None, **kw):
|
|
|
+ a = headers['Authorization']
|
|
|
+ calls.append(a)
|
|
|
+ if a == 'Bearer sk_k1':
|
|
|
+ return FakeResp(status=500) # 仅 sk_k1 永远失败
|
|
|
+ return FakeResp(js={'data': [{'b64_json': base64.b64encode(b'x').decode()}]})
|
|
|
+
|
|
|
+ monkeypatch.setattr(media.requests, 'post', fake_post)
|
|
|
+ for i in range(3):
|
|
|
+ media.ai_image(cfg, 'p', out_path=tmp_path / f'cd{i}.jpeg')
|
|
|
+ # 调用1: k1✗(进冷却)→k2✓;调用2: k3✓;调用3: k1冷却中跳过→k2✓
|
|
|
+ assert calls == ['Bearer sk_k1', 'Bearer sk_k2', 'Bearer sk_k3',
|
|
|
+ 'Bearer sk_k2']
|
|
|
+ assert calls.count('Bearer sk_k1') == 1 # 冷却期内未被重试
|
|
|
+
|
|
|
+
|
|
|
+def test_ai_image_all_cooldown_force_retry(cfg, tmp_path, monkeypatch):
|
|
|
+ """单 key 池且该 key 已在冷却:忽略冷却强试,不饿死。"""
|
|
|
+ cfg['ai']['zhipu_api_key'] = 'only_k'
|
|
|
+ cfg['ai']['pollinations_fallback'] = False
|
|
|
+ flaky = {'fail': True}
|
|
|
+
|
|
|
+ def fake_post(url, json=None, headers=None, **kw):
|
|
|
+ if flaky['fail']:
|
|
|
+ return FakeResp(status=500)
|
|
|
+ return FakeResp(js={'data': [{'b64_json': base64.b64encode(b'x').decode()}]})
|
|
|
+
|
|
|
+ monkeypatch.setattr(media.requests, 'post', fake_post)
|
|
|
+ with pytest.raises(media.MediaError):
|
|
|
+ media.ai_image(cfg, 'p', out_path=tmp_path / 'f1.jpeg') # 失败,only_k 进冷却
|
|
|
+ flaky['fail'] = False
|
|
|
+ out = media.ai_image(cfg, 'p', out_path=tmp_path / 'f2.jpeg') # 全冷却→强试成功
|
|
|
+ assert out.exists()
|
|
|
+
|
|
|
+
|
|
|
+def test_ai_image_cooldown_zero(cfg, tmp_path, monkeypatch):
|
|
|
+ """key_cooldown_sec=0(不冷却):失败的 key 立即可重试,按轮换顺序照常取用。"""
|
|
|
+ cfg['ai']['key_cooldown_sec'] = 0
|
|
|
+ calls = []
|
|
|
+
|
|
|
+ def fake_post(url, json=None, headers=None, **kw):
|
|
|
+ a = headers['Authorization']
|
|
|
+ calls.append(a)
|
|
|
+ if a == 'Bearer sk_k1':
|
|
|
+ return FakeResp(status=500)
|
|
|
+ return FakeResp(js={'data': [{'b64_json': base64.b64encode(b'x').decode()}]})
|
|
|
+
|
|
|
+ monkeypatch.setattr(media.requests, 'post', fake_post)
|
|
|
+ for i in range(3):
|
|
|
+ media.ai_image(cfg, 'p', out_path=tmp_path / f'z{i}.jpeg')
|
|
|
+ # 调用1: k1✗→k2✓;调用2: k3✓;调用3: 又轮到 k1(未冷却)→k1✗→k2✓
|
|
|
+ assert calls == ['Bearer sk_k1', 'Bearer sk_k2', 'Bearer sk_k3',
|
|
|
+ 'Bearer sk_k1', 'Bearer sk_k2']
|
|
|
+
|
|
|
+
|
|
|
def test_ai_image_default_dir(cfg, tmp_path, monkeypatch):
|
|
|
"""不给 out_path 时默认落 实拍/*.jpeg。"""
|
|
|
monkeypatch.setattr(media.requests, 'post',
|
|
|
@@ -274,6 +366,41 @@ def test_ai_image_default_dir(cfg, tmp_path, monkeypatch):
|
|
|
assert p.suffix == '.jpeg' and p.exists()
|
|
|
|
|
|
|
|
|
+def test_ai_image_size_configurable(cfg, tmp_path, monkeypatch):
|
|
|
+ """F线 fn_cogview_a(prompt,w,h):尺寸经 ai.image_size 注入 cogview 请求体。"""
|
|
|
+ cap = {}
|
|
|
+
|
|
|
+ def fake_post(url, json=None, headers=None, **kw):
|
|
|
+ cap['body'] = json
|
|
|
+ return FakeResp(js={'data': [{'b64_json': base64.b64encode(b'x').decode()}]})
|
|
|
+
|
|
|
+ monkeypatch.setattr(media.requests, 'post', fake_post)
|
|
|
+ cfg['ai']['image_size'] = '768x1024'
|
|
|
+ media.ai_image(cfg, 'p', out_path=tmp_path / 's.jpeg')
|
|
|
+ assert cap['body']['size'] == '768x1024'
|
|
|
+
|
|
|
+
|
|
|
+def test_ai_image_pollinations_params(cfg, tmp_path, monkeypatch):
|
|
|
+ """F线 §2.3 锚点串:model=flux/nologo/enhance/safe/seed + width/height 传齐。"""
|
|
|
+ got = {}
|
|
|
+
|
|
|
+ def fake_get(url, params=None, **kw):
|
|
|
+ got['url'], got['params'] = url, params
|
|
|
+ return FakeResp(content=b'P')
|
|
|
+
|
|
|
+ monkeypatch.setattr(media.requests, 'post',
|
|
|
+ lambda url, **kw: FakeResp(status=500)) # 智谱全失败→兜底
|
|
|
+ monkeypatch.setattr(media.requests, 'get', fake_get)
|
|
|
+ cfg['ai']['image_size'] = '720x1280'
|
|
|
+ media.ai_image(cfg, 'p', out_path=tmp_path / 'pp.jpeg')
|
|
|
+ p = got['params']
|
|
|
+ assert p['model'] == 'flux' and p['nologo'] == 'true'
|
|
|
+ assert p['enhance'] == 'true' and p['safe'] == 'true'
|
|
|
+ assert p['width'] == '720' and p['height'] == '1280'
|
|
|
+ assert int(p['seed']) >= 1 # seed 每次随机
|
|
|
+ assert got['url'].startswith('https://image.pollinations.ai/prompt/')
|
|
|
+
|
|
|
+
|
|
|
# --------------------------------------------------------------------------
|
|
|
# 4. compose(ffmpeg;不可用则 skip)
|
|
|
# --------------------------------------------------------------------------
|
|
|
@@ -319,6 +446,38 @@ def test_compose_no_font_skips_text(cfg, tmp_path, monkeypatch):
|
|
|
assert out.exists() and out.stat().st_size > 0
|
|
|
|
|
|
|
|
|
+def test_caption_filter_params(cfg, tmp_path):
|
|
|
+ """J线 §1.4 文字封面参数面:默认值 + cfg['caption'] 覆盖映射进 drawtext 串。"""
|
|
|
+ tf = tmp_path / 'title.txt'
|
|
|
+ tf.write_text('标题', encoding='utf-8')
|
|
|
+ f, font = media._caption_filter(cfg, str(tf))
|
|
|
+ assert 'box=1' in f and 'boxcolor=black@0.5' in f # 默认:黑底@0.5 背景框
|
|
|
+ assert 'fontsize=32' in f and 'fontcolor=white' in f
|
|
|
+ assert ':borderw=' not in f and 'shadowcolor=' not in f # 默认无描边/阴影
|
|
|
+ cfg['caption'] = {'border_w': 3, 'border_color': 'yellow',
|
|
|
+ 'shadow_color': 'black', 'box': False,
|
|
|
+ 'font_size': 48, 'font_color': 'red'}
|
|
|
+ f2, _ = media._caption_filter(cfg, str(tf))
|
|
|
+ assert 'fontsize=48' in f2 and 'fontcolor=red' in f2
|
|
|
+ assert ':borderw=3' in f2 and 'bordercolor=yellow' in f2
|
|
|
+ assert 'shadowcolor=black:shadowx=2:shadowy=2' in f2
|
|
|
+ assert 'box=1' not in f2 # 背景框关闭
|
|
|
+
|
|
|
+
|
|
|
+@need_ff
|
|
|
+def test_compose_caption_config(cfg, tmp_path):
|
|
|
+ """caption 参数端到端:描边+阴影滤镜串被 ffmpeg 接受,成片正常产出。"""
|
|
|
+ cfg['caption'] = {'border_w': 2, 'border_color': 'white',
|
|
|
+ 'shadow_color': 'black', 'font_size': 28}
|
|
|
+ src = tmp_path / 'vidcap.mp4'
|
|
|
+ make_video(src)
|
|
|
+ img = tmp_path / 'ai9.jpeg'
|
|
|
+ make_image(img)
|
|
|
+ out = media.compose(cfg, src, img, '封面参数端到端测试')
|
|
|
+ assert out == Path(cfg['dirs']['临存视频']) / 'vidcap.mp4'
|
|
|
+ assert out.exists() and out.stat().st_size > 0
|
|
|
+
|
|
|
+
|
|
|
def test_compose_missing_input_raises(cfg, tmp_path):
|
|
|
"""源视频不存在 → MediaError(纯 Python,不依赖 ffmpeg)。"""
|
|
|
with pytest.raises(media.MediaError):
|
|
|
@@ -382,7 +541,7 @@ def test_process_full(cfg, tmp_path, monkeypatch):
|
|
|
|
|
|
def fake_post(url, data=None, json=None, headers=None, **kw):
|
|
|
cap['posts'].append((url, json, data, headers))
|
|
|
- if 'fanyi-api.baidu.com' in url:
|
|
|
+ if 'api.fanyi.baidu.com' in url:
|
|
|
return FakeResp(js={'trans_result': [
|
|
|
{'src': '神器清洁海绵', 'dst': 'magic cleaning sponge'}]})
|
|
|
return FakeResp(js={'data': [
|
|
|
@@ -403,7 +562,7 @@ def test_process_full(cfg, tmp_path, monkeypatch):
|
|
|
assert prompt.startswith('magic cleaning sponge')
|
|
|
assert media.NEGATIVE.split(', ')[0] in prompt # 'no people'
|
|
|
# 翻译请求确实发生且 from/to 正确
|
|
|
- tr_calls = [d for (u, b, d, h) in cap['posts'] if 'fanyi-api.baidu.com' in u]
|
|
|
+ tr_calls = [d for (u, b, d, h) in cap['posts'] if 'api.fanyi.baidu.com' in u]
|
|
|
assert tr_calls and tr_calls[0]['from'] == 'zh' and tr_calls[0]['to'] == 'en'
|
|
|
# AI 图落 实拍/*.jpeg
|
|
|
assert list(Path(cfg['dirs']['实拍']).glob('*.jpeg'))
|