A long scan leaves hundreds of recordings, each with a JSON sidecar of measurements and, where a recogniser heard speech, a transcript. Reading that meant opening files one at a time and guessing which were worth playing. saunterbrowse is a second executable in the same package. Arrow keys move through the recordings; the transcript of whichever is highlighted fills the top of the screen, because that is the part anyone actually wants to read. Enter plays it, handing the file to whichever player is installed -- the recordings are ordinary WAVs, every desktop already has something that plays them, and a browser that cannot start would be worse than one that cannot play. t opens the whole transcript full screen when it is longer than the panel, and says so rather than cutting the end off silently. / filters on the frequency, the name, the identification, or anything that was said, which is the point of it: "was the repeater mentioned" is a question about content. Sidecars are read only for the rows on screen, so a directory of ten thousand recordings opens instantly. Where there is no transcript the panel says which of the reasons applies -- Morse (decoded, and shown), data, a bare carrier, or speech never offered to a recogniser -- because those want different things done about them. It only ever reads. Two things were only found by driving it through a real terminal. sys.stdin.read(1) goes through a buffered text wrapper, which in cbreak mode waits for more bytes than one keypress provides: the program drew its first frame and then hung, while tests against a stand-in stream object passed. It reads the file descriptor now, and the tests drive a pty. And stopping playback signalled only the direct child, so a player that is a wrapper script kept the sound going with nothing on screen to stop it; the whole process group is signalled instead, which is what start_new_session was there for. man saunterbrowse ships beside man bandsaunter, and the two point at each other. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PsWPTweCT6pwxKngvVxcg
657 lines
22 KiB
Python
657 lines
22 KiB
Python
"""saunterbrowse: reading and listening to what a scan collected.
|
|
|
|
The browser never writes to the recordings directory, so every test here is
|
|
about what it *shows* and what it does with a keypress. Playback is checked
|
|
against a recorded command rather than a real one: whether audio came out of
|
|
the speakers is not something a test can see, but which file was handed to
|
|
which player is.
|
|
"""
|
|
import json
|
|
import re
|
|
import time
|
|
import wave
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
from rich.console import Console
|
|
|
|
from bandsaunter.browse import (Browser, Player, PLAYERS, Keyboard, main,
|
|
scan_directory)
|
|
|
|
|
|
# -- fixtures ----------------------------------------------------------------
|
|
|
|
def write_wav(path: Path, seconds: float = 1.0, rate: int = 16_000) -> None:
|
|
n = int(seconds * rate)
|
|
tone = (np.sin(2 * np.pi * 440 * np.arange(n) / rate) * 8000)
|
|
with wave.open(str(path), "wb") as w:
|
|
w.setnchannels(1)
|
|
w.setsampwidth(2)
|
|
w.setframerate(rate)
|
|
w.writeframes(tone.astype(np.int16).tobytes())
|
|
|
|
|
|
def make_capture(directory: Path, mhz: float, when: str, mode: str,
|
|
seconds: float = 1.0, transcript: str = "",
|
|
meta: dict | None = None) -> Path:
|
|
"""One recording and its sidecars, named the way a scan names them."""
|
|
stem = f"{mhz:011.6f}MHz--{when}-{mode}"
|
|
wav = directory / f"{stem}.wav"
|
|
write_wav(wav, seconds)
|
|
if meta is not None:
|
|
body = {"frequency_hz": mhz * 1e6, "hit": meta}
|
|
(directory / f"{stem}.json").write_text(json.dumps(body))
|
|
if transcript:
|
|
(directory / f"{stem}_transcription.txt").write_text(transcript + "\n")
|
|
return wav
|
|
|
|
|
|
@pytest.fixture
|
|
def library(tmp_path):
|
|
"""A small recordings directory covering the cases that render differently."""
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm", 4.0,
|
|
transcript="Net control, this is W1AW, standing by.",
|
|
meta={"category": "voice", "snr_db": 21.5,
|
|
"classification": "Narrowband FM voice",
|
|
"confidence": 0.88, "ctcss_hz": 100.0,
|
|
"band_labels": ["2 m Amateur"]})
|
|
make_capture(tmp_path, 856.5625, "2026-08-22_11_00_00", "fsk", 2.0,
|
|
meta={"category": "trunk", "snr_db": 38.0, "baud": 3600.0,
|
|
"classification": "Motorola SMARTNET / SmartZone "
|
|
"(Type I/II) trunking control channel",
|
|
"confidence": 0.95})
|
|
make_capture(tmp_path, 144.1, "2026-08-22_09_00_00", "cw", 3.0,
|
|
meta={"category": "cw", "morse_text": "VVV DE W1AW",
|
|
"morse_wpm": 18.0, "classification": "CW / Morse"})
|
|
return tmp_path
|
|
|
|
|
|
def browser(directory, width=100, height=30, player=None) -> Browser:
|
|
console = Console(width=width, height=height, force_terminal=True)
|
|
return Browser(directory, console=console,
|
|
player=player if player is not None else Player([]))
|
|
|
|
|
|
def frame(b: Browser) -> str:
|
|
with b.console.capture() as cap:
|
|
b.console.print(b.render())
|
|
return cap.get()
|
|
|
|
|
|
class FakePlayer(Player):
|
|
"""Records what it was asked to play instead of playing it."""
|
|
|
|
def __init__(self):
|
|
super().__init__(["/bin/true"])
|
|
self.played: list[Path] = []
|
|
self.stops = 0
|
|
self._active = False
|
|
|
|
def play(self, cap):
|
|
self.played.append(cap.path)
|
|
self.playing = cap
|
|
self._active = True
|
|
self.error = ""
|
|
|
|
def stop(self):
|
|
if self._active:
|
|
self.stops += 1
|
|
self._active = False
|
|
self.playing = None
|
|
|
|
@property
|
|
def active(self):
|
|
return self._active
|
|
|
|
|
|
# -- reading the directory ---------------------------------------------------
|
|
|
|
def test_every_recording_is_found(library):
|
|
assert len(scan_directory(library)) == 3
|
|
|
|
|
|
def test_the_newest_recording_comes_first(library):
|
|
caps = scan_directory(library)
|
|
assert [c.mode for c in caps] == ["fsk", "nfm", "cw"]
|
|
|
|
|
|
def test_the_filename_alone_gives_frequency_time_and_mode(library):
|
|
cap = [c for c in scan_directory(library) if c.mode == "nfm"][0]
|
|
assert cap.frequency == pytest.approx(146_520_000.0)
|
|
assert cap.when == datetime(2026, 8, 22, 10, 0, 0)
|
|
|
|
|
|
def test_length_comes_from_the_wav_not_the_sidecar(tmp_path):
|
|
"""A combined file grows after its sidecar was written, and a sidecar can
|
|
be missing entirely."""
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm", 2.5,
|
|
meta={"duration": 999.0})
|
|
cap = scan_directory(tmp_path)[0]
|
|
assert cap.duration == pytest.approx(2.5, abs=0.05)
|
|
|
|
|
|
def test_a_recording_with_no_sidecar_is_still_listed(tmp_path):
|
|
write_wav(tmp_path / "0146.520000MHz--2026-08-22_10_00_00-nfm.wav")
|
|
cap = scan_directory(tmp_path)[0]
|
|
assert cap.category == "unknown"
|
|
assert cap.classification == "unclassified"
|
|
|
|
|
|
def test_a_file_the_browser_cannot_name_is_shown_rather_than_hidden(tmp_path):
|
|
write_wav(tmp_path / "something-else.wav")
|
|
caps = scan_directory(tmp_path)
|
|
assert len(caps) == 1 and caps[0].frequency == 0.0
|
|
|
|
|
|
def test_a_combined_per_frequency_file_is_recognised(tmp_path):
|
|
write_wav(tmp_path / "0146.880000MHz.wav")
|
|
cap = scan_directory(tmp_path)[0]
|
|
assert cap.combined
|
|
assert cap.frequency == pytest.approx(146_880_000.0)
|
|
assert cap.category == "combined"
|
|
|
|
|
|
def test_sidecars_are_only_read_when_something_asks(library):
|
|
"""A directory of ten thousand recordings has to open instantly."""
|
|
caps = scan_directory(library)
|
|
assert all(c._meta is None for c in caps)
|
|
_ = caps[0].category
|
|
assert caps[0]._meta is not None
|
|
|
|
|
|
def test_a_missing_directory_is_not_an_exception(tmp_path):
|
|
assert scan_directory(tmp_path / "nowhere") == []
|
|
|
|
|
|
# -- the transcript ----------------------------------------------------------
|
|
|
|
def test_the_transcript_is_at_the_top_of_the_screen(library):
|
|
b = browser(library)
|
|
b.index = [i for i, c in enumerate(b.view) if c.mode == "nfm"][0]
|
|
out = frame(b)
|
|
head = out.split("recordings in")[0]
|
|
assert "Net control, this is W1AW" in head
|
|
assert "transcript" in head
|
|
|
|
|
|
def test_the_txt_beside_the_recording_beats_the_sidecar(tmp_path):
|
|
"""The sidecar records what was recognised at the time; the file is what a
|
|
later re-run wrote."""
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
|
|
transcript="the newer text",
|
|
meta={"transcript": "the older text", "category": "voice"})
|
|
assert scan_directory(tmp_path)[0].transcript == "the newer text"
|
|
|
|
|
|
def test_the_sidecar_transcript_is_used_when_there_is_no_txt(tmp_path):
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
|
|
meta={"transcript": "only in the sidecar", "category": "voice"})
|
|
assert scan_directory(tmp_path)[0].transcript == "only in the sidecar"
|
|
|
|
|
|
@pytest.mark.parametrize("meta,expected", [
|
|
({"category": "digital"}, "data, not speech"),
|
|
({"category": "trunk"}, "data, not speech"),
|
|
({"category": "carrier"}, "silence by definition"),
|
|
({"category": "cw"}, "Morse"),
|
|
({"category": "voice"}, "no transcript beside it"),
|
|
])
|
|
def test_an_empty_transcript_says_which_reason_applies(tmp_path, meta,
|
|
expected):
|
|
""""No transcript" is the same shape for Morse, for data and for a
|
|
recogniser that was never installed, and those want different things
|
|
done about them."""
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm", meta=meta)
|
|
b = browser(tmp_path)
|
|
assert expected in frame(b)
|
|
|
|
|
|
def test_decoded_morse_is_shown_where_the_transcript_would_be(library):
|
|
b = browser(library)
|
|
b.index = [i for i, c in enumerate(b.view) if c.mode == "cw"][0]
|
|
assert "VVV DE W1AW" in frame(b)
|
|
|
|
|
|
def test_a_transcript_too_long_for_the_panel_says_so(tmp_path):
|
|
"""Cutting the end off a transmission silently is how a reader ends up
|
|
believing they have read all of it."""
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
|
|
transcript=" ".join(f"word{i}" for i in range(400)),
|
|
meta={"category": "voice"})
|
|
out = frame(browser(tmp_path, height=24))
|
|
assert "more line(s)" in out and "press t" in out
|
|
|
|
|
|
def test_the_reader_shows_what_the_panel_could_not(tmp_path):
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
|
|
transcript=" ".join(f"word{i}" for i in range(400)),
|
|
meta={"category": "voice"})
|
|
b = browser(tmp_path, height=24)
|
|
panel_text = frame(b)
|
|
b.handle("t")
|
|
assert b.reading
|
|
reader = frame(b)
|
|
assert "word0" in reader
|
|
assert "word200" in reader and "word200" not in panel_text
|
|
|
|
|
|
def test_the_reader_scrolls_and_comes_back(tmp_path):
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
|
|
transcript=" ".join(f"word{i}" for i in range(400)),
|
|
meta={"category": "voice"})
|
|
b = browser(tmp_path, height=20)
|
|
b.handle("t")
|
|
first = frame(b)
|
|
b.handle("pgdn")
|
|
assert frame(b) != first
|
|
b.handle("home")
|
|
assert frame(b) == first
|
|
b.handle("escape")
|
|
assert not b.reading
|
|
|
|
|
|
def test_the_reader_refuses_when_there_is_nothing_to_read(library):
|
|
b = browser(library)
|
|
b.index = [i for i, c in enumerate(b.view) if c.mode == "fsk"][0]
|
|
b.handle("t")
|
|
assert not b.reading
|
|
assert "no transcript" in b.message
|
|
|
|
|
|
# -- the frame ---------------------------------------------------------------
|
|
|
|
@pytest.mark.parametrize("width,height",
|
|
[(60, 16), (80, 24), (100, 30), (200, 60)])
|
|
def test_the_frame_never_overflows_the_terminal(library, width, height):
|
|
"""A frame taller than the terminal cannot be redrawn in place: every
|
|
refresh scrolls another copy of it into the scrollback."""
|
|
b = browser(library, width=width, height=height)
|
|
lines = frame(b).rstrip("\n").split("\n")
|
|
assert len(lines) <= height, f"{len(lines)} lines in {height}"
|
|
bare = [re.sub(r"\x1b\[[0-9;]*m", "", line) for line in lines]
|
|
assert max(len(line) for line in bare) <= width, "a line ran off the side"
|
|
|
|
|
|
def test_one_row_per_recording_however_long_the_transcript(tmp_path):
|
|
"""A wrapped row would push the ones below it off the bottom, and the
|
|
cursor arithmetic counts rows."""
|
|
for i in range(6):
|
|
make_capture(tmp_path, 146.0 + i, f"2026-08-22_10_0{i}_00", "nfm",
|
|
transcript=" ".join(f"word{n}" for n in range(200)),
|
|
meta={"category": "voice"})
|
|
b = browser(tmp_path, height=30)
|
|
body = frame(b).split("recordings in")[1]
|
|
assert body.count("word0") == 6, "a row wrapped instead of truncating"
|
|
|
|
|
|
def test_the_header_names_the_frequency_mode_and_category(library):
|
|
b = browser(library)
|
|
b.index = [i for i, c in enumerate(b.view) if c.mode == "nfm"][0]
|
|
head = frame(b).split("transcript")[0]
|
|
assert "146.52 MHz" in head and "NFM" in head and "voice" in head
|
|
|
|
|
|
def test_a_symbol_rate_is_only_shown_where_it_means_something(library):
|
|
"""The estimator returns a figure for every capture, and "120 baud"
|
|
beside a conversation is noise."""
|
|
b = browser(library)
|
|
b.index = [i for i, c in enumerate(b.view) if c.mode == "fsk"][0]
|
|
assert "3600 baud" in frame(b)
|
|
b.index = [i for i, c in enumerate(b.view) if c.mode == "nfm"][0]
|
|
assert "baud" not in frame(b)
|
|
|
|
|
|
def test_an_empty_directory_renders_rather_than_crashing(tmp_path):
|
|
b = browser(tmp_path)
|
|
assert "nothing here yet" in frame(b)
|
|
|
|
|
|
# -- moving around -----------------------------------------------------------
|
|
|
|
def test_the_arrow_keys_move_the_cursor(library):
|
|
b = browser(library)
|
|
assert b.index == 0
|
|
b.handle("down")
|
|
assert b.index == 1
|
|
b.handle("up")
|
|
assert b.index == 0
|
|
|
|
|
|
def test_the_cursor_stops_at_both_ends(library):
|
|
b = browser(library)
|
|
for _ in range(20):
|
|
b.handle("up")
|
|
assert b.index == 0
|
|
for _ in range(20):
|
|
b.handle("down")
|
|
assert b.index == len(b.view) - 1
|
|
|
|
|
|
def test_home_and_end_go_to_the_ends(library):
|
|
b = browser(library)
|
|
b.handle("end")
|
|
assert b.index == len(b.view) - 1
|
|
b.handle("home")
|
|
assert b.index == 0
|
|
|
|
|
|
def test_the_cursor_stays_on_screen_in_a_long_list(tmp_path):
|
|
for i in range(60):
|
|
make_capture(tmp_path, 146.0, f"2026-08-22_10_{i // 60:02d}_{i % 60:02d}",
|
|
"nfm", meta={"category": "voice"})
|
|
b = browser(tmp_path, height=24)
|
|
b.handle("end")
|
|
frame(b)
|
|
assert b.top <= b.index < b.top + b._rows()
|
|
|
|
|
|
def test_sorting_cycles_and_reorders(library):
|
|
b = browser(library)
|
|
assert b.sort == "time"
|
|
b.handle("s")
|
|
assert b.sort == "frequency"
|
|
assert [c.frequency for c in b.view] == sorted(c.frequency
|
|
for c in b.view)
|
|
b.handle("s")
|
|
assert b.sort == "duration"
|
|
assert b.view[0].duration >= b.view[-1].duration
|
|
|
|
|
|
def test_quitting_stops_the_loop(library):
|
|
assert browser(library).handle("q") is False
|
|
|
|
|
|
# -- searching ---------------------------------------------------------------
|
|
|
|
def test_search_matches_what_was_said(library):
|
|
"""The point of it: "did anyone mention the repeater" is a question about
|
|
content, not about filenames."""
|
|
b = browser(library)
|
|
b.handle("/")
|
|
for ch in "standing by":
|
|
b.handle(ch)
|
|
b.handle("enter")
|
|
assert len(b.view) == 1 and b.view[0].mode == "nfm"
|
|
|
|
|
|
def test_search_matches_the_identification(library):
|
|
b = browser(library)
|
|
b.query = "smartnet"
|
|
b.apply()
|
|
assert len(b.view) == 1 and b.view[0].mode == "fsk"
|
|
|
|
|
|
def test_search_matches_the_frequency_in_the_name(library):
|
|
b = browser(library)
|
|
b.query = "0856"
|
|
b.apply()
|
|
assert len(b.view) == 1
|
|
|
|
|
|
def test_backspace_edits_the_search(library):
|
|
b = browser(library)
|
|
b.handle("/")
|
|
for ch in "smartnetX":
|
|
b.handle(ch)
|
|
assert b.view == []
|
|
b.handle("backspace")
|
|
assert len(b.view) == 1
|
|
|
|
|
|
def test_escape_clears_the_search_before_it_quits(library):
|
|
"""Escape with a filter in force means "show me everything again", not
|
|
"throw the program away"."""
|
|
b = browser(library)
|
|
b.query = "smartnet"
|
|
b.apply()
|
|
assert b.handle("escape") is True
|
|
assert b.query == "" and len(b.view) == 3
|
|
assert b.handle("escape") is False
|
|
|
|
|
|
def test_a_search_that_matches_nothing_says_so(library):
|
|
b = browser(library)
|
|
b.query = "no such thing"
|
|
b.apply()
|
|
assert "nothing matches" in frame(b)
|
|
|
|
|
|
def test_keys_typed_while_searching_are_not_commands(library):
|
|
"""'q' has to be a letter in the search box, not an instruction to quit."""
|
|
b = browser(library)
|
|
b.handle("/")
|
|
assert b.handle("q") is True
|
|
assert b.query == "q"
|
|
|
|
|
|
# -- playing -----------------------------------------------------------------
|
|
|
|
def test_enter_plays_the_highlighted_recording(library):
|
|
p = FakePlayer()
|
|
b = browser(library, player=p)
|
|
b.handle("down")
|
|
b.handle("enter")
|
|
assert p.played == [b.current.path]
|
|
|
|
|
|
def test_space_stops_what_is_playing(library):
|
|
p = FakePlayer()
|
|
b = browser(library, player=p)
|
|
b.handle("enter")
|
|
b.handle("space")
|
|
assert p.stops == 1 and not p.active
|
|
|
|
|
|
def test_playing_a_second_recording_stops_the_first(tmp_path):
|
|
"""Two players talking over each other is worse than either alone."""
|
|
calls = []
|
|
|
|
class Recorder(Player):
|
|
def play(self, cap):
|
|
calls.append(("play", cap.path.name))
|
|
self.playing = cap
|
|
|
|
def stop(self):
|
|
calls.append(("stop", None))
|
|
|
|
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm")
|
|
make_capture(tmp_path, 147.52, "2026-08-22_10_01_00", "nfm")
|
|
b = browser(tmp_path, player=Recorder([]))
|
|
b.handle("enter")
|
|
b.handle("down")
|
|
b.handle("enter")
|
|
assert [c[0] for c in calls] == ["play", "play"]
|
|
|
|
|
|
def test_the_real_player_stops_the_previous_file_first(library, tmp_path):
|
|
"""The stop is in Player.play, so every caller gets it."""
|
|
p = Player(["/bin/sleep", "5"])
|
|
caps = scan_directory(library)
|
|
p.play(caps[0])
|
|
first = p.proc
|
|
assert first is not None
|
|
p.play(caps[1])
|
|
assert first.poll() is not None, "the first player was left running"
|
|
p.stop()
|
|
|
|
|
|
def test_stopping_kills_the_player_and_anything_it_started(library, tmp_path):
|
|
"""Several of these players are wrapper scripts that fork the real one.
|
|
Signalling only the script leaves the sound playing with nothing on
|
|
screen to stop it."""
|
|
import subprocess as sp
|
|
marker = tmp_path / "child.pid"
|
|
script = tmp_path / "wrapper.sh"
|
|
script.write_text("#!/bin/sh\nsleep 30 &\necho $! > %s\nwait\n" % marker)
|
|
script.chmod(0o755)
|
|
|
|
p = Player([str(script)])
|
|
p.play(scan_directory(library)[0])
|
|
for _ in range(50):
|
|
if marker.exists():
|
|
break
|
|
time.sleep(0.05)
|
|
child = int(marker.read_text().strip())
|
|
assert sp.run(["kill", "-0", str(child)]).returncode == 0, "never started"
|
|
p.stop()
|
|
for _ in range(50):
|
|
if sp.run(["kill", "-0", str(child)],
|
|
capture_output=True).returncode != 0:
|
|
break
|
|
time.sleep(0.05)
|
|
assert sp.run(["kill", "-0", str(child)],
|
|
capture_output=True).returncode != 0, "still playing"
|
|
|
|
|
|
def test_no_player_installed_is_reported_not_crashed(library):
|
|
b = browser(library, player=Player([])) # [] means "none installed"
|
|
b.handle("enter")
|
|
assert "no audio player" in b.message
|
|
assert all(name in b.message for name, _ in PLAYERS)
|
|
|
|
|
|
def test_the_file_is_the_last_argument_to_the_player(library):
|
|
p = Player(["/bin/true", "--flag"])
|
|
cap = scan_directory(library)[0]
|
|
p.play(cap)
|
|
assert p.command == ["/bin/true", "--flag"]
|
|
assert p.playing is cap
|
|
p.stop()
|
|
|
|
|
|
def test_playback_progress_is_shown(library):
|
|
p = FakePlayer()
|
|
b = browser(library, player=p)
|
|
b.handle("enter")
|
|
p.started_at = 0.0 # started long ago: the bar is full
|
|
assert "♪" in frame(b)
|
|
|
|
|
|
# -- the keyboard ------------------------------------------------------------
|
|
|
|
# Driven through a real pty, not a stand-in object. A fake stream with a
|
|
# read() method passed every one of these while the program hung on the first
|
|
# keypress: sys.stdin.read(1) goes through a buffered text wrapper, which in
|
|
# cbreak mode waits for more bytes than a single key provides.
|
|
|
|
|
|
@pytest.fixture
|
|
def terminal():
|
|
"""A pty whose slave end is what Keyboard will read."""
|
|
import os
|
|
import pty
|
|
master, slave = pty.openpty()
|
|
stream = os.fdopen(slave, "r")
|
|
yield master, stream
|
|
for fd in (master,):
|
|
try:
|
|
os.close(fd)
|
|
except OSError:
|
|
pass
|
|
try:
|
|
stream.close()
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
@pytest.mark.parametrize("raw,name", [
|
|
("\x1b[A", "up"), ("\x1b[B", "down"), ("\x1b[C", "right"),
|
|
("\x1b[D", "left"), ("\x1b[5~", "pgup"), ("\x1b[6~", "pgdn"),
|
|
("\x1b[H", "home"), ("\x1b[F", "end"),
|
|
("\x1bOA", "up"), ("\x1bOB", "down"),
|
|
("\r", "enter"), ("\n", "enter"), (" ", "space"),
|
|
("\x7f", "backspace"), ("q", "q"), ("/", "/"), ("t", "t"),
|
|
])
|
|
def test_a_key_arrives_as_one_key(terminal, raw, name):
|
|
"""An arrow is "\\x1b[A" -- three bytes. Read one at a time it becomes
|
|
three commands, and the cursor jumps somewhere unasked."""
|
|
import os
|
|
master, stream = terminal
|
|
with Keyboard(stream) as kb:
|
|
assert kb.enabled
|
|
os.write(master, raw.encode())
|
|
assert kb.get(0.5) == name
|
|
assert kb.get(0.05) == "", "one keypress produced more than one key"
|
|
|
|
|
|
def test_a_bare_escape_is_not_mistaken_for_an_arrow(terminal):
|
|
import os
|
|
master, stream = terminal
|
|
with Keyboard(stream) as kb:
|
|
os.write(master, b"\x1b")
|
|
assert kb.get(0.5) == "escape"
|
|
|
|
|
|
def test_keys_arriving_together_are_delivered_one_at_a_time(terminal):
|
|
"""Held down, or pasted, several keys land in a single read."""
|
|
import os
|
|
master, stream = terminal
|
|
with Keyboard(stream) as kb:
|
|
os.write(master, b"\x1b[B\x1b[Bq")
|
|
assert [kb.get(0.5) for _ in range(3)] == ["down", "down", "q"]
|
|
|
|
|
|
def test_nothing_pressed_returns_nothing(terminal):
|
|
_, stream = terminal
|
|
with Keyboard(stream) as kb:
|
|
assert kb.get(0.05) == ""
|
|
|
|
|
|
def test_the_terminal_is_restored_afterwards(terminal):
|
|
"""Left in cbreak, the shell that follows has no line editing and no echo."""
|
|
import termios
|
|
_, stream = terminal
|
|
before = termios.tcgetattr(stream.fileno())
|
|
with Keyboard(stream) as kb:
|
|
assert kb.enabled
|
|
assert termios.tcgetattr(stream.fileno()) != before
|
|
assert termios.tcgetattr(stream.fileno()) == before
|
|
|
|
|
|
def test_a_stream_that_is_not_a_terminal_is_not_an_error(tmp_path):
|
|
path = tmp_path / "notatty"
|
|
path.write_text("q")
|
|
with open(path) as fh, Keyboard(fh) as kb:
|
|
assert not kb.enabled
|
|
assert kb.get(0.0) == ""
|
|
|
|
|
|
# -- the command line --------------------------------------------------------
|
|
|
|
def test_list_prints_one_line_per_recording(library, capsys):
|
|
assert main(["--list", str(library)]) == 0
|
|
lines = [ln for ln in capsys.readouterr().out.splitlines() if ln.strip()]
|
|
assert len(lines) == 3
|
|
assert any("Net control" in ln for ln in lines)
|
|
|
|
|
|
def test_list_honours_the_filter(library, capsys):
|
|
assert main(["--list", "--filter", "smartnet", str(library)]) == 0
|
|
lines = [ln for ln in capsys.readouterr().out.splitlines() if ln.strip()]
|
|
assert len(lines) == 1
|
|
|
|
|
|
def test_a_missing_directory_is_explained(tmp_path, capsys):
|
|
assert main([str(tmp_path / "nowhere")]) == 2
|
|
assert "no such directory" in capsys.readouterr().out
|
|
|
|
|
|
def test_an_empty_directory_is_explained(tmp_path, capsys):
|
|
assert main([str(tmp_path)]) == 1
|
|
assert "no recordings" in capsys.readouterr().out
|
|
|
|
|
|
def test_it_refuses_to_draw_where_there_is_no_terminal(library, capsys):
|
|
"""Piped or redirected, the full-screen frame would be gibberish."""
|
|
assert main([str(library)]) == 2
|
|
out = capsys.readouterr().out
|
|
assert "needs a terminal" in out and "--list" in out
|
|
|
|
|
|
def test_the_output_directory_is_found_without_being_told(monkeypatch,
|
|
tmp_path):
|
|
monkeypatch.setenv("BANDSAUNTER_OUTPUT", str(tmp_path))
|
|
from bandsaunter.browse import default_directory
|
|
assert default_directory() == tmp_path
|