Files
hermes-agent/tests/hermes_cli/test_prompt_stash.py
ethernet 890bbbda1f Merge remote-tracking branch 'origin/main' into ethie/pm-clean
# Conflicts:
#	apps/desktop/e2e/archived-hidden-session-recoverable.spec.ts
#	apps/desktop/e2e/bot-chat-message-agent-friendly-name.spec.ts
#	apps/desktop/e2e/bot-mailbox-unreadable-ticket.spec.ts
#	apps/desktop/e2e/bot-mode-roster-localized.spec.ts
#	apps/desktop/e2e/bot-mode-row-click-mirrors-registry.spec.ts
#	apps/desktop/e2e/bot-mode-tab-shows-bot-name.spec.ts
#	apps/desktop/e2e/bot-roster-group-row-organisation.spec.ts
#	apps/desktop/e2e/bot-roster-ignores-infra-dirs.spec.ts
#	apps/desktop/e2e/bot-roster-timestamp-meta.spec.ts
#	apps/desktop/e2e/bot-roster-user-sections.spec.ts
#	apps/desktop/e2e/bot-routines-pane-narrow.spec.ts
#	apps/desktop/e2e/bot-row-open-recent-session.spec.ts
#	apps/desktop/e2e/bot-tile-ignores-ambient-composer-model.spec.ts
#	apps/desktop/e2e/group-composer-auto-grow.spec.ts
#	apps/desktop/e2e/group-create-gate-remote-roster.spec.ts
#	apps/desktop/e2e/group-prompt-renamed-primary-handle.spec.ts
#	apps/desktop/e2e/hosted-room-backend-continuity.spec.ts
#	apps/desktop/e2e/hosted-room-legacy-store-migration.spec.ts
#	apps/desktop/e2e/settings-scope-chips-bot-title.spec.ts
#	apps/desktop/e2e/worktree-branch-status.spec.ts
#	apps/desktop/electron/backend-probes.test.ts
#	apps/desktop/electron/connection-apply.test.ts
#	apps/desktop/electron/desktop-electron-pin.test.ts
#	apps/desktop/electron/desktop-uninstall.test.ts
#	apps/desktop/electron/gateway-file-download-transport.test.ts
#	apps/desktop/electron/gateway-stop-before-update.test.ts
#	apps/desktop/electron/github-api-auth.test.ts
#	apps/desktop/electron/registry-primary-profile-scope.test.ts
#	apps/desktop/electron/update-api-check.test.ts
#	apps/desktop/electron/update-handoff-marker.test.ts
#	apps/desktop/electron/venv-blocker-scan.test.ts
#	apps/desktop/scripts/after-extract.test.mjs
#	apps/desktop/scripts/local-pack-publish.test.mjs
#	apps/desktop/scripts/tasks-scroll.test.mjs
#	apps/desktop/src/app/settings/model-settings.test.tsx
#	apps/desktop/src/app/updates-overlay.blockers.test.tsx
#	apps/desktop/src/components/desktop-install-overlay.test.tsx
#	apps/desktop/src/lib/update-copy.test.ts
#	scripts/ci/check_os_marker_fakes.py
#	tests-js/desktop-mac-usage-descriptions.test.ts
#	tests-js/node-engine-alignment.test.ts
#	tests/agent/lsp/test_install_and_lint_fixes.py
#	tests/agent/test_command_token_source.py
#	tests/agent/test_compression_boundary_hook.py
#	tests/agent/test_create_openai_client_ssl_verify.py
#	tests/agent/test_custom_provider_ca_probes.py
#	tests/agent/test_endpoint_blackhole.py
#	tests/agent/test_estimator_parity.py
#	tests/agent/test_in_place_compaction.py
#	tests/agent/test_moa_loop_mode.py
#	tests/agent/test_model_metadata.py
#	tests/agent/test_skill_session_platform_gate.py
#	tests/agent/test_skill_utils.py
#	tests/agent/test_ssl_ca_guard.py
#	tests/computer_use/test_doctor.py
#	tests/cron/test_codex_execution_paths.py
#	tests/cron/test_cron_bot_chat_delivery.py
#	tests/cron/test_cron_script.py
#	tests/cron/test_media_delivery_parity.py
#	tests/cron/test_misfire_catchup.py
#	tests/cron/test_parallel_pool.py
#	tests/cron/test_recurring_eagain_redispatch.py
#	tests/gateway/test_choice_picker.py
#	tests/gateway/test_control_socket_windows_live.py
#	tests/gateway/test_dingtalk.py
#	tests/gateway/test_feishu.py
#	tests/gateway/test_feishu_onboard.py
#	tests/gateway/test_gateway_shutdown.py
#	tests/gateway/test_matrix.py
#	tests/gateway/test_model_command_custom_providers.py
#	tests/gateway/test_reasoning_command.py
#	tests/gateway/test_runtime_footer.py
#	tests/gateway/test_session.py
#	tests/gateway/test_session_hygiene.py
#	tests/gateway/test_status.py
#	tests/gateway/test_teams.py
#	tests/gateway/test_turn_lease.py
#	tests/gateway/test_whatsapp_connect.py
#	tests/hermes_cli/test_approvals_command.py
#	tests/hermes_cli/test_auth_store_lock_concurrent.py
#	tests/hermes_cli/test_backup.py
#	tests/hermes_cli/test_banner_git_state.py
#	tests/hermes_cli/test_certifi_repair.py
#	tests/hermes_cli/test_cmd_update.py
#	tests/hermes_cli/test_compat_manifest_targets.py
#	tests/hermes_cli/test_computer_use_cli.py
#	tests/hermes_cli/test_cpr_local_leak.py
#	tests/hermes_cli/test_dashboard_auth_gate.py
#	tests/hermes_cli/test_dashboard_procs_kill_grace.py
#	tests/hermes_cli/test_desktop_lifecycle_windows_live.py
#	tests/hermes_cli/test_doctor.py
#	tests/hermes_cli/test_doctor_command_install.py
#	tests/hermes_cli/test_fleet_config_migration_windows_live.py
#	tests/hermes_cli/test_gateway.py
#	tests/hermes_cli/test_gateway_platform_gating.py
#	tests/hermes_cli/test_gateway_restart_loop.py
#	tests/hermes_cli/test_gateway_task_probe.py
#	tests/hermes_cli/test_gateway_wsl.py
#	tests/hermes_cli/test_gui_command.py
#	tests/hermes_cli/test_install_cua_driver.py
#	tests/hermes_cli/test_kanban_db.py
#	tests/hermes_cli/test_lazy_command_exports.py
#	tests/hermes_cli/test_lazy_refresh_venv_repair.py
#	tests/hermes_cli/test_linux_desktop_entry.py
#	tests/hermes_cli/test_local_runtime.py
#	tests/hermes_cli/test_local_runtime_updates.py
#	tests/hermes_cli/test_managed_uv.py
#	tests/hermes_cli/test_mcp_reload_confirm_gate.py
#	tests/hermes_cli/test_nous_subscription.py
#	tests/hermes_cli/test_npm_engine.py
#	tests/hermes_cli/test_personality_none.py
#	tests/hermes_cli/test_pet_toggle.py
#	tests/hermes_cli/test_plan_reconciliation_windows_live.py
#	tests/hermes_cli/test_plugin_event_bus.py
#	tests/hermes_cli/test_plugin_manifest_v2.py
#	tests/hermes_cli/test_plugin_packs.py
#	tests/hermes_cli/test_plugins_cmd.py
#	tests/hermes_cli/test_plugins_cmd_enable_disable_nested.py
#	tests/hermes_cli/test_process_identity.py
#	tests/hermes_cli/test_profiles.py
#	tests/hermes_cli/test_profiles_sidebar_cache.py
#	tests/hermes_cli/test_pty_bridge.py
#	tests/hermes_cli/test_resolve_turn_limit.py
#	tests/hermes_cli/test_serve_runtime_inventory.py
#	tests/hermes_cli/test_session_vacuum_config.py
#	tests/hermes_cli/test_set_config_value.py
#	tests/hermes_cli/test_signal_handler_kanban_worker.py
#	tests/hermes_cli/test_slash_confirm_windows.py
#	tests/hermes_cli/test_stale_pid_guard.py
#	tests/hermes_cli/test_startup_fast_guards.py
#	tests/hermes_cli/test_status.py
#	tests/hermes_cli/test_telegram_managed_bot.py
#	tests/hermes_cli/test_tools_config.py
#	tests/hermes_cli/test_update_apply_shallow_count.py
#	tests/hermes_cli/test_update_autostash.py
#	tests/hermes_cli/test_update_concurrent_quarantine.py
#	tests/hermes_cli/test_update_fetch_failure_classifier.py
#	tests/hermes_cli/test_update_fleet_probe_resume_token.py
#	tests/hermes_cli/test_update_handoff_backend_reap.py
#	tests/hermes_cli/test_update_handoff_desktop_rebuild.py
#	tests/hermes_cli/test_update_head_moved_gate.py
#	tests/hermes_cli/test_update_host_obligation.py
#	tests/hermes_cli/test_update_import_guard.py
#	tests/hermes_cli/test_update_interrupted_recovery.py
#	tests/hermes_cli/test_update_inventory.py
#	tests/hermes_cli/test_update_launchd_unloaded_gateway.py
#	tests/hermes_cli/test_update_missing_configured_deps.py
#	tests/hermes_cli/test_update_modified_notice.py
#	tests/hermes_cli/test_update_multiplex_migration_hook.py
#	tests/hermes_cli/test_update_no_gateway_restart.py
#	tests/hermes_cli/test_update_orphan_backend_reap.py
#	tests/hermes_cli/test_update_parked_branch_guard.py
#	tests/hermes_cli/test_update_post_pull_syntax_guard.py
#	tests/hermes_cli/test_update_receipt.py
#	tests/hermes_cli/test_update_self_lock.py
#	tests/hermes_cli/test_update_shim_fail_closed.py
#	tests/hermes_cli/test_update_shim_self_lock.py
#	tests/hermes_cli/test_update_sqlite_remediation.py
#	tests/hermes_cli/test_update_stale_dashboard.py
#	tests/hermes_cli/test_update_stale_virtualenv.py
#	tests/hermes_cli/test_update_venv_health.py
#	tests/hermes_cli/test_update_venv_ownership_preflight.py
#	tests/hermes_cli/test_update_wedged_gateway.py
#	tests/hermes_cli/test_update_yes_flag.py
#	tests/hermes_cli/test_update_zip_two_phase.py
#	tests/hermes_cli/test_urllib_security.py
#	tests/hermes_cli/test_ux_messages_auth_config.py
#	tests/hermes_cli/test_ux_messages_startup.py
#	tests/hermes_cli/test_venv_holder_classifier.py
#	tests/hermes_cli/test_verify_console_scripts.py
#	tests/hermes_cli/test_verify_core_dependencies.py
#	tests/hermes_cli/test_web_server.py
#	tests/hermes_cli/test_web_server_console_ws.py
#	tests/hermes_cli/test_web_server_ws_ping.py
#	tests/hermes_cli/test_web_ui_build.py
#	tests/hermes_state/test_fts_rebuild_admission.py
#	tests/hermes_state/test_hermes_state.py
#	tests/plugins/memory/test_memory_lazy_install.py
#	tests/plugins/test_google_meet_plugin.py
#	tests/plugins/test_langfuse_plugin.py
#	tests/plugins/test_security_guidance_plugin.py
#	tests/plugins/test_transform_llm_output_hook.py
#	tests/scripts/desktop_update/test_desktop_update_windows_gateway_flag.py
#	tests/scripts/desktop_update/test_desktop_update_windows_python_handoff.py
#	tests/scripts/desktop_update/test_desktop_update_windows_timestamp.py
#	tests/scripts/install/test_install_clone_throttle_fallback.py
#	tests/scripts/install/test_install_lockfile_churn.py
#	tests/scripts/install/test_install_no_initial_commit.py
#	tests/scripts/install/test_install_sh_browser_install.py
#	tests/scripts/install/test_install_sh_node_prerelease.py
#	tests/scripts/install/test_install_sh_symlink_stomp.py
#	tests/scripts/install/test_install_sh_uv_lock_config.py
#	tests/scripts/install/test_install_unmerged_index.py
#	tests/scripts/test_contributor_map.py
#	tests/scripts/test_run_tests_parallel.py
#	tests/skills/test_competitor_news_monitor_skill.py
#	tests/skills/test_document_to_action_items_skill.py
#	tests/skills/test_google_workspace_setup.py
#	tests/skills/test_google_workspace_setup_deps.py
#	tests/skills/test_grounded_citations_skill.py
#	tests/skills/test_ip_as_logo_skill.py
#	tests/skills/test_live_dashboard_skill.py
#	tests/skills/test_mcp_oauth_remote_gateway_skill.py
#	tests/skills/test_office_document_skills.py
#	tests/skills/test_openclaw_migration.py
#	tests/skills/test_product_price_monitor_skill.py
#	tests/skills/test_scrollcraft_skill.py
#	tests/skills/test_setup_wizard_generator_skill.py
#	tests/skills/test_weekly_review_planning_skill.py
#	tests/test_engines_satisfiable.py
#	tests/test_fast_safe_load.py
#	tests/test_hermes_bootstrap.py
#	tests/test_hermes_constants.py
#	tests/test_hermes_logging.py
#	tests/test_managed_runtime_resolution.py
#	tests/test_model_tools_async_bridge.py
#	tests/test_packaging_build_guard.py
#	tests/test_packaging_metadata.py
#	tests/test_yaml_indent_consistency.py
#	tests/tools/test_approval_timeout_overflow.py
#	tests/tools/test_base_environment.py
#	tests/tools/test_bot_mode_dm.py
#	tests/tools/test_browser_chromium_check.py
#	tests/tools/test_browser_hardening.py
#	tests/tools/test_browser_homebrew_paths.py
#	tests/tools/test_browser_npx_warmup.py
#	tests/tools/test_browser_orphan_reaper.py
#	tests/tools/test_browser_real_profile.py
#	tests/tools/test_browser_use_cli.py
#	tests/tools/test_clipboard.py
#	tests/tools/test_code_execution.py
#	tests/tools/test_code_execution_modes.py
#	tests/tools/test_code_execution_windows_env.py
#	tests/tools/test_computer_use.py
#	tests/tools/test_delegate_liveness_timeout.py
#	tests/tools/test_execute_code_approval_cluster.py
#	tests/tools/test_execution_flag_detection.py
#	tests/tools/test_fal_common.py
#	tests/tools/test_file_operations.py
#	tests/tools/test_file_tools.py
#	tests/tools/test_file_tools_cwd_resolution.py
#	tests/tools/test_file_tools_live.py
#	tests/tools/test_lazy_deps.py
#	tests/tools/test_lazy_deps_durable_target.py
#	tests/tools/test_lazy_deps_managed.py
#	tests/tools/test_local_env_blocklist.py
#	tests/tools/test_local_tempdir.py
#	tests/tools/test_macos_protected_search.py
#	tests/tools/test_mcp_npx_cached_bin.py
#	tests/tools/test_oneshot_completion_linger.py
#	tests/tools/test_process_registry.py
#	tests/tools/test_read_file_schema_gating.py
#	tests/tools/test_skill_improvements.py
#	tests/tools/test_skills_sync.py
#	tests/tools/test_termux_api_detection.py
#	tests/tools/test_tirith_security.py
#	tests/tools/test_transcription_tools.py
#	tests/tools/test_tts_streaming.py
#	tests/tools/test_wake_word.py
#	tests/tui_gateway/test_compute_host_borrowed_lease.py
#	tests/tui_gateway/test_compute_host_turn_protocol.py
#	tests/tui_gateway/test_isolated_orphan_activity.py
#	tests/tui_gateway/test_protocol.py
#	tests/tui_gateway/test_slash_worker_profile_home.py
#	tests/tui_gateway/test_subprocess_encoding.py
#	tests/tui_gateway/test_tui_gateway_server.py
#	ui-tui/src/__tests__/terminalParity.test.ts
#	ui-tui/src/__tests__/termuxComposerLayout.test.ts
#	ui-tui/src/__tests__/textInputFastEcho.test.ts
2026-09-23 07:02:44 -04:00

361 lines
12 KiB
Python

"""Tests for the Ctrl+S prompt stash state machine (hermes_cli.prompt_stash).
Covers the pure state machine directly — no prompt_toolkit, no TUI:
- stashing an empty/whitespace buffer is a no-op
- stash → restore round-trips exact text including newlines
- repeated stashes never silently clobber an earlier draft
- indicator / placeholder state
- browse-panel cursor, delete, and restore
- the resolve_ctrl_s decision table
"""
from __future__ import annotations
import pytest
from hermes_cli.prompt_stash import (
ACTION_CLOSE_PANEL,
ACTION_NOOP,
ACTION_OPEN_PANEL,
ACTION_RESTORED,
ACTION_STASHED,
MAX_STASH_ITEMS,
PromptStash,
StashEntry,
build_preview,
resolve_ctrl_s,
)
class _FakeClock:
"""Deterministic monotonic clock for age assertions."""
def __init__(self, start: float = 1000.0):
self.now = start
def __call__(self) -> float:
return self.now
def advance(self, secs: float) -> None:
self.now += secs
@pytest.fixture
def stash():
return PromptStash(clock=_FakeClock())
# --------------------------------------------------------------- no-op cases
class TestStashNoOp:
"""An empty or whitespace-only composer must not create a stash entry."""
@pytest.mark.parametrize("text", ["", " \n \n ", None])
def test_stash_blank_buffer_is_noop(self, stash, text):
assert stash.stash(text) is False
assert len(stash) == 0
assert stash.indicator() == ""
def test_pop_empty_stash_returns_none(self, stash):
assert stash.pop() is None
def test_open_panel_on_empty_stash_refused(self, stash):
assert stash.open_panel() is False
assert stash.panel_open is False
def test_delete_on_empty_stash_is_noop(self, stash):
assert stash.delete_at_cursor() is False
def test_restore_at_cursor_on_empty_stash(self, stash):
assert stash.restore_at_cursor() is None
def test_images_only_draft_is_stashable(self, stash):
"""Blank text but attached images is still worth parking."""
assert stash.stash("", ["/tmp/a.png"]) is True
assert len(stash) == 1
assert stash.peek().preview == "(images only)"
# ------------------------------------------------------------- round-tripping
class TestRoundTrip:
"""Restore must return the draft byte-for-byte."""
@pytest.mark.parametrize(
"text",
[
"\n leading blank and indented\n",
"para one\n\npara two\n\n\npara three",
"tabs\there\tand\there ",
"unicode ünïcödé 中文 🎉 mixed",
],
)
def test_stash_then_restore_round_trips_exactly(self, stash, text):
assert stash.stash(text) is True
result = stash.pop()
assert result is not None
restored, images = result
assert restored == text
assert images == []
# Popping consumed the entry.
assert len(stash) == 0
def test_round_trip_through_resolve_ctrl_s(self, stash):
"""The full gesture: Ctrl+S to park, Ctrl+S on empty to bring back."""
draft = "a long prompt\nwith several lines\n"
action, payload = resolve_ctrl_s(stash, draft)
assert action == ACTION_STASHED
assert payload is None
assert len(stash) == 1
action, payload = resolve_ctrl_s(stash, "")
assert action == ACTION_RESTORED
assert payload == (draft, [])
assert len(stash) == 0
def test_images_round_trip(self, stash):
imgs = ["/tmp/one.png", "/tmp/two.png"]
stash.stash("with pics", imgs)
text, restored = stash.pop()
assert text == "with pics"
assert restored == imgs
# The stash must hold its own copy — mutating the caller's list after
# stashing cannot corrupt the parked entry.
imgs.append("/tmp/three.png")
assert restored == ["/tmp/one.png", "/tmp/two.png"]
# --------------------------------------------------------------- no clobbering
class TestNoSilentClobber:
"""A second Ctrl+S must not destroy the first draft."""
def test_second_stash_keeps_first(self, stash):
stash.stash("first draft")
stash.stash("second draft")
assert len(stash) == 2
texts = [e.text for e in stash.items]
assert "first draft" in texts
assert "second draft" in texts
def test_newest_first_ordering(self, stash):
stash.stash("oldest")
stash.stash("middle")
stash.stash("newest")
assert [e.text for e in stash.items] == ["newest", "middle", "oldest"]
# Default pop takes the most recent — the "undo my last Ctrl+S" case.
assert stash.pop()[0] == "newest"
def test_two_items_opens_panel_instead_of_guessing(self, stash):
"""With 2+ drafts, Ctrl+S must not silently pick one."""
stash.stash("first")
stash.stash("second")
action, payload = resolve_ctrl_s(stash, "")
assert action == ACTION_OPEN_PANEL
assert payload is None
assert stash.panel_open is True
# Nothing was consumed.
assert len(stash) == 2
def test_stash_cap_drops_oldest_not_newest(self):
s = PromptStash(max_items=3, clock=_FakeClock())
for i in range(5):
s.stash(f"draft {i}")
assert len(s) == 3
assert [e.text for e in s.items] == ["draft 4", "draft 3", "draft 2"]
def test_default_cap_is_bounded(self, stash):
for i in range(MAX_STASH_ITEMS + 10):
stash.stash(f"d{i}")
assert len(stash) == MAX_STASH_ITEMS
# -------------------------------------------------------------- indicator state
class TestIndicatorState:
def test_empty_stash_has_no_indicator(self, stash):
assert stash.indicator() == ""
assert stash.placeholder_hint() == ""
assert bool(stash) is False
def test_count_grows_with_stash(self, stash):
stash.stash("a")
assert stash.indicator() == "📌 1"
stash.stash("b")
assert stash.indicator() == "📌 2"
stash.stash("c")
assert stash.indicator() == "📌 3"
def test_indicator_clears_after_restoring_last_item(self, stash):
stash.stash("only")
stash.pop()
assert stash.indicator() == ""
def test_placeholder_hint_single_shows_preview(self, stash):
stash.stash("write the migration guide")
hint = stash.placeholder_hint()
assert "Ctrl+S" in hint
assert "write the migration guide" in hint
def test_clear_resets_all_state(self, stash):
stash.stash("a")
stash.stash("b")
stash.open_panel()
stash.clear()
assert len(stash) == 0
assert stash.panel_open is False
assert stash.panel_cursor == 0
assert stash.indicator() == ""
# ----------------------------------------------------------------- previewing
class TestBuildPreview:
def test_empty_text(self):
assert build_preview("") == ""
def test_single_line_passthrough(self):
assert build_preview("hello world") == "hello world"
def test_newlines_collapse_to_marker(self):
assert build_preview("a\nb") == "a ⏎ b"
def test_crlf_normalized(self):
assert build_preview("a\r\nb") == "a ⏎ b"
def test_preview_is_always_single_line(self):
preview = build_preview("x\n" * 30, width=200)
assert "\n" not in preview
def test_long_text_ellipsized_to_width(self):
preview = build_preview("y" * 500, width=20)
assert len(preview) == 20
assert preview.endswith("…")
def test_whitespace_runs_collapsed(self):
assert build_preview("a b\t\tc") == "a b c"
class TestStashEntry:
def test_as_dict_shape_matches_panel_renderer(self, stash):
stash.stash("draft text")
row = stash.panel_rows()[0]
# _render_stash_panel indexes these exact keys.
assert set(row) >= {"text", "images", "stashed_at", "preview"}
assert row["text"] == "draft text"
assert row["preview"] == "draft text"
def test_as_dict_copies_images(self):
imgs = ["/tmp/a.png"]
entry = StashEntry(text="t", images=imgs)
entry.as_dict()["images"].append("/tmp/b.png")
assert imgs == ["/tmp/a.png"]
def test_stashed_at_uses_injected_clock(self):
clock = _FakeClock(start=500.0)
s = PromptStash(clock=clock)
s.stash("first")
clock.advance(60)
s.stash("second")
entries = s.items
assert entries[0].stashed_at == 560.0 # newest first
assert entries[1].stashed_at == 500.0
# --------------------------------------------------------------- panel browsing
class TestPanelBrowsing:
@pytest.fixture
def three(self):
s = PromptStash(clock=_FakeClock())
s.stash("oldest")
s.stash("middle")
s.stash("newest")
s.open_panel()
return s
def test_open_panel_starts_at_top(self, three):
assert three.panel_open is True
assert three.panel_cursor == 0
def test_cursor_moves_and_clamps(self, three):
assert three.move_cursor(1) == 1
assert three.move_cursor(1) == 2
# Clamped at the bottom — no wraparound, no IndexError.
assert three.move_cursor(1) == 2
assert three.move_cursor(-5) == 0
def test_restore_at_cursor_picks_highlighted_entry(self, three):
three.move_cursor(1) # "middle"
result = three.restore_at_cursor()
assert result == ("middle", [])
assert three.panel_open is False
assert [e.text for e in three.items] == ["newest", "oldest"]
def test_delete_at_cursor_removes_only_that_entry(self, three):
three.move_cursor(1)
assert three.delete_at_cursor() is True
assert [e.text for e in three.items] == ["newest", "oldest"]
assert three.panel_open is True
def test_delete_last_row_reclamps_cursor(self, three):
three.move_cursor(2) # bottom row
three.delete_at_cursor()
assert three.panel_cursor == 1 # clamped into the shortened list
def test_deleting_everything_closes_panel(self, three):
for _ in range(3):
three.delete_at_cursor()
assert len(three) == 0
assert three.panel_open is False
assert three.panel_cursor == 0
def test_new_stash_closes_open_panel(self, three):
three.stash("brand new")
assert three.panel_open is False
assert three.panel_cursor == 0
def test_panel_rows_ordered_newest_first(self, three):
assert [r["text"] for r in three.panel_rows()] == [
"newest",
"middle",
"oldest",
]
def test_pop_out_of_range_is_none(self, three):
assert three.pop(99) is None
assert three.pop(-1) is None
assert len(three) == 3
# ------------------------------------------------------ resolve_ctrl_s table
class TestResolveCtrlS:
def test_empty_buffer_empty_stash_is_noop(self, stash):
assert resolve_ctrl_s(stash, "") == (ACTION_NOOP, None)
def test_content_stashes(self, stash):
action, payload = resolve_ctrl_s(stash, "some draft")
assert (action, payload) == (ACTION_STASHED, None)
assert len(stash) == 1
def test_open_panel_then_ctrl_s_closes_it(self, stash):
stash.stash("a")
stash.stash("b")
stash.open_panel()
action, payload = resolve_ctrl_s(stash, "")
assert (action, payload) == (ACTION_CLOSE_PANEL, None)
assert stash.panel_open is False
def test_close_panel_takes_priority_over_stashing(self, stash):
"""With the panel open, Ctrl+S closes it rather than stashing text."""
stash.stash("a")
stash.stash("b")
stash.open_panel()
action, _ = resolve_ctrl_s(stash, "text the user typed")
assert action == ACTION_CLOSE_PANEL
assert len(stash) == 2 # nothing new pushed
def test_images_only_buffer_stashes(self, stash):
action, _ = resolve_ctrl_s(stash, "", ["/tmp/x.png"])
assert action == ACTION_STASHED
def test_whitespace_only_buffer_with_stash_restores(self, stash):
"""Whitespace-only counts as empty, so the restore half fires."""
stash.stash("real draft")
action, payload = resolve_ctrl_s(stash, " ")
assert action == ACTION_RESTORED
assert payload == ("real draft", [])
# --------------------------------------------------------------------- ages