|
| 1 | +""" |
| 2 | +TDD tests for content-engine compile-dna.py and compose-video.py scripts. |
| 3 | +""" |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +import pytest |
| 7 | + |
| 8 | +SCRIPTS_DIR = Path(__file__).resolve().parent.parent / "scripts" |
| 9 | + |
| 10 | +sys.path.insert(0, str(SCRIPTS_DIR)) |
| 11 | + |
| 12 | + |
| 13 | +class TestCompileDnaModule: |
| 14 | + """Tests for compile-dna.py script functions.""" |
| 15 | + |
| 16 | + def test_module_imports(self): |
| 17 | + """compile-dna.py should be importable.""" |
| 18 | + try: |
| 19 | + import importlib.util |
| 20 | + spec = importlib.util.spec_from_file_location( |
| 21 | + "compile_dna", SCRIPTS_DIR / "compile-dna.py" |
| 22 | + ) |
| 23 | + mod = importlib.util.module_from_spec(spec) |
| 24 | + spec.loader.exec_module(mod) |
| 25 | + except Exception as e: |
| 26 | + pytest.fail(f"compile-dna.py failed to import: {e}") |
| 27 | + |
| 28 | + def test_hash_file_produces_deterministic_hash(self, tmp_path): |
| 29 | + """hash_file should produce consistent SHA-256 hashes.""" |
| 30 | + import importlib.util |
| 31 | + spec = importlib.util.spec_from_file_location( |
| 32 | + "compile_dna", SCRIPTS_DIR / "compile-dna.py" |
| 33 | + ) |
| 34 | + mod = importlib.util.module_from_spec(spec) |
| 35 | + spec.loader.exec_module(mod) |
| 36 | + |
| 37 | + test_file = tmp_path / "test.txt" |
| 38 | + test_file.write_text("hello world") |
| 39 | + h1 = mod.hash_file(test_file) |
| 40 | + h2 = mod.hash_file(test_file) |
| 41 | + assert h1 == h2 |
| 42 | + assert len(h1) == 16 |
| 43 | + |
| 44 | + def test_hash_file_different_content_produces_different_hash(self, tmp_path): |
| 45 | + """Different file content should produce different hashes.""" |
| 46 | + import importlib.util |
| 47 | + spec = importlib.util.spec_from_file_location( |
| 48 | + "compile_dna", SCRIPTS_DIR / "compile-dna.py" |
| 49 | + ) |
| 50 | + mod = importlib.util.module_from_spec(spec) |
| 51 | + spec.loader.exec_module(mod) |
| 52 | + |
| 53 | + f1 = tmp_path / "a.txt" |
| 54 | + f2 = tmp_path / "b.txt" |
| 55 | + f1.write_text("hello") |
| 56 | + f2.write_text("world") |
| 57 | + assert mod.hash_file(f1) != mod.hash_file(f2) |
| 58 | + |
| 59 | + def test_discover_entities_finds_structure(self, tmp_path): |
| 60 | + """discover_entities should discover brand/character/style from directory.""" |
| 61 | + import importlib.util |
| 62 | + spec = importlib.util.spec_from_file_location( |
| 63 | + "compile_dna", SCRIPTS_DIR / "compile-dna.py" |
| 64 | + ) |
| 65 | + mod = importlib.util.module_from_spec(spec) |
| 66 | + spec.loader.exec_module(mod) |
| 67 | + |
| 68 | + brands_dir = tmp_path / "brand-assets" / "test-brand" |
| 69 | + brands_dir.mkdir(parents=True) |
| 70 | + (brands_dir / "hero.jpg").write_text("fake image") |
| 71 | + mod.BRAND_ASSETS = brands_dir.parent |
| 72 | + mod.CHARACTER_REFS = tmp_path / "character-refs" |
| 73 | + mod.STYLE_INSPIRATION = tmp_path / "style-inspiration" |
| 74 | + |
| 75 | + mod.CHARACTER_REFS.mkdir(parents=True, exist_ok=True) |
| 76 | + mod.STYLE_INSPIRATION.mkdir(parents=True, exist_ok=True) |
| 77 | + |
| 78 | + entities = mod.discover_entities(tmp_path) |
| 79 | + assert "test-brand" in entities["brands"] |
| 80 | + assert len(entities["brands"]["test-brand"]["assets"]) == 1 |
| 81 | + |
| 82 | + def test_compile_brand_produces_frontmatter(self): |
| 83 | + """compile_brand should produce Markdown with YAML frontmatter.""" |
| 84 | + import importlib.util |
| 85 | + spec = importlib.util.spec_from_file_location( |
| 86 | + "compile_dna", SCRIPTS_DIR / "compile-dna.py" |
| 87 | + ) |
| 88 | + mod = importlib.util.module_from_spec(spec) |
| 89 | + spec.loader.exec_module(mod) |
| 90 | + |
| 91 | + data = {"assets": [], "hash": "abc123"} |
| 92 | + result = mod.compile_brand("test-brand", data, None, dry_run=True) |
| 93 | + assert result.startswith("---") |
| 94 | + assert 'name: "test-brand"' in result |
| 95 | + assert 'type: brand-dna' in result |
| 96 | + |
| 97 | + def test_needs_recompilation_new_file(self, tmp_path, monkeypatch): |
| 98 | + """needs_recompilation should return True for non-existent compiled file.""" |
| 99 | + import importlib.util |
| 100 | + spec = importlib.util.spec_from_file_location( |
| 101 | + "compile_dna", SCRIPTS_DIR / "compile-dna.py" |
| 102 | + ) |
| 103 | + mod = importlib.util.module_from_spec(spec) |
| 104 | + spec.loader.exec_module(mod) |
| 105 | + |
| 106 | + monkeypatch.setattr(mod, "COMPILED_DIR", tmp_path / "compiled") |
| 107 | + assert mod.needs_recompilation("nonexistent", "brands", "hash123") |
| 108 | + |
| 109 | + def test_run_lint_no_files(self, tmp_path, monkeypatch): |
| 110 | + """run_lint should return 0 when no compiled files exist.""" |
| 111 | + import importlib.util |
| 112 | + spec = importlib.util.spec_from_file_location( |
| 113 | + "compile_dna", SCRIPTS_DIR / "compile-dna.py" |
| 114 | + ) |
| 115 | + mod = importlib.util.module_from_spec(spec) |
| 116 | + spec.loader.exec_module(mod) |
| 117 | + |
| 118 | + monkeypatch.setattr(mod, "COMPILED_DIR", tmp_path / "compiled") |
| 119 | + (tmp_path / "compiled").mkdir(parents=True, exist_ok=True) |
| 120 | + result = mod.run_lint() |
| 121 | + assert result == 0 |
| 122 | + |
| 123 | + |
| 124 | +class TestComposeVideoModule: |
| 125 | + """Tests for compose-video.py script functions.""" |
| 126 | + |
| 127 | + def test_module_imports(self): |
| 128 | + """compose-video.py should be importable.""" |
| 129 | + try: |
| 130 | + import importlib.util |
| 131 | + spec = importlib.util.spec_from_file_location( |
| 132 | + "compose_video", SCRIPTS_DIR / "compose-video.py" |
| 133 | + ) |
| 134 | + mod = importlib.util.module_from_spec(spec) |
| 135 | + spec.loader.exec_module(mod) |
| 136 | + except Exception as e: |
| 137 | + pytest.fail(f"compose-video.py failed to import: {e}") |
| 138 | + |
| 139 | + def test_parse_storyboard(self, tmp_path): |
| 140 | + """parse_storyboard should extract title and shots from markdown.""" |
| 141 | + import importlib.util |
| 142 | + spec = importlib.util.spec_from_file_location( |
| 143 | + "compose_video", SCRIPTS_DIR / "compose-video.py" |
| 144 | + ) |
| 145 | + mod = importlib.util.module_from_spec(spec) |
| 146 | + spec.loader.exec_module(mod) |
| 147 | + |
| 148 | + sb = tmp_path / "storyboard.md" |
| 149 | + sb.write_text("""--- |
| 150 | +title: "Test Video" |
| 151 | +brand: arcan-studio |
| 152 | +aspect_ratio: 16:9 |
| 153 | +--- |
| 154 | +
|
| 155 | +## Shot 1: Establishing |
| 156 | +Cinematic wide shot of city at night. |
| 157 | +
|
| 158 | +## Shot 2: Character Introduction |
| 159 | +Model walks through rain-soaked street. |
| 160 | +""") |
| 161 | + result = mod.parse_storyboard(sb) |
| 162 | + assert result["meta"]["title"] == "Test Video" |
| 163 | + assert len(result["shots"]) == 2 |
| 164 | + assert result["shots"][0]["name"] == "Shot 1: Establishing" |
| 165 | + |
| 166 | + def test_parse_storyboard_single_shot(self, tmp_path): |
| 167 | + """parse_storyboard should handle single-shot storyboards.""" |
| 168 | + import importlib.util |
| 169 | + spec = importlib.util.spec_from_file_location( |
| 170 | + "compose_video", SCRIPTS_DIR / "compose-video.py" |
| 171 | + ) |
| 172 | + mod = importlib.util.module_from_spec(spec) |
| 173 | + spec.loader.exec_module(mod) |
| 174 | + |
| 175 | + sb = tmp_path / "single.md" |
| 176 | + sb.write_text("""--- |
| 177 | +title: "Single Shot" |
| 178 | +--- |
| 179 | +
|
| 180 | +## Shot 1: Only |
| 181 | +Simple description. |
| 182 | +""") |
| 183 | + result = mod.parse_storyboard(sb) |
| 184 | + assert len(result["shots"]) == 1 |
| 185 | + assert result["meta"]["title"] == "Single Shot" |
| 186 | + |
| 187 | + def test_inject_brand_into_prompt(self): |
| 188 | + """inject_brand_into_prompt should append visual style suffix.""" |
| 189 | + import importlib.util |
| 190 | + spec = importlib.util.spec_from_file_location( |
| 191 | + "compose_video", SCRIPTS_DIR / "compose-video.py" |
| 192 | + ) |
| 193 | + mod = importlib.util.module_from_spec(spec) |
| 194 | + spec.loader.exec_module(mod) |
| 195 | + |
| 196 | + brand_dna = { |
| 197 | + "mood": "cinematic", |
| 198 | + "lighting_type": "dramatic", |
| 199 | + "lighting_temp": "cool", |
| 200 | + "composition": "rule-of-thirds", |
| 201 | + } |
| 202 | + original = "A person walking down the street." |
| 203 | + result = mod.inject_brand_into_prompt(original, brand_dna) |
| 204 | + assert original in result |
| 205 | + assert "cinematic" in result |
| 206 | + assert "dramatic lighting" in result |
| 207 | + assert "rule-of-thirds composition" in result |
| 208 | + |
| 209 | + def test_inject_brand_into_prompt_empty_dna(self): |
| 210 | + """inject_brand_into_prompt should return original for empty brand DNA.""" |
| 211 | + import importlib.util |
| 212 | + spec = importlib.util.spec_from_file_location( |
| 213 | + "compose_video", SCRIPTS_DIR / "compose-video.py" |
| 214 | + ) |
| 215 | + mod = importlib.util.module_from_spec(spec) |
| 216 | + spec.loader.exec_module(mod) |
| 217 | + |
| 218 | + result = mod.inject_brand_into_prompt("Test prompt.", {}) |
| 219 | + assert result == "Test prompt." |
| 220 | + |
| 221 | + def test_load_brand_dna_nonexistent(self, tmp_path, monkeypatch): |
| 222 | + """load_brand_dna should return None for nonexistent brands.""" |
| 223 | + import importlib.util |
| 224 | + spec = importlib.util.spec_from_file_location( |
| 225 | + "compose_video", SCRIPTS_DIR / "compose-video.py" |
| 226 | + ) |
| 227 | + mod = importlib.util.module_from_spec(spec) |
| 228 | + spec.loader.exec_module(mod) |
| 229 | + |
| 230 | + monkeypatch.setattr(mod, "COMPILED_DIR", tmp_path / "compiled") |
| 231 | + result = mod.load_brand_dna("nonexistent") |
| 232 | + assert result is None |
0 commit comments