Files
tracer-cloud--opensre/tests/interactive_shell/runtime/test_prompt_input_reader.py
T
wehub-resource-sync 4b6817381b
Benchmark image — build + push to ECR (any adapter) / build + push (push) Waiting to run
CI / quality (ubuntu-latest) (push) Waiting to run
CI / test (tools-runtime) (push) Waiting to run
CI / test (e2e-general) (push) Waiting to run
CI / test (cli-runtime) (push) Waiting to run
CI / test (e2e-provider-and-openclaw) (push) Waiting to run
CI / test (integrations-and-misc) (push) Waiting to run
CI / coverage-report (push) Blocked by required conditions
CI / test-kubernetes (push) Waiting to run
CI / should-run-thorough (push) Waiting to run
CI / test-thorough (cloudwatch-demo) (push) Blocked by required conditions
CI / test-thorough (flink-ecs) (push) Blocked by required conditions
CI / test-thorough (upstream-lambda) (push) Blocked by required conditions
CI / test-thorough (prefect-ecs-fargate) (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Blocked by required conditions
Release / publish-release (push) Blocked by required conditions
Release / publish-main-release (push) Blocked by required conditions
Release / prepare (push) Waiting to run
Release / verify (push) Blocked by required conditions
Release / build-python-dist (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Blocked by required conditions
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Waiting to run
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

165 lines
4.6 KiB
Python

"""Tests for prompt input event conversion."""
from __future__ import annotations
import asyncio
import io
import threading
from collections.abc import Callable
from typing import Any
import pytest
from rich.console import Console
from surfaces.interactive_shell.runtime.core.state import ReplState
from surfaces.interactive_shell.runtime.input import (
InputCancelled,
InputClosed,
InputSubmitted,
PromptInputReader,
)
from surfaces.interactive_shell.runtime.input import prompt_input_reader as reader_module
from surfaces.interactive_shell.session import Session
class FakePrompt:
def __init__(self, read: Callable[[], str]) -> None:
self._read = read
async def read_prompt_text(self) -> str:
return self._read()
class SequencePrompt:
def __init__(self, values: list[str]) -> None:
self._values = values
async def read_prompt_text(self) -> str:
return self._values.pop(0)
def _reader(
prompt: Any,
state: ReplState | None = None,
session: Session | None = None,
console: Console | None = None,
) -> PromptInputReader:
return PromptInputReader(
prompt,
state or ReplState(),
session or Session(),
console or Console(file=io.StringIO(), force_terminal=False),
)
def _running_state() -> tuple[ReplState, asyncio.Task[None]]:
state = ReplState()
task = asyncio.create_task(asyncio.sleep(60))
state.start_dispatch(task=task, cancel_event=threading.Event())
return state, task
@pytest.mark.asyncio
async def test_prompt_input_reader_submits_normal_text(
monkeypatch: pytest.MonkeyPatch,
) -> None:
reset_calls = 0
def _reset() -> None:
nonlocal reset_calls
reset_calls += 1
monkeypatch.setattr(reader_module, "repl_reset_ctrl_c_gate", _reset)
event = await _reader(FakePrompt(lambda: "show incidents")).read()
assert event == InputSubmitted("show incidents")
assert reset_calls == 1
@pytest.mark.asyncio
async def test_prompt_input_reader_eof_with_dispatch_running_returns_cancelled() -> None:
state, task = _running_state()
try:
event = await _reader(FakePrompt(lambda: (_ for _ in ()).throw(EOFError)), state).read()
finally:
task.cancel()
await asyncio.gather(task, return_exceptions=True)
assert event == InputCancelled()
@pytest.mark.asyncio
async def test_prompt_input_reader_eof_without_dispatch_renders_resume_hint() -> None:
output = io.StringIO()
session = Session()
session.session_id = "session-123"
console = Console(file=output, force_terminal=False, color_system=None)
event = await _reader(
FakePrompt(lambda: (_ for _ in ()).throw(EOFError)),
session=session,
console=console,
).read()
assert event == InputClosed()
assert "Resume this session with:" in output.getvalue()
assert "/resume session-123" in output.getvalue()
assert "--resume session-123" in output.getvalue()
assert "Goodbye!" in output.getvalue()
@pytest.mark.asyncio
async def test_prompt_input_reader_keyboard_interrupt_with_dispatch_running_returns_cancelled() -> (
None
):
state, task = _running_state()
try:
event = await _reader(
FakePrompt(lambda: (_ for _ in ()).throw(KeyboardInterrupt)),
state,
).read()
finally:
task.cancel()
await asyncio.gather(task, return_exceptions=True)
assert event == InputCancelled()
@pytest.mark.asyncio
async def test_prompt_input_reader_keyboard_interrupt_without_dispatch_uses_ctrl_c_gate(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(reader_module, "repl_prompt_note_ctrl_c", lambda _console, _sid: False)
event = await _reader(FakePrompt(lambda: (_ for _ in ()).throw(KeyboardInterrupt))).read()
assert event == InputCancelled()
@pytest.mark.asyncio
async def test_prompt_input_reader_keyboard_interrupt_without_dispatch_can_close(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(reader_module, "repl_prompt_note_ctrl_c", lambda _console, _sid: True)
event = await _reader(FakePrompt(lambda: (_ for _ in ()).throw(KeyboardInterrupt))).read()
assert event == InputClosed()
@pytest.mark.asyncio
async def test_prompt_input_reader_strips_cpr_sequences() -> None:
event = await _reader(FakePrompt(lambda: "status\x1b[12;80R now")).read()
assert event == InputSubmitted("status now")
@pytest.mark.asyncio
async def test_prompt_input_reader_ignores_cpr_only_input() -> None:
event = await _reader(
SequencePrompt(["\x1b[12;80R", "show status"]),
).read()
assert event == InputSubmitted("show status")