Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -108,15 +108,28 @@ class LoopRuns(unittest.TestCase):
|
||||
self.assertEqual(reload.exec_if_edited(a_loop(fake.effects(), launcher=launcher), "stale", main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="")), Stop(1, "could not reload loop-claude: No such file or directory; rerun loop-claude <task>"))
|
||||
|
||||
|
||||
def steps(start_chunks, **others):
|
||||
"""The steps of a loop that starts chunks through start_chunks, and takes each other step through the double given for it, else fails."""
|
||||
def unused(*args):
|
||||
raise AssertionError(args)
|
||||
|
||||
return main.Steps(**{"fingerprint": unused, "exec_if_edited": unused, "next_cycle": unused, "run_cycle": unused, "closed_cycle": unused, "check_routing": unused, **others}, start_chunks=start_chunks)
|
||||
|
||||
|
||||
class WindowStarts(unittest.TestCase):
|
||||
"""The loop-claude the human runs, which starts the windows, and the stop of a window's loop whose chunk finished."""
|
||||
HOME = pathlib.Path("/h")
|
||||
|
||||
def run_loop(self, argv, fake, env=None, started=("item-3", "item-4")):
|
||||
"""run_loop's stop and what it said on stdout, with start_chunks answering `started`; self.asked holds what start_chunks was asked."""
|
||||
with unittest.mock.patch.object(main, "start_chunks", return_value=list(started) if isinstance(started, tuple) else started) as start, contextlib.redirect_stdout(io.StringIO()) as said:
|
||||
stop = main.run_loop(argv, env or {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, fake.effects())
|
||||
self.asked = [call.args[:2] for call in start.call_args_list]
|
||||
self.asked = []
|
||||
|
||||
def start(*args):
|
||||
self.asked.append(args[:2])
|
||||
return list(started) if isinstance(started, tuple) else started
|
||||
|
||||
with contextlib.redirect_stdout(io.StringIO()) as said:
|
||||
stop = main.run_loop(argv, env or {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, fake.effects(), steps(start))
|
||||
return stop, said.getvalue()
|
||||
|
||||
def test_refuses_to_start_anything_on_a_tmux_older_than_needed(self):
|
||||
@@ -141,9 +154,10 @@ class WindowStarts(unittest.TestCase):
|
||||
def test_hands_the_window_starts_its_paths_environment_launcher_and_effects(self):
|
||||
fake = FakeEffects(git=["/r\n"], on_terminal=False)
|
||||
effects, env, launcher = fake.effects(), {"LOOP_CLAUDE_X": "1"}, pathlib.Path("/b/loop-claude")
|
||||
with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]) as start, contextlib.redirect_stdout(io.StringIO()):
|
||||
main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects)
|
||||
self.assertEqual(start.call_args.args, (main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects))
|
||||
asked = []
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
main.run_loop(["do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects, steps(lambda *args: asked.append(args) or ["item-3"]))
|
||||
self.assertEqual(asked, [(main.LoopRun(0, "/r", "do it", ""), False, paths.paths_from(self.HOME), env, launcher, effects)])
|
||||
|
||||
def test_a_window_loop_runs_its_chunk_in_its_own_session_and_window_and_hands_on_once_it_finished(self):
|
||||
fake = FakeEffects(git=["/r\n"])
|
||||
@@ -153,14 +167,15 @@ class WindowStarts(unittest.TestCase):
|
||||
unfinished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Unfinished))
|
||||
finished = unittest.mock.Mock(verdict=unittest.mock.Mock(spec=Finished))
|
||||
closed = [main.LoopRun(1, "/r", "do it", "", chunk="item-3"), main.LoopRun(2, "/r", "do it", "", chunk="item-3")]
|
||||
with unittest.mock.patch.object(main, "fingerprint", return_value=""), unittest.mock.patch.object(main, "exec_if_edited", return_value=None), unittest.mock.patch.object(main, "next_cycle", return_value=cycle) as next_cycle, \
|
||||
unittest.mock.patch.object(main, "run_cycle", side_effect=[unfinished, finished]), unittest.mock.patch.object(main, "closed_cycle", side_effect=closed), \
|
||||
unittest.mock.patch.object(main, "check_routing", return_value=None), unittest.mock.patch.object(main, "handed_on", return_value=Stop(0, "")) as handed_on:
|
||||
stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects)
|
||||
concluded, cycles, started = [unfinished, finished], [], []
|
||||
taken = steps(lambda *args: started.append(args) or [], fingerprint=lambda launcher: "", exec_if_edited=lambda *args: None, next_cycle=lambda *args: cycles.append(args) or cycle,
|
||||
run_cycle=lambda cycle: concluded.pop(0), closed_cycle=lambda *args: closed.pop(0), check_routing=lambda *args: None)
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
stop = main.run_loop(["--chunk", "item-3", "do it"], env, paths.paths_from(self.HOME), launcher, CWD, effects, taken)
|
||||
loop = this_loop.Loop(root="/r", session=session.session_named("A r loop-claude item-3", self.HOME / ".claude" / "projects"), window=tmux.Window("loop-r", "item-3", "%4"), paths=paths.paths_from(self.HOME), env=env, launcher=launcher, effects=effects)
|
||||
self.assertEqual(stop, Stop(0, ""))
|
||||
self.assertEqual([call.args for call in next_cycle.call_args_list], [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)])
|
||||
handed_on.assert_called_once_with(closed[1], loop)
|
||||
self.assertEqual(cycles, [(main.LoopRun(1, "/r", "do it", "", chunk="item-3"), loop), (main.LoopRun(2, "/r", "do it", "", chunk="item-3"), loop)])
|
||||
self.assertEqual(started, [(main.LoopRun(2, "/r", "do it", "", chunk="item-3"), True, loop.paths, env, launcher, effects)])
|
||||
|
||||
def test_a_window_loop_given_no_task_prints_the_help(self):
|
||||
self.assertEqual(main.loop_run_from(["--chunk", "item-3"], CWD, FakeEffects(git=["/r\n"]).effects()), Stop(1, usage.HELP))
|
||||
@@ -184,8 +199,8 @@ class WindowStarts(unittest.TestCase):
|
||||
def test_stops_where_tmux_cannot_show_the_session(self):
|
||||
fake = FakeEffects(git=["/r\n"])
|
||||
effects = dataclasses.replace(fake.effects(), tmux_on_terminal=lambda args: 1)
|
||||
with unittest.mock.patch.object(main, "start_chunks", return_value=["item-3"]), contextlib.redirect_stdout(io.StringIO()):
|
||||
stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects)
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
stop = main.run_loop(["do it"], {}, paths.paths_from(self.HOME), pathlib.Path("/b/loop-claude"), CWD, effects, steps(lambda *args: ["item-3"]))
|
||||
self.assertEqual(stop, Stop(1, "`tmux attach-session -t =loop-r` failed; the chunks go on in the tmux session"))
|
||||
|
||||
def test_a_window_loop_outside_tmux_stops(self):
|
||||
@@ -201,11 +216,11 @@ class WindowStarts(unittest.TestCase):
|
||||
loop_run = main.LoopRun(4, "/r", "do it", "", chunk="item-7")
|
||||
for started, said, stop in ((["item-9", "item-12"], "the chunk finished; started item-9, item-12", Stop(0, "")), ([], "the chunk finished; started no other, since the chunks running fill what may run now", Stop(0, "")),
|
||||
(Failure("no plan"), "", Stop(1, "the chunk finished, but the next could not start: no plan"))):
|
||||
with self.subTest(started=started), unittest.mock.patch.object(main, "start_chunks", return_value=started) as start, contextlib.redirect_stdout(io.StringIO()) as printed:
|
||||
loop = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"})
|
||||
self.assertEqual(main.handed_on(loop_run, loop), stop)
|
||||
with self.subTest(started=started), contextlib.redirect_stdout(io.StringIO()) as printed:
|
||||
loop, asked = a_loop(FakeEffects().effects(), env={"LOOP_CLAUDE_X": "1"}), []
|
||||
self.assertEqual(main.handed_on(loop_run, loop, lambda *args: asked.append(args) or started), stop)
|
||||
self.assertEqual(printed.getvalue(), f"{STAMP} {said}\n" if said else "")
|
||||
self.assertEqual(start.call_args.args, (loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects))
|
||||
self.assertEqual(asked, [(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects)])
|
||||
|
||||
|
||||
class StopDms(unittest.TestCase):
|
||||
@@ -214,28 +229,33 @@ class StopDms(unittest.TestCase):
|
||||
code = main.main(argv, {}, paths.paths_from(pathlib.Path(home)), pathlib.Path(home, "loop-claude"), CWD, fake.effects())
|
||||
return code, said.getvalue()
|
||||
|
||||
def exit_code(self, run, fake):
|
||||
with contextlib.redirect_stderr(io.StringIO()) as said, contextlib.redirect_stdout(io.StringIO()):
|
||||
code = main.exit_code(run, CWD, fake.effects())
|
||||
return code, said.getvalue()
|
||||
|
||||
def raising(self, error):
|
||||
raise error
|
||||
|
||||
def test_dms_the_reason_of_a_stop_with_exit_2_such_as_a_chunk_left_unfinished_too_often(self):
|
||||
fake = FakeEffects()
|
||||
with unittest.mock.patch.object(main, "run_loop", return_value=Stop(2, "the human is needed to look at the chunk, but it stayed unfinished")):
|
||||
self.assertEqual(self.run_main(["do it"], fake)[0], 2)
|
||||
self.assertEqual(self.exit_code(lambda: Stop(2, "the human is needed to look at the chunk, but it stayed unfinished"), fake)[0], 2)
|
||||
self.assertEqual(fake.dms, [f"loop-claude in {CWD} stopped: the human is needed to look at the chunk, but it stayed unfinished"])
|
||||
|
||||
def test_skips_the_dm_of_a_stop_without_a_reason_as_once_the_windows_started(self):
|
||||
fake = FakeEffects()
|
||||
with unittest.mock.patch.object(main, "run_loop", return_value=Stop(0, "")):
|
||||
self.assertEqual(self.run_main(["do it"], fake), (0, ""))
|
||||
self.assertEqual(self.exit_code(lambda: Stop(0, ""), fake), (0, ""))
|
||||
self.assertEqual(fake.dms, [])
|
||||
|
||||
def test_skips_the_dm_where_ctrl_c_stopped_the_loop(self):
|
||||
fake = FakeEffects()
|
||||
with unittest.mock.patch.object(main, "run_loop", side_effect=KeyboardInterrupt):
|
||||
self.assertEqual(self.run_main(["do it"], fake)[0], 130)
|
||||
self.assertEqual(self.exit_code(lambda: self.raising(KeyboardInterrupt()), fake)[0], 130)
|
||||
self.assertEqual(fake.dms, [])
|
||||
|
||||
def test_dms_a_crash_before_it_reaches_the_terminal(self):
|
||||
fake = FakeEffects()
|
||||
with unittest.mock.patch.object(main, "run_loop", side_effect=RuntimeError("boom")), self.assertRaises(RuntimeError):
|
||||
self.run_main(["do it"], fake)
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.exit_code(lambda: self.raising(RuntimeError("boom")), fake)
|
||||
self.assertEqual(fake.dms, [f"loop-claude in {CWD} crashed: RuntimeError('boom')"])
|
||||
|
||||
def test_dms_the_reason_where_a_loop_given_a_task_stops_with_exit_1(self):
|
||||
|
||||
Reference in New Issue
Block a user