forked from AstrBotDevs/AstrBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_computer_skill_sync.py
More file actions
151 lines (121 loc) · 4.63 KB
/
test_computer_skill_sync.py
File metadata and controls
151 lines (121 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from __future__ import annotations
import asyncio
from pathlib import Path
from typing import cast
from astrbot.core.computer import computer_client
from astrbot.core.computer.booters.base import ComputerBooter
def _extract_embedded_python(command: str) -> str:
start_marker = "$PYBIN - <<'PY'\n"
end_marker = "\nPY"
start = command.find(start_marker)
assert start != -1
start += len(start_marker)
end = command.rfind(end_marker)
assert end != -1
return command[start:end]
class _FakeShell:
def __init__(self, sync_payload_json: str):
self.sync_payload_json = sync_payload_json
self.commands: list[str] = []
async def exec(self, command: str, **kwargs):
_ = kwargs
self.commands.append(command)
if "PYBIN" in command and "managed_skills" in command:
return {
"success": True,
"stdout": self.sync_payload_json,
"stderr": "",
"exit_code": 0,
}
return {"success": True, "stdout": "", "stderr": "", "exit_code": 0}
class _FakeBooter:
def __init__(self, sync_payload_json: str):
self.shell = _FakeShell(sync_payload_json)
self.uploads: list[tuple[str, str]] = []
async def upload_file(self, path: str, file_name: str) -> dict:
self.uploads.append((path, file_name))
return {"success": True}
def test_sync_skills_keeps_builtin_skills_when_local_is_empty(
monkeypatch, tmp_path: Path
):
skills_root = tmp_path / "skills"
temp_root = tmp_path / "temp"
skills_root.mkdir(parents=True, exist_ok=True)
temp_root.mkdir(parents=True, exist_ok=True)
captured = {"skills": None}
def _fake_set_cache(self, skills):
captured["skills"] = skills
monkeypatch.setattr(
"astrbot.core.computer.computer_client.get_astrbot_skills_path",
lambda: str(skills_root),
)
monkeypatch.setattr(
"astrbot.core.computer.computer_client.get_astrbot_temp_path",
lambda: str(temp_root),
)
monkeypatch.setattr(
"astrbot.core.computer.computer_client.SkillManager.set_sandbox_skills_cache",
_fake_set_cache,
)
booter = _FakeBooter(
'{"skills":[{"name":"python-sandbox","description":"ship","path":"skills/python-sandbox/SKILL.md"}]}'
)
asyncio.run(computer_client._sync_skills_to_sandbox(cast(ComputerBooter, booter)))
assert booter.uploads == []
assert any(cmd == "rm -f skills/skills.zip" for cmd in booter.shell.commands)
assert captured["skills"] == [
{
"name": "python-sandbox",
"description": "ship",
"path": "skills/python-sandbox/SKILL.md",
}
]
def test_sync_skills_uses_managed_strategy_instead_of_wiping_all(
monkeypatch,
tmp_path: Path,
):
skills_root = tmp_path / "skills"
temp_root = tmp_path / "temp"
skill_dir = skills_root / "custom-agent-skill"
skill_dir.mkdir(parents=True, exist_ok=True)
skill_dir.joinpath("SKILL.md").write_text("# demo", encoding="utf-8")
temp_root.mkdir(parents=True, exist_ok=True)
captured = {"skills": None}
def _fake_set_cache(self, skills):
captured["skills"] = skills
monkeypatch.setattr(
"astrbot.core.computer.computer_client.get_astrbot_skills_path",
lambda: str(skills_root),
)
monkeypatch.setattr(
"astrbot.core.computer.computer_client.get_astrbot_temp_path",
lambda: str(temp_root),
)
monkeypatch.setattr(
"astrbot.core.computer.computer_client.SkillManager.set_sandbox_skills_cache",
_fake_set_cache,
)
booter = _FakeBooter(
'{"skills":[{"name":"custom-agent-skill","description":"","path":"skills/custom-agent-skill/SKILL.md"}]}'
)
asyncio.run(computer_client._sync_skills_to_sandbox(cast(ComputerBooter, booter)))
assert len(booter.uploads) == 1
assert booter.uploads[0][1] == "skills/skills.zip"
assert not any(
"find skills -mindepth 1 -delete" in cmd for cmd in booter.shell.commands
)
assert captured["skills"] == [
{
"name": "custom-agent-skill",
"description": "",
"path": "skills/custom-agent-skill/SKILL.md",
}
]
def test_build_scan_command_frontmatter_newline_is_escaped_literal():
command = computer_client._build_scan_command()
script = _extract_embedded_python(command)
assert 'frontmatter = "\\n".join(lines[1:end_idx])' in script
def test_build_scan_command_embedded_python_is_syntax_valid():
command = computer_client._build_scan_command()
script = _extract_embedded_python(command)
compile(script, "<scan_script>", "exec")