File size: 9,551 Bytes
851e5ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | from pathlib import Path
import pytest
from src.clip_source_map import (
load_clip_source_ranges,
save_clip_source_ranges,
split_source_ranges,
trim_source_ranges,
)
from src.services.task_service import TaskService
def test_trim_source_ranges_preserves_non_contiguous_mapping():
trimmed = trim_source_ranges([(10.0, 11.0), (13.0, 15.0)], 1.0, 0.0)
assert trimmed == [(13.0, 15.0)]
def test_split_source_ranges_preserves_non_contiguous_mapping():
first, second = split_source_ranges([(10.0, 11.0), (13.0, 15.0)], 1.0)
assert first == [(10.0, 11.0)]
assert second == [(13.0, 15.0)]
def test_save_and_load_clip_source_ranges(tmp_path):
clip_path = tmp_path / "clip.mp4"
clip_path.write_bytes(b"")
save_clip_source_ranges(clip_path, [(10.0, 11.0), (13.0, 15.0)])
assert load_clip_source_ranges(clip_path) == [(10.0, 11.0), (13.0, 15.0)]
class _FakeClipRepo:
def __init__(self, clip: dict):
self.clip = dict(clip)
async def get_clip_by_id(self, _db, _clip_id: str):
return dict(self.clip)
async def update_clip(
self,
_db,
_clip_id: str,
filename: str,
file_path: str,
start_time: str,
end_time: str,
duration: float,
text: str,
workflow_meta=None,
):
self.clip.update(
{
"filename": filename,
"file_path": file_path,
"start_time": start_time,
"end_time": end_time,
"duration": duration,
"text": text,
}
)
@pytest.mark.asyncio
async def test_trim_clip_uses_persisted_source_ranges(isolated_clip_edits, monkeypatch, tmp_path):
input_path = tmp_path / "clip.mp4"
input_path.write_bytes(b"input")
save_clip_source_ranges(input_path, [(10.0, 11.0), (13.0, 15.0)])
output_path = tmp_path / "trimmed.mp4"
output_path.write_bytes(b"output")
import threading
request_thread = threading.get_ident()
render_threads = []
def render_trim(*_args, **_kwargs):
render_threads.append(threading.get_ident())
return output_path
monkeypatch.setattr("src.services.clip_service.trim_clip_file", render_trim)
repo = _FakeClipRepo(
{
"id": "clip-1",
"task_id": "task-1",
"file_path": str(input_path),
"filename": "clip.mp4",
"start_time": "00:10",
"end_time": "00:20",
"duration": 3.0,
"text": "hello",
}
)
service = TaskService(db=None)
service.clip_repo = repo
clip = await service.trim_clip("task-1", "clip-1", 1.0, 0.0)
assert render_threads and render_threads[0] != request_thread
assert clip["start_time"] == "00:13"
assert clip["end_time"] == "00:15"
assert clip["duration"] == pytest.approx(2.0)
assert load_clip_source_ranges(output_path) == [(13.0, 15.0)]
@pytest.mark.asyncio
@pytest.mark.parametrize("clip_length,cap", [(None, 60), ("short", 60), ("long", 120)])
async def test_regenerate_all_clips_reuses_persisted_source_ranges(isolated_clip_edits, monkeypatch, tmp_path, clip_length, cap):
source_path = tmp_path / "source.mp4"
source_path.write_bytes(b"source")
clip_path = tmp_path / "clip.mp4"
clip_path.write_bytes(b"clip")
save_clip_source_ranges(clip_path, [(10.0, 11.0), (13.0, 15.0)])
captured: dict[str, object] = {}
class _FakeTaskRepo:
async def get_task_by_id(self, _db, _task_id: str):
return {
"id": "task-1",
"source_url": "upload://source.mp4",
"source_type": "video_url",
}
async def update_task_clips(self, _db, _task_id: str, _clip_ids):
return None
class _FakeClipRepoWithTask:
async def get_clips_by_task(self, _db, _task_id: str):
return [
{
"id": "clip-1",
"task_id": "task-1",
"file_path": str(clip_path),
"start_time": "00:10",
"end_time": "00:20",
"duration": 3.0,
"text": "hello",
"relevance_score": 0.8,
"reasoning": "test",
"virality_score": 1,
"hook_score": 1,
"engagement_score": 1,
"value_score": 1,
"shareability_score": 1,
"hook_type": "hook",
}
]
async def delete_clips_by_task(self, _db, _task_id: str):
return None
async def create_clip(self, _db, **_kwargs):
return "new-clip"
class _FakeVideoService:
def resolve_local_video_path(self, _url: str) -> Path:
return source_path
async def prepare_source(self, path: Path) -> Path:
return path
async def create_video_clips(self, _video_path: Path, segments, *_args, **kwargs):
captured["segments"] = segments
captured["max_clip_seconds"] = kwargs["max_clip_seconds"]
return [{"clip_id": 1, "filename": "new.mp4", "path": "new.mp4", "start_time": "00:10", "end_time": "00:15", "duration": 3.0}]
service = TaskService(db=None)
async def fake_load_task_source_settings(_task_id: str):
return {
"output_format": "vertical",
"add_subtitles": True,
"cut_long_pauses": False,
"pause_threshold_ms": 900,
"remove_filler_words": False,
"filtered_words": [],
**({"clip_length": clip_length} if clip_length else {}),
}
monkeypatch.setattr(service, "_load_task_source_settings", fake_load_task_source_settings)
service.task_repo = _FakeTaskRepo()
service.clip_repo = _FakeClipRepoWithTask()
service.video_service = _FakeVideoService()
await service.regenerate_all_clips_for_task(
"task-1",
"TikTokSans-Regular",
24,
"#FFFFFF",
"default",
cleanup_settings={},
)
segments = captured["segments"]
assert segments[0]["keep_ranges"] == [(10.0, 11.0), (13.0, 15.0)]
# Merged or trimmed clips are never cut tighter than before presets existed.
assert captured["max_clip_seconds"] == cap
@pytest.mark.asyncio
async def test_regenerate_all_clips_recomputes_cleanup_from_source_ranges(isolated_clip_edits,
monkeypatch, tmp_path
):
source_path = tmp_path / "source.mp4"
source_path.write_bytes(b"source")
clip_path = tmp_path / "clip.mp4"
clip_path.write_bytes(b"clip")
save_clip_source_ranges(clip_path, [(10.0, 11.0), (13.0, 15.0)])
captured: dict[str, object] = {}
class _FakeTaskRepo:
async def get_task_by_id(self, _db, _task_id: str):
return {
"id": "task-1",
"source_url": "upload://source.mp4",
"source_type": "video_url",
}
async def update_task_clips(self, _db, _task_id: str, _clip_ids):
return None
class _FakeClipRepoWithTask:
async def get_clips_by_task(self, _db, _task_id: str):
return [
{
"id": "clip-1",
"task_id": "task-1",
"file_path": str(clip_path),
"start_time": "00:10",
"end_time": "00:20",
"duration": 3.0,
"text": "hello",
"relevance_score": 0.8,
"reasoning": "test",
"virality_score": 1,
"hook_score": 1,
"engagement_score": 1,
"value_score": 1,
"shareability_score": 1,
"hook_type": "hook",
}
]
async def delete_clips_by_task(self, _db, _task_id: str):
return None
async def create_clip(self, _db, **_kwargs):
return "new-clip"
class _FakeVideoService:
def resolve_local_video_path(self, _url: str) -> Path:
return source_path
async def prepare_source(self, path: Path) -> Path:
return path
async def create_video_clips(self, _video_path: Path, segments, *_args, **_kwargs):
captured["segments"] = segments
return [{"clip_id": 1, "filename": "new.mp4", "path": "new.mp4", "start_time": "00:10", "end_time": "00:15", "duration": 3.0}]
service = TaskService(db=None)
async def fake_load_task_source_settings(_task_id: str):
return {
"output_format": "vertical",
"add_subtitles": True,
"cut_long_pauses": False,
"pause_threshold_ms": 900,
"remove_filler_words": False,
"filtered_words": [],
}
monkeypatch.setattr(service, "_load_task_source_settings", fake_load_task_source_settings)
service.task_repo = _FakeTaskRepo()
service.clip_repo = _FakeClipRepoWithTask()
service.video_service = _FakeVideoService()
await service.regenerate_all_clips_for_task(
"task-1",
"TikTokSans-Regular",
24,
"#FFFFFF",
"default",
cleanup_settings={"cut_long_pauses": True},
)
segments = captured["segments"]
assert "keep_ranges" not in segments[0]
assert segments[0]["source_ranges"] == [(10.0, 11.0), (13.0, 15.0)]
|