Build and validate the reload state in one unit

This commit was merged in pull request #70.
This commit is contained in:
2026-09-27 16:50:32 +02:00
parent d4cd4ffeac
commit 63c37c54c9
4 changed files with 56 additions and 50 deletions
+4 -4
View File
@@ -436,19 +436,19 @@ class Reload(unittest.TestCase):
script = os.path.join(root, "loop-claude")
open(script, "w").write(loop.RELOAD_BASELINE.replace("import glob\n", "import glob\nimport no_such_module\n"))
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
loop.reload_if_edited("/r", "do it", cycle_number=1, failures=0)
loop.reload_if_edited(loop.LoopState(1, 0, "/r", "do it"))
execv.assert_not_called()
open(script, "w").write(loop.RELOAD_BASELINE.replace("RELOAD_FLAG = ", "RELOAD_FLAG = \"x\" or "))
environment = dict(os.environ)
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
loop.reload_if_edited("/r", "do it", cycle_number=1, failures=2)
loop.reload_if_edited(loop.LoopState(1, 2, "/r", "do it"))
execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, json.dumps({"cycle_number": 1, "failures": 2, "root": "/r", "task": "do it"})])
self.assertEqual(dict(os.environ), environment)
class Main(unittest.TestCase):
def test_exits_naming_the_reload_when_it_hands_over_anything_but_the_state_with_a_task(self):
for handed in (["3", "2", "/r", "do it"], ["3"], ["[1]"], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": ""})]):
for handed in (["3", "2", "/r", "do it"], ["3"], ["[1]"], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": ""})], [json.dumps({"cycle_number": "3", "failures": 0, "root": "/r", "task": "do it"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "", "task": "do it"})], [json.dumps({"cycle_number": 3, "failures": 0, "root": "/r", "task": "do it", "turns": 1})]):
with self.subTest(handed=handed), unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, *handed]), self.assertRaises(SystemExit) as stopped:
loop.main()
self.assertEqual(stopped.exception.code, f"the reload cannot read the loop's state from {handed}; rerun loop-claude <task>")
@@ -464,7 +464,7 @@ class Main(unittest.TestCase):
loop.main()
mocks[0].assert_not_called()
mocks[1].assert_called_once_with("/r")
mocks[2].assert_called_once_with("/r", "do it", 3, 2)
mocks[2].assert_called_once_with(loop.LoopState(3, 2, "/r", "do it"))
self.assertEqual(mocks[3].call_args.args[0], "/r")
mocks[4].assert_called_once_with("/r")
self.assertEqual(mocks[3].call_args.args[-1], mocks[4].return_value)