fix: resolve terminal display wobble and effect dimension stability

- Fix TerminalDisplay: add screen clear each frame (cursor home + erase down)
- Fix CameraStage: use set_canvas_size instead of read-only viewport properties
- Fix Glitch effect: preserve visible line lengths, remove cursor positioning
- Fix Fade effect: return original line when fade=0 instead of empty string
- Fix Noise effect: use input line length instead of terminal_width
- Remove HUD effect from all presets (redundant with border FPS display)
- Add regression tests for effect dimension stability
- Add docs/ARCHITECTURE.md with Mermaid diagrams
- Add mise tasks: diagram-ascii, diagram-validate, diagram-check
- Move markdown docs to docs/ (ARCHITECTURE, Refactor, hardware specs)
- Remove redundant requirements files (use pyproject.toml)
- Add *.dot and *.png to .gitignore

Closes #25
This commit is contained in:
2026-03-18 03:34:36 -07:00
parent a65fb50464
commit b926b346ad
30 changed files with 1472 additions and 40 deletions

View File

@@ -2,6 +2,7 @@
Tests for engine.display module.
"""
import sys
from unittest.mock import MagicMock
from engine.display import DisplayRegistry, NullDisplay, TerminalDisplay
@@ -115,6 +116,83 @@ class TestTerminalDisplay:
display = TerminalDisplay()
display.cleanup()
def test_get_dimensions_returns_cached_value(self):
"""get_dimensions returns cached dimensions for stability."""
display = TerminalDisplay()
display.init(80, 24)
# First call should set cache
d1 = display.get_dimensions()
assert d1 == (80, 24)
def test_show_clears_screen_before_each_frame(self):
"""show clears previous frame to prevent visual wobble.
Regression test: Previously show() didn't clear the screen,
causing old content to remain and creating visual wobble.
The fix adds \\033[H\\033[J (cursor home + erase down) before each frame.
"""
from io import BytesIO
from unittest.mock import patch
display = TerminalDisplay()
display.init(80, 24)
buffer = ["line1", "line2", "line3"]
fake_buffer = BytesIO()
fake_stdout = MagicMock()
fake_stdout.buffer = fake_buffer
with patch.object(sys, "stdout", fake_stdout):
display.show(buffer)
output = fake_buffer.getvalue().decode("utf-8")
assert output.startswith("\033[H\033[J"), (
f"Output should start with clear sequence, got: {repr(output[:20])}"
)
def test_show_clears_screen_on_subsequent_frames(self):
"""show clears screen on every frame, not just the first.
Regression test: Ensures each show() call includes the clear sequence.
"""
from io import BytesIO
from unittest.mock import patch
# Use target_fps=0 to disable frame skipping in test
display = TerminalDisplay(target_fps=0)
display.init(80, 24)
buffer = ["line1", "line2"]
for i in range(3):
fake_buffer = BytesIO()
fake_stdout = MagicMock()
fake_stdout.buffer = fake_buffer
with patch.object(sys, "stdout", fake_stdout):
display.show(buffer)
output = fake_buffer.getvalue().decode("utf-8")
assert output.startswith("\033[H\033[J"), (
f"Frame {i} should start with clear sequence"
)
def test_get_dimensions_stable_across_rapid_calls(self):
"""get_dimensions should not fluctuate when called rapidly.
This test catches the bug where os.get_terminal_size() returns
inconsistent values, causing visual wobble.
"""
display = TerminalDisplay()
display.init(80, 24)
# Get dimensions 10 times rapidly (simulating frame loop)
dims = [display.get_dimensions() for _ in range(10)]
# All should be the same - this would fail if os.get_terminal_size()
# returns different values each call
assert len(set(dims)) == 1, f"Dimensions should be stable, got: {set(dims)}"
class TestNullDisplay:
"""Tests for NullDisplay class."""
@@ -141,6 +219,27 @@ class TestNullDisplay:
display = NullDisplay()
display.cleanup()
def test_show_stores_last_buffer(self):
"""show stores last buffer for testing inspection."""
display = NullDisplay()
display.init(80, 24)
buffer = ["line1", "line2", "line3"]
display.show(buffer)
assert display._last_buffer == buffer
def test_show_tracks_last_buffer_across_calls(self):
"""show updates last_buffer on each call."""
display = NullDisplay()
display.init(80, 24)
display.show(["first"])
assert display._last_buffer == ["first"]
display.show(["second"])
assert display._last_buffer == ["second"]
class TestMultiDisplay:
"""Tests for MultiDisplay class."""