Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

circuitjs: cover the verdict to 100%, judge non-UTF-8 files instead of crashing

dee473fc4e15e51
diff --git a/store/agents/circuitjs/toolchain/test_verdict.py b/store/agents/circuitjs/toolchain/test_verdict.py
index 5af60e0f7..ae871dc75 100644
--- a/store/agents/circuitjs/toolchain/test_verdict.py
+++ b/store/agents/circuitjs/toolchain/test_verdict.py
@@ -6,12 +6,22 @@ ever calls one of those an error, the checker is wrong, not CircuitJS1.
"""
from __future__ import annotations
+import contextlib
+import io
+import json
+import os
+import runpy
+import sys
+import tempfile
import unittest
from pathlib import Path
-
-import verdict
+from unittest import mock
HERE = Path(__file__).resolve().parent
+sys.path.insert(0, str(HERE)) # `python3 -m unittest toolchain/test_verdict.py` from the package
+
+import verdict # noqa: E402
+
EXAMPLES = HERE.parent / "skills" / "circuitjs" / "examples"
TEMPLATE = HERE.parent / "template" / "circuit.txt"
@@ -149,6 +159,107 @@ class TestWarnings(unittest.TestCase):
self.assertIn("no_scope", kinds(v, "info"))
+class TestLineForms(unittest.TestCase):
+ def test_blank_lines_are_skipped_and_only_the_first_options_line_counts(self):
+ # The second `$` line would switch to the 8-px grid; it is ignored, so 344 stays off-grid.
+ text = RC.replace("r 176 112 336 112", "r 176 112 344 112") + "\n\n$ 3 0.000005 10 50 5 43\n"
+ v = verdict.judge(text, "circuit.txt")
+ self.assertEqual([], kinds(v, "error"))
+ self.assertIn("grid", kinds(v, "warning"))
+
+ def test_a_short_options_line_is_an_error(self):
+ v = verdict.judge(RC.replace("$ 1 0.000005 10.20027730826997 50 5 43 5e-11", "$ 1 0.000005"), "circuit.txt")
+ self.assertFalse(v["ready"])
+ self.assertEqual(["header"], kinds(v, "error"))
+
+ def test_a_non_numeric_options_flag_reads_as_zero_the_16px_grid(self):
+ text = RC.replace("$ 1 ", "$ x ").replace("r 176 112 336 112", "r 176 112 344 112")
+ self.assertIn("grid", kinds(verdict.judge(text, "circuit.txt"), "warning"))
+
+ def test_a_short_scope_line(self):
+ v = verdict.judge(RC.replace("o 2 64 0 2 5 0.0125", "o 2 64"), "circuit.txt")
+ self.assertEqual(["scope"], kinds(v, "error"))
+ self.assertIn("a scope line is", v["findings"][0]["message"])
+
+ def test_a_scope_that_names_no_element_index(self):
+ v = verdict.judge(RC.replace("o 2 64 0 2 5 0.0125", "o R1 64 0 2 5 0.0125"), "circuit.txt")
+ self.assertEqual(["scope"], kinds(v, "error"))
+ self.assertIn("'R1'", v["findings"][0]["message"])
+
+ def test_a_short_slider_line(self):
+ v = verdict.judge(RC + "38\n", "circuit.txt")
+ self.assertEqual(["slider"], kinds(v, "error"))
+
+ def test_a_slider_that_names_no_element_index(self):
+ v = verdict.judge(RC + "38 R1 F0 0 1 101 Resistance 0\n", "circuit.txt")
+ self.assertEqual(["slider"], kinds(v, "error"))
+ self.assertIn("'R1'", v["findings"][0]["message"])
+
+ def test_a_shared_slider_may_carry_minus_one(self):
+ v = verdict.judge(RC + "38 1 F1 0 1 101 -1 Shared 0\n", "circuit.txt")
+ self.assertNotIn("slider_label", kinds(v, "warning"))
+
+ def test_unreadable_slider_flags_read_as_unshared(self):
+ v = verdict.judge(RC + "38 1 Fx 0 1 101 -1 Resistance 0\n", "circuit.txt")
+ self.assertIn("slider_label", kinds(v, "warning"))
+
+
+class TestMain(unittest.TestCase):
+ """main(): the file it reads, the verdict it writes, what it prints, the exit code."""
+
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.ws = Path(self.tmp.name).resolve()
+ self.addCleanup(self.tmp.cleanup)
+
+ def run_main(self, *args: str) -> tuple[int, str, dict]:
+ out = io.StringIO()
+ with mock.patch.object(verdict, "WS", self.ws), contextlib.redirect_stdout(out):
+ code = verdict.main(["verdict.py", *args])
+ return code, out.getvalue(), json.loads((self.ws / ".harness" / "verdict.json").read_text())
+
+ def test_the_default_file_is_circuit_txt(self):
+ (self.ws / "circuit.txt").write_text(RC)
+ code, printed, written = self.run_main()
+ self.assertEqual(0, code)
+ self.assertTrue(written["ready"])
+ self.assertEqual("circuit.txt", written["artifact"])
+ self.assertTrue(printed.startswith("ready · circuit.txt · 5 elements · 1 scope · running"))
+ self.assertIn(" info the simulation runs in the pane", printed)
+
+ def test_a_relative_and_an_absolute_file(self):
+ (self.ws / "filters").mkdir()
+ (self.ws / "filters" / "lowpass.txt").write_text(RC)
+ self.assertEqual("filters/lowpass.txt", self.run_main("filters/lowpass.txt")[2]["artifact"])
+ self.assertEqual("filters/lowpass.txt", self.run_main(str(self.ws / "filters" / "lowpass.txt"))[2]["artifact"])
+
+ def test_a_missing_file_is_not_ready(self):
+ code, printed, written = self.run_main()
+ self.assertEqual(1, code)
+ self.assertFalse(written["ready"])
+ self.assertIn("not ready · circuit.txt · 1 error", printed)
+ self.assertIn(" error circuit.txt is not there yet", printed)
+
+ def test_bytes_that_are_not_utf8_are_judged_not_a_crash(self):
+ # A Latin-1 µ in a text element: before the fix read_text() raised UnicodeDecodeError, no
+ # verdict was written and the header kept whatever the last good pass said.
+ (self.ws / "circuit.txt").write_bytes(RC.encode() + b"x 16 16 64 16 0 12 10\xb5F cap\n")
+ code, _, written = self.run_main()
+ self.assertEqual(0, code)
+ self.assertTrue(written["ready"])
+
+ def test_run_as_a_script(self):
+ (self.ws / "circuit.txt").write_text("r 0 0 16 0 0 1000\n")
+ out = io.StringIO()
+ with mock.patch.dict(os.environ, {"HARNESS_WORKSPACE": str(self.ws)}), \
+ mock.patch.object(sys, "argv", ["verdict.py"]), contextlib.redirect_stdout(out), \
+ self.assertRaises(SystemExit) as exit_:
+ runpy.run_path(str(HERE / "verdict.py"), run_name="__main__")
+ self.assertEqual(1, exit_.exception.code)
+ self.assertIn("not ready", out.getvalue())
+ self.assertFalse(json.loads((self.ws / ".harness" / "verdict.json").read_text())["ready"])
+
+
class TestShape(unittest.TestCase):
def test_verdict_conforms_to_spec_1(self):
v = verdict.judge(RC, "circuit.txt")
diff --git a/store/agents/circuitjs/toolchain/verdict.py b/store/agents/circuitjs/toolchain/verdict.py
index f9338893d..73e186de0 100644
--- a/store/agents/circuitjs/toolchain/verdict.py
+++ b/store/agents/circuitjs/toolchain/verdict.py
@@ -304,7 +304,9 @@ def main(argv: list[str]) -> int:
target = target if target.is_absolute() else WS / target
rel = os.path.relpath(target, WS)
try:
- text = target.read_text()
+ # As the pane reads it (served as UTF-8, decoded with replacement): a stray byte is a line
+ # to judge, not a crash that leaves the last verdict standing.
+ text = target.read_text(encoding="utf-8", errors="replace")
except OSError:
text = None
verdict = judge(text, rel)