Files
AstrBot/tests/test_local_filesystem_component.py
Soulter 2472a12671 feat: filesystem grep, read, write, edit file and workspace support (#7402)
* feat: filesystem grep, read, edit file

* feat: add file write tool and enhance file read functionality

* feat: enhance tool prompt formatting and add escaped text decoding for file editing

* feat: remove redundant safe path tests from security restrictions

* feat: implement file read tool with support for text and image files, including validation for large files

* feat: add file read utilities and integrate with filesystem tools

* refactor: move computer tools to builtin tools registry

* refactor: remove unused plugin_context parameter from _apply_sandbox_tools

* feat: supports to display enabled builtin tools in configs

* feat: add tooltip for disabled builtin tools and update localization strings

* feat: add workspace extra prompt handling in message processing

* feat: add ripgrep installation to Dockerfile

* perf: shell executed in workspace dir in local env

* feat: enhance file reading capabilities to support PDF and DOCX parsing, including workspace storage for long documents

* feat: update converted text notice to suggest using grep for large files

* feat: implement handling for large tool results with overflow file writing and read tool integration

* fix: test

* feat: enhance onboarding steps to include computer access configuration and related help information

* feat: add support for additional temporary path in restricted environment checks

* feat: update computer access hints and add detailed configuration instructions
2026-04-11 17:01:54 +08:00

56 lines
1.7 KiB
Python

from __future__ import annotations
import asyncio
from pathlib import Path
from astrbot.core.computer.booters import local as local_booter
from astrbot.core.computer.booters.local import LocalFileSystemComponent
def _allow_tmp_root(monkeypatch, tmp_path: Path) -> None:
monkeypatch.setattr(local_booter, "get_astrbot_root", lambda: str(tmp_path))
def test_local_file_system_component_prefers_utf8_before_windows_locale(
monkeypatch,
tmp_path: Path,
):
_allow_tmp_root(monkeypatch, tmp_path)
monkeypatch.setattr(local_booter.os, "name", "nt", raising=False)
monkeypatch.setattr(
local_booter.locale,
"getpreferredencoding",
lambda _do_setlocale=False: "cp936",
)
skill_path = tmp_path / "skills" / "demo.txt"
skill_path.parent.mkdir(parents=True, exist_ok=True)
skill_path.write_bytes("技能内容".encode())
result = asyncio.run(LocalFileSystemComponent().read_file(str(skill_path)))
assert result["success"] is True
assert result["content"] == "技能内容"
def test_local_file_system_component_falls_back_to_gbk_on_windows(
monkeypatch,
tmp_path: Path,
):
_allow_tmp_root(monkeypatch, tmp_path)
monkeypatch.setattr(local_booter.os, "name", "nt", raising=False)
monkeypatch.setattr(
local_booter.locale,
"getpreferredencoding",
lambda _do_setlocale=False: "cp1252",
)
skill_path = tmp_path / "skills" / "weibo-hot.txt"
skill_path.parent.mkdir(parents=True, exist_ok=True)
skill_path.write_bytes("微博热搜".encode("gbk"))
result = asyncio.run(LocalFileSystemComponent().read_file(str(skill_path)))
assert result["success"] is True
assert result["content"] == "微博热搜"