| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- # -*- coding: utf-8 -*-
- """aigen 模块测试:prompt 组装与生成器构造(不发起网络请求)。"""
- import sys
- from pathlib import Path
- sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
- from kuaishou_tool import aigen
- def test_build_prompt_structure():
- p = aigen.build_prompt('a red ceramic teapot')
- # 主体在首位
- assert p.startswith('a red ceramic teapot')
- # 负面人物词在末尾
- assert 'people, person, human' in p
- assert p.endswith('excluded)')
- def test_build_prompt_randomness():
- # 词库随机:多次生成应产生差异(极小概率全同, 取8次比较)
- ps = {aigen.build_prompt('x') for _ in range(8)}
- assert len(ps) > 1
- def test_vocab_buckets_all_nonempty():
- for name, bucket in aigen.VOCAB.items():
- words = [w for w in bucket.split('|') if w.strip()]
- assert len(words) >= 5, f'桶 {name} 词数不足: {words}'
- def test_load_generator_from_cfg(tmp_path=None):
- cfg = {'ai': {'zhipu_api_key': 'k', 'model': 'cogview-3-flash',
- 'pollinations_fallback': True}}
- gen = aigen.load_generator(cfg, str(Path(tmp_path or '.')))
- assert gen.api_key == 'k'
- assert gen.model == 'cogview-3-flash'
- assert gen.fallback is True
- assert gen.out_dir.name == '实拍'
- def test_generator_no_key_skips_main_channel():
- gen = aigen.AIGenerator(api_key='', out_dir='实拍')
- r = gen._zhipu_generate('test prompt')
- assert r is None # 无 key 直接跳过主通道
- if __name__ == '__main__':
- import inspect
- fails = 0
- for name, fn in sorted(inspect.getmembers(sys.modules[__name__], inspect.isfunction)):
- if not name.startswith('test_'):
- continue
- try:
- sig = inspect.signature(fn)
- if 'tmp_path' in sig.parameters or 'caplog' in sig.parameters:
- import tempfile
- with tempfile.TemporaryDirectory() as td:
- fn(td if 'tmp_path' in sig.parameters else None)
- else:
- fn()
- print(f'PASS {name}')
- except Exception as e:
- fails += 1
- print(f'FAIL {name}: {e}')
- sys.exit(1 if fails else 0)
|