import json import pathlib import subprocess import sys import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from claude_code import entries, reply, transcript from failure import Failure def assistant(*parts): return {"type": "assistant", "message": {"content": list(parts)}} def human(content): return {"type": "user", "message": {"content": content}} def kinds(found): return [entries.entry_from(entry) for entry in found] def queued(prompt, kind): return {"type": "attachment", "attachment": {"type": "queued_command", "commandMode": "prompt", "origin": {"kind": kind}, "prompt": prompt}} class Render(unittest.TestCase): def test_joins_assistant_text_tool_calls_human_turns_and_loop_prompts_labelling_a_typed_answer_the_humans_and_skips_the_rest(self): said = transcript.render(kinds([ human("do it"), assistant({"type": "text", "text": "Starting."}, {"type": "tool_use", "name": "Bash", "input": {"command": "ls"}}), assistant({"type": "text", "text": " "}), human([{"type": "tool_result", "content": "not typed"}]), human("/exit"), human([{"type": "text", "text": "yes, "}, {"type": "text", "text": "merge it"}]), queued("B, while you work", "human"), queued("origin/main moved", "coordinator"), {**human("Continue the task; the previous run stopped before it was finished."), "promptSource": "sdk"}, {**human("Answered: use B\nYour question: A or B?"), "promptSource": "sdk"}, ]), lambda prompt: transcript.Typed(*reversed(prompt.removeprefix("Answered: ").split("\n"))) if prompt.startswith("Answered: ") else None) self.assertEqual(said, '[human] do it\n\nStarting.\n\n[tool Bash] {"command": "ls"}\n\n[human] yes, merge it\n\n[human] B, while you work\n\n[loop] Continue the task; the previous run stopped before it was finished.\n\n[loop] Your question: A or B?\n\n[human] use B') class Verbatim(unittest.TestCase): def test_a_quote_holds_only_word_for_word_less_emphasis_and_whitespace(self): self.assertTrue(transcript.verbatim("Merge **ff-only**.", "Merge ff-only.\nNever squash.")) self.assertTrue(transcript.verbatim("Merge _ff-only_.", "Merge ff-only.\nNever squash.")) self.assertFalse(transcript.verbatim("Merge ... squash.", "Merge ff-only.\nNever squash.")) self.assertFalse(transcript.verbatim("", "Merge ff-only.")) class FirstTimestampedLine(unittest.TestCase): def test_skips_the_lines_every_chunk_shares_and_else_takes_the_first(self): self.assertEqual(transcript.first_timestamped_line(b'{"type": "custom-title"}\nnot JSON\n{"timestamp": "t"}\n{"timestamp": "u"}\n'), b'{"timestamp": "t"}') self.assertEqual(transcript.first_timestamped_line(b'{"type": "custom-title"}\n[1]\n'), b'{"type": "custom-title"}') self.assertEqual(transcript.first_timestamped_line(b""), b"") class CurrentThread(unittest.TestCase): def test_a_parent_cycle_ends_the_thread(self): looped = [{"uuid": "a", "parentUuid": "b"}, {"uuid": "b", "parentUuid": "a"}] self.assertEqual([entry.uuid for entry in transcript.current_thread(kinds(looped))], ["a", "b"]) def test_entries_without_a_uuid_make_no_thread(self): self.assertEqual(transcript.current_thread(kinds([{"type": "summary"}])), []) class HumanTurns(unittest.TestCase): def test_counts_typed_turns_and_not_tool_results(self): self.assertEqual(transcript.human_turns(kinds([ human("typed"), human([{"type": "text", "text": "typed as a block"}]), human([{"type": "tool_result", "content": "not typed"}]), human("/model"), {**human("<3 merge it"), "promptSource": "typed"}, {**human("done"), "promptSource": "system"}, {**human([{"type": "text", "text": "done"}]), "promptSource": "system"}, {**human("peer message"), "isMeta": True}, {**human("the loop's own prompt"), "promptSource": "sdk"}, {**human("This session is being continued from a previous conversation that ran out of context. The summary below covers the earlier portion of the conversation."), "isCompactSummary": True}, human([{"type": "text", "text": "[Request interrupted by user]"}]), human([]), queued("typed while claude worked", "human"), queued("done", "task-notification"), {"type": "attachment", "attachment": {"type": "queued_command", "commandMode": "task-notification", "prompt": "done"}}, assistant({"type": "text", "text": "said"}), ])), 4) class PartLine(unittest.TestCase): def test_renders_text_and_tool_calls_but_blank_text_as_nothing(self): self.assertEqual(transcript.part_line(entries.Text("hi"), 20), "hi") self.assertEqual(transcript.part_line(entries.Text(" \n"), 20), "") self.assertEqual(transcript.part_line(entries.ToolCall("toolu_1", "Bash", {"command": "x" * 500}), 20), '[tool Bash] {"command": "xxxxxxx') self.assertEqual(transcript.part_line(entries.ToolCall("toolu_1", "Write", {"content": "Hälsa på"}), 40), '[tool Write] {"content": "Hälsa på"}') class Parse(unittest.TestCase): def test_yields_only_json_objects(self): self.assertEqual(entries.json_object('{"type": "user"}'), {"type": "user"}) self.assertIsNone(entries.json_object("not json")) self.assertIsNone(entries.json_object("[1, 2]")) def test_a_transcript_line_parses_into_its_kind(self): self.assertEqual(entries.parse_entry('{"type": "user", "message": {"content": "do it"}}'), entries.HumanTurn(text="do it")) self.assertIsNone(entries.parse_entry("[1, 2]")) class EntryKinds(unittest.TestCase): def test_names_who_wrote_each_user_entry_and_queued_prompt(self): self.assertEqual([entries.entry_from(found) for found in [ human("typed"), human([{"type": "text", "text": "typed "}, {"type": "text", "text": "as blocks"}]), {**human("<3 merge it"), "promptSource": "typed"}, queued("typed while claude worked", "human"), {**human("Continue the task."), "promptSource": "sdk"}, {**human([{"type": "text", "text": "Continue as a block."}]), "promptSource": "sdk"}, human("/model"), {**human("peer message"), "isMeta": True}, {**human("done"), "promptSource": "system"}, human([{"type": "text", "text": "[Request interrupted by user]"}]), human([]), ]], [ entries.HumanTurn(text="typed"), entries.HumanTurn(text="typed as blocks"), entries.HumanTurn(text="<3 merge it"), entries.HumanTurn(text="typed while claude worked"), entries.LoopPrompt(text="Continue the task."), entries.InjectedText(text="Continue as a block."), entries.InjectedText(text="/model"), entries.InjectedText(text="peer message"), entries.InjectedText(text="done"), entries.InjectedText(text="[Request interrupted by user]"), entries.InjectedText(text=""), ]) def test_an_assistant_turn_keeps_its_text_and_tool_calls_and_its_message(self): turn = {**assistant( {"type": "thinking", "thinking": "hm"}, {"type": "text", "text": "Starting."}, {"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {"command": "ls"}}, {"type": "tool_use", "id": "toolu_2", "name": "Bash", "input": "not arguments"}, ), "uuid": "u"} turn["message"] |= {"id": "msg_1", "model": "claude-opus-5-5", "usage": {"input_tokens": 2}} self.assertEqual(entries.entry_from(turn), entries.AssistantTurn( uuid="u", parts=(entries.Text("Starting."), entries.ToolCall("toolu_1", "Bash", {"command": "ls"})), message_id="msg_1", model="claude-opus-5-5", usage={"input_tokens": 2}, )) def test_parses_tool_results_and_cost_states_and_reads_queued_notifications_and_other_kinds_as_plain_entries(self): self.assertEqual(entries.entry_from(human([{"type": "tool_result", "tool_use_id": "toolu_1", "content": "hi", "is_error": True}])), entries.ToolResults(results=(entries.ToolResult("toolu_1", "hi", True),))) self.assertEqual(entries.entry_from(queued("done", "task-notification")), entries.Entry()) self.assertEqual(entries.entry_from({"type": "system", "subtype": "compact_boundary"}), entries.Entry()) cost_state = {"type": "cost-state", "totalCostUSD": 41.1, "modelUsage": {"claude-opus-5-5": {"costUSD": 40.1, "outputTokens": 334504}, "claude-haiku-5-5": {"costUSD": 1}, "odd": {"costUSD": "1"}, "odder": 1}} self.assertEqual(entries.entry_from(cost_state), entries.CostState(costs={"claude-opus-5-5": 40.1, "claude-haiku-5-5": 1})) self.assertEqual(entries.entry_from({"type": "cost-state", "modelUsage": []}), entries.CostState(costs={})) def test_the_api_request_claude_code_logs_yields_its_tool_results_and_none_of_its_text(self): blob = {"type": "api-request-blob", "hash": "h", "message": {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": "hi"}, {"type": "text", "text": "the request again"}]}} self.assertEqual(entries.entry_from(blob), entries.ToolResults(results=(entries.ToolResult("toolu_1", "hi", False),))) self.assertEqual(entries.entry_from({"type": "api-request-blob", "message": {"role": "user", "content": [{"type": "text", "text": "do it"}]}}), entries.ToolResults(results=())) def test_mixed_blocks_join_only_their_text(self): self.assertEqual(entries.entry_from(human([{"type": "text", "text": "see "}, {"type": "image", "source": {}}, {"type": "text", "text": "this"}])), entries.InjectedText(text="see this")) def test_every_kind_carries_where_and_when_it_was_written(self): where = {"uuid": "b", "parentUuid": "a", "timestamp": "2026-10-15T00:00:00.123Z", "cwd": "/r", "gitBranch": "item-1"} carried = {"uuid": "b", "parent_uuid": "a", "timestamp": "2026-10-15T00:00:00.123Z", "cwd": "/r", "git_branch": "item-1"} for found, kind in [ ({"type": "summary"}, entries.Entry(**carried)), (human("typed"), entries.HumanTurn(**carried, text="typed")), (queued("typed while claude worked", "human"), entries.HumanTurn(**carried, text="typed while claude worked")), ({**human("Continue."), "promptSource": "sdk"}, entries.LoopPrompt(**carried, text="Continue.")), (human("/model"), entries.InjectedText(**carried, text="/model")), (human([{"type": "tool_result", "tool_use_id": "toolu_1", "content": "hi"}]), entries.ToolResults(**carried, results=(entries.ToolResult("toolu_1", "hi", False),))), (assistant(), entries.AssistantTurn(**carried)), ({"type": "cost-state", "modelUsage": {}}, entries.CostState(**carried, costs={})), ]: with self.subTest(kind=type(kind).__name__): self.assertEqual(entries.entry_from({**found, **where}), kind) self.assertEqual(entries.entry_from({"type": "summary", "uuid": 1, "parentUuid": None}), entries.Entry()) class ClaudeReply(unittest.TestCase): @staticmethod def claude(*results): def run(args, cwd, prompt): result = results[0] if isinstance(result, OSError): raise result return subprocess.CompletedProcess(args, *result) return run def test_runs_claude_headless_with_the_args_in_cwd_on_the_prompt(self): asked = [] def run(args, cwd, prompt): asked.append((args, cwd, prompt)) return subprocess.CompletedProcess(args, 0, "{}", "") reply.claude_reply(run, ["--tools", ""], "/r", "p", "the judge") self.assertEqual(asked, [(["-p", "--tools", "", "--no-session-persistence", "--output-format", "json", "--permission-mode", "bypassPermissions"], "/r", "p")]) def test_returns_the_json_reply_a_usage_limit_whatever_the_exit_code_or_why_there_is_none(self): self.assertEqual(reply.claude_reply(self.claude((0, '{"result": "ok"}', "")), [], "/r", "p", "the judge"), {"result": "ok"}) self.assertEqual(reply.claude_reply(self.claude((0, "Warning: hook said hi\n", "")), [], "/r", "p", "the judge"), Failure("the judge printed no json reply; it printed: Warning: hook said hi")) self.assertEqual(reply.claude_reply(self.claude((0, "", "")), [], "/r", "p", "the judge"), Failure("the judge printed no json reply; it printed: nothing")) limit = json.dumps({"type": "result", "api_error_status": 429, "result": "You've hit your limit"}) self.assertEqual(reply.claude_reply(self.claude((1, limit, "")), [], "/r", "p", "the judge"), reply.Limited(None, "You've hit your limit")) self.assertEqual(reply.claude_reply(self.claude((2, "", " ")), [], "/r", "p", "the judge"), Failure("the judge exited 2")) self.assertEqual(reply.claude_reply(self.claude((2, "", "boom\n")), [], "/r", "p", "the judge"), Failure("boom")) self.assertEqual(reply.claude_reply(self.claude(FileNotFoundError(2, "No such file or directory")), [], "/r", "p", "claude"), Failure("could not start claude: No such file or directory")) if __name__ == "__main__": unittest.main()