155 lines
7.1 KiB
Python
155 lines
7.1 KiB
Python
"""对白审核必须看到整镜时序,不能用接缝首尾采样代替说话过程。"""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
|
|
from PIL import Image
|
|
import pytest
|
|
|
|
from app.adapters.provider import continuity_media, dialogue_media, qwen
|
|
from app.services.orchestration import native_dialogue_engine as engine
|
|
from app.services.workflows import digest
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_path():
|
|
with tempfile.TemporaryDirectory(prefix="cmvr-dialogue-review-test-") as directory:
|
|
yield Path(directory)
|
|
|
|
|
|
@pytest.fixture
|
|
def tools():
|
|
local = Path(__file__).resolve().parents[2] / ".local/tools/ffmpeg"
|
|
ffmpeg = shutil.which("ffmpeg") or next((str(p) for p in local.glob("*/bin/ffmpeg.exe")), None)
|
|
ffprobe = shutil.which("ffprobe") or next((str(p) for p in local.glob("*/bin/ffprobe.exe")), None)
|
|
if not ffmpeg or not ffprobe:
|
|
pytest.skip("FFmpeg and FFprobe required")
|
|
return ffmpeg, ffprobe
|
|
|
|
|
|
def make_video(tmp_path, tools, source="testsrc2=size=128x72:rate=24", frames=158):
|
|
target = tmp_path / "source.mkv"
|
|
subprocess.run(
|
|
[tools[0], "-v", "error", "-f", "lavfi", "-i", source, "-frames:v", str(frames),
|
|
"-c:v", "ffv1", str(target)],
|
|
check=True, capture_output=True,
|
|
)
|
|
return target
|
|
|
|
|
|
def provider(tools, workspace_root):
|
|
def json_file(work, name, value):
|
|
path = work / name
|
|
path.write_text(json.dumps(value, ensure_ascii=False), encoding="utf-8")
|
|
return path
|
|
|
|
value = SimpleNamespace(
|
|
settings=SimpleNamespace(workspace_root=workspace_root, ffmpeg_bin=tools[0], ffprobe_bin=tools[1],
|
|
dialogue_quality_certified=True, continuity_quality_certified=True),
|
|
_asset=lambda aid, assets: assets[aid],
|
|
_chat=AsyncMock(return_value='{"verdict":"pass","reason":"样本人物与动作相符"}'),
|
|
_json_file=json_file,
|
|
)
|
|
value._image_uri = lambda source: qwen._image_uri(value, source)
|
|
return value
|
|
|
|
|
|
def inputs(start=0, end=158):
|
|
return {"video_asset_id": "a" * 32,
|
|
"expected": {"source_start_frame": start, "source_end_frame": end,
|
|
"fps": 24, "expected_motion": True}}
|
|
|
|
|
|
async def test_dialogue_samples_cover_both_speech_windows_with_ordered_unique_endpoints(tmp_path, tools):
|
|
video = make_video(tmp_path, tools)
|
|
service = provider(tools, tmp_path)
|
|
work = tmp_path / "review"
|
|
work.mkdir()
|
|
result = await dialogue_media.run_cpu(service, "video.dialogue.review", inputs(), {},
|
|
{"a" * 32: video}, work)
|
|
indices = result.result["frame_evidence"]["current_indices"]
|
|
assert any(1.54 <= index / 24 <= 2.14 for index in indices), "must observe the first spoken phrase"
|
|
assert any(4.61 <= index / 24 <= 5.75 for index in indices), "must observe the second spoken phrase"
|
|
assert len(indices) == 8
|
|
assert indices == sorted(set(indices))
|
|
assert indices[0] == 0 and indices[-1] == 157
|
|
assert (work / "join-anchor.png").is_file()
|
|
|
|
|
|
@pytest.mark.parametrize("start,end", [(0, 1), (0, 2), (0, 7), (17, 158)])
|
|
async def test_dialogue_sampling_stays_in_actual_trim_interval(tmp_path, tools, start, end):
|
|
video = make_video(tmp_path, tools)
|
|
service = provider(tools, tmp_path)
|
|
result = await dialogue_media.run_cpu(service, "video.dialogue.review", inputs(start, end), {},
|
|
{"a" * 32: video}, tmp_path)
|
|
indices = result.result["frame_evidence"]["current_indices"]
|
|
assert len(indices) == min(8, end - start)
|
|
assert indices == sorted(set(indices))
|
|
assert indices[0] == start and indices[-1] == end - 1
|
|
assert all(start <= index < end for index in indices)
|
|
|
|
|
|
async def test_dialogue_request_labels_each_actual_video_image_in_time_order(tmp_path, tools):
|
|
video = make_video(tmp_path, tools)
|
|
reference = tmp_path / "reference.png"
|
|
Image.new("RGB", (128, 72), "red").save(reference)
|
|
service = provider(tools, tmp_path)
|
|
request = inputs()
|
|
request["reference_asset_ids"] = ["b" * 32]
|
|
result = await dialogue_media.run_cpu(service, "video.dialogue.review", request, {},
|
|
{"a" * 32: video, "b" * 32: reference}, tmp_path)
|
|
content = service._chat.call_args.args[0][1]["content"]
|
|
video_labels = []
|
|
for i, part in enumerate(content):
|
|
if part["type"] != "text":
|
|
continue
|
|
match = re.search(r"frame=(\d+), time=(\d+\.\d+)s", part["text"])
|
|
if match:
|
|
index, seconds = int(match[1]), float(match[2])
|
|
video_labels.append(index)
|
|
assert seconds == pytest.approx(index / 24, abs=.000001)
|
|
assert content[i + 1]["type"] == "image_url"
|
|
path = tmp_path / ("join-anchor.png" if index == 157 else f"current-{index}.png")
|
|
assert content[i + 1]["image_url"]["url"] == await service._image_uri(path)
|
|
assert video_labels == result.result["frame_evidence"]["current_indices"]
|
|
assert len(video_labels) == 8
|
|
assert "参考" in content[0]["text"]
|
|
assert content[1]["image_url"]["url"] == await service._image_uri(reference)
|
|
|
|
|
|
async def test_plain_continuity_keeps_boundary_sampling(tmp_path, tools):
|
|
video = make_video(tmp_path, tools)
|
|
result = await continuity_media.inspect_continuity(provider(tools, tmp_path), inputs(),
|
|
{"a" * 32: video}, tmp_path)
|
|
assert result.result["frame_evidence"]["current_indices"] == [0, 1, 2, 78, 155, 156, 157]
|
|
|
|
|
|
@pytest.mark.parametrize("color,reason", [("black", "black"), ("gray", "No visible change")])
|
|
async def test_uniform_dialogue_review_keeps_black_and_freeze_rejection(tmp_path, tools, color, reason):
|
|
video = make_video(tmp_path, tools, f"color=c={color}:s=128x72:r=24")
|
|
service = provider(tools, tmp_path)
|
|
result = await dialogue_media.run_cpu(service, "video.dialogue.review", inputs(), {},
|
|
{"a" * 32: video}, tmp_path)
|
|
assert result.result["verdict"] == "reject"
|
|
assert reason in result.result["reason"]
|
|
service._chat.assert_not_awaited()
|
|
|
|
|
|
def test_review_sampling_invalidates_old_review_without_changing_video_context(monkeypatch):
|
|
expected = {"source_start_frame": 0, "source_end_frame": 158, "expected_motion": True}
|
|
video_sha, refs = "a" * 64, {"r": "b" * 64}
|
|
old_review = digest(dict(video_sha=video_sha, refs=refs, expected=expected, policy="native_av_v1"))
|
|
current_review = engine._review_context(video_sha, refs, expected)
|
|
assert current_review != old_review, "boundary-only review must not be reused after sampling changes"
|
|
video_context = engine._video_context("native-context", "c" * 64)
|
|
assert video_context == digest(dict(native_context="native-context", frame_sha256="c" * 64, policy="native_av_v1"))
|
|
monkeypatch.setattr(engine, "DIALOGUE_REVIEW_SAMPLING_POLICY", "next-review-sampling")
|
|
assert engine._review_context(video_sha, refs, expected) != current_review
|
|
assert engine._video_context("native-context", "c" * 64) == video_context
|