mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 04:59:37 +08:00
* fix(video): align scene narration and fit diagrams - Refine bilingual prompts for scene-by-scene narration and visuals - Fit and center SVG diagrams with their labels and strokes - Improve geometry coordinate guidance and preserve layout retries * fix(video): do not report a fitted scene graphic as overflow The kit scales a graphic with a transform on its full-size frame, so the scaled frame box and the SVG canvas can exceed the region while the drawing fits. Measure HTML overflow without that transform and canvas. --------- Co-authored-by: zzzworm <15828873+zzzworm86@user.noreply.gitee.com> Co-authored-by: zszz3 <91608029+zszz3@users.noreply.github.com>
338 lines
15 KiB
Python
338 lines
15 KiB
Python
"""Real Wasm checks; CI enables these with DEEPTUTOR_TEST_VIDEO_SANDBOX=1."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from deeptutor.agents.video_lesson import runtime
|
|
from deeptutor.agents.video_lesson.layout_check import check_layout, chrome_path, sample_frames
|
|
|
|
pytestmark = [
|
|
pytest.mark.asyncio,
|
|
pytest.mark.skipif(
|
|
os.environ.get("DEEPTUTOR_TEST_VIDEO_SANDBOX") != "1",
|
|
reason="requires Node and the Remotion npm runtime",
|
|
),
|
|
]
|
|
|
|
CONFIG = "export const LESSON = {durationInFrames:60, fps:30, width:1920, height:1080};\n"
|
|
VALID = CONFIG + 'export const Lesson = () => <div>{"A safe lesson. ".repeat(50)}</div>;'
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def no_docker(monkeypatch: pytest.MonkeyPatch):
|
|
"""Every integration test must work with Docker unavailable, even on CI hosts."""
|
|
which = runtime.shutil.which
|
|
|
|
def without_docker(name, *args, **kwargs):
|
|
if name in {"docker", "podman", "bwrap", "sandbox-exec"}:
|
|
pytest.fail(f"Video lessons must not probe deployment tooling: {name}")
|
|
return which(name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(runtime.shutil, "which", without_docker)
|
|
# A stray Docker invocation would also fail even on a runner with Docker installed.
|
|
monkeypatch.setenv("DOCKER_HOST", "tcp://127.0.0.1:1")
|
|
|
|
|
|
async def test_real_render_and_runtime_error(tmp_path: Path) -> None:
|
|
lesson = runtime.prepare_workdir(tmp_path, VALID)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert result.ok and len(result.frames) == 25, result.errors
|
|
assert (tmp_path / "player.js").stat().st_size > 0
|
|
lesson.write_text(CONFIG + 'export const Lesson = () => {throw new Error("fixture failure")};')
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert not result.ok and any("fixture failure" in error for error in result.errors)
|
|
|
|
|
|
@pytest.mark.skipif(chrome_path() is None, reason="Chrome is required for the opening layout check")
|
|
async def test_opening_and_flow_compile_in_sandbox(tmp_path: Path) -> None:
|
|
code = """
|
|
import {SceneLayout, ContentFlow, Title, Body, Card, Scenes, lessonLength} from "./kit";
|
|
export const VIDEO_LESSON_SCENES = [{voice: "s1", frames: 60, content: <SceneLayout content={<ContentFlow>
|
|
<Title opening>题目</Title><Body opening delay={10}>已知条件和目标</Body>
|
|
<Card delay={30}>答案</Card><Body>未标记旧内容</Body></ContentFlow>} />},
|
|
{voice: "s1", frames: 60, content: <SceneLayout content={<ContentFlow>
|
|
<Title opening>第二幕</Title></ContentFlow>} />}];
|
|
export const LESSON = {durationInFrames: lessonLength(VIDEO_LESSON_SCENES), fps: 30, width: 1920, height: 1080, sceneContract: "fixed-v1"};
|
|
export const Lesson = () => <Scenes scenes={VIDEO_LESSON_SCENES} />;
|
|
"""
|
|
voice = 'export const VOICE = {s1: {text: "", frames: 30, captions: []}};'
|
|
lesson = runtime.prepare_workdir(tmp_path, code, voice=voice)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert result.ok, result.errors
|
|
assert result.scene_lengths == [60, 60]
|
|
page = runtime.write_page(
|
|
tmp_path / "player.js", tmp_path / "lesson.html", title="test", language="zh"
|
|
)
|
|
assert (
|
|
check_layout(page, [0, 30, 60], require_player_frame=True, expected_scene_lengths=[60, 60])
|
|
== []
|
|
)
|
|
# Same total but incorrect interior cut points cannot be mistaken for the same snapshot.
|
|
assert (
|
|
check_layout(page, [0], require_player_frame=True, expected_scene_lengths=[75, 45]) is None
|
|
)
|
|
script = """<script>
|
|
window.addEventListener('load', () => setTimeout(() => {
|
|
const opacity = (text) => {
|
|
const el = [...document.querySelectorAll('div')].find(e => e.textContent === text && !e.querySelector('div'));
|
|
if (!el) return 'absent';
|
|
let value = 1;
|
|
for (let e = el; e; e = e.parentElement) value *= Number(getComputedStyle(e).opacity);
|
|
return String(value);
|
|
};
|
|
document.body.setAttribute('data-opacities', ['题目', '已知条件和目标', '答案', '未标记旧内容', '第二幕'].map(opacity).join(','));
|
|
}, 1500));</script>"""
|
|
page.write_text(
|
|
page.read_text(encoding="utf-8").replace("</body>", script + "</body>"), encoding="utf-8"
|
|
)
|
|
for frame, expected in ((0, "1,1,0,0,absent"), (60, "absent,absent,absent,absent,0")):
|
|
proc = subprocess.run(
|
|
[
|
|
chrome_path(),
|
|
"--headless=new",
|
|
"--disable-gpu",
|
|
"--no-first-run",
|
|
f"--user-data-dir={tmp_path / ('profile-' + str(frame))}",
|
|
"--virtual-time-budget=8000",
|
|
"--dump-dom",
|
|
f"{page.as_uri()}?frame={frame}",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
encoding="utf-8",
|
|
errors="replace",
|
|
timeout=60,
|
|
)
|
|
assert proc.returncode == 0, proc.stderr
|
|
assert f'data-opacities="{expected}"' in proc.stdout
|
|
|
|
|
|
@pytest.mark.skipif(chrome_path() is None, reason="Chrome is required for the layout check")
|
|
async def test_fitted_graphic_is_not_reported_as_overflow(tmp_path: Path) -> None:
|
|
# A small drawing on a canvas wider than the region: the kit scales and centres
|
|
# it, so the scaled frame and the canvas exceed the region but the drawing fits.
|
|
code = """
|
|
import {SceneLayout, ContentFlow, Title, Body, Scenes, lessonLength} from "./kit";
|
|
const Drawing = () => <svg width={880} height={330}>
|
|
<line x1={300} y1={150} x2={560} y2={150} stroke="black" strokeWidth={4} />
|
|
<text x={300} y={200} fontSize={28}>x</text></svg>;
|
|
export const VIDEO_LESSON_SCENES = [{voice: "s1", frames: 60, content: <SceneLayout
|
|
content={<ContentFlow><Title opening>题目</Title><Body opening>已知条件</Body></ContentFlow>}
|
|
graphic={<Drawing />} />}];
|
|
export const LESSON = {durationInFrames: lessonLength(VIDEO_LESSON_SCENES), fps: 30, width: 1920, height: 1080, sceneContract: "fixed-v1"};
|
|
export const Lesson = () => <Scenes scenes={VIDEO_LESSON_SCENES} />;
|
|
"""
|
|
voice = 'export const VOICE = {s1: {text: "", frames: 30, captions: []}};'
|
|
lesson = runtime.prepare_workdir(tmp_path, code, voice=voice)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert result.ok, result.errors
|
|
page = runtime.write_page(
|
|
tmp_path / "player.js", tmp_path / "lesson.html", title="test", language="zh"
|
|
)
|
|
assert check_layout(page, [0, 30], require_player_frame=True, expected_scene_lengths=[60]) == []
|
|
|
|
|
|
async def test_fixed_timeline_uses_narration_and_keeps_legacy_fallback(tmp_path: Path) -> None:
|
|
code = """
|
|
import {Scenes, Stage, Title, lessonLength} from "./kit";
|
|
export const VIDEO_LESSON_SCENES = [
|
|
{voice: "s1", frames: 60, content: <Stage><Title>First</Title></Stage>},
|
|
{voice: "s2", frames: 60, content: <Stage><Title>Second</Title></Stage>},
|
|
];
|
|
export const LESSON = {durationInFrames: lessonLength(VIDEO_LESSON_SCENES), fps: 30,
|
|
width: 1920, height: 1080, sceneContract: "fixed-v1"};
|
|
export const Lesson = () => <Scenes scenes={VIDEO_LESSON_SCENES} />;
|
|
"""
|
|
voice = 'export const VOICE = {s1:{text:"",frames:100,captions:[]},s2:{text:"",frames:10,captions:[]}};'
|
|
lesson = runtime.prepare_workdir(tmp_path, code, voice=voice)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert result.ok, result.errors
|
|
assert result.scene_lengths == [130, 60]
|
|
assert {0, 15, 129, 130, 145, 189}.issubset(
|
|
sample_frames(190, scene_lengths=result.scene_lengths)
|
|
)
|
|
|
|
# Invalid fixed contract must not invent trusted cuts; legacy Lesson remains playable.
|
|
lesson.write_text(code.replace('sceneContract: "fixed-v1"', 'sceneContract: "unknown"'))
|
|
legacy = await runtime.build_lesson(lesson, tmp_path / "legacy.js")
|
|
assert legacy.ok, legacy.errors
|
|
assert legacy.scene_lengths is None
|
|
|
|
|
|
async def test_real_kit_animation_and_captions(tmp_path: Path) -> None:
|
|
code = (
|
|
CONFIG
|
|
+ """
|
|
import {Sequence} from "remotion";
|
|
import {Stage, Title, Body, Narration} from "./kit";
|
|
export const Lesson = () => <Stage eyebrow="Test">
|
|
<Sequence durationInFrames={60}>
|
|
<Title>安全运行 · Safe rendering</Title>
|
|
<Body x={120} y={300}>A complete lesson with animation and captions.</Body>
|
|
<Narration lines={[{at: 0, text: "不需要 Docker"}, {at: 30, text: "Second caption"}]} />
|
|
</Sequence>
|
|
</Stage>;
|
|
"""
|
|
)
|
|
lesson = runtime.prepare_workdir(tmp_path, code)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert result.ok and len(result.frames) == 25, result.errors
|
|
assert len({frame["chars"] for frame in result.frames}) > 1
|
|
|
|
|
|
async def test_model_cannot_read_host_secrets_files_or_connect_to_network(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
marker = tmp_path / "host-secret.txt"
|
|
marker.write_text("dummy-host-secret")
|
|
monkeypatch.setenv("DT_REVIEW_SENTINEL", "dummy-environment-secret")
|
|
code = """
|
|
for (const name of ["process", "require", "fetch", "XMLHttpRequest", "WebSocket", "Worker"]) {
|
|
if (typeof globalThis[name] !== "undefined") throw new Error("host API exposed: " + name);
|
|
}
|
|
if (({}).constructor.constructor("return typeof process")() !== "undefined")
|
|
throw new Error("constructor escaped to the host");
|
|
if (eval("typeof process") !== "undefined") throw new Error("eval escaped to the host");
|
|
export const Lesson = () => <div>{"Isolated lesson. ".repeat(50)}</div>;
|
|
"""
|
|
lesson = runtime.prepare_workdir(tmp_path, CONFIG + code)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert result.ok, result.errors
|
|
assert marker.read_text() == "dummy-host-secret"
|
|
assert "dummy-host-secret" not in (tmp_path / "player.js").read_text()
|
|
assert "dummy-environment-secret" not in (tmp_path / "player.js").read_text()
|
|
|
|
|
|
@pytest.mark.parametrize("specifier", ["node:fs", "node:child_process", "../host-secret.json"])
|
|
async def test_host_imports_are_rejected(tmp_path: Path, specifier: str) -> None:
|
|
(tmp_path / "host-secret.json").write_text('{"secret":"dummy-host-secret"}')
|
|
lesson = runtime.prepare_workdir(
|
|
tmp_path / "source",
|
|
CONFIG + f"import secret from {json.dumps(specifier)};\n"
|
|
"export const Lesson = () => <div>{JSON.stringify(secret)}</div>;",
|
|
)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "source" / "player.js")
|
|
assert not result.ok and result.stage == "compile"
|
|
assert not (tmp_path / "source" / "player.js").exists()
|
|
|
|
|
|
async def test_cancelled_render_reaps_build_process(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
await runtime.ensure_runtime()
|
|
original = asyncio.create_subprocess_exec
|
|
started = asyncio.Event()
|
|
processes = []
|
|
|
|
async def observe(*argv, **kwargs):
|
|
process = await original(*argv, **kwargs)
|
|
if str(runtime.ASSETS_DIR / "build.mjs") in argv:
|
|
processes.append(process)
|
|
started.set()
|
|
return process
|
|
|
|
monkeypatch.setattr(asyncio, "create_subprocess_exec", observe)
|
|
lesson = runtime.prepare_workdir(
|
|
tmp_path, CONFIG + "export const Lesson = () => { while (true) {} };"
|
|
)
|
|
task = asyncio.create_task(runtime.build_lesson(lesson, tmp_path / "player.js"))
|
|
try:
|
|
await asyncio.wait_for(started.wait(), 30)
|
|
await asyncio.sleep(1)
|
|
assert not task.done(), "infinite renderer should still be running"
|
|
task.cancel()
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await asyncio.wait_for(task, 10)
|
|
assert processes and all(p.returncode is not None for p in processes)
|
|
assert not (tmp_path / "player.js").exists()
|
|
finally:
|
|
if not task.done():
|
|
task.cancel()
|
|
await asyncio.gather(task, return_exceptions=True)
|
|
|
|
|
|
async def test_timed_out_render_is_rejected(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(runtime, "RENDER_TIMEOUT_S", 1)
|
|
lesson = runtime.prepare_workdir(
|
|
tmp_path, CONFIG + "export const Lesson = () => { while (true) {} };"
|
|
)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert not result.ok and result.stage == "render"
|
|
assert any("interrupt" in e or "timed out" in e for e in result.errors)
|
|
assert not (tmp_path / "player.js").exists()
|
|
|
|
|
|
async def test_build_script_uses_isolation_without_container_configuration(tmp_path: Path) -> None:
|
|
root = await runtime.ensure_runtime()
|
|
lesson = runtime.prepare_workdir(tmp_path, VALID)
|
|
result = await runtime._run_tool(
|
|
[
|
|
runtime._node(),
|
|
str(runtime.ASSETS_DIR / "build.mjs"),
|
|
str(lesson),
|
|
str(tmp_path / "player.js"),
|
|
],
|
|
timeout=30,
|
|
env={"DEEPTUTOR_REMOTION_RUNTIME": str(root)},
|
|
)
|
|
assert result.exit_code == 0, result.stderr
|
|
assert json.loads(result.stdout.strip().splitlines()[-1])["ok"]
|
|
assert (tmp_path / "player.js").exists()
|
|
|
|
|
|
@pytest.mark.parametrize("with_audio", [False, True])
|
|
async def test_narrated_scenes_with_and_without_audio(tmp_path: Path, with_audio: bool) -> None:
|
|
code = """
|
|
import {Stage, Title, Scenes, lessonLength} from "./kit";
|
|
const scenes = [{voice: "s1", frames: 60, content: <Stage><Title>声音与字幕</Title></Stage>}];
|
|
export const LESSON = {durationInFrames:lessonLength(scenes), fps:30, width:1920, height:1080};
|
|
export const Lesson = () => <Scenes scenes={scenes} />;
|
|
"""
|
|
clip = {"text": "声音与字幕", "frames": 45, "captions": [{"at": 0, "text": "声音与字幕"}]}
|
|
if with_audio:
|
|
clip["src"] = "data:audio/mpeg;base64,SUQz"
|
|
voice = "export const VOICE = " + json.dumps({"s1": clip}) + ";"
|
|
lesson = runtime.prepare_workdir(tmp_path, code, voice=voice)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert result.ok and len(result.frames) == 25, result.errors
|
|
|
|
|
|
async def test_memory_exhaustion_is_rejected(tmp_path: Path) -> None:
|
|
lesson = runtime.prepare_workdir(
|
|
tmp_path,
|
|
CONFIG
|
|
+ "export const Lesson = () => { const a=[]; while(true) a.push(new Uint8Array(1024*1024)); };",
|
|
)
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert not result.ok and result.stage == "render"
|
|
assert not (tmp_path / "player.js").exists()
|
|
|
|
|
|
async def test_compiler_rejects_local_file_and_symlink_imports(tmp_path: Path) -> None:
|
|
lesson = runtime.prepare_workdir(
|
|
tmp_path,
|
|
CONFIG
|
|
+ 'import secret from "./secret.json"; export const Lesson = () => <div>{secret}</div>;',
|
|
)
|
|
(tmp_path / "secret.json").write_text('"dummy-host-secret"')
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert not result.ok and result.stage == "compile"
|
|
(tmp_path / "china_outline.json").unlink()
|
|
try:
|
|
(tmp_path / "china_outline.json").symlink_to(tmp_path / "secret.json")
|
|
except OSError:
|
|
pytest.skip("symlinks unavailable on this Windows account")
|
|
lesson.write_text(lesson.read_text().replace("./secret.json", "./china_outline.json"))
|
|
result = await runtime.build_lesson(lesson, tmp_path / "player.js")
|
|
assert not result.ok and result.stage == "compile"
|
|
assert not (tmp_path / "player.js").exists()
|