diff --git a/imsgread.py b/imsgread.py index bcccd0d..52b45df 100644 --- a/imsgread.py +++ b/imsgread.py @@ -1109,9 +1109,35 @@ def build_parser() -> argparse.ArgumentParser: return p +class Terminal: + """The person running the command, if there is one. + + `interactive` is False when stdin is not a terminal -- the way an agent + runs. ask() returns None when there is no one to answer, including when + they end input (EOF or Ctrl-C), so a caller treats "no answer" one way. + """ + + @property + def interactive(self) -> bool: + return sys.stdin.isatty() + + def ask(self, prompt: str) -> str | None: + if not self.interactive: + return None + try: + return input(prompt) + except (EOFError, KeyboardInterrupt): + print() + return None + + +TERMINAL = Terminal() + + def _resolve_marker_choice( robot: bool | None, - configured: bool | None = None, + configured: bool | None, + terminal: Terminal, ) -> bool | None: """Decide whether to mark, asking a human but never guessing for a program. @@ -1129,14 +1155,14 @@ def _resolve_marker_choice( return robot if configured is not None: return configured - if not sys.stdin.isatty(): + answer = terminal.ask(f"Mark this as sent by an agent ({AGENT_MARKER})? [Y/n] ") + if answer is None: print( "error: pass --robot or --no-robot when not running interactively", file=sys.stderr, ) return None - answer = input(f"Mark this as sent by an agent ({AGENT_MARKER})? [Y/n] ").strip().lower() - return answer not in {"n", "no"} + return answer.strip().lower() not in {"n", "no"} CONFIG_COMMANDS = """commands: @@ -1156,7 +1182,7 @@ def _print_config(path: Path, entries: dict[str, Contact]) -> None: print(f" {name:<{width}} {contact.identifier} marker: {marker_word(contact.marker)}") -def _run_config(args: argparse.Namespace) -> int: +def _run_config(args: argparse.Namespace, terminal: Terminal) -> int: """Edit contacts.toml, but only for a person sitting at a terminal. A contact's marker setting is allowed to answer for a program that passed @@ -1164,7 +1190,7 @@ def _run_config(args: argparse.Namespace) -> int: Refusing without a tty is what makes that true: an agent cannot give itself permission to send unmarked, because it cannot reach this code at all. """ - if not sys.stdin.isatty(): + if not terminal.interactive: print( "error: config changes must be made by a human at a terminal", file=sys.stderr, @@ -1183,11 +1209,10 @@ def _run_config(args: argparse.Namespace) -> int: print(CONFIG_COMMANDS) while True: - try: - parts = input("> ").strip().split() - except (EOFError, KeyboardInterrupt): - print() + line = terminal.ask("> ") + if line is None: return 0 + parts = line.strip().split() if not parts: continue cmd, rest = parts[0].lower(), parts[1:] @@ -1272,7 +1297,7 @@ def _run_find_groups(args: argparse.Namespace) -> int: return 0 -def _run_send(args: argparse.Namespace) -> int: +def _run_send(args: argparse.Namespace, terminal: Terminal) -> int: if args.message is None and args.message_file is None: print("error: --send needs --message or --message-file", file=sys.stderr) return 1 @@ -1288,7 +1313,7 @@ def _run_send(args: argparse.Namespace) -> int: print(f"error: {exc}", file=sys.stderr) return 1 - robot = _resolve_marker_choice(args.robot, recipient.marker) + robot = _resolve_marker_choice(args.robot, recipient.marker, terminal) if robot is None: return 1 @@ -1309,11 +1334,12 @@ def _run_send(args: argparse.Namespace) -> int: return 0 -def main(argv: list[str] | None = None) -> int: +def main(argv: list[str] | None = None, terminal: Terminal | None = None) -> int: args = build_parser().parse_args(argv) + terminal = terminal or TERMINAL if args.config: - return _run_config(args) + return _run_config(args, terminal) if args.contacts: try: @@ -1349,7 +1375,7 @@ def main(argv: list[str] | None = None) -> int: return _run_find_groups(args) if args.send: - return _run_send(args) + return _run_send(args, terminal) try: book = _open_book(args) diff --git a/test_imsgread.py b/test_imsgread.py index 7771bed..3c09f70 100644 --- a/test_imsgread.py +++ b/test_imsgread.py @@ -32,6 +32,28 @@ def no_real_chat_db(tmp_path, monkeypatch): monkeypatch.setattr(imsgread, "DEFAULT_DB", tmp_path / "no-real-chat.db") +class FakeTerminal: + """A person at a keyboard, scripted: each ask() takes the next reply. + + None for replies means no one is there, the way an agent runs.""" + + def __init__(self, replies=()): + self.interactive = replies is not None + self._replies = iter(replies or ()) + self.prompts = [] + + def ask(self, prompt): + self.prompts.append(prompt) + return next(self._replies, None) if self.interactive else None + + +@pytest.fixture(autouse=True) +def no_one_at_the_terminal(monkeypatch): + """Every test runs the way an agent does unless it hands main() a + FakeTerminal with replies. Nothing may read the real stdin.""" + monkeypatch.setattr(imsgread, "TERMINAL", FakeTerminal(None)) + + def book_ids(book) -> dict[str, str]: return {name: c.identifier for name, c in book.items()} @@ -332,7 +354,6 @@ def test_a_group_with_no_guid_is_refused_rather_than_guessed(messages, send_db, def test_non_interactive_send_must_state_the_marker_choice(messages, send_db, monkeypatch, capsys): """An agent cannot send unmarked by omission.""" - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) code = main(["--send", "--chat", "+15555550123", "--message", "hi", "--confirm-timeout", "0", "--db", str(send_db)]) @@ -343,20 +364,26 @@ def test_non_interactive_send_must_state_the_marker_choice(messages, send_db, mo def test_non_interactive_send_proceeds_when_told(messages, send_db, monkeypatch): - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) assert main(["--send", "--chat", "+15555550123", "--message", "hi", "--robot", "--confirm-timeout", "0", "--db", str(send_db)]) == 0 assert messages.calls == [("chat", "any;-;+15555550123", "hi \U0001F916")] -def test_interactive_send_asks_before_marking(messages, send_db, monkeypatch): - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: True) - monkeypatch.setattr("builtins.input", lambda _: "n") +def test_a_person_at_the_terminal_can_decline_the_marker(messages, send_db): + terminal = FakeTerminal(["n"]) assert main(["--send", "--chat", "+15555550123", "--message", "hi", "--confirm-timeout", "0", - "--db", str(send_db)]) == 0 + "--db", str(send_db)], terminal=terminal) == 0 assert messages.calls[-1][2] == "hi" + assert "Mark this" in terminal.prompts[0] + + +def test_a_person_who_ends_input_at_the_marker_question_sends_nothing(messages, send_db): + """EOF at the prompt is no answer, not consent either way.""" + assert main(["--send", "--chat", "+15555550123", "--message", "hi", "--confirm-timeout", "0", + "--db", str(send_db)], terminal=FakeTerminal([])) != 0 + assert messages.calls == [] # --- Claude skill ------------------------------------------------------------ @@ -542,7 +569,6 @@ def test_a_contact_default_answers_for_a_program(tmp_path, messages, send_db, mo recorded, instead of being refused.""" cfg = write_toml(tmp_path / "c.toml", f'[contacts]\ncolleague = {{ id = "+1555", marker = {stored} }}\n') - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) assert main(send_args(cfg, send_db)) == 0 assert messages.calls[-1][1] == "+1555" @@ -556,7 +582,6 @@ def test_a_contact_default_answers_for_a_program(tmp_path, messages, send_db, mo def test_an_explicit_flag_beats_the_contact_default(tmp_path, messages, send_db, monkeypatch, stored, flag, expected): cfg = write_toml(tmp_path / "c.toml", f'[contacts]\ncolleague = {{ id = "+1555", marker = {stored} }}\n') - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) assert main(send_args(cfg, send_db, flag)) == 0 assert was_marked(messages) is expected @@ -565,7 +590,6 @@ def test_an_explicit_flag_beats_the_contact_default(tmp_path, messages, send_db, def test_a_contact_without_a_setting_still_refuses(tmp_path, messages, send_db, monkeypatch, capsys): """Unchanged behavior for everyone who has not opted in.""" cfg = write_contacts(tmp_path / "c.toml", "colleague", "+1555") - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) assert main(send_args(cfg, send_db)) != 0 assert "--robot" in capsys.readouterr().err @@ -577,7 +601,6 @@ def test_a_raw_chat_identifier_ignores_any_contact_default(tmp_path, messages, s recorded -- even when the same number is configured.""" cfg = write_toml(tmp_path / "c.toml", '[contacts]\ncolleague = { id = "+1555", marker = false }\n') - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) assert main(["--send", "--chat", "+1555", "--message", "hi", "--confirm-timeout", "0", "--contacts-file", str(cfg), "--db", str(send_db)]) != 0 @@ -677,7 +700,6 @@ def test_config_mode_refuses_without_a_terminal(tmp_path, monkeypatch, capsys): from imsgread import main cfg = write_contacts(tmp_path / "c.toml", "colleague", "+1555") - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) assert main(["--config", "--contacts-file", str(cfg)]) != 0 assert "human at a terminal" in capsys.readouterr().err @@ -692,11 +714,9 @@ def test_config_mode_records_a_decision(tmp_path, monkeypatch, word, expected): cfg = write_toml(tmp_path / "c.toml", '[contacts]\ncolleague = { id = "+1555", marker = true }\n') - replies = iter([f"set colleague marker {word}", "quit"]) - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: True) - monkeypatch.setattr("builtins.input", lambda _: next(replies)) + terminal = FakeTerminal([f"set colleague marker {word}", "quit"]) - assert main(["--config", "--contacts-file", str(cfg)]) == 0 + assert main(["--config", "--contacts-file", str(cfg)], terminal=terminal) == 0 assert imsgread.load_contact_entries(cfg)["colleague"].marker is expected @@ -707,11 +727,9 @@ def test_config_mode_adds_a_contact_and_keeps_its_decision(tmp_path, monkeypatch cfg = write_toml(tmp_path / "c.toml", '[contacts]\ncolleague = { id = "+1555", marker = false }\n') - replies = iter(["add family +1777", "add colleague +1888", "quit"]) - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: True) - monkeypatch.setattr("builtins.input", lambda _: next(replies)) + terminal = FakeTerminal(["add family +1777", "add colleague +1888", "quit"]) - assert main(["--config", "--contacts-file", str(cfg)]) == 0 + assert main(["--config", "--contacts-file", str(cfg)], terminal=terminal) == 0 entries = imsgread.load_contact_entries(cfg) assert entries["family"] == imsgread.Contact("+1777", None) assert entries["colleague"] == imsgread.Contact("+1888", False) @@ -721,11 +739,9 @@ def test_config_mode_survives_a_bad_command(tmp_path, monkeypatch, capsys): from imsgread import main cfg = write_contacts(tmp_path / "c.toml", "colleague", "+1555") - replies = iter(["nonsense", "set nobody marker never", "set colleague marker sometimes", "quit"]) - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: True) - monkeypatch.setattr("builtins.input", lambda _: next(replies)) + terminal = FakeTerminal(["nonsense", "set nobody marker never", "set colleague marker sometimes", "quit"]) - assert main(["--config", "--contacts-file", str(cfg)]) == 0 + assert main(["--config", "--contacts-file", str(cfg)], terminal=terminal) == 0 out = capsys.readouterr().out assert "unknown command" in out assert "unknown contact" in out @@ -765,7 +781,6 @@ def test_a_group_send_honors_the_alias_marker(tmp_path, messages, send_db, monke marker setting, exactly like a one-to-one contact.""" cfg = write_toml(tmp_path / "c.toml", '[contacts]\ncrew = { id = "chat9001", marker = true }\n') - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) code = main(["--send", "--contact", "crew", "--message", "hi", "--confirm-timeout", "0", "--contacts-file", str(cfg), "--db", str(send_db)]) @@ -780,15 +795,11 @@ def test_a_one_to_one_send_prefers_the_chat_guid(tmp_path, messages, send_db, mo that way writes the message into the thread and never delivers it.""" cfg = write_toml(tmp_path / "c.toml", '[contacts]\ncolleague = { id = "+15555550123", marker = true }\n') - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) assert main(send_args(cfg, send_db)) == 0 assert messages.calls[-1][:2] == ("chat", "any;-;+15555550123") - - - # -------------------------------------------------------------------------- # delivery confirmation # -------------------------------------------------------------------------- @@ -851,7 +862,6 @@ def test_a_failed_delivery_makes_the_command_fail(tmp_path, send_db, monkeypatch """A send that never arrived must not exit 0. That was the bug.""" cfg = write_toml(tmp_path / "c.toml", '[contacts]\ncolleague = { id = "+1555", marker = true }\n') - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) monkeypatch.setattr(imsgread, "TRANSPORT", FakeTransport(send_db, delivered=0, error=22)) assert main(send_args(cfg, send_db)) == 1 @@ -863,7 +873,6 @@ def test_a_send_without_a_receipt_still_succeeds(tmp_path, send_db, monkeypatch, rather than failing or claiming more than it knows.""" cfg = write_toml(tmp_path / "c.toml", '[contacts]\ncolleague = { id = "+1555", marker = true }\n') - monkeypatch.setattr("imsgread.sys.stdin.isatty", lambda: False) monkeypatch.setattr(imsgread, "TRANSPORT", FakeTransport(send_db, delivered=0, service="SMS")) assert main(send_args(cfg, send_db)) == 0 @@ -1170,7 +1179,6 @@ def test_a_send_with_no_target_or_no_body_sends_nothing(send_db, chat_id, text, assert fake.calls == [] - # --- the Messages adapter: the one place osascript runs ----------------------- def fake_osascript(monkeypatch, *, returncode=0, stderr=""):