import json import pathlib import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from claude_code import entries, reply, session, transcript from failure import Failure from test_env import assert_not_root 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 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]")) def test_a_stream_parses_into_an_entry_per_line_that_is_a_json_object(self): raw = b'{"type": "user", "message": {"content": "do it"}}\nnot json\n\n{"type": "summary"}\n{}\n' self.assertEqual(entries.entries_of(raw), [entries.HumanTurn(text="do it"), entries.Entry(), entries.Entry()]) self.assertEqual(entries.entries_of(b""), []) 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_messages_group_the_turns_that_share_a_message_id_and_fall_back_to_the_uuid_where_there_is_none(self): first = entries.AssistantTurn(uuid="a", message_id="msg_1", usage={"input_tokens": 1}) other = entries.AssistantTurn(uuid="b", message_id="msg_2") again = entries.AssistantTurn(uuid="c", message_id="msg_1", usage={"input_tokens": 2}) bare = entries.AssistantTurn(uuid="d") self.assertEqual(entries.messages([first, entries.HumanTurn(text="hi"), other, again, bare]), [[first, again], [other], [bare]]) 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", "stream-json", "--verbose", "--permission-mode", "bypassPermissions"], "/r", "p")]) def test_reads_the_reply_from_the_streams_last_line(self): stream = "\n".join(json.dumps(event) for event in ({"type": "system", "subtype": "init"}, {"type": "assistant"}, {"type": "result", "result": "ok"})) self.assertEqual(reply.claude_reply(self.claude((0, stream + "\n", "")), [], "/r", "p", "the judge"), {"type": "result", "result": "ok"}) def test_a_stream_that_ends_in_no_json_names_only_its_last_line(self): self.assertEqual(reply.claude_reply(self.claude((0, '{"type": "system"}\nWarning: bye\n', "")), [], "/r", "p", "the judge"), Failure("the judge printed no json reply; its last line was: Warning: bye")) 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; its last line was: Warning: hook said hi")) self.assertEqual(reply.claude_reply(self.claude((0, "", "")), [], "/r", "p", "the judge"), Failure("the judge printed no json reply; its last line was: 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")) class EmptyTranscript(unittest.TestCase): def setUp(self): temp = tempfile.TemporaryDirectory() self.addCleanup(temp.cleanup) self.session = session.session_named("A proj a-driver", pathlib.Path(temp.name)) self.path = pathlib.Path(temp.name) / "-home-me-code-log" / f"{self.session.id}.jsonl" self.path.parent.mkdir() def test_a_session_is_open_only_where_its_transcript_holds_something(self): self.assertFalse(session.session_open(self.session)) self.path.touch() self.assertFalse(session.session_open(self.session)) self.path.write_text("{") self.assertTrue(session.session_open(self.session)) def test_discarding_removes_an_empty_transcript_and_keeps_one_that_holds_something(self): self.path.touch() self.assertIsNone(session.discard_empty_transcript(self.session)) self.assertFalse(self.path.exists()) self.path.write_text("{") self.assertIsNone(session.discard_empty_transcript(self.session)) self.assertEqual(self.path.read_text(), "{") def test_discarding_fails_naming_the_file_it_could_not_remove(self): assert_not_root(self) self.path.touch() self.path.parent.chmod(0o500) self.addCleanup(self.path.parent.chmod, 0o700) failure = session.discard_empty_transcript(self.session) self.assertIsInstance(failure, Failure) self.assertEqual(failure.reason, f"could not remove the empty transcript {self.path}: Permission denied; claude refuses to start the session while that file stands") if __name__ == "__main__": unittest.main()