Add complete test infrastructure for yaml2pptx project with 245+ tests covering unit, integration, and end-to-end scenarios. Test structure: - Unit tests: elements, template system, validators, loaders, utils - Integration tests: presentation and rendering flows - E2E tests: CLI commands (convert, check, preview) Key features: - PptxFileValidator for Level 2 PPTX validation (file structure, element count, content matching, position tolerance) - Comprehensive fixtures for test data consistency - Mock-based testing for external dependencies - Test images generated with PIL/Pillow - Boundary case coverage for edge scenarios Dependencies added: - pytest, pytest-cov, pytest-mock - pillow (for test image generation) Documentation updated: - README.md: test running instructions - README_DEV.md: test development guide Co-authored-by: OpenSpec change: add-comprehensive-tests
202 lines
5.9 KiB
Python
202 lines
5.9 KiB
Python
"""
|
|
Preview 命令端到端测试
|
|
|
|
测试 yaml2pptx.py preview 命令的 HTML 生成功能(不启动真实服务器)
|
|
"""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
from unittest.mock import patch, MagicMock
|
|
from preview.server import generate_preview_html, create_flask_app
|
|
|
|
|
|
class TestGeneratePreviewHtml:
|
|
"""generate_preview_html 函数测试类"""
|
|
|
|
def test_generate_html_from_valid_yaml(self, sample_yaml):
|
|
"""测试从有效 YAML 生成 HTML"""
|
|
html = generate_preview_html(str(sample_yaml), None)
|
|
|
|
assert isinstance(html, str)
|
|
assert "<!DOCTYPE html>" in html
|
|
assert "<html>" in html
|
|
assert "</html>" in html
|
|
|
|
def test_html_contains_slide_content(self, sample_yaml):
|
|
"""测试 HTML 包含幻灯片内容"""
|
|
html = generate_preview_html(str(sample_yaml), None)
|
|
|
|
# 应该包含文本内容
|
|
assert "Hello, World!" in html
|
|
|
|
def test_html_contains_css_styles(self, sample_yaml):
|
|
"""测试 HTML 包含 CSS 样式"""
|
|
html = generate_preview_html(str(sample_yaml), None)
|
|
|
|
assert "<style>" in html
|
|
assert ".slide" in html
|
|
assert "position: absolute" in html
|
|
|
|
def test_html_with_template(self, temp_dir, sample_template):
|
|
"""测试使用模板生成 HTML"""
|
|
yaml_content = f"""
|
|
metadata:
|
|
size: 16:9
|
|
|
|
slides:
|
|
- template: title-slide
|
|
vars:
|
|
title: "Template Title"
|
|
subtitle: "Template Subtitle"
|
|
"""
|
|
yaml_path = temp_dir / "test.yaml"
|
|
yaml_path.write_text(yaml_content)
|
|
|
|
html = generate_preview_html(str(yaml_path), str(sample_template))
|
|
|
|
assert "Template Title" in html
|
|
|
|
def test_html_with_invalid_yaml(self, temp_dir):
|
|
"""测试无效 YAML 返回错误页面"""
|
|
yaml_path = temp_dir / "invalid.yaml"
|
|
yaml_path.write_text("invalid: [unclosed")
|
|
|
|
html = generate_preview_html(str(yaml_path), None)
|
|
|
|
# 应该返回错误页面
|
|
assert "<!DOCTYPE html>" in html
|
|
assert "错误" in html or "error" in html.lower()
|
|
|
|
def test_html_with_multiple_slides(self, temp_dir):
|
|
"""测试多张幻灯片的 HTML"""
|
|
yaml_content = """
|
|
metadata:
|
|
size: 16:9
|
|
|
|
slides:
|
|
- elements:
|
|
- type: text
|
|
box: [1, 1, 8, 1]
|
|
content: "Slide 1"
|
|
font:
|
|
size: 24
|
|
|
|
- elements:
|
|
- type: text
|
|
box: [1, 1, 8, 1]
|
|
content: "Slide 2"
|
|
font:
|
|
size: 24
|
|
"""
|
|
yaml_path = temp_dir / "test.yaml"
|
|
yaml_path.write_text(yaml_content)
|
|
|
|
html = generate_preview_html(str(yaml_path), None)
|
|
|
|
# 应该包含两张幻灯片的内容
|
|
assert "Slide 1" in html
|
|
assert "Slide 2" in html
|
|
|
|
def test_html_contains_slide_number(self, sample_yaml):
|
|
"""测试 HTML 包含幻灯片编号"""
|
|
html = generate_preview_html(str(sample_yaml), None)
|
|
|
|
assert "slide-number" in html
|
|
|
|
def test_html_contains_sse_script(self, sample_yaml):
|
|
"""测试 HTML 包含 SSE 事件脚本"""
|
|
html = generate_preview_html(str(sample_yaml), None)
|
|
|
|
assert "EventSource" in html
|
|
assert "/events" in html
|
|
|
|
|
|
class TestCreateFlaskApp:
|
|
"""create_flask_app 函数测试类"""
|
|
|
|
@patch('preview.server.current_yaml_file', 'test.yaml')
|
|
@patch('preview.server.current_template_dir', None)
|
|
@patch('preview.server.change_queue')
|
|
def test_creates_flask_app(self, mock_queue):
|
|
"""测试创建 Flask 应用"""
|
|
app = create_flask_app()
|
|
|
|
assert app is not None
|
|
assert hasattr(app, 'url_map')
|
|
|
|
@patch('preview.server.current_yaml_file', 'test.yaml')
|
|
@patch('preview.server.current_template_dir', None)
|
|
@patch('preview.server.change_queue')
|
|
def test_has_index_route(self, mock_queue):
|
|
"""测试有 / 路由"""
|
|
app = create_flask_app()
|
|
|
|
# 检查路由
|
|
rules = [rule.rule for rule in app.url_map.iter_rules()]
|
|
assert '/' in rules
|
|
|
|
@patch('preview.server.current_yaml_file', 'test.yaml')
|
|
@patch('preview.server.current_template_dir', None)
|
|
@patch('preview.server.change_queue')
|
|
def test_has_events_route(self, mock_queue):
|
|
"""测试有 /events 路由"""
|
|
app = create_flask_app()
|
|
|
|
rules = [rule.rule for rule in app.url_map.iter_rules()]
|
|
assert '/events' in rules
|
|
|
|
|
|
class TestYAMLChangeHandler:
|
|
"""YAMLChangeHandler 测试类"""
|
|
|
|
def test_on_modified_with_yaml_file(self):
|
|
"""测试处理 YAML 文件修改"""
|
|
from preview.server import YAMLChangeHandler
|
|
from unittest.mock import MagicMock
|
|
|
|
handler = YAMLChangeHandler()
|
|
mock_queue = MagicMock()
|
|
import preview.server
|
|
preview.server.change_queue = mock_queue
|
|
|
|
event = MagicMock()
|
|
event.src_path = "test.yaml"
|
|
|
|
handler.on_modified(event)
|
|
|
|
mock_queue.put.assert_called_once_with('reload')
|
|
|
|
def test_on_modified_with_non_yaml_file(self):
|
|
"""测试忽略非 YAML 文件修改"""
|
|
from preview.server import YAMLChangeHandler
|
|
from unittest.mock import MagicMock
|
|
|
|
handler = YAMLChangeHandler()
|
|
mock_queue = MagicMock()
|
|
import preview.server
|
|
preview.server.change_queue = mock_queue
|
|
|
|
event = MagicMock()
|
|
event.src_path = "test.txt"
|
|
|
|
handler.on_modified(event)
|
|
|
|
mock_queue.put.assert_not_called()
|
|
|
|
|
|
class TestPreviewHTMLTemplate:
|
|
"""HTML 模板常量测试"""
|
|
|
|
def test_html_template_is_defined(self):
|
|
"""测试 HTML_TEMPLATE 已定义"""
|
|
from preview.server import HTML_TEMPLATE
|
|
assert isinstance(HTML_TEMPLATE, str)
|
|
assert "<!DOCTYPE html>" in HTML_TEMPLATE
|
|
|
|
def test_error_template_is_defined(self):
|
|
"""测试 ERROR_TEMPLATE 已定义"""
|
|
from preview.server import ERROR_TEMPLATE
|
|
assert isinstance(ERROR_TEMPLATE, str)
|
|
assert "<!DOCTYPE html>" in ERROR_TEMPLATE
|
|
assert "错误" in ERROR_TEMPLATE or "error" in ERROR_TEMPLATE.lower()
|