import json import os import pathlib import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from test_env import isolated_env from command_patterns.tally import pattern, records, Row, tally TOOL = pathlib.Path(__file__).resolve().parents[1] / "command-patterns" def call(id: str, command: str, at: str) -> str: return json.dumps({"type": "assistant", "timestamp": at, "message": {"id": f"m-{id}", "content": [{"type": "tool_use", "id": id, "name": "Bash", "input": {"command": command}}]}}) def result(id: str, text: str, at: str) -> str: return json.dumps({"type": "user", "timestamp": at, "message": {"content": [{"type": "tool_result", "tool_use_id": id, "content": text}]}}) def thread(*lines: str) -> bytes: return ("\n".join(lines) + "\n").encode() class Pattern(unittest.TestCase): def test_names_the_tool_and_its_subcommands_as_in_tea_pr_create(self): self.assertEqual(pattern("tea pr create --title t"), "tea pr create") def test_skips_a_leading_cd(self): self.assertEqual(pattern("cd /x && git status --short"), "git status") def test_stops_at_the_first_option_or_path(self): self.assertEqual(pattern("git -C /x status"), "git") self.assertEqual(pattern("~/.claude/bin/run-heavy ./ci.sh"), "run-heavy") def test_keeps_two_subcommands_at_most(self): self.assertEqual(pattern("git remote add origin x"), "git remote add") def test_names_a_command_it_cannot_parse_unparsed(self): self.assertEqual(pattern('echo "unclosed'), "(unparsed)") def test_takes_a_lone_cd_as_cd(self): self.assertEqual(pattern("cd /x"), "cd") class Tally(unittest.TestCase): def test_counts_each_patterns_chunks_calls_tokens_and_tool_time(self): first = [thread(call("a", "tea pr create --title t", "2026-11-01T10:00:00Z"), result("a", "x" * 401, "2026-11-01T10:00:03Z"), call("b", "git status", "2026-11-01T10:01:00Z"), result("b", "", "2026-11-01T10:01:00Z"))] second = [thread(call("c", "tea pr create --title u", "2026-11-02T10:00:00Z"), result("c", "y" * 40, "2026-11-02T10:00:02Z")), thread(call("d", "tea pr create", "2026-11-02T11:00:00Z"))] rows = tally([first, second]) self.assertEqual(rows[0], Row("tea pr create", chunks=2, calls=3, command_tokens=(23 + 23 + 13) // 4, result_tokens=(401 + 40) // 4, seconds=5)) self.assertEqual(rows[1], Row("git status", chunks=1, calls=1, command_tokens=10 // 4, result_tokens=0, seconds=0)) def test_counts_a_result_logged_twice_once(self): rows = tally([[thread(call("a", "ls", "2026-11-01T10:00:00Z"), result("a", "abcd", "2026-11-01T10:00:01Z"), result("a", "abcd", "2026-11-01T10:00:09Z"))]]) self.assertEqual(rows, [Row("ls", chunks=1, calls=1, command_tokens=0, result_tokens=1, seconds=1)]) class Records(unittest.TestCase): def test_gives_each_call_its_chunk_pattern_seconds_sizes_and_text(self): first = [thread(call("a", "cd /x && git status --short", "2026-11-01T10:00:00Z"), result("a", "x" * 7, "2026-11-01T10:00:03Z"))] second = [thread(call("b", "ls", "2026-11-02T10:00:00Z"))] self.assertEqual(records([first, second]), [ {"chunk": 1, "pattern": "git status", "seconds": 3.0, "command_characters": 27, "result_characters": 7, "command": "cd /x && git status --short"}, {"chunk": 2, "pattern": "ls", "seconds": 0.0, "command_characters": 2, "result_characters": 0, "command": "ls"}, ]) def test_keeps_a_chunks_calls_from_each_of_its_threads_under_its_number(self): both = [thread(call("a", "ls", "2026-11-01T10:00:00Z")), thread(call("b", "pwd", "2026-11-01T11:00:00Z"))] self.assertEqual([(each["chunk"], each["command"]) for each in records([both])], [(1, "ls"), (1, "pwd")]) class Main(unittest.TestCase): """A checkout tagged v0.1.0 at 2026-11-01 and v0.2.0 at 2026-11-05, and three archived chunks, finished before, between and after them.""" def setUp(self): self.home = pathlib.Path(os.path.realpath(tempfile.mkdtemp())) self.addCleanup(subprocess.run, ["rm", "-rf", str(self.home)]) self.checkout = self.home / "code" / "project" self.checkout.mkdir(parents=True) self.env = {**isolated_env(), "HOME": str(self.home), "GIT_CONFIG_NOSYSTEM": "1", "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@t"} self.git("init", "-q", "-b", "main") self.git("commit", "-q", "--allow-empty", "-m", "Start") self.tag("v0.1.0", "2026-11-01T00:00:00Z") self.tag("v0.2.0", "2026-11-05T00:00:00Z") archive = self.home / ".claude" / "loop-claude-transcripts" / "project" for name, finished, command in (("before", "2026-10-30T00:00:00Z", "make old"), ("between", "2026-11-03T00:00:00Z", "tea pr create"), ("after", "2026-11-07T00:00:00Z", "gh pr view")): folder = archive / name folder.mkdir(parents=True) (folder / "worker.jsonl").write_bytes(thread(call(name, command, finished), result(name, "ok", finished))) (folder / "worker.outcomes.md").write_text(f"Task: t\n\n{finished} Verdict: finished\n") def git(self, *args: str, at: str | None = None) -> None: subprocess.run(["git", *args], check=True, cwd=self.checkout, env={**self.env, **({"GIT_COMMITTER_DATE": at} if at else {})}, capture_output=True) def tag(self, name: str, at: str) -> None: self.git("tag", "-a", "-m", name, name, at=at) def run_tool(self, *args: str) -> subprocess.CompletedProcess[str]: return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.checkout, env=self.env, text=True) def test_reads_the_chunks_finished_since_the_newest_tag_by_default(self): ran = self.run_tool() self.assertEqual(ran.returncode, 0, ran.stderr) self.assertIn("| gh pr view | 1 | 1 |", ran.stdout) self.assertNotIn("tea pr create", ran.stdout) self.assertIn("1 chunk", ran.stdout) def test_reads_the_chunks_finished_between_the_tag_before_a_release_and_its_own(self): ran = self.run_tool("0.2.0") self.assertEqual(ran.returncode, 0, ran.stderr) self.assertIn("| tea pr create | 1 | 1 |", ran.stdout) self.assertNotIn("make old", ran.stdout) self.assertNotIn("gh pr view", ran.stdout) def test_reads_every_chunk_before_the_first_tag_for_the_first_release(self): ran = self.run_tool("0.1.0") self.assertIn("| make old | 1 | 1 |", ran.stdout) self.assertNotIn("tea pr create", ran.stdout) def test_writes_each_call_as_a_json_line_beside_the_table(self): saved = self.home / "calls.jsonl" ran = self.run_tool("0.2.0", "--calls", str(saved)) self.assertEqual(ran.returncode, 0, ran.stderr) self.assertIn("| tea pr create | 1 | 1 |", ran.stdout) self.assertEqual([json.loads(line) for line in saved.read_text().splitlines()], [{"chunk": 1, "pattern": "tea pr create", "seconds": 0.0, "command_characters": 13, "result_characters": 2, "command": "tea pr create"}]) def test_makes_the_calls_file_readable_by_its_owner_alone_even_where_it_existed(self): saved = self.home / "calls.jsonl" saved.write_text("old") saved.chmod(0o644) self.assertEqual(self.run_tool("0.2.0", "--calls", str(saved)).returncode, 0) self.assertEqual(saved.stat().st_mode & 0o777, 0o600) self.assertNotIn("old", saved.read_text()) def test_takes_the_calls_flag_before_the_version_too(self): saved = self.home / "calls.jsonl" self.assertEqual(self.run_tool("--calls", str(saved), "0.2.0").returncode, 0) self.assertEqual(len(saved.read_text().splitlines()), 1) def test_refuses_a_calls_flag_without_a_file(self): ran = self.run_tool("--calls") self.assertEqual(ran.returncode, 1) self.assertIn("--calls", ran.stderr) def test_fails_where_the_calls_file_cannot_be_written(self): ran = self.run_tool("--calls", str(self.home / "no-such-folder" / "calls.jsonl")) self.assertEqual(ran.returncode, 1) self.assertIn("no-such-folder", ran.stderr) self.assertEqual(ran.stdout, "") def test_refuses_a_release_no_tag_names(self): ran = self.run_tool("0.9.0") self.assertEqual(ran.returncode, 1) self.assertIn("v0.9.0", ran.stderr) def test_refuses_more_than_one_version(self): ran = self.run_tool("0.1.0", "0.2.0") self.assertEqual(ran.returncode, 1) self.assertIn("one version at most", ran.stderr) def test_refuses_an_unknown_option(self): ran = self.run_tool("--all") self.assertEqual(ran.returncode, 1) self.assertIn("unknown option", ran.stderr) if __name__ == "__main__": unittest.main()