Files
linuxcncWebWASM/scripts/test_protocol.py

69 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""Smoke tests for the fixed protocol ABI and its generated WASM exports."""
from __future__ import annotations
import subprocess
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
class ProtocolTests(unittest.TestCase):
def test_contract_declares_versions_and_errors(self):
header = (ROOT / "wasm-core/include/sim_protocol.h").read_text(encoding="utf-8")
for token in ("SIM_PROTOCOL_ERR_SCHEMA", "SIM_PROTOCOL_ERR_SEQUENCE", "sim_protocol_accept", "sim_protocol_last_seq"):
self.assertIn(token, header)
def test_native_protocol_binary_passes(self):
binary = ROOT / "build-native/sim_protocol_test"
if not binary.is_file():
self.skipTest("native build has not been generated")
completed = subprocess.run([str(binary)], cwd=ROOT, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.assertEqual(completed.returncode, 0, completed.stderr.decode(errors="replace"))
def test_native_event_binary_passes(self):
binary = ROOT / "build-native/sim_event_test"
if not binary.is_file():
self.skipTest("native build has not been generated")
completed = subprocess.run([str(binary)], cwd=ROOT, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.assertEqual(completed.returncode, 0, completed.stderr.decode(errors="replace"))
def test_wasm_exports_protocol_functions(self):
module = ROOT / "build-wasm/linuxcnc-sim-core.js"
if not module.is_file():
self.skipTest("WASM build has not been generated")
text = module.read_text(encoding="utf-8")
for symbol in ("sim_protocol_reset", "sim_protocol_accept", "sim_protocol_last_seq", "sim_event_append", "sim_event_export"):
self.assertIn(symbol, text)
def test_worker_session_smoke(self):
completed = subprocess.run(
["node", "web/worker/test_sim_session.mjs"],
cwd=ROOT,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
self.assertEqual(completed.returncode, 0, completed.stderr)
self.assertIn("sim_session=passed", completed.stdout)
def test_real_wasm_session_smoke(self):
completed = subprocess.run(
["node", "web/worker/test_wasm_session.mjs"],
cwd=ROOT,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
self.assertEqual(completed.returncode, 0, completed.stderr)
self.assertIn("wasm_session=passed", completed.stdout)
if __name__ == "__main__":
unittest.main()