Skip to content

Official Codebase of 3DCodeVerse

Python

tests: node helpers run through spatial.node.run_node and the sky test compiles through the production host — glsl_compile.mjs re-implemented serve/importmap/gpu_launch

gaoypeng51fb2bc1dd75
diff --git a/harness/tests/prompts/conftest.py b/harness/tests/prompts/conftest.py
index 248cb6e4..ca86725c 100644
--- a/harness/tests/prompts/conftest.py
+++ b/harness/tests/prompts/conftest.py
@@ -15,13 +15,12 @@ from pathlib import Path
import pytest
from codeverse3d.config import get_settings
+from codeverse3d.spatial.node import run_node
PROMPTS_DIR = Path(__file__).resolve().parents[2] / "codeverse3d" / "prompts"
#: the scene starter's harness-owned library (D51): a scene workspace always has it at
#: `src/lib/`, so a snippet may import from it exactly as an agent's file would
STARTER_LIB = Path(__file__).resolve().parents[2] / "codeverse3d" / "languages" / "scene_threejs" / "starter" / "src" / "lib"
-RUNTIME_JS = Path(__file__).resolve().parents[2] / "runtime_js"
-HELPERS = Path(__file__).resolve().parent / "helpers"
#: every file of the prompt corpus (relative to codeverse3d/prompts/), read off the tree —
#: test_files.test_every_prompt_file_is_reached says how the package loads each one
@@ -65,22 +64,14 @@ def labelled_files(rel: str, tag: str = "js") -> dict[str, str]:
# ----------------------------------------------------------------------- runners
def run_node_module(tmp_path: Path, code: str, timeout: int = 120) -> str:
- """Write ``code`` as an ES module next to a node_modules symlink and run it."""
- node = shutil.which(get_settings().binaries.node) or shutil.which("node")
- if node is None:
- pytest.skip("node not available")
- link = tmp_path / "node_modules"
- if not link.exists():
- link.symlink_to(RUNTIME_JS / "node_modules")
+ """Write ``code`` as an ES module and run it under the harness's ``three`` resolve hook."""
for lib in (tmp_path / "lib", tmp_path / "src" / "lib"): # `./lib/...` from run.mjs and from src/*.js
- if not lib.exists(): # a COPY: a symlinked lib resolves its bare `three` from the starter dir, which has none
+ if not lib.exists():
shutil.copytree(STARTER_LIB, lib)
mod = tmp_path / "run.mjs"
mod.write_text(code)
- proc = subprocess.run(
- [node, "run.mjs"], cwd=tmp_path, capture_output=True, text=True, timeout=timeout
- )
- assert proc.returncode == 0, f"node failed:\nSTDOUT:{proc.stdout[-3000:]}\nSTDERR:{proc.stderr[-3000:]}"
+ proc = run_node(mod, cwd=tmp_path, timeout_s=timeout, three_hook=True, check=False)
+ assert proc.rc == 0, f"node failed:\nSTDOUT:{proc.stdout[-3000:]}\nSTDERR:{proc.stderr[-3000:]}"
return proc.stdout
diff --git a/harness/tests/prompts/helpers/glsl_compile.mjs b/harness/tests/prompts/helpers/glsl_compile.mjs
deleted file mode 100644
index 91d1f6ed..00000000
--- a/harness/tests/prompts/helpers/glsl_compile.mjs
+++ /dev/null
@@ -1,101 +0,0 @@
-// Compile every `make*Material(THREE, opts)` export of a module in headless Chrome (real WebGL)
-// and report shader errors. Usage: node glsl_compile.mjs <dir-with-node_modules-and-module> <module.mjs>
-// Prints one JSON line: {ok, gpu, results:[{name, mesh:{ok, errors}, instanced:{ok, errors}}]}
-import http from 'node:http';
-import fs from 'node:fs';
-import path from 'node:path';
-import { createRequire } from 'node:module';
-import url from 'node:url';
-
-const RUNTIME = process.env.C3D_RUNTIME_JS
- || path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '../../../runtime_js');
-const require_ = createRequire(path.join(RUNTIME, 'package.json'));
-const puppeteer = require_('puppeteer');
-
-const [,, rootArg, moduleRel] = process.argv;
-const root = path.resolve(rootArg);
-const MIME = { '.js': 'text/javascript', '.mjs': 'text/javascript', '.html': 'text/html', '.json': 'application/json', '.glb': 'model/gltf-binary' };
-
-const html = `<!doctype html><html><head><meta charset="utf-8">
-<script type="importmap">{"imports":{"three":"/node_modules/three/build/three.module.js","three/addons/":"/node_modules/three/examples/jsm/"}}</script>
-</head><body><script type="module">
-import * as THREE from 'three';
-import * as mod from '/${moduleRel}';
-const out = { results: [], gpu: '' };
-const canvas = document.createElement('canvas'); canvas.width = 256; canvas.height = 256; document.body.appendChild(canvas);
-const renderer = new THREE.WebGLRenderer({ canvas, antialias: false, logarithmicDepthBuffer: true });
-renderer.setSize(256, 256, false);
-renderer.outputColorSpace = THREE.SRGBColorSpace; renderer.toneMapping = THREE.ACESFilmicToneMapping;
-renderer.shadowMap.enabled = true;
-const gl = renderer.getContext(); const ext = gl.getExtension('WEBGL_debug_renderer_info');
-out.gpu = ext ? gl.getParameter(ext.UNMASKED_RENDERER_WEBGL) : 'unknown';
-let errs = [];
-renderer.debug.onShaderError = (gl, program, vs, fs) => {
- const vlog = gl.getShaderInfoLog(vs) || '', flog = gl.getShaderInfoLog(fs) || '', plog = gl.getProgramInfoLog(program) || '';
- errs.push(('VS:' + vlog + ' FS:' + flog + ' P:' + plog).slice(0, 1500));
-};
-const camera = new THREE.PerspectiveCamera(50, 1, 0.1, 500); camera.position.set(3, 3, 5); camera.lookAt(0, 0, 0);
-const names = Object.keys(mod).filter(k => /^make\\w*Material$/.test(k) && typeof mod[k] === 'function');
-for (const name of names) {
- const res = { name };
- for (const kind of ['mesh', 'instanced']) {
- errs = [];
- const scene = new THREE.Scene(); scene.fog = new THREE.Fog(0x8899aa, 5, 50);
- scene.add(new THREE.HemisphereLight(0xffffff, 0x444444, 1), new THREE.DirectionalLight(0xffffff, 1));
- let mat;
- try { mat = mod[name](THREE, {}); } catch (e) { res[kind] = { ok: false, errors: ['factory threw: ' + (e && e.message)] }; continue; }
- const geo = new THREE.PlaneGeometry(2, 2, 4, 4);
- const obj = kind === 'mesh' ? new THREE.Mesh(geo, mat) : new THREE.InstancedMesh(geo, mat, 4);
- if (kind === 'instanced') { const m = new THREE.Matrix4(); for (let i = 0; i < 4; i++) { m.makeTranslation(i, 0, 0); obj.setMatrixAt(i, m); } }
- scene.add(obj);
- try {
- if (mat.uniforms && mat.uniforms.uTime) mat.uniforms.uTime.value = 1.0;
- if (typeof mat.userData?.update === 'function') mat.userData.update(1.0, 0.016);
- renderer.compile(scene, camera); renderer.render(scene, camera);
- } catch (e) { errs.push('render threw: ' + (e && e.message)); }
- res[kind] = { ok: errs.length === 0, errors: errs.slice() };
- }
- out.results.push(res);
-}
-out.ok = out.results.every(r => r.mesh.ok && r.instanced.ok) && out.results.length > 0;
-window.__result = out;
-</script></body></html>`;
-
-const server = http.createServer((req, res) => {
- const url = decodeURIComponent(req.url.split('?')[0]);
- if (url === '/' || url === '/index.html') { res.writeHead(200, { 'content-type': 'text/html' }); res.end(html); return; }
- const abs = path.join(root, url);
- if (!abs.startsWith(root) || !fs.existsSync(abs) || !fs.statSync(abs).isFile()) { res.writeHead(404); res.end('nf'); return; }
- res.writeHead(200, { 'content-type': MIME[path.extname(abs)] || 'application/octet-stream' }); res.end(fs.readFileSync(abs));
-});
-await new Promise(r => server.listen(0, '127.0.0.1', r));
-const base = `http://127.0.0.1:${server.address().port}`;
-const GPU_ENV = { GALLIUM_DRIVER: 'd3d12', MESA_LOADER_DRIVER_OVERRIDE: 'd3d12', MESA_D3D12_DEFAULT_ADAPTER_NAME: 'NVIDIA' };
-const BASE = ['--no-sandbox', '--disable-setuid-sandbox', '--enable-webgl', '--ignore-gpu-blocklist'];
-async function launch(gpu) {
- return puppeteer.launch({ headless: true, protocolTimeout: 120000,
- args: gpu ? [...BASE, '--enable-gpu-rasterization', '--use-angle=gl-egl'] : [...BASE, '--enable-unsafe-swiftshader', '--use-angle=swiftshader'],
- env: gpu ? { ...process.env, ...GPU_ENV } : process.env });
-}
-let result = null, lastErr = '';
-for (const gpu of [true, false]) {
- let browser;
- try {
- browser = await launch(gpu);
- const page = await browser.newPage();
- const consoleErrs = [];
- page.on('console', m => { if (m.type() === 'error') consoleErrs.push(m.text().slice(0, 400)); });
- page.on('pageerror', e => consoleErrs.push('pageerror: ' + e.message));
- await page.goto(base + '/', { waitUntil: 'load', timeout: 60000 });
- await page.waitForFunction('window.__result !== undefined', { timeout: 90000 });
- result = await page.evaluate(() => window.__result);
- result.console = consoleErrs.filter(t => !/GL_INVALID|THREE\.WebGLProgram/.test(t));
- result.mode = gpu ? 'gpu' : 'cpu';
- if (!result.gpu || /swiftshader|llvmpipe|software/i.test(result.gpu)) { if (gpu) { await browser.close(); continue; } }
- break;
- } catch (e) { lastErr = String(e && e.message); } finally { if (browser) await browser.close().catch(() => {}); }
-}
-server.close();
-if (!result) { console.log(JSON.stringify({ ok: false, error: lastErr || 'no result' })); process.exit(2); }
-console.log(JSON.stringify(result));
-process.exit(result.ok ? 0 : 1);
diff --git a/harness/tests/prompts/test_snippets_run.py b/harness/tests/prompts/test_snippets_run.py
index 6ac477db..1293fc14 100644
--- a/harness/tests/prompts/test_snippets_run.py
+++ b/harness/tests/prompts/test_snippets_run.py
@@ -8,14 +8,9 @@
from __future__ import annotations
-import json
-import shutil
-import subprocess
-
import pytest
from tests.prompts.conftest import (
- HELPERS,
blocks,
js_prelude,
labelled_files,
@@ -23,6 +18,7 @@ from tests.prompts.conftest import (
run_node_module,
strip_imports_exports,
)
+from tests.scene_runtime.lib._probe import compile_scene
# --------------------------------------------------------------------- cadquery
@@ -217,29 +213,19 @@ console.log('SCENE_OK', r.cameras.map((c) => c.name).join(','));
@pytest.mark.node
-def test_scene_cookbook_sky_shader_compiles(tmp_path) -> None:
- """The inline sky-dome ShaderMaterial from the scene cookbook must also compile."""
- node = shutil.which("node")
- if node is None:
- pytest.skip("node not available")
+def test_scene_cookbook_sky_shader_compiles() -> None:
+ """The inline sky-dome ShaderMaterial from the scene cookbook must also compile — as a Mesh
+ (the dome) and as an InstancedMesh, through the production host (``check_shaders.mjs``)."""
body = "".join("\n" + strip_imports_exports(b) for b in blocks("scene_threejs/cookbook.md", "js")[:2])
- mod = (
+ scene = (
"import * as THREE from 'three';\n" + body
- + "\nexport function makeSkyMaterial(THREE_) {\n"
- " const s = new THREE_.Scene();\n"
- " buildEnv({ THREE: THREE_, scene: s });\n"
- " return s.getObjectByName('SkyDome').material;\n"
+ + "\nexport function createScene() {\n"
+ " const scene = new THREE.Scene();\n"
+ " const env = buildEnv({ THREE, scene });\n"
+ " const sky = scene.getObjectByName('SkyDome');\n"
+ " scene.add(new THREE.InstancedMesh(new THREE.PlaneGeometry(2, 2), sky.material, 4));\n"
+ " return { scene, cameras: [{ name: 'a', position: [3, 3, 5], lookAt: [0, 0, 0] }], update: env.update };\n"
"}\n"
)
- link = tmp_path / "node_modules"
- if not link.exists():
- from tests.prompts.conftest import RUNTIME_JS
-
- link.symlink_to(RUNTIME_JS / "node_modules")
- (tmp_path / "sky_mod.mjs").write_text(mod)
- proc = subprocess.run(
- [node, str(HELPERS / "glsl_compile.mjs"), str(tmp_path), "sky_mod.mjs"],
- capture_output=True, text=True, timeout=300,
- )
- report = json.loads(proc.stdout.strip().splitlines()[-1])
- assert report.get("ok"), f"sky shader failed to compile: {json.dumps(report)[:3000]}"
+ code, out = compile_scene(scene)
+ assert code == 0, f"sky shader failed to compile:\n{out}"
diff --git a/harness/tests/scene_runtime/conftest.py b/harness/tests/scene_runtime/conftest.py
index 963bea8d..92724358 100644
--- a/harness/tests/scene_runtime/conftest.py
+++ b/harness/tests/scene_runtime/conftest.py
@@ -4,13 +4,14 @@ from __future__ import annotations
import json
import shutil
-import subprocess
+import tempfile
from pathlib import Path
import pytest
from codeverse3d.config import get_settings
from codeverse3d.languages.scene_threejs import write_example
+from codeverse3d.spatial.node import run_node
from codeverse3d.workspace import Workspace
RUNTIME_JS = get_settings().runtime_js_dir()
@@ -42,10 +43,11 @@ def starter_ws(tmp_path: Path) -> Workspace:
return w
-def run_node_json(script_body: str, *, cwd: Path = RUNTIME_JS, timeout: int = 60) -> dict | list:
- """Run an inline ESM snippet under runtime_js and parse its last stdout line as JSON."""
- proc = subprocess.run(
- ["node", "--input-type=module", "-e", script_body], cwd=str(cwd), capture_output=True, text=True, timeout=timeout,
- )
- assert proc.returncode == 0, proc.stderr
+def run_node_json(script_body: str, *, timeout: int = 60) -> dict | list:
+ """Run an ESM snippet whose ``./lib/`` is runtime_js/lib and parse its last stdout line as JSON."""
+ with tempfile.TemporaryDirectory(prefix="c3d-node-json-") as tmp:
+ (Path(tmp) / "lib").symlink_to(RUNTIME_JS / "lib")
+ (Path(tmp) / "probe.mjs").write_text(script_body, encoding="utf-8")
+ proc = run_node(Path(tmp) / "probe.mjs", cwd=tmp, timeout_s=timeout, three_hook=True, check=False)
+ assert proc.rc == 0, proc.stderr_tail
return json.loads(proc.stdout.strip().splitlines()[-1])
diff --git a/harness/tests/scene_runtime/lib/_probe.py b/harness/tests/scene_runtime/lib/_probe.py
index 3bd91a69..d37dcb51 100644
--- a/harness/tests/scene_runtime/lib/_probe.py
+++ b/harness/tests/scene_runtime/lib/_probe.py
@@ -15,19 +15,17 @@ two hooks patchStandard injects into, and ``compile`` / ``count`` / ``mains`` /
from __future__ import annotations
import json
-import os
import re
import shutil
-import subprocess
import tempfile
from pathlib import Path
import pytest
+from codeverse3d.spatial.node import node_modules_dir, run_node, runtime_js_dir
+
HARNESS = Path(__file__).resolve().parents[3]
LIB_DIR = HARNESS / "codeverse3d" / "languages" / "scene_threejs" / "starter" / "src" / "lib"
-RESOLVE_HOOK = HARNESS / "runtime_js" / "lib" / "resolve_three.mjs"
-NODE_MODULES = HARNESS / "runtime_js" / "node_modules"
pytestmark = pytest.mark.node
@@ -70,7 +68,7 @@ def _main_body(src: str) -> str:
def _three_ready() -> None:
- if not (NODE_MODULES / "three" / "package.json").is_file():
+ if not (node_modules_dir() / "three" / "package.json").is_file():
pytest.skip("runtime_js/node_modules/three missing (npm ci in runtime_js)")
@@ -89,12 +87,8 @@ def measure(script: str, libs: tuple[str, ...] = (), *, timeout_s: float = 120.0
root = Path(tmp)
_stage(root, libs)
(root / "probe.mjs").write_text(script, encoding="utf-8")
- out = subprocess.run(
- ["node", "--import", str(RESOLVE_HOOK), str(root / "probe.mjs")],
- capture_output=True, text=True, timeout=timeout_s, cwd=str(root),
- env={**os.environ, "NODE_PATH": str(NODE_MODULES)},
- )
- assert out.returncode == 0, out.stderr[-2000:]
+ out = run_node(root / "probe.mjs", cwd=root, timeout_s=timeout_s, three_hook=True, check=False)
+ assert out.rc == 0, out.stderr_tail
lines = [ln for ln in out.stdout.strip().splitlines() if ln.strip()]
assert lines, "probe printed nothing"
return json.loads(lines[-1])
@@ -140,12 +134,9 @@ def compile_scene(scene_src: str, libs: tuple[str, ...] = (), *,
(root / "src" / "scene.js").write_text(scene_src, encoding="utf-8")
for rel, text in (extra or {}).items():
(root / "src" / rel).write_text(text, encoding="utf-8")
- out = subprocess.run(
- ["node", "--import", str(RESOLVE_HOOK), str(HARNESS / "runtime_js" / "check_shaders.mjs"),
- "--ws", str(root), "--timeout-ms", str(int(timeout_s * 1000))]
- + (["--module", audit_module] if audit_module else [])
- + (["--out", str(report)] if report else []),
- capture_output=True, text=True, timeout=timeout_s + 30, cwd=str(HARNESS / "runtime_js"),
- env={**os.environ, "NODE_PATH": str(NODE_MODULES)},
- )
- return out.returncode, (out.stdout + "\n" + out.stderr)[-4000:]
+ out = run_node(runtime_js_dir() / "check_shaders.mjs",
+ ["--ws", str(root), "--timeout-ms", str(int(timeout_s * 1000))]
+ + (["--module", audit_module] if audit_module else [])
+ + (["--out", str(report)] if report else []),
+ cwd=runtime_js_dir(), timeout_s=timeout_s + 30, three_hook=True, check=False)
+ return out.rc, (out.stdout + "\n" + out.stderr)[-4000:]