|
|
@@ -0,0 +1,112 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+"""cli.py 单元测试:config.load 与全部网络函数 monkeypatch 打桩,绝不真实联网。
|
|
|
+
|
|
|
+运行:python -X utf8 -m pytest tests/test_cli.py -q
|
|
|
+"""
|
|
|
+import sys
|
|
|
+from pathlib import Path
|
|
|
+
|
|
|
+import pytest
|
|
|
+
|
|
|
+ROOT = Path(__file__).resolve().parent.parent
|
|
|
+sys.path.insert(0, str(ROOT))
|
|
|
+
|
|
|
+from kuaishou_tool import cli, collect, config, db, media, niu # noqa: E402
|
|
|
+
|
|
|
+
|
|
|
+@pytest.fixture(autouse=True)
|
|
|
+def _env(tmp_path, monkeypatch):
|
|
|
+ """临时 cfg + 临时库;cli.config.load 返回隔离配置。"""
|
|
|
+ cfg = {'db_path': str(tmp_path / 'data.db'),
|
|
|
+ 'dirs': {k: str(tmp_path / k) for k in config.DEFAULTS['dirs']},
|
|
|
+ 'collect': {}, 'publish': {}, 'ai': {}, 'ads': {'enabled': False}}
|
|
|
+ monkeypatch.setattr(cli.config, 'load', lambda path=None: cfg)
|
|
|
+ old = db._CONN
|
|
|
+ db._CONN = None
|
|
|
+ db.conn(cfg)
|
|
|
+ yield cfg
|
|
|
+ try:
|
|
|
+ db._CONN.close()
|
|
|
+ except Exception:
|
|
|
+ pass
|
|
|
+ db._CONN = old
|
|
|
+
|
|
|
+
|
|
|
+def test_monitor_batch(_env, tmp_path, monkeypatch, capsys):
|
|
|
+ """batch:读文件(空行/#注释剔除)+--fenlei 透传,输出新增/跳过计数。"""
|
|
|
+ f = tmp_path / 'uids.txt'
|
|
|
+ f.write_text('u1\nu2\n\n# 注释行\nu3\n', encoding='utf-8')
|
|
|
+ rec = {}
|
|
|
+
|
|
|
+ def fake_batch(uids, fenlei='', beizhu=''):
|
|
|
+ rec.update(uids=uids, fenlei=fenlei)
|
|
|
+ return 3, 1
|
|
|
+
|
|
|
+ monkeypatch.setattr(collect, 'batch_add', fake_batch)
|
|
|
+ assert cli.main(['monitor', 'batch', str(f), '--fenlei', '测试组']) == 0
|
|
|
+ out = capsys.readouterr().out
|
|
|
+ assert rec['uids'] == ['u1', 'u2', 'u3'] and rec['fenlei'] == '测试组'
|
|
|
+ assert '新增 3' in out and '跳过 1' in out
|
|
|
+
|
|
|
+
|
|
|
+def test_monitor_del_and_refresh(_env, monkeypatch, capsys):
|
|
|
+ monkeypatch.setattr(collect, 'delete_monitor', lambda uid: True)
|
|
|
+ assert cli.main(['monitor', 'del', 'u1']) == 0
|
|
|
+ assert '已删除' in capsys.readouterr().out
|
|
|
+ monkeypatch.setattr(collect, 'refresh_monitor_stats', lambda cfg, uid=None: 2)
|
|
|
+ assert cli.main(['monitor', 'refresh', 'u9']) == 0
|
|
|
+ out = capsys.readouterr().out
|
|
|
+ assert '资料刷新成功 2 个账号' in out
|
|
|
+
|
|
|
+
|
|
|
+def test_resolve(_env, monkeypatch, capsys):
|
|
|
+ fake = {'videoId': '3x', 'biaoti': '磁力好物', 'likes': 5, 'plays': 6,
|
|
|
+ 'itemId': 'g1', 'url': 'http://v/3x.mp4'}
|
|
|
+ monkeypatch.setattr(collect, 'resolve_photo', lambda cfg, t: fake)
|
|
|
+ assert cli.main(['resolve', '看看', 'https://v.kuaishou.com/ShortL']) == 0
|
|
|
+ out = capsys.readouterr().out
|
|
|
+ assert '3x' in out and '磁力好物' in out and 'g1' in out
|
|
|
+ monkeypatch.setattr(collect, 'resolve_photo', lambda cfg, t: None)
|
|
|
+ assert cli.main(['resolve', '纯文本无链接']) == 1
|
|
|
+ assert '失效' in capsys.readouterr().out
|
|
|
+
|
|
|
+
|
|
|
+def test_process_single(_env, monkeypatch, capsys):
|
|
|
+ monkeypatch.setattr(media, 'process_full', lambda cfg, vid: 'OUT1')
|
|
|
+ assert cli.main(['process', 'vidX']) == 0
|
|
|
+ assert 'OUT1' in capsys.readouterr().out
|
|
|
+
|
|
|
+
|
|
|
+def test_process_all_pending(_env, monkeypatch, capsys):
|
|
|
+ """缺省参数:处理全部待处理(panduan='0' 且素材已落地),无素材的跳过。"""
|
|
|
+ db.exe("INSERT INTO biaoti (biaoti1, biaoti2, panduan) VALUES ('v1','t','0')")
|
|
|
+ db.exe("INSERT INTO biaoti (biaoti1, biaoti2, panduan) VALUES ('v2','t','0')")
|
|
|
+ monkeypatch.setattr(media, '_find_source',
|
|
|
+ lambda cfg, vid: Path('Z:/x.mp4') if vid == 'v1' else None)
|
|
|
+ made = []
|
|
|
+
|
|
|
+ def fake_process(cfg, vid):
|
|
|
+ made.append(vid)
|
|
|
+ return 'OK'
|
|
|
+
|
|
|
+ monkeypatch.setattr(media, 'process_full', fake_process)
|
|
|
+ assert cli.main(['process']) == 0
|
|
|
+ assert made == ['v1'] # v2 无素材被跳过
|
|
|
+ assert '本轮成片 1 个' in capsys.readouterr().out
|
|
|
+
|
|
|
+
|
|
|
+def test_niu_status(_env, monkeypatch, capsys):
|
|
|
+ """niu status:遍历 anchor 输出单轮摘要(默认只读)。"""
|
|
|
+ db.exe("INSERT INTO anchor (uid, userName, cookie1) VALUES ('U1','号A','ck')")
|
|
|
+ rec = []
|
|
|
+
|
|
|
+ def fake_manage(cfg, row, **kw):
|
|
|
+ rec.append(row['uid'])
|
|
|
+ return {'uid': row['uid'], 'balance': 88.5, 'stopped': ['c1'],
|
|
|
+ 'adjusted': [], 'recharge': None}
|
|
|
+
|
|
|
+ monkeypatch.setattr(niu, 'auto_manage', fake_manage)
|
|
|
+ assert cli.main(['niu', 'status']) == 0
|
|
|
+ assert rec == ['U1']
|
|
|
+ out = capsys.readouterr().out
|
|
|
+ assert '88.50' in out and '停计划1' in out and '充值下单否' in out
|