feat(webui): add a host directory picker and refine composer controls

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-08 20:33:15 +08:00
1 parent 3865bde3fe
commit 66c8d41f66
32 files changed
+1974 -464

No files matched your search

+51
View File
@@ -418,3 +418,54 @@ def test_project_selection_can_remain_disabled_for_untrusted_connections(
can_change_project=False,
can_use_full_access=False,
)
def test_recent_projects_survive_controller_restart_without_changing_access(tmp_path, monkeypatch) -> None:
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
default = tmp_path / "default"
project = tmp_path / "project"
default.mkdir()
project.mkdir()
controller = WebUIWorkspaceController(
session_manager=None, default_workspace=default, default_restrict_to_workspace=True,
)
scope = controller.scope_for_new_chat(
{WORKSPACE_SCOPE_METADATA_KEY: {"project_path": str(project), "access_mode": "restricted"}},
can_change_project=True, can_use_full_access=False,
)
controller.stage_scope("first", scope)
controller.stage_scope("second", scope)
restarted = WebUIWorkspaceController(
session_manager=None, default_workspace=default, default_restrict_to_workspace=True,
)
payload = restarted.payload(can_change_project=True, can_use_full_access=False)
assert payload["recent_projects"] == [{"name": "project", "path": str(project)}]
assert payload["default_scope"]["project_path"] == str(default)
assert payload["default_scope"]["access_mode"] == "restricted"
assert payload["controls"]["can_browse_directories"] is True
def test_favorites_keep_pin_order_across_restart_and_can_remove_deleted_folder(tmp_path, monkeypatch) -> None:
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
default, alpha, beta = (tmp_path / name for name in ("default", "alpha", "beta"))
for folder in (default, alpha, beta):
folder.mkdir()
controller = WebUIWorkspaceController(
session_manager=None, default_workspace=default, default_restrict_to_workspace=True,
)
controller.set_favorite_project(str(beta), True)
controller.set_favorite_project(str(alpha), True)
controller.set_favorite_project(str(beta), True)
controller.resolve_project(str(alpha))
restarted = WebUIWorkspaceController(
session_manager=None, default_workspace=default, default_restrict_to_workspace=True,
)
payload = restarted.payload(can_change_project=True, can_use_full_access=False)
assert [item["path"] for item in payload["favorite_projects"]] == [str(beta), str(alpha)]
assert [item["path"] for item in payload["recent_projects"]] == [str(alpha)]
assert payload["default_scope"]["project_path"] == str(default)
assert payload["default_scope"]["access_mode"] == "restricted"
beta.rmdir()
assert restarted.set_favorite_project(str(beta), False) == [{"name": "alpha", "path": str(alpha)}]
with pytest.raises(WorkspaceScopeError, match="existing directory"):
restarted.set_favorite_project(str(beta), True)
+69
View File
@@ -0,0 +1,69 @@
"""Project selection directory metadata contracts."""
from pathlib import Path
import pytest
from nanobot.security.workspace_access import WorkspaceScopeError
from nanobot.webui.workspace_browser import browse_workspace_directories
def test_directory_browser_filters_folders_and_exposes_navigation(tmp_path: Path) -> None:
(tmp_path / "Beta").mkdir()
(tmp_path / "alpha").mkdir()
(tmp_path / ".hidden").mkdir()
(tmp_path / "secret.txt").write_text("file contents")
result = browse_workspace_directories(str(tmp_path), default_workspace=tmp_path)
assert [entry["name"] for entry in result["entries"]] == ["alpha", "Beta"]
assert result["path"] == str(tmp_path)
assert result["parent"] == str(tmp_path.parent)
hidden = browse_workspace_directories(str(tmp_path), default_workspace=tmp_path, show_hidden=True, query="HID")
assert hidden["entries"] == [{"name": ".hidden", "path": str(tmp_path / ".hidden")}]
def test_directory_filter_reaches_folders_beyond_display_limit(tmp_path: Path) -> None:
for index in range(501):
(tmp_path / f"project-{index:03}").mkdir()
result = browse_workspace_directories(str(tmp_path), default_workspace=tmp_path)
assert len(result["entries"]) == 500
assert result["truncated"] is True
filtered = browse_workspace_directories(str(tmp_path), default_workspace=tmp_path, query="500")
assert filtered["entries"] == [{"name": "project-500", "path": str(tmp_path / "project-500")}]
assert filtered["truncated"] is False
@pytest.mark.parametrize("path", ["relative/project", "invalid\0path"])
def test_directory_browser_rejects_invalid_user_paths(tmp_path: Path, path: str) -> None:
with pytest.raises(WorkspaceScopeError):
browse_workspace_directories(path, default_workspace=tmp_path)
def test_partial_address_lists_matching_parent_folders_and_exact_address_lists_children(tmp_path: Path) -> None:
for name in ("workspace", "workspace-two", "other"):
(tmp_path / name).mkdir()
(tmp_path / "workspace" / "src").mkdir()
(tmp_path / "worksp-file").write_text("not a folder")
partial = browse_workspace_directories(str(tmp_path / "worksp"), default_workspace=tmp_path, allow_partial=True)
assert partial["path"] == str(tmp_path)
assert partial["partial"] is True
assert [entry["name"] for entry in partial["entries"]] == ["workspace", "workspace-two"]
exact = browse_workspace_directories(str(tmp_path / "workspace"), default_workspace=tmp_path, allow_partial=True)
assert exact["partial"] is False
assert exact["entries"] == [{"name": "src", "path": str(tmp_path / "workspace" / "src")}]
missing = browse_workspace_directories(str(tmp_path / "missing"), default_workspace=tmp_path, allow_partial=True)
assert missing["partial"] is True
assert missing["entries"] == []
with pytest.raises(WorkspaceScopeError):
browse_workspace_directories(str(tmp_path / "worksp"), default_workspace=tmp_path)
def test_partial_address_matches_before_display_limit_and_explicit_hidden_prefix(tmp_path: Path) -> None:
for index in range(501):
(tmp_path / f"a-workspace-{index:03}").mkdir()
(tmp_path / "workspace").mkdir()
(tmp_path / ".private").mkdir()
result = browse_workspace_directories(str(tmp_path / "worksp"), default_workspace=tmp_path, allow_partial=True)
assert result["entries"] == [{"name": "workspace", "path": str(tmp_path / "workspace")}]
assert result["truncated"] is False
hidden = browse_workspace_directories(str(tmp_path / ".priv"), default_workspace=tmp_path, allow_partial=True)
assert hidden["entries"] == [{"name": ".private", "path": str(tmp_path / ".private")}]