XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

Cinematic-Game-Montage-skill

【Skill】电影感游戏剪辑Skill

公开
关注 0 Fork 0 Star 0
UTF-8
from copy import deepcopy
from fractions import Fraction
import json
from pathlib import Path
import sys
import unittest

ROOT = Path(__file__).resolve().parents[1]
SKILL = ROOT / "skills" / "cinematic-game-montage"
sys.path.insert(0, str(SKILL / "scripts"))
import timeline


class TimelineTests(unittest.TestCase):
    def setUp(self):
        self.plan = json.loads((SKILL / "assets" / "timeline.example.json").read_text(encoding="utf-8"))

    def test_absolute_beat_grid_does_not_drift(self):
        beats = timeline.beat_grid("126", "60", ".49", 10001)
        self.assertEqual(beats[0]["frame"], 29)
        target = (Fraction(49, 100) + 10000 * Fraction(60, 126)) * 60
        self.assertLessEqual(abs(beats[-1]["frame"] - target), Fraction(1, 2))
        steps = {b["frame"] - a["frame"] for a, b in zip(beats, beats[1:])}
        self.assertEqual(steps, {28, 29})

    def test_ntsc_rate_remains_rational(self):
        beats = timeline.beat_grid(120, "60000/1001", 0, 2001)
        self.assertEqual(beats[-1]["frame"], 59940)
        self.assertEqual(timeline.round_frame(Fraction(1, 2)), 1)
        self.assertEqual(timeline.round_frame(Fraction(-1, 2)), -1)

    def test_invalid_numbers_fail(self):
        for bad in (0, -1, "NaN", "inf", "1/0", True):
            with self.subTest(bad=bad), self.assertRaises(ValueError):
                timeline.beat_grid(120, bad, 0, 10)

    def test_example_compiles_with_shared_audio_video_boundaries(self):
        result = timeline.compile_plan(self.plan)
        self.assertEqual(result["frames"], 480)
        self.assertEqual(result["duration_seconds"], 8)
        self.assertEqual(result["shots"][1]["start_frame"], 180)
        self.assertEqual(result["shots"][-1]["end_sample"], 384000)

    def test_global_sample_budget_avoids_fractional_rate_drift(self):
        self.plan["fps"] = "60000/1001"
        self.plan["shots"] = [{"id": f"s{i}", "source": "source_a", "frames": 1,
                                "speed": 1, "keep_ranges": [[i, i+1]]} for i in range(301)]
        result = timeline.compile_plan(self.plan)
        total = sum(s["end_sample"] - s["start_sample"] for s in result["shots"])
        self.assertEqual(total, timeline.round_frame(Fraction(301 * 48000 * 1001, 60000)))
        for a, b in zip(result["shots"], result["shots"][1:]):
            self.assertEqual(a["end_sample"], b["start_sample"])

    def test_stable_ids_survive_insertions(self):
        new = deepcopy(self.plan["shots"][0])
        new["id"] = "new_opening"
        self.plan["shots"].insert(0, new)
        result = timeline.compile_plan(self.plan)
        shot = next(s for s in result["shots"] if s["id"] == "ascent_repaired")
        self.assertEqual(shot["keep_ranges"], [[900, 978], [984, 1062]])
        self.assertEqual(shot["start_frame"], 360)

    def test_unreviewed_repairs_fail(self):
        self.plan["shots"][1]["repair_reviewed"] = False
        with self.assertRaisesRegex(ValueError, "repair_reviewed"):
            timeline.compile_plan(self.plan)

    def test_unexplained_speed_change_fails(self):
        self.plan["shots"][1].pop("retime_reason")
        with self.assertRaisesRegex(ValueError, "retime_reason"):
            timeline.compile_plan(self.plan)

    def test_shortened_payoff_is_not_silently_padded(self):
        self.plan["shots"][0]["frames"] = 240
        with self.assertRaisesRegex(ValueError, "endpoint"):
            timeline.compile_plan(self.plan)

    def test_bad_ranges_and_duplicate_ids_fail(self):
        for ranges in ([[12, 11]], [[10, 20], [19, 25]], [[0, 6001]], [[False, 3]]):
            plan = deepcopy(self.plan)
            plan["shots"][0]["keep_ranges"] = ranges
            with self.subTest(ranges=ranges), self.assertRaises(ValueError):
                timeline.compile_plan(plan)
        self.plan["shots"][1]["id"] = self.plan["shots"][0]["id"]
        with self.assertRaisesRegex(ValueError, "unique"):
            timeline.compile_plan(self.plan)

    def test_touching_ranges_merge(self):
        self.assertEqual(timeline.validate_ranges([[0, 12], [12, 30], [35, 40]]), [[0, 30], [35, 40]])

    def test_atempo_cascade_preserves_requested_factor(self):
        for factor in (Fraction(1, 8), Fraction(13, 10), Fraction(1), Fraction(8)):
            parts = timeline.atempo_factors(factor)
            product = Fraction(1)
            for part in parts:
                self.assertLessEqual(part, 2)
                self.assertGreaterEqual(part, Fraction(1, 2))
                product *= part
            self.assertEqual(product, factor)

    def test_ellipse_is_symmetric_and_open_at_top_and_bottom(self):
        points = timeline.ellipse_layout(3840, 2160)["points"]
        self.assertEqual(len(points), 6)
        for left, right in zip(points[::2], points[1::2]):
            self.assertAlmostEqual(left["x"] + right["x"], 3840)
            self.assertEqual(left["y"], right["y"])
            self.assertLess(left["x"], 960)
            self.assertGreater(right["x"], 2880)
            self.assertGreater(left["y"], 0)
            self.assertLess(left["y"], 2160)


if __name__ == "__main__":
    unittest.main()