#!/usr/bin/env python3 """ 测试增强版小红书发布器集成效果 """ import os import sys import json import tempfile from pathlib import Path # 添加当前目录到Python路径 sys.path.append(os.path.dirname(os.path.abspath(__file__))) def test_content_analysis(): """测试内容分析功能""" print("=" * 60) print("测试内容分析功能") print("=" * 60) try: from xiaohongshu_enhanced_publisher import XiaohongshuEnhancedPublisher # 创建发布器 publisher = XiaohongshuEnhancedPublisher() # 测试内容 test_contents = [ { "content": "高效学习法:番茄工作法、费曼学习法、间隔重复法、主动回忆法", "expected_type": "知识分享" }, { "content": "春季养生小贴士:早睡早起、多喝水、适量运动、保持心情愉快", "expected_type": "健康生活" }, { "content": "小红书运营技巧:封面要美,标题要钩,内容要值,互动要勤", "expected_type": "知识分享" } ] for i, test in enumerate(test_contents, 1): print(f"\n测试 {i}: {test['content'][:50]}...") analysis = publisher.analyze_content(test["content"]) print(f" 内容类型: {analysis['content_type']}") print(f" 推荐风格: {analysis['recommended_style'].value}") print(f" 推荐布局: {analysis['recommended_layout'].value}") print(f" 建议图片数: {analysis['image_count']}") if analysis['content_type'] == test['expected_type']: print(" ✅ 内容类型分析正确") else: print(f" ⚠️ 内容类型分析可能不准确") print("\n✅ 内容分析功能测试完成") return True except Exception as e: print(f"❌ 内容分析测试失败: {e}") return False def test_image_generation(): """测试图片生成功能""" print("\n" + "=" * 60) print("测试图片生成功能") print("=" * 60) try: from xiaohongshu_enhanced_publisher import XiaohongshuEnhancedPublisher # 创建发布器 publisher = XiaohongshuEnhancedPublisher() # 测试内容 test_content = "高效学习法:番茄工作法、费曼学习法、间隔重复法、主动回忆法" print(f"测试内容: {test_content}") # 测试不同风格 test_styles = ["notion", "cute", "fresh", "minimal"] for style in test_styles: print(f"\n测试风格: {style}") # 生成图片 images = publisher.generate_images_for_content(test_content, style, "balanced") if images: print(f" ✅ 图片生成成功: {images[0]}") # 检查文件是否存在 if os.path.exists(images[0]): file_size = os.path.getsize(images[0]) / 1024 / 1024 print(f" 文件大小: {file_size:.2f} MB") else: print(" ⚠️ 文件不存在") else: print(f" ⚠️ 图片生成失败或已禁用") print("\n✅ 图片生成功能测试完成") return True except Exception as e: print(f"❌ 图片生成测试失败: {e}") return False def test_markdown_processing(): """测试Markdown处理功能""" print("\n" + "=" * 60) print("测试Markdown处理功能") print("=" * 60) try: from xiaohongshu_enhanced_publisher import XiaohongshuEnhancedPublisher # 创建发布器 publisher = XiaohongshuEnhancedPublisher() # 创建测试Markdown文件 test_md = """# AI技术新突破 最近,AI图像生成技术取得了重大突破。新的模型能够生成更加逼真、细节丰富的图片。 ## 主要改进 1. **图像质量提升** - 分辨率大幅提升 - 细节更加丰富 - 色彩更加自然 2. **创意控制增强** - 支持复杂提示词 - 可以指定艺术风格 - 支持多图生成 3. **生成速度优化** - 推理时间减少 - 支持批量生成 - 内存占用优化 这项技术将改变内容创作的方式。 """ # 保存到临时文件 with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False, encoding='utf-8') as f: f.write(test_md) test_file = f.name print(f"创建测试文件: {test_file}") # 测试内容提取 title = publisher._extract_title(test_md) key_content = publisher._extract_key_content(test_md) print(f"提取标题: {title}") print(f"提取关键内容: {key_content[:100]}...") # 清理临时文件 os.unlink(test_file) print("\n✅ Markdown处理功能测试完成") return True except Exception as e: print(f"❌ Markdown处理测试失败: {e}") return False def test_config_loading(): """测试配置加载功能""" print("\n" + "=" * 60) print("测试配置加载功能") print("=" * 60) try: from xiaohongshu_enhanced_publisher import XiaohongshuEnhancedPublisher # 创建测试配置文件 test_config = { "xiaohongshu": { "cli_path": "xhs_test", "auto_generate_images": False, "default_style": "cute" } } with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False, encoding='utf-8') as f: json.dump(test_config, f, ensure_ascii=False, indent=2) config_file = f.name print(f"创建测试配置文件: {config_file}") # 使用测试配置创建发布器 publisher = XiaohongshuEnhancedPublisher(config_file) # 验证配置 config = publisher.config print(f"CLI路径: {config['xiaohongshu']['cli_path']}") print(f"自动生成图片: {config['xiaohongshu']['auto_generate_images']}") print(f"默认风格: {config['xiaohongshu']['default_style']}") # 验证配置合并 if (config['xiaohongshu']['cli_path'] == "xhs_test" and config['xiaohongshu']['auto_generate_images'] == False and config['xiaohongshu']['default_style'] == "cute"): print("✅ 配置加载和合并正确") else: print("❌ 配置加载或合并错误") # 清理临时文件 os.unlink(config_file) print("\n✅ 配置加载功能测试完成") return True except Exception as e: print(f"❌ 配置加载测试失败: {e}") return False def test_command_line_interface(): """测试命令行接口""" print("\n" + "=" * 60) print("测试命令行接口") print("=" * 60) try: import subprocess # 测试帮助命令 print("测试帮助命令...") result = subprocess.run( ["python3", "xiaohongshu_enhanced_publisher.py", "--help"], capture_output=True, text=True, encoding='utf-8' ) if result.returncode == 0: print("✅ 帮助命令正常") else: print("❌ 帮助命令失败") print(f"错误: {result.stderr}") # 测试配置命令 print("\n测试配置命令...") result = subprocess.run( ["python3", "xiaohongshu_enhanced_publisher.py", "config"], capture_output=True, text=True, encoding='utf-8' ) if result.returncode == 0: print("✅ 配置命令正常") print(f"输出: {result.stdout[:200]}...") else: print("❌ 配置命令失败") # 测试分析命令 print("\n测试分析命令...") result = subprocess.run( ["python3", "xiaohongshu_enhanced_publisher.py", "analyze", "--content", "测试内容"], capture_output=True, text=True, encoding='utf-8' ) if result.returncode == 0: print("✅ 分析命令正常") else: print("❌ 分析命令失败") print("\n✅ 命令行接口测试完成") return True except Exception as e: print(f"❌ 命令行接口测试失败: {e}") return False def test_integration_with_existing_system(): """测试与现有系统的集成""" print("\n" + "=" * 60) print("测试与现有系统的集成") print("=" * 60) try: # 检查现有系统文件 existing_files = [ "xiaohongshu_publisher_fixed.py", "content_converter_fixed.py", "config_real.json" ] print("检查现有系统文件...") for file in existing_files: if os.path.exists(file): print(f" ✅ {file} 存在") else: print(f" ⚠️ {file} 不存在") # 测试导入现有模块 print("\n测试导入现有模块...") try: from content_converter_fixed import ContentConverterFixed print(" ✅ ContentConverterFixed 导入成功") converter = ContentConverterFixed() print(" ✅ ContentConverterFixed 初始化成功") except Exception as e: print(f" ❌ 导入失败: {e}") # 测试配置兼容性 print("\n测试配置兼容性...") if os.path.exists("config_real.json"): try: with open("config_real.json", 'r', encoding='utf-8') as f: real_config = json.load(f) print(f" ✅ 现有配置加载成功") print(f" 小红书配置: {real_config.get('xiaohongshu', {}).get('enabled', '未设置')}") except Exception as e: print(f" ❌ 现有配置加载失败: {e}") else: print(" ⚠️ 现有配置文件不存在") print("\n✅ 与现有系统的集成测试完成") return True except Exception as e: print(f"❌ 集成测试失败: {e}") return False def main(): """主测试函数""" print("小红书增强版发布器集成测试") print("=" * 60) test_results = [] # 运行所有测试 test_results.append(("内容分析", test_content_analysis())) test_results.append(("图片生成", test_image_generation())) test_results.append(("Markdown处理", test_markdown_processing())) test_results.append(("配置加载", test_config_loading())) test_results.append(("命令行接口", test_command_line_interface())) test_results.append(("系统集成", test_integration_with_existing_system())) # 输出测试结果 print("\n" + "=" * 60) print("测试结果汇总") print("=" * 60) total_tests = len(test_results) passed_tests = sum(1 for _, result in test_results if result) failed_tests = total_tests - passed_tests for test_name, result in test_results: status = "✅ 通过" if result else "❌ 失败" print(f"{test_name}: {status}") print(f"\n总计: {total_tests} 个测试") print(f"通过: {passed_tests} 个") print(f"失败: {failed_tests} 个") if failed_tests == 0: print("\n🎉 所有测试通过!增强版发布器集成成功。") return True else: print(f"\n⚠️ 有 {failed_tests} 个测试失败,请检查问题。") return False if __name__ == "__main__": success = main() sys.exit(0 if success else 1)