|
| 1 | +"""Tests for branding overlay resolution and context building.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +import reeln |
| 11 | +from reeln.core.branding import build_branding_context, resolve_branding |
| 12 | +from reeln.core.errors import RenderError |
| 13 | +from reeln.core.templates import format_ass_time |
| 14 | +from reeln.models.branding import BrandingConfig |
| 15 | + |
| 16 | +# --------------------------------------------------------------------------- |
| 17 | +# build_branding_context |
| 18 | +# --------------------------------------------------------------------------- |
| 19 | + |
| 20 | + |
| 21 | +class TestBuildBrandingContext: |
| 22 | + def test_version_included(self) -> None: |
| 23 | + ctx = build_branding_context(3.0) |
| 24 | + assert ctx.get("version") == f"v{reeln.__version__}" |
| 25 | + |
| 26 | + def test_branding_end_format(self) -> None: |
| 27 | + ctx = build_branding_context(3.0) |
| 28 | + assert ctx.get("branding_end") == format_ass_time(3.0) |
| 29 | + |
| 30 | + def test_custom_duration(self) -> None: |
| 31 | + ctx = build_branding_context(5.0) |
| 32 | + assert ctx.get("branding_end") == format_ass_time(5.0) |
| 33 | + |
| 34 | + def test_zero_duration(self) -> None: |
| 35 | + ctx = build_branding_context(0.0) |
| 36 | + assert ctx.get("branding_end") == format_ass_time(0.0) |
| 37 | + |
| 38 | + |
| 39 | +# --------------------------------------------------------------------------- |
| 40 | +# resolve_branding |
| 41 | +# --------------------------------------------------------------------------- |
| 42 | + |
| 43 | + |
| 44 | +class TestResolveBranding: |
| 45 | + def test_disabled_returns_none(self, tmp_path: Path) -> None: |
| 46 | + config = BrandingConfig(enabled=False) |
| 47 | + result = resolve_branding(config, tmp_path) |
| 48 | + assert result is None |
| 49 | + |
| 50 | + def test_builtin_template_renders(self, tmp_path: Path) -> None: |
| 51 | + config = BrandingConfig() |
| 52 | + result = resolve_branding(config, tmp_path) |
| 53 | + assert result is not None |
| 54 | + assert result.is_file() |
| 55 | + assert result.suffix == ".ass" |
| 56 | + content = result.read_text(encoding="utf-8") |
| 57 | + assert f"v{reeln.__version__}" in content |
| 58 | + result.unlink() |
| 59 | + |
| 60 | + def test_builtin_template_contains_fade(self, tmp_path: Path) -> None: |
| 61 | + config = BrandingConfig() |
| 62 | + result = resolve_branding(config, tmp_path) |
| 63 | + assert result is not None |
| 64 | + content = result.read_text(encoding="utf-8") |
| 65 | + assert "\\fad(300,800)" in content |
| 66 | + result.unlink() |
| 67 | + |
| 68 | + def test_builtin_template_contains_branding_end(self, tmp_path: Path) -> None: |
| 69 | + config = BrandingConfig(duration=5.0) |
| 70 | + result = resolve_branding(config, tmp_path) |
| 71 | + assert result is not None |
| 72 | + content = result.read_text(encoding="utf-8") |
| 73 | + assert format_ass_time(5.0) in content |
| 74 | + result.unlink() |
| 75 | + |
| 76 | + def test_custom_template(self, tmp_path: Path) -> None: |
| 77 | + template = tmp_path / "custom.ass" |
| 78 | + template.write_text( |
| 79 | + "[Script Info]\nScriptType: v4.00+\n\n[Events]\n" |
| 80 | + "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n" |
| 81 | + "Dialogue: 0,0:00:00.00,{{branding_end}},Default,,0,0,0,,custom {{version}}\n", |
| 82 | + encoding="utf-8", |
| 83 | + ) |
| 84 | + config = BrandingConfig(template=str(template)) |
| 85 | + result = resolve_branding(config, tmp_path) |
| 86 | + assert result is not None |
| 87 | + content = result.read_text(encoding="utf-8") |
| 88 | + assert f"v{reeln.__version__}" in content |
| 89 | + assert format_ass_time(5.0) in content |
| 90 | + result.unlink() |
| 91 | + |
| 92 | + def test_missing_custom_template(self, tmp_path: Path) -> None: |
| 93 | + config = BrandingConfig(template="/nonexistent/branding.ass") |
| 94 | + with pytest.raises(RenderError, match="Template file not found"): |
| 95 | + resolve_branding(config, tmp_path) |
| 96 | + |
| 97 | + def test_missing_builtin_template(self, tmp_path: Path) -> None: |
| 98 | + config = BrandingConfig(template="builtin:nonexistent") |
| 99 | + with pytest.raises(RenderError, match="Builtin template not found"): |
| 100 | + resolve_branding(config, tmp_path) |
| 101 | + |
| 102 | + def test_write_failure(self, tmp_path: Path) -> None: |
| 103 | + config = BrandingConfig() |
| 104 | + with ( |
| 105 | + patch("reeln.core.branding.Path.write_text", side_effect=OSError("disk full")), |
| 106 | + pytest.raises(RenderError, match="Failed to write rendered branding"), |
| 107 | + ): |
| 108 | + resolve_branding(config, tmp_path) |
0 commit comments