clipwright/tests/test_gemini_block_split.py
pedro-bento 83ad1feaba
All checks were successful
CI / backend-tests (pull_request) Successful in 30s
CI / frontend-build (pull_request) Successful in 29s
refactor: move backend into the clipwright package
Restructure the 37 root-level Python modules into a proper package:
  clipwright/{core,api,pipeline,layouts,media,integrations,tools}

- Rewrite every local import (module-level and deferred) to absolute
  clipwright.<group>.<mod> form; no legacy root shims or re-exports.
- Add clipwright/core/paths.py and route fonts/assets/cookies through it.
- Entry points: uvicorn clipwright.api.app:app; jobs run as
  python -m clipwright.pipeline.main; probe as -m ...quality_probe.
- child_env() appends REPO_ROOT to PYTHONPATH so the -m workers work.
- transcribe_backends: sys.modules key now clipwright.media.scene_detection.
- Tooling: root pyproject.toml (pytest pythonpath=[.], ruff), shared
  requirements-test.txt, CI installs it, Docker PYTHONPATH=/app.
- Tests: all imports/fakes/source-scans repointed; new tests/test_package_layout.py
  enforces no root modules, docstring-only __init__, and no legacy imports.
- Docs/comments updated for the new layout.

Backend: 744 passed, 19 skipped. Frontend lint+build green.
2026-10-04 20:40:38 +01:00

43 lines
2.1 KiB
Python

"""A policy block on a batch of windows must bisect, not kill the job."""
import pytest
main = pytest.importorskip("clipwright.pipeline.main")
from clipwright.pipeline import gemini_worker
def _windows(n):
return [{"id": f"w{i}", "start": i * 10, "end": i * 10 + 10, "text": f"t{i}"} for i in range(n)]
def test_block_on_pair_isolates_and_drops_only_the_culprit(monkeypatch):
calls = []
def fake_stage(client, model, prompt, schema):
ids = [w["id"] for w in __import__("json").loads(prompt)]
calls.append(ids)
if "w5" in ids and "w6" in ids: # the pair that trips the filter
raise gemini_worker.GeminiBlockedError("PROHIBITED_CONTENT")
if ids == ["w3"]: # and w3 blocks even on its own
raise gemini_worker.GeminiBlockedError("PROHIBITED_CONTENT")
return {"windows": [{"id": i, "score": 50} for i in ids]}, {"input_tokens": 1}
monkeypatch.setattr(main, "_run_gemini_stage", fake_stage)
costs = []
out = main._run_stage_split(None, "m", _windows(8), lambda ws: __import__("json").dumps(ws),
None, "windows", costs, "score")
got = sorted(w["id"] for w in out)
# w5 and w6 each survive once separated; w3 only ever reached the model on
# its own inside the [0-3] batch, which passed, so it is kept too.
assert got == [f"w{i}" for i in range(8)]
assert calls[0] == [f"w{i}" for i in range(8)] # tried the whole batch first
# Halving [4-7] separates w5 from w6 at once: two blocked calls, then
# three that pass: [0-3], [4,5], [6,7].
assert [c for c in calls if "w5" in c and "w6" in c] == [calls[0], ["w4", "w5", "w6", "w7"]]
assert len(costs) == 3
def test_no_block_is_a_single_call(monkeypatch):
calls = []
def fake_stage(client, model, prompt, schema):
calls.append(prompt)
return {"shorts": [{"start": 0, "end": 20}]}, None
monkeypatch.setattr(main, "_run_gemini_stage", fake_stage)
out = main._run_stage_split(None, "m", _windows(3), lambda ws: "p", None, "shorts", [], "detail")
assert out == [{"start": 0, "end": 20}] and len(calls) == 1