Fix test_sx_js: temp file for large scripts, globalThis for Node file mode

sx-browser.js grew past OS arg length limit for node -e. Write to
temp file instead. Also fix Sx global scope: Node file mode sets
`this` to module.exports, not globalThis, so the IIFE wrapper needs
.call(globalThis) to make Sx accessible to sx-test.js.

855 passed (2 pre-existing empty .sx file failures).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 10:06:00 +00:00
parent 8366088ee1
commit e09bc3b601

View File

@@ -20,19 +20,39 @@ SX_TEST_JS = Path(__file__).resolve().parents[2] / "static" / "scripts" / "sx-te
def _js_render(sx_text: str, components_text: str = "") -> str:
"""Run sx.js + sx-test.js in Node and return the renderToString result."""
# Build a small Node script
import tempfile, os
# Build a small Node script that requires the source files
script = f"""
global.document = undefined; // no DOM needed for string render
{SX_JS.read_text()}
globalThis.document = undefined; // no DOM needed for string render
// sx.js IIFE uses (typeof window !== "undefined" ? window : this).
// In Node file mode, `this` is module.exports, not globalThis.
// Patch: make the IIFE target globalThis so Sx is accessible.
var _origThis = this;
Object.defineProperty(globalThis, 'document', {{ value: undefined, writable: true }});
(function() {{
var _savedThis = globalThis;
{SX_JS.read_text()}
// Hoist Sx from module.exports to globalThis if needed
if (typeof Sx === 'undefined' && typeof module !== 'undefined' && module.exports && module.exports.Sx) {{
globalThis.Sx = module.exports.Sx;
}}
}}).call(globalThis);
{SX_TEST_JS.read_text()}
if ({json.dumps(components_text)}) Sx.loadComponents({json.dumps(components_text)});
var result = Sx.renderToString({json.dumps(sx_text)});
process.stdout.write(result);
"""
result = subprocess.run(
["node", "-e", script],
capture_output=True, text=True, timeout=5,
)
# Write to temp file to avoid OS arg length limits
fd, tmp = tempfile.mkstemp(suffix=".js")
try:
with os.fdopen(fd, "w") as f:
f.write(script)
result = subprocess.run(
["node", tmp],
capture_output=True, text=True, timeout=5,
)
finally:
os.unlink(tmp)
if result.returncode != 0:
pytest.fail(f"Node.js error:\n{result.stderr}")
return result.stdout