Answer the architecture pass's first round

This commit is contained in:
2026-10-02 12:16:33 +02:00
parent f51900106f
commit e1707183fd
3 changed files with 92 additions and 40 deletions
+15 -5
View File
@@ -1,5 +1,6 @@
import datetime
import json
import os
import pathlib
import subprocess
import sys
@@ -17,7 +18,7 @@ UTC = datetime.timezone.utc
def render(status, branch="", home="/home/me"):
return statusline.render(status, branch, home, NOW, UTC)
return statusline.render(statusline.parse(status), branch, home, NOW, UTC)
class Render(unittest.TestCase):
@@ -37,10 +38,15 @@ class Render(unittest.TestCase):
]),
]))
def test_shows_nothing_for_an_empty_or_unparsed_status(self):
def test_shows_nothing_for_an_empty_status(self):
self.assertEqual(render({}), "")
self.assertEqual(render([]), "")
self.assertEqual(render(None), "")
def test_skips_infinity_and_keeps_an_integer_too_big_for_a_float(self):
self.assertEqual(render({"context_window": {"used_percentage": float("inf")}}), "")
self.assertEqual(render({"context_window": {"used_percentage": 10**400}}), f"{DIM}ctx{RESET} {RED}{10**400}%{RESET}")
def test_skips_a_field_of_the_wrong_type(self):
status = {"context_window": {"used_percentage": "42"}, "cwd": 7, "model": {"display_name": None}, "rate_limits": {"five_hour": {"used_percentage": True}}}
self.assertEqual(render(status), "")
@@ -66,21 +72,25 @@ class Render(unittest.TestCase):
def test_humanizes_token_counts(self):
self.assertEqual([statusline.humanize_tokens(n) for n in (999, 1000, 999_999, 1_250_000)], ["999", "1k", "999k", "1.2M"])
def test_omits_a_reset_out_of_the_clocks_range(self):
self.assertEqual(render({"rate_limits": {"seven_day": {"resets_at": 1790618085238, "used_percentage": 1}}}), f"{DIM}7d{RESET} {GREEN}1%{RESET}")
self.assertEqual(render({"rate_limits": {"seven_day": {"resets_at": 1e20, "used_percentage": 1}}}), f"{DIM}7d{RESET} {GREEN}1%{RESET}")
def test_counts_a_past_reset_as_zero_left(self):
self.assertEqual(statusline.reset_suffix(NOW - 60, NOW, UTC), f" {DIM}0m left (11:59){RESET}")
class Script(unittest.TestCase):
def run_script(self, stdin):
return subprocess.run([sys.executable, str(pathlib.Path(statusline.__file__))], capture_output=True, env={"HOME": "/nonexistent", "PATH": "/usr/bin:/bin"}, input=stdin, text=True)
return subprocess.run([statusline.__file__], capture_output=True, env={"HOME": "/nonexistent", "PATH": os.environ["PATH"]}, input=stdin, text=True)
def test_prints_the_status_without_a_trailing_newline(self):
ran = self.run_script(json.dumps({"cwd": "/nonexistent/a", "model": {"display_name": "Opus"}}))
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, SEP.join([f"{CYAN}~/a{RESET}", f"{DIM}Opus{RESET}"]), ""))
def test_prints_nothing_for_input_that_is_not_json(self):
def test_warns_on_the_status_line_for_input_that_is_not_json(self):
ran = self.run_script("not json")
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, "", ""))
self.assertEqual((ran.returncode, ran.stdout, ran.stderr), (0, f"{RED}warning: statusline.py could not parse its input as JSON{RESET}", ""))
def test_shows_the_branch_of_the_directory(self):
with tempfile.TemporaryDirectory() as checkout: