import importlib.machinery import importlib.util import json import os import pathlib import stat import tempfile import unittest LOADER = importlib.machinery.SourceFileLoader("loop_claude", str(pathlib.Path(__file__).with_name("loop-claude"))) SPEC = importlib.util.spec_from_loader("loop_claude", LOADER) loop = importlib.util.module_from_spec(SPEC) LOADER.exec_module(loop) def assistant(*parts): return {"type": "assistant", "message": {"content": list(parts)}} def human(content): return {"type": "user", "message": {"content": content}} class Sections(unittest.TestCase): def test_keeps_only_headed_sections_with_a_body(self): text = "# Title\n\nintro\n\n## Goals\n\n1. one\n2. two\n\n## Empty\n\n## Audience\nme\n" self.assertEqual(loop.sections(text), {"Goals": "1. one\n2. two", "Audience": "me"}) class Render(unittest.TestCase): def test_joins_assistant_text_and_tool_calls_and_skips_the_rest(self): said = loop.render([ human("do it"), assistant({"type": "text", "text": "Starting."}, {"type": "tool_use", "name": "Bash", "input": {"command": "ls"}}), assistant({"type": "text", "text": " "}), ]) self.assertEqual(said, 'Starting.\n\n[tool Bash] {"command": "ls"}') class HumanTurns(unittest.TestCase): def test_counts_typed_turns_and_not_tool_results(self): self.assertEqual(loop.human_turns([ human("typed"), human([{"type": "text", "text": "typed as a block"}]), human([{"type": "tool_result", "content": "not typed"}]), assistant({"type": "text", "text": "said"}), ]), 2) class WorkerArgs(unittest.TestCase): def test_a_fresh_round_names_the_session_and_asks_to_continue_open_work(self): args = loop.worker_args("the task", "A name", "slot-id", resuming=False) self.assertIn("--session-id", args) self.assertEqual(args[args.index("--name") + 1], "A name") self.assertTrue(args[1].startswith("the task\n\nContinue any open draft PR")) def test_a_resumed_round_reuses_the_session(self): args = loop.worker_args("paint the fence", "A name", "slot-id", resuming=True) self.assertEqual(args[args.index("--resume") + 1], "slot-id") self.assertNotIn("--name", args) self.assertNotIn("paint the fence", args[1]) class JudgePrompt(unittest.TestCase): def test_hands_the_judge_the_tail_of_a_long_transcript(self): prompt = loop.judge_prompt("task", "## Goals\n1. g", "early " * 20000 + "LATE") self.assertIn("LATE", prompt) self.assertLess(prompt.count("early"), 20000) self.assertTrue(prompt.endswith(loop.JUDGE_RULES)) class Archive(unittest.TestCase): def test_keeps_the_copy_as_private_as_the_transcript(self): with tempfile.TemporaryDirectory() as root: transcript = os.path.join(root, "session.jsonl") open(transcript, "w").write(json.dumps(assistant({"type": "text", "text": "said"})) + "\n") loop.ARCHIVE = os.path.join(root, "archive") loop.archive(transcript, "note") [copy] = [os.path.join(folder, name) for folder, _, names in os.walk(loop.ARCHIVE) for name in names] self.assertEqual(stat.S_IMODE(os.stat(copy).st_mode), 0o600) self.assertEqual(stat.S_IMODE(os.stat(os.path.dirname(copy)).st_mode), 0o700) self.assertIn("said", open(copy).read()) class Grounded(unittest.TestCase): def test_an_interrupt_must_quote_the_transcript(self): self.assertTrue(loop.grounded({"ask": False, "evidence": ""}, "anything")) self.assertTrue(loop.grounded({"ask": True, "evidence": "Needs you: x"}, "so.\n\nNeeds you: x\n")) self.assertFalse(loop.grounded({"ask": True, "evidence": "invented"}, "the transcript")) if __name__ == "__main__": unittest.main()