Repository navigation
Expand file tree
/
Copy pathtest_skill_encoding.py
More file actions
110 lines (77 loc) · 4.12 KB
/
Copy pathtest_skill_encoding.py
File metadata and controls
110 lines (77 loc) · 4.12 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
"""DETERMINISTIC EVAL — procedural skills use a portable text encoding."""
from io import BytesIO
from pathlib import Path
from types import SimpleNamespace
import pytest
from prometheus.memory.procedural import installer
from prometheus.memory.procedural.loader import SkillLoader
from prometheus.ops.dashboard import memory_action
from prometheus.tools.memory_admin import make_create_skill_tool
DESCRIPTION = "\u5904\u7406\u4e2d\u6587\u5468\u62a5"
BODY = "\u7b2c\u4e00\u6b65\uff1a\u603b\u7ed3\u672c\u5468\u3002 \U0001f680"
@pytest.fixture
def require_explicit_text_encoding(monkeypatch):
"""Make implicit writes fail on every OS, as they can on Windows."""
original_write_text = Path.write_text
def reject_implicit_encoding(path, data, encoding=None, *args, **kwargs):
if encoding is None:
raise UnicodeEncodeError("gbk", "\U0001f680", 0, 1, "illegal multibyte sequence")
return original_write_text(path, data, *args, encoding=encoding, **kwargs)
monkeypatch.setattr(Path, "write_text", reject_implicit_encoding)
def assert_utf8_skill(path: Path, name: str) -> None:
text = path.read_bytes().decode("utf-8")
assert DESCRIPTION in text and BODY in text
assert [skill.name for skill in SkillLoader([path.parents[1]]).skills] == [name]
def test_skill_loader_reads_utf8_when_platform_default_cannot(tmp_path, monkeypatch):
skill_path = tmp_path / "demo" / "SKILL.md"
skill_path.parent.mkdir()
skill_path.write_text(
"---\nname: demo\ndescription: portable skill\n---\nUse an em dash \u2014 safely.\n",
encoding="utf-8",
)
original_read_text = Path.read_text
def reject_implicit_encoding(path, encoding=None, *args, **kwargs):
if encoding is None:
raise UnicodeDecodeError("gbk", b"\x94", 0, 1, "illegal multibyte sequence")
return original_read_text(path, *args, encoding=encoding, **kwargs)
monkeypatch.setattr(Path, "read_text", reject_implicit_encoding)
loader = SkillLoader([tmp_path])
assert [skill.name for skill in loader.skills] == ["demo"]
assert "em dash \u2014 safely" in loader.skills[0].body
def test_agent_created_skill_is_written_as_utf8(tmp_path, require_explicit_text_encoding):
home = tmp_path / "home"
memory = SimpleNamespace(skills=SkillLoader([home / "skills"]))
settings = SimpleNamespace(home=home)
tool = make_create_skill_tool(settings, memory)
result = tool.fn(name="chinese-report", description=DESCRIPTION, body=BODY)
assert result.startswith("Created skill 'chinese-report'")
assert_utf8_skill(home / "skills" / "chinese-report" / "SKILL.md", "chinese-report")
def test_installed_skill_is_written_as_utf8(
tmp_path, monkeypatch, require_explicit_text_encoding
):
home = tmp_path / "home"
source = (
f"---\nname: installed-report\ndescription: {DESCRIPTION}\n---\n\n{BODY}\n"
).encode()
monkeypatch.setenv("PROMETHEUS_HOME", str(home))
monkeypatch.setattr(installer.urllib.request, "urlopen", lambda *args, **kwargs: BytesIO(source))
installer.install("https://example.com/SKILL.md")
assert_utf8_skill(home / "skills" / "installed-report" / "SKILL.md", "installed-report")
def test_dashboard_edited_skill_is_written_as_utf8(
tmp_path, monkeypatch, require_explicit_text_encoding
):
home = tmp_path / "home"
path = home / "skills" / "dashboard-report" / "SKILL.md"
path.parent.mkdir(parents=True)
content = f"---\nname: dashboard-report\ndescription: {DESCRIPTION}\n---\n\n{BODY}\n"
monkeypatch.setenv("PROMETHEUS_HOME", str(home))
result = memory_action({"action": "save_skill", "path": str(path), "content": content})
assert result == {"ok": True}
assert_utf8_skill(path, "dashboard-report")
def test_dashboard_soul_is_written_as_utf8(tmp_path, monkeypatch, require_explicit_text_encoding):
home = tmp_path / "home"
monkeypatch.setenv("PROMETHEUS_HOME", str(home))
soul = "用户偏好中文回复 🚀"
result = memory_action({"action": "save_soul", "content": soul})
assert result == {"ok": True}
assert (home / "SOUL.md").read_bytes().decode("utf-8").replace("\r\n", "\n") == soul + "\n"