refactor review-plan safety and validation
This commit is contained in:
+99
-1
@@ -8,11 +8,15 @@ import pytest
|
||||
|
||||
from vlm.io import (
|
||||
_video_file_from_record,
|
||||
execution_plan_from_record,
|
||||
execution_plan_to_record,
|
||||
load_analysis_json,
|
||||
load_execution_plan,
|
||||
load_identities_json,
|
||||
save_execution_plan,
|
||||
save_identities_json,
|
||||
)
|
||||
from vlm.models import VideoFile
|
||||
from vlm.models import ExecutionPlan, FileOperation, VideoFile
|
||||
|
||||
|
||||
class TestVideoFileFromRecord:
|
||||
@@ -105,6 +109,100 @@ class TestVideoFileFromRecord:
|
||||
assert vf.bitrate_kbps is None
|
||||
|
||||
|
||||
class TestExecutionPlanIo:
|
||||
"""Tests for the validated typed execution plan boundary."""
|
||||
|
||||
def test_execution_plan_record_round_trip(self):
|
||||
plan = ExecutionPlan(
|
||||
plan_id="plan-123",
|
||||
created_at=datetime(2026, 4, 7, 12, 0, tzinfo=timezone.utc),
|
||||
operations=[
|
||||
FileOperation(
|
||||
operation_type="move",
|
||||
source_path=Path("/library/movie/source.mkv"),
|
||||
destination_path=Path("/library/movie/target.mkv"),
|
||||
reason="move movie",
|
||||
has_conflict=False,
|
||||
conflict_reason=None,
|
||||
)
|
||||
],
|
||||
summary={"total": 1, "move": 1, "rename": 0, "quarantine": 0, "no-op": 0},
|
||||
summary_by_reason={"move movie": 1},
|
||||
human_summary="计划已生成",
|
||||
metadata={"analysis_source": "analysis.json"},
|
||||
)
|
||||
|
||||
record = execution_plan_to_record(plan)
|
||||
loaded = execution_plan_from_record(record)
|
||||
|
||||
assert loaded.plan_id == plan.plan_id
|
||||
assert loaded.created_at == plan.created_at
|
||||
assert loaded.operations[0].source_path == plan.operations[0].source_path
|
||||
assert loaded.operations[0].destination_path == plan.operations[0].destination_path
|
||||
assert loaded.summary == plan.summary
|
||||
assert loaded.summary_by_reason == plan.summary_by_reason
|
||||
assert loaded.human_summary == plan.human_summary
|
||||
assert loaded.metadata == plan.metadata
|
||||
|
||||
def test_load_execution_plan_normalizes_naive_timestamp(self, tmp_path):
|
||||
plan_path = tmp_path / "plan.json"
|
||||
plan_path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"vlm_schema_version": "1.0",
|
||||
"plan_id": "plan-naive",
|
||||
"created_at": "2026-04-07T12:00:00",
|
||||
"operations": [
|
||||
{
|
||||
"operation_type": "no-op",
|
||||
"source_path": "/library/movie/source.mkv",
|
||||
"destination_path": None,
|
||||
"reason": "manual review",
|
||||
"has_conflict": False,
|
||||
"conflict_reason": None,
|
||||
}
|
||||
],
|
||||
"summary": {"total": 1, "move": 0, "rename": 0, "quarantine": 0, "no-op": 1},
|
||||
"summary_by_reason": {"manual review": 1},
|
||||
"human_summary": "summary",
|
||||
"metadata": {},
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
loaded = load_execution_plan(plan_path)
|
||||
|
||||
assert loaded.created_at.tzinfo == timezone.utc
|
||||
assert loaded.created_at.isoformat() == "2026-04-07T12:00:00+00:00"
|
||||
|
||||
def test_save_execution_plan_writes_validated_schema(self, tmp_path):
|
||||
plan = ExecutionPlan(
|
||||
plan_id="plan-save",
|
||||
created_at=datetime(2026, 4, 7, 13, 0, tzinfo=timezone.utc),
|
||||
operations=[
|
||||
FileOperation(
|
||||
operation_type="quarantine",
|
||||
source_path=Path("/library/movie/duplicate.mkv"),
|
||||
destination_path=None,
|
||||
reason="duplicate",
|
||||
has_conflict=False,
|
||||
conflict_reason=None,
|
||||
)
|
||||
],
|
||||
summary={"total": 1, "move": 0, "rename": 0, "quarantine": 1, "no-op": 0},
|
||||
)
|
||||
|
||||
output_path = tmp_path / "plan.json"
|
||||
save_execution_plan(plan, output_path)
|
||||
saved = json.loads(output_path.read_text(encoding="utf-8"))
|
||||
|
||||
assert saved["vlm_schema_version"] == "1.0"
|
||||
assert saved["plan_id"] == "plan-save"
|
||||
assert saved["operations"][0]["operation_type"] == "quarantine"
|
||||
assert saved["operations"][0]["source_path"] == "/library/movie/duplicate.mkv"
|
||||
|
||||
|
||||
class TestIdentitiesJsonVersioning:
|
||||
"""Tests for identities.json schema versioning."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user