ForcePilot/backend/test/unit/backends/test_sandbox_paths.py
Kris 59cc1daee1 feat: 新增AgentRun终态感知能力与配套优化
本次提交实现了AgentRun全生命周期终态处理能力,覆盖以下核心变更:
1. 新增4种AgentRun终态错误类型与HTTP状态映射,透传上游错误上下文
2. 新增AgentRun终态信息查询接口与DTO,支持非事件流场景下的状态感知
3. 优化沙箱路径转义规则,兼容服务账号UID格式
4. 新增出站管道SKIP逻辑,针对取消/中断场景避免无效重试
5. 补充完整单元测试覆盖所有新功能与优化点
2026-07-18 03:42:49 +08:00

45 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Tests for yuxi.agents.backends.sandbox.paths uid sanitize behavior.
服务账号 uid 格式 ``svc:channel:{channel_type}:{account_id}`` 含冒号,
paths.py._sanitize_uid 必须将其转义为文件系统安全的路径组件,且与
docker/sandbox_provisioner/app.py 中的转义规则保持一致,确保宿主机
路径与容器挂载路径映射一致。
"""
from __future__ import annotations
import pytest
from yuxi.agents.backends.sandbox.paths import _sanitize_uid
def test_sanitize_uid_passes_through_safe_chars() -> None:
assert _sanitize_uid("user-1_2") == "user-1_2"
assert _sanitize_uid("abcDEF123") == "abcDEF123"
def test_sanitize_uid_escapes_colons_in_service_account_uid() -> None:
# 服务账号 uid: svc:channel:{channel_type}:{account_id}
assert _sanitize_uid("svc:channel:wechat_woc:acc-1") == "svc_channel_wechat_woc_acc-1"
assert _sanitize_uid("svc:channel:feishu:host_1_bridge_2") == "svc_channel_feishu_host_1_bridge_2"
def test_sanitize_uid_escapes_unsafe_chars_to_underscore() -> None:
assert _sanitize_uid("user/name") == "user_name"
assert _sanitize_uid("user name") == "user_name"
assert _sanitize_uid("user;rm") == "user_rm"
assert _sanitize_uid("user.name") == "user_name"
# 路径穿越尝试被转义后不再包含 ..(每个非法字符独立替换为 _
assert _sanitize_uid("../user") == "___user"
assert _sanitize_uid("..") == "__"
def test_sanitize_uid_rejects_empty_or_whitespace() -> None:
for value in ["", " ", None]:
with pytest.raises(ValueError):
_sanitize_uid(value) # type: ignore[arg-type]
def test_sanitize_uid_strips_surrounding_whitespace() -> None:
assert _sanitize_uid(" user-1 ") == "user-1"