|
1 | | -import pytest |
2 | | -import os |
3 | | -import tempfile |
4 | | -from math_anim import render_animation, is_safe_manim_code, generate_manim_code |
5 | | - |
6 | | -def mock_generate_manim(*args, **kwargs): |
7 | | - return \"\"\" |
8 | | -from manim import * |
9 | | -class SafeScene(Scene): |
10 | | - def construct(self): |
11 | | - text = Text("Hello AST!") |
12 | | - self.play(Write(text)) |
13 | | - self.wait(1) |
14 | | -\"\"\" |
15 | | - |
16 | | -def mock_generate_manim_unsafe(*args, **kwargs): |
17 | | - return \"\"\" |
18 | | -import os |
19 | | -from manim import * |
20 | | -class UnsafeScene(Scene): |
21 | | - def construct(self): |
22 | | - os.system("echo 'pwned'") |
23 | | -\"\"\" |
24 | | - |
25 | | -def test_safe_integration(monkeypatch): |
26 | | - monkeypatch.setattr("math_anim.generate_manim_code", mock_generate_manim) |
27 | | - code = generate_manim_code("test topic") |
28 | | - is_safe, msg = is_safe_manim_code(code) |
29 | | - assert is_safe |
30 | | - |
31 | | -def test_unsafe_integration(monkeypatch): |
32 | | - monkeypatch.setattr("math_anim.generate_manim_code", mock_generate_manim_unsafe) |
33 | | - code = generate_manim_code("test topic") |
34 | | - is_safe, msg = is_safe_manim_code(code) |
35 | | - assert not is_safe |
36 | | - assert "not allowed" in msg |
| 1 | +from types import SimpleNamespace |
| 2 | + |
| 3 | +import math_anim |
| 4 | + |
| 5 | + |
| 6 | +def test_render_animation_returns_failure_when_manim_exits_nonzero(tmp_path, monkeypatch): |
| 7 | + calls = [] |
| 8 | + |
| 9 | + def fake_run(cmd, capture_output, text, timeout): |
| 10 | + calls.append(cmd) |
| 11 | + if len(calls) == 1: |
| 12 | + return SimpleNamespace(returncode=0, stdout="Manim Community v0.18.0", stderr="") |
| 13 | + return SimpleNamespace(returncode=1, stdout="", stderr="render failed") |
| 14 | + |
| 15 | + monkeypatch.setattr(math_anim.subprocess, "run", fake_run) |
| 16 | + |
| 17 | + result = math_anim.render_animation( |
| 18 | + "from manim import *\n\nclass ExplanationScene(Scene):\n def construct(self):\n self.wait(1)\n", |
| 19 | + output_dir=str(tmp_path), |
| 20 | + ) |
| 21 | + |
| 22 | + assert result["success"] is False |
| 23 | + assert "render failed" in result["error"] |
| 24 | + |
| 25 | + |
| 26 | +def test_create_math_animation_returns_error_when_code_generation_is_empty(monkeypatch): |
| 27 | + monkeypatch.setattr(math_anim, "generate_manim_code", lambda *args, **kwargs: "") |
| 28 | + |
| 29 | + result = math_anim.create_math_animation("Fourier transform intuition") |
| 30 | + |
| 31 | + assert result["success"] is False |
| 32 | + assert "Failed to generate Manim code" in result["error"] |
0 commit comments