cmvr-visual/backend/tests/test_native_review_sampling_upgrade.py

100 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""升级途中已经产生的旧采帧报告,不能被collect或accept重新认证。"""
from copy import deepcopy
from types import SimpleNamespace as N
from unittest.mock import AsyncMock
from fastapi import HTTPException
import pytest
from app.services.orchestration import editor_engine, native_dialogue_engine as engine
from app.services.workflows import digest
def visual_report(policy="dialogue-uniform-8-v1", indices=None):
report = dict(
decision="pass", verdict="pass", reason="画面清楚且动作符合预期",
source_start_frame=0, source_end_frame=158,
frame_evidence=dict(current_indices=indices if indices is not None else [0, 22, 45, 67, 90, 112, 135, 157]),
)
if policy is not None:
report["review_sampling_policy"] = policy
return report
def collected_review(monkeypatch, report):
asset = N(id="c" * 32, sha256="d" * 64, kind="json")
session = N(scalars=AsyncMock(return_value=N(all=lambda: [asset])))
run = N(id="run-before-upgrade", owner_id="owner", result_manifest={"bindings": {}})
job = N(id="review-before-upgrade", owner_id="owner", run_id=run.id,
step_key="nr_0001", explicit_attempt=1)
expected = dict(source_start_frame=0, source_end_frame=158, fps=24, expected_motion=True)
video_id, video_sha = "a" * 32, "b" * 64
old = digest(dict(video_sha=video_sha, refs={}, expected=expected, policy=engine.POLICY))
step = N(kind="video.dialogue.review", step_key=job.step_key, explicit_attempt=1,
selected_job_id=job.id, semantic_input_hash="old", version=0, state="ready",
recipe_json={"unit_id": "shot", "binding_key": "dialogue_report:shot", "context_hash": old},
resolved_input_json={"input": {"video_asset_id": video_id, "expected": expected},
"input_sha256": {video_id: video_sha}, "parameters": {}})
monkeypatch.setattr(engine, "verified_output", AsyncMock(return_value=True))
monkeypatch.setattr(engine, "_read_report", lambda asset, settings: deepcopy(report))
monkeypatch.setattr(editor_engine, "_add_ref", AsyncMock())
return session, run, step, job
@pytest.mark.parametrize("report", [
visual_report(None, [0, 1, 2, 78, 155, 156, 157]),
visual_report("dialogue-uniform-8-v1", [0, 1, 2, 78, 155, 156, 157]),
visual_report("old-policy"),
visual_report("dialogue-uniform-8-v1", [False, 22, 45, 67, 90, 112, 135, 157]),
])
async def test_collect_does_not_relabel_old_or_incorrect_evidence_as_new_sampling(monkeypatch, report):
session, run, step, job = collected_review(monkeypatch, report)
with pytest.raises(HTTPException) as exc:
await engine.collect(session, run, step, job, N())
assert exc.value.detail["code"] == "invalid_output"
assert run.result_manifest == {"bindings": {}}
assert step.state == "ready"
async def test_collect_accepts_actual_uniform_sampling_on_an_old_frozen_recipe(monkeypatch):
session, run, step, job = collected_review(monkeypatch, visual_report())
await engine.collect(session, run, step, job, N())
assert step.state == "succeeded"
assert step.result_json["context_hash"] == engine._review_context(
"b" * 64, {}, step.resolved_input_json["input"]["expected"]
)
assert step.result_json["context_hash"] != step.recipe_json["context_hash"]
@pytest.mark.parametrize("report", [visual_report(None), visual_report(indices=[0, 1, 2, 78, 155, 156, 157])])
async def test_accept_rejects_old_review_already_collected_before_upgrade(monkeypatch, report):
video = dict(asset_id="video", sha256="a" * 64, kind="video", duration_frames=158)
audio = dict(asset_id="audio", sha256="b" * 64, kind="audio", sample_count=315600)
results = {"v": video, "a": audio,
"q": dict(asset_id="speech-report", sha256="c" * 64, kind="json"),
"r": dict(asset_id="visual-report", sha256="d" * 64, kind="json",
provenance={"video_sha256": video["sha256"]})}
speech = dict(
native_av_policy=engine.POLICY, audio_sha256=audio["sha256"], expected_text="你好。",
speaker_id="speaker", source_start_sample=0, source_end_sample=315600, sample_rate=48000,
timestamp_source="whisper-word-timestamps", timestamp_origin="audio_asset",
asr_revision="41f01f3fe87f28c78e2fbf8b568835947dd65ed9", decision="pass", reasons=[],
)
run = N(owner_id="owner", result_manifest={"bindings": {}, "spec": {"dialogue": {"utterances": [
dict(id="line", speaker_id="speaker", text="你好。")
]}}})
step = N(recipe_json=dict(unit_id="shot", video_key="v", audio_key="a", report_key="q",
review_key="r", native_utterance_ids=["line"]))
session = N(get=AsyncMock(side_effect=lambda model, key: N(id=key)))
from app.services.orchestration import dialogue_engine
monkeypatch.setattr(dialogue_engine, "_step", AsyncMock(side_effect=lambda session, run, key: N(result_json=results[key])))
monkeypatch.setattr(editor_engine, "_asset", AsyncMock(return_value=True))
monkeypatch.setattr(engine, "_read_report", lambda asset, settings: speech if asset.id == "speech-report" else report)
# ASR共识与本测试无关,保持其既有入口为通过,只观察视觉证据升级边界。
monkeypatch.setattr(engine, "_content_consensus", lambda report, expected: True)
with pytest.raises(HTTPException) as exc:
await engine.accept_shot(session, run, step, N())
assert exc.value.detail["code"] == "native_visual_rejected"