feat(12-01): implement SRTProcessor and Markdown generation
- Add SRTInput/SRTOutput schemas to pipeline - Create SRTProcessor following BaseProcessor pattern - Generate Markdown with YAML frontmatter - Support metadata extraction and formatting - Handle missing metadata gracefully Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,9 @@ Tests for:
|
||||
- SRTProcessor (pipeline processor)
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass
|
||||
@@ -340,3 +343,150 @@ class TestASRCorrector:
|
||||
result = corrector.correct("")
|
||||
|
||||
assert result == ""
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test 4: SRTProcessor
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def srt_test_files(tmp_path: Path):
|
||||
"""创建测试用的 SRT 和元数据文件"""
|
||||
# 创建 SRT 文件
|
||||
srt_content = """1
|
||||
00:00:00,000 --> 00:00:05,000
|
||||
第一句字幕内容
|
||||
|
||||
2
|
||||
00:00:05,000 --> 00:00:10,000
|
||||
第二句字幕内容
|
||||
|
||||
3
|
||||
00:00:10,000 --> 00:00:15,000
|
||||
第三句字幕内容
|
||||
"""
|
||||
srt_file = tmp_path / "test.srt"
|
||||
srt_file.write_text(srt_content, encoding="utf-8")
|
||||
|
||||
# 创建元数据文件
|
||||
metadata = {
|
||||
"title": "测试视频标题",
|
||||
"author": "测试作者",
|
||||
"platform": "bilibili",
|
||||
"bvid": "BV1test123",
|
||||
"duration": 60,
|
||||
"url": "https://www.bilibili.com/video/BV1test123",
|
||||
"pubtime": "2026-03-19"
|
||||
}
|
||||
metadata_file = tmp_path / "metadata.json"
|
||||
metadata_file.write_text(json.dumps(metadata, ensure_ascii=False), encoding="utf-8")
|
||||
|
||||
# 输出文件路径
|
||||
output_file = tmp_path / "output.md"
|
||||
|
||||
return {
|
||||
"srt_file": str(srt_file),
|
||||
"metadata_file": str(metadata_file),
|
||||
"output_file": str(output_file)
|
||||
}
|
||||
|
||||
|
||||
class TestSRTProcessor:
|
||||
"""测试 SRTProcessor 处理器"""
|
||||
|
||||
def test_srt_processor_generates_markdown(self, srt_test_files):
|
||||
"""SRTProcessor 能处理 SRT 文件生成 Markdown"""
|
||||
from kb_workflow.pipeline.processors.srt import SRTProcessor
|
||||
from kb_workflow.pipeline.schemas import SRTInput
|
||||
|
||||
processor = SRTProcessor(enable_correction=False)
|
||||
|
||||
input_data = SRTInput(
|
||||
content_id="BV1test123",
|
||||
platform="bilibili",
|
||||
srt_file=srt_test_files["srt_file"],
|
||||
metadata_file=srt_test_files["metadata_file"],
|
||||
output_file=srt_test_files["output_file"]
|
||||
)
|
||||
|
||||
# 运行处理
|
||||
result = asyncio.run(processor.process(input_data))
|
||||
|
||||
assert result.success
|
||||
assert Path(result.output_file).exists()
|
||||
|
||||
# 验证 Markdown 内容
|
||||
content = Path(result.output_file).read_text(encoding="utf-8")
|
||||
assert "---" in content # frontmatter
|
||||
assert "title:" in content
|
||||
assert "测试视频标题" in content
|
||||
assert "## 内容" in content
|
||||
assert "第一句字幕内容" in content
|
||||
|
||||
def test_srt_processor_with_yaml_frontmatter(self, srt_test_files):
|
||||
"""输出包含 YAML frontmatter(元数据和标签)"""
|
||||
from kb_workflow.pipeline.processors.srt import SRTProcessor
|
||||
from kb_workflow.pipeline.schemas import SRTInput
|
||||
|
||||
processor = SRTProcessor(enable_correction=False)
|
||||
|
||||
input_data = SRTInput(
|
||||
content_id="BV1test123",
|
||||
platform="bilibili",
|
||||
srt_file=srt_test_files["srt_file"],
|
||||
metadata_file=srt_test_files["metadata_file"],
|
||||
output_file=srt_test_files["output_file"]
|
||||
)
|
||||
|
||||
result = asyncio.run(processor.process(input_data))
|
||||
|
||||
content = Path(result.output_file).read_text(encoding="utf-8")
|
||||
|
||||
# 验证 frontmatter 字段
|
||||
assert "title:" in content
|
||||
assert "date:" in content
|
||||
assert "author:" in content
|
||||
assert "platform:" in content
|
||||
assert "bvid:" in content
|
||||
assert "url:" in content
|
||||
|
||||
def test_srt_processor_follows_base_processor_pattern(self):
|
||||
"""处理器遵循 BaseProcessor 模式"""
|
||||
from kb_workflow.pipeline.processors.srt import SRTProcessor
|
||||
from kb_workflow.pipeline.processors.base import BaseProcessor
|
||||
|
||||
# SRTProcessor 应该继承 BaseProcessor
|
||||
assert issubclass(SRTProcessor, BaseProcessor)
|
||||
|
||||
# 应该有 STEP_NAME
|
||||
assert hasattr(SRTProcessor, 'STEP_NAME')
|
||||
assert SRTProcessor.STEP_NAME == "srt"
|
||||
|
||||
def test_srt_processor_handles_missing_metadata(self, tmp_path: Path):
|
||||
"""元数据文件缺失时能正常处理"""
|
||||
from kb_workflow.pipeline.processors.srt import SRTProcessor
|
||||
from kb_workflow.pipeline.schemas import SRTInput
|
||||
|
||||
# 创建 SRT 文件
|
||||
srt_content = "1\n00:00:00,000 --> 00:00:05,000\n测试内容\n"
|
||||
srt_file = tmp_path / "test.srt"
|
||||
srt_file.write_text(srt_content, encoding="utf-8")
|
||||
|
||||
# 不存在的元数据文件
|
||||
output_file = tmp_path / "output.md"
|
||||
|
||||
processor = SRTProcessor(enable_correction=False)
|
||||
|
||||
input_data = SRTInput(
|
||||
content_id="test123",
|
||||
platform="bilibili",
|
||||
srt_file=str(srt_file),
|
||||
metadata_file=str(tmp_path / "nonexistent.json"),
|
||||
output_file=str(output_file)
|
||||
)
|
||||
|
||||
result = asyncio.run(processor.process(input_data))
|
||||
|
||||
assert result.success
|
||||
assert Path(result.output_file).exists()
|
||||
|
||||
Reference in New Issue
Block a user