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.

12.5 KB
Raw Blame
"""python3 -m unittest toolchain/test_verdict.py — what the verdict says about a track."""
from __future__ import annotations

import contextlib
import io
import json
import os
import runpy
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
from unittest import mock

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))

import verdict  # noqa: E402

TEMPLATE = HERE.parent / "template" / "track.strudel"
GOOD = 'setcpm(120/4)\nstack(\n  s("sbd*4").decay(0.2),\n  note("<c2 g1>(3,8)").s("sawtooth").lpf(800)\n)\n'


def kinds(result: dict, severity: str) -> list[str]:
    return [f["kind"] for f in result["findings"] if f["severity"] == severity]


class Judging(unittest.TestCase):
    def test_a_synth_track_is_ready_and_plays_offline(self) -> None:
        result = verdict.judge(GOOD, "track.strudel")
        self.assertTrue(result["ready"], result["findings"])
        self.assertEqual(result["artifact"], "track.strudel")
        self.assertIn("plays offline", result["summary"])
        self.assertEqual([p["state"] for p in result["phases"]], ["done", "done", "done"])

    def test_an_empty_file_is_not_written_yet(self) -> None:
        result = verdict.judge("\n// nothing here yet\n\n", "track.strudel")
        self.assertFalse(result["ready"])
        self.assertIn("empty", kinds(result, "error"))
        self.assertEqual([p["state"] for p in result["phases"]], ["active", "pending", "pending"])

    def test_an_unclosed_bracket_is_an_error_with_a_line(self) -> None:
        result = verdict.judge('stack(\n  s("sbd*4"),\n  note("c2")\n', "track.strudel")
        self.assertFalse(result["ready"])
        self.assertIn("syntax", kinds(result, "error"))
        self.assertTrue(any("line 1" in f["message"] for f in result["findings"]))

    def test_a_stray_bracket_is_an_error(self) -> None:
        result = verdict.judge('s("sbd*4"))\n', "track.strudel")
        self.assertFalse(result["ready"])
        self.assertTrue(any("stray )" in f["message"] for f in result["findings"]))

    def test_an_unclosed_quote_is_an_error(self) -> None:
        result = verdict.judge('s("sbd*4)\n', "track.strudel")
        self.assertFalse(result["ready"])
        self.assertTrue(any('" string is never closed' in f["message"] for f in result["findings"]))

    def test_brackets_inside_strings_and_comments_do_not_count(self) -> None:
        source = '// stack( [ {\n/* ) ] } */\ns("[bd sd]*2, white*8").gain("[0.3 0.14]*4")\n'
        result = verdict.judge(source, "track.strudel")
        self.assertEqual(kinds(result, "error"), [])

    @unittest.skipIf(subprocess.run(["node", "--version"], capture_output=True).returncode != 0, "node is not on PATH")
    def test_node_catches_what_the_bracket_scan_cannot(self) -> None:
        # Balanced brackets, balanced quotes, and still not a program.
        result = verdict.judge('note("c3") ,, .s("sawtooth")\n', "track.strudel")
        self.assertFalse(result["ready"])
        self.assertIn("syntax", kinds(result, "error"))
        self.assertTrue(any("node could not parse" in f["message"] for f in result["findings"]))

    @unittest.skipIf(subprocess.run(["node", "--version"], capture_output=True).returncode != 0, "node is not on PATH")
    def test_node_allows_top_level_await_the_way_the_repl_does(self) -> None:
        self.assertEqual(verdict.node_check('await samples("github:x/y")\ns("bd")\n'), [])

    def test_import_is_refused(self) -> None:
        result = verdict.judge('import { x } from "y"\n' + GOOD, "track.strudel")
        self.assertFalse(result["ready"])
        self.assertIn("scope", kinds(result, "error"))

    def test_prose_without_a_pattern_call_is_not_a_track(self) -> None:
        result = verdict.judge("const bpm = 120\n", "track.strudel")
        self.assertFalse(result["ready"])
        self.assertIn("pattern", kinds(result, "error"))

    def test_sample_sounds_are_a_warning_not_an_error(self) -> None:
        result = verdict.judge('setcpm(30)\ns("bd sd, hh*8").bank("RolandTR909")\n', "track.strudel")
        self.assertTrue(result["ready"], result["findings"])
        self.assertIn("network", kinds(result, "warning"))
        message = next(f["message"] for f in result["findings"] if f["kind"] == "network")
        self.assertIn("bd", message)
        self.assertIn("internet", result["summary"])

    def test_synth_aliases_do_not_trip_the_network_warning(self) -> None:
        result = verdict.judge('setcps(0.5)\ns("<saw sqr tri sin supersaw pulse white sbd>*4")\n', "track.strudel")
        self.assertEqual(kinds(result, "warning"), [])

    def test_a_missing_tempo_is_only_a_note(self) -> None:
        result = verdict.judge('s("sbd*4")\n', "track.strudel")
        self.assertTrue(result["ready"])
        self.assertIn("tempo", kinds(result, "info"))

    def test_an_unclosed_block_comment_is_an_error(self) -> None:
        result = verdict.judge('s("sbd*4")\n/* the bass\n', "track.strudel")
        self.assertEqual(kinds(result, "error"), ["syntax"])
        self.assertIn("line 2: a /* comment is never closed", result["findings"][0]["message"])

    def test_a_block_comment_keeps_the_line_count(self) -> None:
        result = verdict.judge('/* one\ntwo */\ns("sbd"))\n', "track.strudel")
        self.assertIn("line 3: a stray )", [f["message"] for f in result["findings"]])

    def test_a_string_cut_off_by_the_end_of_the_file(self) -> None:
        result = verdict.judge('s("sbd*4', "track.strudel")
        self.assertIn('line 1: a " string is never closed', [f["message"] for f in result["findings"]])

    def test_an_escaped_quote_does_not_close_the_string(self) -> None:
        result = verdict.judge('setcps(1)\ns("sbd \\" sbd")\n', "track.strudel")
        self.assertTrue(result["ready"], result["findings"])

    def test_a_template_literal_spans_lines(self) -> None:
        result = verdict.judge('setcps(1)\ns(`sbd\n  sbd`)\n)\n', "track.strudel")
        self.assertIn("line 4: a stray )", [f["message"] for f in result["findings"]])

    def test_a_line_continuation_inside_a_string_keeps_the_line_count(self) -> None:
        # A backslash-newline continues a '…' or "…" string onto the next line. Before the fix the
        # escape skipped the newline without counting it, so every later finding was a line early.
        result = verdict.judge('s("sbd \\\nsbd")\n)\n', "track.strudel")
        self.assertIn("line 3: a stray )", [f["message"] for f in result["findings"]])

    def test_a_commented_out_sample_is_not_a_network_dependency(self) -> None:
        # Before the fix the sound scan read the raw text, comments included, so a voice the agent
        # had commented out still said "this track needs the internet".
        result = verdict.judge('setcps(1)\n// s("bd*4")\n/* s("hh*8") */\ns("sbd*4")\n', "track.strudel")
        self.assertEqual(kinds(result, "warning"), [])
        self.assertIn("plays offline", result["summary"])

    def test_without_node_the_scan_is_the_only_syntax_check(self) -> None:
        for error in (OSError("node"), subprocess.TimeoutExpired("node", 20)):
            with self.subTest(error=type(error).__name__), \
                    mock.patch.object(verdict.subprocess, "run", side_effect=error):
                self.assertEqual(verdict.node_check('s("sbd")'), [])

    def test_no_node_on_path_or_in_harness_is_the_scan_alone(self) -> None:
        done = subprocess.CompletedProcess(["with-node.sh"], 127, stdout="", stderr="miss node >= 18, and Harness's own Node is not in …\n")
        with mock.patch.object(verdict.subprocess, "run", return_value=done) as run:
            self.assertEqual(verdict.node_check('s("sbd")'), [])
        self.assertEqual(run.call_args.args[0], [str(HERE / "with-node.sh"), "node", "--check", "--input-type=module"])

    def test_a_node_failure_without_a_syntax_error_line(self) -> None:
        done = subprocess.CompletedProcess(["node"], 1, stdout="", stderr="node: bad option\n")
        with mock.patch.object(verdict.subprocess, "run", return_value=done):
            self.assertEqual(verdict.node_check('s("sbd")')[0]["message"],
                             "node could not parse the pattern: syntax error")

    def test_the_template_is_ready_and_offline(self) -> None:
        result = verdict.judge(TEMPLATE.read_text(), "track.strudel")
        self.assertTrue(result["ready"], result["findings"])
        self.assertEqual(kinds(result, "warning"), [])
        self.assertIn("plays offline", result["summary"])


class Writing(unittest.TestCase):
    def run_in(self, workspace: Path, *args: str) -> tuple[int, dict]:
        env = {**os.environ, "HARNESS_WORKSPACE": str(workspace)}
        done = subprocess.run([sys.executable, str(HERE / "verdict.py"), *args], env=env, capture_output=True, text=True)
        written = workspace / ".harness" / "verdict.json"
        return done.returncode, json.loads(written.read_text()) if written.exists() else {}

    def test_it_writes_the_verdict_beside_the_track(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            workspace = Path(tmp)
            (workspace / "track.strudel").write_text(GOOD)
            code, written = self.run_in(workspace)
            self.assertEqual(code, 0)
            self.assertEqual(written["spec"], 1)
            self.assertTrue(written["ready"])
            self.assertEqual(written["artifact"], "track.strudel")
            self.assertEqual(len(written["phases"]), 3)

    def test_a_missing_file_is_unreadable_not_a_crash(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            code, written = self.run_in(Path(tmp))
            self.assertEqual(code, 1)
            self.assertFalse(written["ready"])
            self.assertIn("unreadable", written["summary"])

    def test_it_judges_the_file_it_is_given(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            workspace = Path(tmp)
            (workspace / "b-side.strudel").write_text(GOOD)
            code, written = self.run_in(workspace, "b-side.strudel")
            self.assertEqual(code, 0)
            self.assertEqual(written["artifact"], "b-side.strudel")


class Main(unittest.TestCase):
    """main() in this process, so what it does is measured, not only what it leaves on disk."""

    def setUp(self) -> None:
        self.tmp = tempfile.TemporaryDirectory()
        self.addCleanup(self.tmp.cleanup)
        self.ws = Path(self.tmp.name).resolve()

    def 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_track_prints_the_summary_and_each_finding(self) -> None:
        (self.ws / "track.strudel").write_text('s("sbd*4")\n')
        code, printed, written = self.main()
        self.assertEqual(code, 0)
        self.assertEqual(written["artifact"], "track.strudel")
        self.assertEqual(printed.splitlines(), [
            "ready · track.strudel · 1 line · plays offline",
            "  info    no setcps/setcpm: Strudel runs at its default 0.5 cycles per second",
        ])

    def test_an_absolute_path_and_an_unreadable_one(self) -> None:
        (self.ws / "b.strudel").write_text(GOOD)
        self.assertEqual(self.main(str(self.ws / "b.strudel"))[2]["artifact"], "b.strudel")
        code, printed, written = self.main("missing.strudel")
        self.assertEqual(code, 1)
        self.assertEqual(written["summary"], "missing.strudel · unreadable")
        self.assertEqual([p["state"] for p in written["phases"]], ["active", "pending", "pending"])
        self.assertIn("  error   missing.strudel: ", printed)

    def test_bytes_that_are_not_utf8_are_judged_not_a_crash(self) -> None:
        # Before the fix read_text() raised UnicodeDecodeError: no verdict, the last one left standing.
        (self.ws / "track.strudel").write_bytes(b'setcps(1)\n// caf\xe9 set\ns("sbd*4")\n')
        code, _, written = self.main()
        self.assertEqual(code, 0)
        self.assertTrue(written["ready"])

    def test_run_as_a_script(self) -> None:
        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(exit_.exception.code, 1)
        self.assertIn("not ready · track.strudel · unreadable", out.getvalue())


if __name__ == "__main__":
    unittest.main()