from __future__ import annotations import importlib.util import json import tempfile import unittest from pathlib import Path WORKER_PATH = ( Path(__file__).resolve().parents[1] / "windows-node" / "origin-worker" / "worker.py" ) SPEC = importlib.util.spec_from_file_location("zcbot_origin_worker", WORKER_PATH) assert SPEC and SPEC.loader worker = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(worker) class OriginWorkerUnitTests(unittest.TestCase): def test_csv_and_json_inputs_are_read_without_origin(self) -> None: with tempfile.TemporaryDirectory() as directory: root = Path(directory) csv_path = root / "input.csv" csv_path.write_text("x,y\n1,2\n3,4\n", encoding="utf-8") self.assertEqual(worker._read_rows(csv_path, None), (["x", "y"], [["1", "2"], ["3", "4"]])) json_path = root / "input.json" json_path.write_text(json.dumps([{"x": 1, "y": 2}, {"x": 3, "y": 4}]), encoding="utf-8") self.assertEqual(worker._read_rows(json_path, None), (["x", "y"], [[1, 2], [3, 4]])) def test_manifest_uses_stable_id_and_streaming_digest(self) -> None: with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "plot-spec.json" path.write_text("{}", encoding="utf-8") manifest = worker._manifest(path, "application/json") self.assertEqual(manifest["artifact_id"], "plot_spec") self.assertEqual(manifest["sha256"], worker._file_sha256(path)) self.assertEqual(manifest["size_bytes"], 2) def test_axis_title_includes_units(self) -> None: self.assertEqual(worker._axis_title({"title": "Stress", "unit": "MPa"}, "Y"), "Stress (MPa)") self.assertEqual(worker._axis_title(None, "Time"), "Time") if __name__ == "__main__": unittest.main()