From bd480b4c8c7560600f71d34d0d48c3afc2f0515f Mon Sep 17 00:00:00 2001 From: infinitycrew39 Date: Thu, 2 Jul 2026 22:33:10 +0700 Subject: [PATCH] test(gateway): cover per-backend cwd placeholder resolution Add unit tests for resolve_placeholder_terminal_cwd and extend the config bridge simulation for docker mount-on vs mount-off vs local fallback. Co-authored-by: Cursor --- tests/gateway/test_config_cwd_bridge.py | 60 +++++++++++++++++++--- tests/gateway/test_cwd_placeholder.py | 68 +++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 tests/gateway/test_cwd_placeholder.py diff --git a/tests/gateway/test_config_cwd_bridge.py b/tests/gateway/test_config_cwd_bridge.py index 9d81da99d01..ca449b94b62 100644 --- a/tests/gateway/test_config_cwd_bridge.py +++ b/tests/gateway/test_config_cwd_bridge.py @@ -12,6 +12,7 @@ asserting the expected env var outcomes. import os import json +from gateway.cwd_placeholder import CWD_PLACEHOLDERS, resolve_placeholder_terminal_cwd from tools.terminal_tool import _is_ssh_remote_tilde_cwd @@ -42,6 +43,7 @@ def _simulate_config_bridge(cfg: dict, initial_env: dict | None = None): "container_cpu": "TERMINAL_CONTAINER_CPU", "container_memory": "TERMINAL_CONTAINER_MEMORY", "container_disk": "TERMINAL_CONTAINER_DISK", + "docker_mount_cwd_to_workspace": "TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", } for cfg_key, env_var in terminal_env_map.items(): if cfg_key in terminal_cfg: @@ -75,14 +77,23 @@ def _simulate_config_bridge(cfg: dict, initial_env: dict | None = None): alias_val = os.path.expanduser(alias_val) env[alias_env] = alias_val.strip() - # --- Replicate lines 144-147: MESSAGING_CWD fallback --- + # --- Replicate gateway/run.py placeholder TERMINAL_CWD resolution --- configured_cwd = env.get("TERMINAL_CWD", "") - if not configured_cwd or configured_cwd in {".", "auto", "cwd"}: - if env.get("TERMINAL_ENV", "").strip().lower() == "ssh": + if not configured_cwd or configured_cwd in CWD_PLACEHOLDERS: + resolved = resolve_placeholder_terminal_cwd( + configured_cwd=configured_cwd, + terminal_backend=env.get("TERMINAL_ENV", ""), + messaging_cwd=env.get("MESSAGING_CWD"), + docker_mount_cwd_to_workspace=env.get( + "TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", "false" + ).lower() + in {"true", "1", "yes"}, + home_fallback="/root", + ) + if resolved is None: env.pop("TERMINAL_CWD", None) else: - messaging_cwd = env.get("MESSAGING_CWD") or "/root" # Path.home() for root - env["TERMINAL_CWD"] = messaging_cwd + env["TERMINAL_CWD"] = resolved return env @@ -225,7 +236,44 @@ class TestNestedTerminalCwdPlaceholderSkip: result = _simulate_config_bridge(cfg, {"MESSAGING_CWD": "/from/env"}) assert result["TERMINAL_ENV"] == "docker" assert result["TERMINAL_TIMEOUT"] == "300" - assert result["TERMINAL_CWD"] == "/from/env" + assert result.get("TERMINAL_CWD") is None + + def test_docker_placeholder_does_not_inherit_host_home(self): + """terminal.cwd: '.' + docker + mount off must not resolve to host home.""" + cfg = { + "terminal": { + "cwd": ".", + "backend": "docker", + "docker_mount_cwd_to_workspace": False, + }, + } + result = _simulate_config_bridge(cfg, {"MESSAGING_CWD": "/home/user"}) + assert "TERMINAL_CWD" not in result + + def test_docker_placeholder_mount_on_preserves_messaging_cwd(self): + """Mount-enabled docker still needs the host cwd signal for /workspace.""" + cfg = { + "terminal": { + "cwd": ".", + "backend": "docker", + "docker_mount_cwd_to_workspace": True, + }, + } + result = _simulate_config_bridge( + cfg, {"MESSAGING_CWD": "/host/project"} + ) + assert result["TERMINAL_CWD"] == "/host/project" + assert result["TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE"] == "True" + + def test_ssh_placeholder_does_not_inherit_host_home(self): + cfg = {"terminal": {"cwd": "auto", "backend": "ssh"}} + result = _simulate_config_bridge(cfg, {"MESSAGING_CWD": "/home/user"}) + assert "TERMINAL_CWD" not in result + + def test_local_placeholder_still_falls_back_to_messaging_cwd(self): + cfg = {"terminal": {"cwd": ".", "backend": "local"}} + result = _simulate_config_bridge(cfg, {"MESSAGING_CWD": "/home/user"}) + assert result["TERMINAL_CWD"] == "/home/user" def test_terminal_home_mode_bridges_to_env(self): cfg = {"terminal": {"home_mode": "profile"}} diff --git a/tests/gateway/test_cwd_placeholder.py b/tests/gateway/test_cwd_placeholder.py new file mode 100644 index 00000000000..3a31cbe905a --- /dev/null +++ b/tests/gateway/test_cwd_placeholder.py @@ -0,0 +1,68 @@ +"""Unit tests for gateway.cwd_placeholder.resolve_placeholder_terminal_cwd.""" + +from gateway.cwd_placeholder import resolve_placeholder_terminal_cwd + + +class TestResolvePlaceholderTerminalCwd: + def test_local_placeholder_uses_messaging_cwd(self): + assert resolve_placeholder_terminal_cwd( + configured_cwd=".", + terminal_backend="local", + messaging_cwd="/home/user/project", + docker_mount_cwd_to_workspace=False, + home_fallback="/home/user", + ) == "/home/user/project" + + def test_local_placeholder_falls_back_to_home(self): + assert resolve_placeholder_terminal_cwd( + configured_cwd="auto", + terminal_backend="local", + messaging_cwd=None, + docker_mount_cwd_to_workspace=False, + home_fallback="/home/user", + ) == "/home/user" + + def test_docker_placeholder_mount_off_unset(self): + assert resolve_placeholder_terminal_cwd( + configured_cwd=".", + terminal_backend="docker", + messaging_cwd="/home/user", + docker_mount_cwd_to_workspace=False, + home_fallback="/home/user", + ) is None + + def test_docker_placeholder_mount_on_preserves_host_path(self): + assert resolve_placeholder_terminal_cwd( + configured_cwd=".", + terminal_backend="docker", + messaging_cwd="/host/project", + docker_mount_cwd_to_workspace=True, + home_fallback="/home/user", + ) == "/host/project" + + def test_docker_placeholder_mount_on_without_messaging_cwd_unset(self): + assert resolve_placeholder_terminal_cwd( + configured_cwd=".", + terminal_backend="docker", + messaging_cwd=None, + docker_mount_cwd_to_workspace=True, + home_fallback="/home/user", + ) is None + + def test_ssh_placeholder_unset(self): + assert resolve_placeholder_terminal_cwd( + configured_cwd="cwd", + terminal_backend="ssh", + messaging_cwd="/home/user", + docker_mount_cwd_to_workspace=False, + home_fallback="/home/user", + ) is None + + def test_explicit_configured_cwd_passthrough(self): + assert resolve_placeholder_terminal_cwd( + configured_cwd="/explicit/path", + terminal_backend="docker", + messaging_cwd="/home/user", + docker_mount_cwd_to_workspace=False, + home_fallback="/home/user", + ) == "/explicit/path"