import pytest from mulit_agent_app.domain.attachments import TaskAttachment from mulit_agent_app.infrastructure.desktop_filesystem_tool import DesktopFilesystemTool def test_creates_one_new_direct_child_of_desktop(tmp_path): tool = DesktopFilesystemTool(enabled=True, desktop_path=tmp_path) result = tool.create_directory_from_instruction("在电脑桌面新建 test 文件夹") assert result == "已在桌面创建文件夹:test" assert (tmp_path / "test").is_dir() def test_existing_directory_is_never_overwritten(tmp_path): (tmp_path / "test").mkdir() tool = DesktopFilesystemTool(enabled=True, desktop_path=tmp_path) with pytest.raises(ValueError, match="不会覆盖"): tool.create_directory_from_instruction("在电脑桌面新建 test 文件夹") def test_disabled_tool_rejects_a_recognized_request_instead_of_returning_none(tmp_path): tool = DesktopFilesystemTool(enabled=False, desktop_path=tmp_path) with pytest.raises(ValueError, match="未部署或尚未应用"): tool.create_directory_from_instruction("在电脑桌面新建 unavailable-test 文件夹") def test_path_traversal_request_is_rejected_by_policy(tmp_path): tool = DesktopFilesystemTool(enabled=True, desktop_path=tmp_path) with pytest.raises(ValueError, match="请求被策略拒绝"): tool.create_directory_from_instruction("在电脑桌面新建 ../outside 文件夹") def test_creates_folder_and_saves_controller_attachment_without_a_model(tmp_path): tool = DesktopFilesystemTool(enabled=True, desktop_path=tmp_path) result = tool.create_directory_from_instruction( "帮我在桌面创建一个plimg的文件夹,然后把图片保存进去", (TaskAttachment("桌面原图.png", "image/png", "data:image/png;base64,aW1hZ2U="),), ) assert result == "已在桌面创建文件夹并保存 1 张图片:plimg" assert (tmp_path / "plimg" / "桌面原图.png").read_bytes() == b"image" def test_creates_explicitly_missing_desktop_destination_and_saves_attachment(tmp_path): tool = DesktopFilesystemTool(enabled=True, desktop_path=tmp_path) result = tool.create_directory_from_instruction( "这个图片帮我复制到桌面的img文件夹中,如果没有这个文件夹,就创建,然后复制进去", (TaskAttachment("原图.png", "image/png", "data:image/png;base64,aW1hZ2U="),), ) assert result == "已在桌面创建文件夹并保存 1 张图片:img" assert (tmp_path / "img" / "原图.png").read_bytes() == b"image" def test_destination_name_without_explicit_creation_permission_is_rejected(tmp_path): tool = DesktopFilesystemTool(enabled=True, desktop_path=tmp_path) with pytest.raises(ValueError, match="无法识别安全"): tool.create_directory_from_instruction("把图片复制到桌面的img文件夹中", ())