Cyberpunk 2077 look for the server view, refreshed every second
Rajdhani font (OFL), chamfered/glitch drawing kit, Docker list every 5 s and process ranking over 3 s windows so the 1 s refresh stays cheap and calm. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
edba341f30
commit
5e64bfb64e
15 files changed
+524
-95
No files matched your search
@@ -7,3 +7,11 @@ def count_colour(img: Image.Image, colour, box=None) -> int:
|
||||
if box is not None:
|
||||
img = img.crop(box)
|
||||
return int((np.asarray(img.convert("RGB")) == np.array(colour)).all(axis=2).sum())
|
||||
|
||||
|
||||
def count_near(img: Image.Image, colour, tolerance: int = 70, box=None) -> int:
|
||||
"""Pixels within `tolerance` (per channel) of `colour`: for anti-aliased text, which is never exactly pure."""
|
||||
if box is not None:
|
||||
img = img.crop(box)
|
||||
diff = np.abs(np.asarray(img.convert("RGB")).astype(int) - np.array(colour))
|
||||
return int((diff.max(axis=2) <= tolerance).sum())
|
||||
@@ -0,0 +1,78 @@
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
import cyber
|
||||
from cyber import BG, CYAN, DIM, RED, SCAN, YELLOW
|
||||
from tests.helpers import count_colour, count_near
|
||||
|
||||
|
||||
def canvas(w=200, h=100):
|
||||
img = Image.new("RGB", (w, h), BG)
|
||||
return img, ImageDraw.Draw(img)
|
||||
|
||||
|
||||
def test_background_has_scanlines_and_is_a_private_copy():
|
||||
a = cyber.background()
|
||||
assert a.size == (480, 320)
|
||||
assert a.getpixel((5, 0)) == SCAN and a.getpixel((5, 1)) == BG
|
||||
a.putpixel((5, 1), (1, 2, 3))
|
||||
assert cyber.background().getpixel((5, 1)) == BG # the cached original is untouched
|
||||
|
||||
|
||||
def test_chamfer_cuts_the_requested_corner_only():
|
||||
img, d = canvas()
|
||||
cyber.chamfer(d, (10, 10, 90, 50), 12, fill=YELLOW, cuts="tl")
|
||||
assert img.getpixel((11, 11)) == BG # cut away
|
||||
assert img.getpixel((88, 48)) == YELLOW # the bottom-right corner is square
|
||||
assert img.getpixel((30, 30)) == YELLOW
|
||||
|
||||
|
||||
def test_glitch_text_shows_all_three_colours():
|
||||
img, d = canvas()
|
||||
cyber.glitch_text(d, (20, 10), "88", cyber.font(48))
|
||||
for colour in (YELLOW, CYAN, RED): # the fringes are anti-aliased, so compare with a tolerance
|
||||
assert count_near(img, colour) > 0
|
||||
|
||||
|
||||
def test_slanted_bar_lights_proportionally():
|
||||
img, d = canvas()
|
||||
cyber.slanted_bar(d, 10, 10, 50, 10, 10, 8, 2)
|
||||
assert count_colour(img, YELLOW) > 0 and count_colour(img, DIM) > 0
|
||||
lit, unlit = count_colour(img, YELLOW), count_colour(img, DIM)
|
||||
assert abs(lit - unlit) <= lit * 0.15 # 5 of 10 segments
|
||||
|
||||
|
||||
def test_slanted_bar_turns_red_when_hot():
|
||||
img, d = canvas()
|
||||
cyber.slanted_bar(d, 10, 10, 90, 10, 10, 8, 2)
|
||||
assert count_colour(img, RED) > 0 and count_colour(img, YELLOW) == 0
|
||||
|
||||
|
||||
def test_slanted_bar_none_is_all_dim():
|
||||
img, d = canvas()
|
||||
cyber.slanted_bar(d, 10, 10, None, 10, 10, 8, 2)
|
||||
assert count_colour(img, DIM) > 0
|
||||
assert count_colour(img, YELLOW) == 0 and count_colour(img, RED) == 0
|
||||
|
||||
|
||||
def test_tag_is_sized_from_its_text_and_returns_its_box():
|
||||
img, d = canvas(300, 60)
|
||||
f = cyber.font(22)
|
||||
short = cyber.tag(d, (10, 10), "HI", f)
|
||||
long_ = cyber.tag(d, (10, 10), "HOME // SERVER", f)
|
||||
assert (long_[2] - long_[0]) > (short[2] - short[0])
|
||||
assert (long_[2] - long_[0]) >= d.textlength("HOME // SERVER", font=f)
|
||||
assert count_colour(img, YELLOW) > 0
|
||||
|
||||
|
||||
def test_tag_colour_can_be_changed():
|
||||
img, d = canvas()
|
||||
cyber.tag(d, (10, 10), "STARÉ", cyber.font(14), colour=RED)
|
||||
assert count_colour(img, RED) > 0 and count_colour(img, YELLOW) == 0
|
||||
|
||||
|
||||
def test_fit_shortens_long_text_with_an_ellipsis():
|
||||
img, d = canvas()
|
||||
f = cyber.font(15)
|
||||
text = cyber.fit(d, "A-VERY-LONG-CONTAINER-NAME-INDEED", f, 80)
|
||||
assert text.endswith("…") and d.textlength(text, font=f) <= 80
|
||||
assert cyber.fit(d, "SHORT", f, 80) == "SHORT"
|
||||
@@ -22,3 +22,9 @@ def test_make_view_unknown_lists_valid_names():
|
||||
def test_make_view_empty_name_is_unknown():
|
||||
with pytest.raises(UnknownView):
|
||||
make_view("")
|
||||
|
||||
|
||||
def test_server_view_polls_every_second_and_weather_every_quarter_hour():
|
||||
import registry
|
||||
assert registry.SERVER_REFRESH_S == 1
|
||||
assert registry.WEATHER_REFRESH_S == 15 * 60
|
||||
+47
-12
@@ -2,10 +2,10 @@ from datetime import datetime
|
||||
|
||||
from PIL import ImageChops
|
||||
|
||||
from lcdkit import INK
|
||||
from cyber import DIM, RED, TEXT
|
||||
from server_view import ServerView
|
||||
from stats_data import Container, Proc, Stats
|
||||
from tests.helpers import count_colour
|
||||
from tests.helpers import count_colour, count_near
|
||||
|
||||
NOW = datetime(2026, 10, 22, 14, 32, 8)
|
||||
|
||||
@@ -49,10 +49,13 @@ def test_render_size_is_480x320():
|
||||
assert view(stats()).render(NOW).size == (480, 320)
|
||||
|
||||
|
||||
def test_frame_key_changes_every_five_seconds():
|
||||
v = view(stats())
|
||||
assert v.frame_key(datetime(2026, 10, 22, 14, 32, 5)) == v.frame_key(datetime(2026, 10, 22, 14, 32, 9))
|
||||
assert v.frame_key(datetime(2026, 10, 22, 14, 32, 9)) != v.frame_key(datetime(2026, 10, 22, 14, 32, 10))
|
||||
def test_frame_key_follows_the_data_not_the_clock():
|
||||
p = StubPoller(stats(), version=1)
|
||||
v = ServerView(p)
|
||||
before = v.frame_key(datetime(2026, 10, 22, 14, 32, 5))
|
||||
assert before == v.frame_key(datetime(2026, 10, 22, 14, 32, 40)) # the clock does not matter
|
||||
p.version = 2
|
||||
assert v.frame_key(datetime(2026, 10, 22, 14, 32, 5)) != before # new data does
|
||||
|
||||
|
||||
def test_frame_key_changes_on_new_data_and_stale_flip():
|
||||
@@ -89,14 +92,21 @@ def test_stale_tag_only_when_old():
|
||||
old = view(stats(), age=ServerView.STALE_AFTER + 5).render(NOW)
|
||||
bbox = region_diff(fresh, old)
|
||||
assert bbox is not None
|
||||
assert bbox[0] >= 360 and bbox[1] >= 12 and bbox[3] <= 40
|
||||
assert bbox[0] >= 360 and bbox[1] >= 12 and bbox[3] <= 44 # only the top-right status corner changes
|
||||
|
||||
|
||||
def test_bar_reflects_value():
|
||||
def test_gauge_reflects_value():
|
||||
idle = view(stats(cpu_pct=0.0)).render(NOW)
|
||||
busy = view(stats(cpu_pct=100.0)).render(NOW)
|
||||
box = (72, 54, 232, 78) # the CPU bar
|
||||
assert count_colour(busy, INK, box) > count_colour(idle, INK, box) + 500
|
||||
busy = view(stats(cpu_pct=60.0)).render(NOW)
|
||||
box = (24, 76, 246, 86) # the CPU gauge
|
||||
assert count_colour(busy, DIM, box) < count_colour(idle, DIM, box) - 500
|
||||
|
||||
|
||||
def test_hot_gauge_turns_red():
|
||||
box = (24, 76, 246, 86)
|
||||
calm = view(stats(cpu_pct=20.0)).render(NOW)
|
||||
hot = view(stats(cpu_pct=95.0)).render(NOW)
|
||||
assert count_colour(hot, RED, box) > count_colour(calm, RED, box) + 500
|
||||
|
||||
|
||||
def test_no_docker_differs_from_container_list():
|
||||
@@ -104,7 +114,7 @@ def test_no_docker_differs_from_container_list():
|
||||
missing = view(stats(containers=None)).render(NOW)
|
||||
bbox = region_diff(listed, missing)
|
||||
assert bbox is not None
|
||||
assert bbox[1] >= 160 and bbox[2] <= 304 # only the docker panel changes
|
||||
assert bbox[1] >= 176 and bbox[2] <= 322 # only the docker panel changes
|
||||
|
||||
|
||||
def test_attention_containers_sort_first():
|
||||
@@ -113,3 +123,28 @@ def test_attention_containers_sort_first():
|
||||
first_rows = view(stats(containers=fine + (sick,))).render(NOW)
|
||||
sorted_ok = view(stats(containers=(sick,) + fine)).render(NOW)
|
||||
assert region_diff(first_rows, sorted_ok) is None # input order does not matter
|
||||
|
||||
|
||||
def test_failed_container_gets_a_red_chip():
|
||||
ok = view(stats(containers=containers(3))).render(NOW)
|
||||
broken = view(stats(containers=containers(2) + (Container("zzz", "exited", None),))).render(NOW)
|
||||
panel = (24, 196, 322, 300)
|
||||
assert count_colour(broken, RED, panel) > count_colour(ok, RED, panel) + 100
|
||||
|
||||
|
||||
def test_long_container_name_stays_in_its_column():
|
||||
long_name = Container("an-extraordinarily-long-container-name-that-would-run-into-the-process-list", "running", "healthy")
|
||||
img = view(stats(containers=(long_name,) + containers(2))).render(NOW)
|
||||
process_column = (326, 196, 470, 300)
|
||||
assert count_near(img, TEXT, 20, process_column) == 0 # container names are the only near-white text
|
||||
|
||||
|
||||
def test_status_chip_is_wide_enough_for_its_label():
|
||||
import numpy as np
|
||||
from cyber import INK
|
||||
img = view(stats(containers=(Container("web", "running", "unhealthy"),))).render(NOW)
|
||||
panel = np.asarray(img.crop((24, 196, 322, 300)).convert("RGB"))
|
||||
ys, xs = np.where((panel == np.array(RED)).all(axis=2))
|
||||
ink_ys, ink_xs = np.where((panel == np.array(INK)).all(axis=2)) # the label is drawn in INK on the chip
|
||||
assert len(xs) > 0 and len(ink_xs) > 0
|
||||
assert ink_xs.min() > xs.min() and ink_xs.max() < xs.max() # the label sits inside the chip, not past its edges
|
||||
@@ -80,12 +80,14 @@ class FakeDocker:
|
||||
|
||||
def __init__(self, path: str, body: bytes = b"[]", hang: bool = False):
|
||||
self.release = threading.Event()
|
||||
self.requests = 0
|
||||
outer = self
|
||||
|
||||
class Handler(socketserver.StreamRequestHandler):
|
||||
def handle(self):
|
||||
while self.rfile.readline() not in (b"\r\n", b""):
|
||||
pass
|
||||
outer.requests += 1
|
||||
if hang:
|
||||
outer.release.wait(5)
|
||||
return
|
||||
@@ -111,6 +113,7 @@ def docker_in(tmp_path, monkeypatch):
|
||||
def make(body=b"[]", hang=False):
|
||||
s = FakeDocker("d.sock", body, hang)
|
||||
servers.append(s)
|
||||
make.server = s
|
||||
return "d.sock"
|
||||
|
||||
yield make
|
||||
@@ -159,6 +162,8 @@ def thermal(sys_root: Path, zones: dict[str, tuple[str, int]]):
|
||||
|
||||
|
||||
def make_reader(tmp_path, docker_sock="missing.sock", **kw):
|
||||
kw.setdefault("docker_every", 0.0)
|
||||
kw.setdefault("proc_every", 0.0)
|
||||
proc, sys_root = tmp_path / "proc", tmp_path / "sys"
|
||||
thermal(sys_root, {"thermal_zone0": ("acpitz", 30000), "thermal_zone1": ("x86_pkg_temp", 41000)})
|
||||
write_proc(proc, "cpu 100 0 50 800 50 0 0 0 0 0", {10: pid_line(10, "postgres", 0), 11: pid_line(11, "postgres", 0),
|
||||
@@ -239,3 +244,49 @@ def test_reader_missing_proc_files_give_none_not_error(tmp_path):
|
||||
s = reader.read()
|
||||
assert (s.cpu_pct, s.mem_pct, s.disk_pct, s.load, s.temp_c, s.uptime_s, s.containers) == (None,) * 7
|
||||
assert s.procs == ()
|
||||
|
||||
|
||||
class FakeClock:
|
||||
def __init__(self):
|
||||
self.t = 0.0
|
||||
|
||||
def __call__(self):
|
||||
return self.t
|
||||
|
||||
|
||||
def test_reader_asks_docker_only_every_docker_every_seconds(tmp_path, docker_in):
|
||||
sock = docker_in((FIXTURES / "docker_containers.json").read_bytes())
|
||||
clock = FakeClock()
|
||||
reader, _ = make_reader(tmp_path, docker_sock=sock, docker_every=5.0, clock=clock)
|
||||
first = reader.read()
|
||||
clock.t = 1.0
|
||||
second = reader.read()
|
||||
assert docker_in.server.requests == 1
|
||||
assert second.containers == first.containers and second.containers is not None
|
||||
clock.t = 5.5
|
||||
reader.read()
|
||||
assert docker_in.server.requests == 2
|
||||
|
||||
|
||||
def test_reader_caches_a_docker_failure_for_the_interval_too(tmp_path):
|
||||
clock = FakeClock()
|
||||
reader, _ = make_reader(tmp_path, docker_sock=str(tmp_path / "nope.sock"), docker_every=5.0, clock=clock)
|
||||
assert reader.read().containers is None
|
||||
clock.t = 1.0
|
||||
assert reader.read().containers is None # no new attempt, still reported as unavailable
|
||||
|
||||
|
||||
def test_reader_ranks_processes_over_a_longer_window(tmp_path):
|
||||
clock = FakeClock()
|
||||
reader, proc = make_reader(tmp_path, proc_every=3.0, clock=clock)
|
||||
assert reader.read().procs == () # first snapshot
|
||||
clock.t = 1.0
|
||||
write_proc(proc, "cpu 600 0 300 2400 100 0 0 0 0 0", {10: pid_line(10, "postgres", 10)})
|
||||
assert reader.read().procs == () # window not over yet: nothing new to rank
|
||||
clock.t = 3.5
|
||||
write_proc(proc, "cpu 900 0 450 3600 150 0 0 0 0 0", {10: pid_line(10, "postgres", 60)})
|
||||
s = reader.read()
|
||||
# 60 ticks over a jiffy window of 5100 - 1000 = 4100 -> 1.46 %
|
||||
assert [(p.name, round(p.cpu_pct, 2)) for p in s.procs] == [("postgres", 1.46)]
|
||||
clock.t = 4.0
|
||||
assert reader.read().procs == s.procs # between windows the last ranking stays on screen
|
||||
Reference in new issue
Block a user