Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

test: force UTF-8 stdio and fix locale-dependent tests

程凯0a230bd285e5
diff --git a/tests/unit_tests/agentserver/test_deep_adapter_interrupt.py b/tests/unit_tests/agentserver/test_deep_adapter_interrupt.py
index 5093354077..f7b4a162a0 100644
--- a/tests/unit_tests/agentserver/test_deep_adapter_interrupt.py
+++ b/tests/unit_tests/agentserver/test_deep_adapter_interrupt.py
@@ -970,9 +970,14 @@ def test_runtime_route_keeps_artifact_leaf_lexical(tmp_path: Path) -> None:
outside = tmp_path / "outside"
agent_workspace.mkdir()
outside.mkdir()
- (agent_workspace / "projects").symlink_to(
- outside, target_is_directory=True
- )
+ try:
+ (agent_workspace / "projects").symlink_to(
+ outside, target_is_directory=True
+ )
+ except OSError:
+ # Creating symlinks needs privilege on Windows; the rejection
+ # semantics are covered where symlinks are creatable.
+ pytest.skip("symlink creation requires privilege on this platform")
adapter = _make_adapter(
_env_service_id="service-output",
_env_agent_id="agent-output",
diff --git a/tests/unit_tests/agentserver/test_team_entity_store.py b/tests/unit_tests/agentserver/test_team_entity_store.py
index 22a31e577a..edf6eb6dbb 100644
--- a/tests/unit_tests/agentserver/test_team_entity_store.py
+++ b/tests/unit_tests/agentserver/test_team_entity_store.py
@@ -220,7 +220,12 @@ def test_team_entity_store_delete_does_not_follow_team_symlink(tmp_path) -> None
external_file = external_path / "keep.txt"
external_file.write_text("keep", encoding="utf-8")
team_link = teams_home / "research_team"
- team_link.symlink_to(external_path, target_is_directory=True)
+ try:
+ team_link.symlink_to(external_path, target_is_directory=True)
+ except OSError:
+ # Creating symlinks needs privilege on Windows; the rejection
+ # semantics are covered where symlinks are creatable.
+ pytest.skip("symlink creation requires privilege on this platform")
store = TeamEntityStore(teams_home)
diff --git a/tests/unit_tests/channel/test_trajectory_frontend_artifacts.py b/tests/unit_tests/channel/test_trajectory_frontend_artifacts.py
index 670f4981ce..f9e913c579 100644
--- a/tests/unit_tests/channel/test_trajectory_frontend_artifacts.py
+++ b/tests/unit_tests/channel/test_trajectory_frontend_artifacts.py
@@ -53,6 +53,8 @@ def test_trajectory_license_and_notice_ship_in_vite_dist_and_python_wheel(tmp_pa
check=True,
capture_output=True,
text=True,
+ encoding="utf-8",
+ errors="replace",
)
public_root = _FRONTEND_ROOT / "public" / _NOTICE_ROOT
@@ -91,6 +93,10 @@ def test_trajectory_license_and_notice_ship_in_vite_dist_and_python_wheel(tmp_pa
with zipfile.ZipFile(wheel_paths[0]) as wheel:
for artifact_name in _ARTIFACT_FILES:
archive_name = f"{wheel_prefix}/{_NOTICE_ROOT.as_posix()}/{artifact_name}"
- wheel_contents[artifact_name] = wheel.read(archive_name).decode("utf-8")
+ # Normalize line endings: on Windows checkouts the packaged bytes
+ # keep CRLF while read_text() above yields LF.
+ wheel_contents[artifact_name] = wheel.read(archive_name).decode(
+ "utf-8"
+ ).replace("\r\n", "\n")
assert wheel_contents == public_contents
_assert_notice_contents(wheel_contents)
diff --git a/tests/unit_tests/cli/test_chat.py b/tests/unit_tests/cli/test_chat.py
index a6ee1ecbd2..090d4be8d3 100644
--- a/tests/unit_tests/cli/test_chat.py
+++ b/tests/unit_tests/cli/test_chat.py
@@ -9,7 +9,6 @@ import asyncio
import io
import json
import os
-import tempfile
from pathlib import Path
from unittest.mock import patch
@@ -178,7 +177,9 @@ class TestBuildRequest:
trusted_dir=None,
)
req = _build_request(args, "test")
- assert req["params"]["cwd"] == "/custom/cwd"
+ # The CLI resolves the argument; compare resolved forms so the
+ # assertion holds on both POSIX and Windows path shapes.
+ assert Path(req["params"]["cwd"]) == Path("/custom/cwd").resolve()
@staticmethod
def test_with_project_dir(monkeypatch):
@@ -191,7 +192,9 @@ class TestBuildRequest:
trusted_dir=None,
)
req = _build_request(args, "test")
- assert req["params"]["project_dir"] == "/custom/project"
+ assert Path(req["params"]["project_dir"]) == Path(
+ "/custom/project"
+ ).resolve()
@staticmethod
def test_with_trusted_dirs(monkeypatch):
@@ -208,9 +211,9 @@ class TestBuildRequest:
trusted_dir=["/dir1", "/dir2"],
)
req = _build_request(args, "test")
- assert sorted(str(d) for d in req["params"]["trusted_dirs"]) == sorted(
- ["/dir1", "/dir2"]
- )
+ assert sorted(
+ Path(d) for d in req["params"]["trusted_dirs"]
+ ) == sorted([Path("/dir1").resolve(), Path("/dir2").resolve()])
@staticmethod
def test_mode_included(monkeypatch):
@@ -1005,8 +1008,6 @@ class TestSpinner:
@staticmethod
def test_spinner_verb_rotates():
- from jiuwenswarm.cli.render import _VERBS
-
fake_time = 1000.0
def mock_monotonic():
diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py
index 71af65ffcd..099e04fd68 100644
--- a/tests/unit_tests/conftest.py
+++ b/tests/unit_tests/conftest.py
@@ -19,11 +19,23 @@ functions, hence this hook.
from __future__ import annotations
import os
+import sys
from pathlib import Path
import pytest
-import jiuwenswarm.common.utils as _utils
+# Force UTF-8 stdio for the test process and any child it spawns. On Windows
+# the interpreter starts with locale (GBK) std streams; app logging that
+# reaches the original stdout/stderr then writes GBK bytes into pytest's
+# UTF-8 fd-level capture, breaking the whole run at test boundaries.
+os.environ.setdefault("PYTHONUTF8", "1")
+for _stream in (sys.__stdout__, sys.__stderr__):
+ try:
+ _stream.reconfigure(encoding="utf-8", errors="replace")
+ except (AttributeError, OSError, ValueError):
+ pass
+
+import jiuwenswarm.common.utils as _utils # noqa: E402 (must follow the stdio reconfigure above)
_REAL_GET_CONFIG_FILE = _utils.get_config_file
_REAL_GET_AGENT_WORKSPACE_DIR = _utils.get_agent_workspace_dir
diff --git a/tests/unit_tests/extensions/test_agentos_router.py b/tests/unit_tests/extensions/test_agentos_router.py
index 36bfc69f25..1436812b80 100644
--- a/tests/unit_tests/extensions/test_agentos_router.py
+++ b/tests/unit_tests/extensions/test_agentos_router.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio
from collections.abc import AsyncIterator
+from pathlib import Path
from typing import Any
import pytest
@@ -221,7 +222,11 @@ def test_resolve_agent_workspace_defaults_under_agentos_users(tmp_path, monkeypa
assert resolve_agent_workspace("alice/../bob") == (
str(expected_root / "alice_.._bob")
)
- assert resolve_agent_workspace("u1", workspace_root="/data/ws") == "/data/ws/u1"
+ # Compare resolved forms: on Windows the rooted POSIX-style literal
+ # resolves against the current drive.
+ assert Path(
+ resolve_agent_workspace("u1", workspace_root="/data/ws")
+ ) == Path("/data/ws/u1").resolve()
@pytest.mark.asyncio
@@ -764,7 +769,9 @@ async def test_create_uses_configured_workspace_root() -> None:
session_id="sess-1",
)
assert response["ok"] is True
- assert yuanrong.create_payloads[0]["workspace"] == "/mnt/workspaces/u1"
+ assert Path(yuanrong.create_payloads[0]["workspace"]) == Path(
+ "/mnt/workspaces/u1"
+ ).resolve()
finally:
await client.shutdown()
diff --git a/tests/unit_tests/test_dependency_constraints.py b/tests/unit_tests/test_dependency_constraints.py
index b312ee4196..93bdf709af 100644
--- a/tests/unit_tests/test_dependency_constraints.py
+++ b/tests/unit_tests/test_dependency_constraints.py
@@ -16,7 +16,9 @@ OTEL_REQUIREMENTS = {
def test_offline_html_runtime_dependency_includes_python_markdown():
- pyproject = tomllib.loads((PROJECT_ROOT / "pyproject.toml").read_text())
+ pyproject = tomllib.loads(
+ (PROJECT_ROOT / "pyproject.toml").read_text(encoding="utf-8")
+ )
requirements = {
Requirement(raw).name for raw in pyproject["project"]["dependencies"]
}
@@ -25,7 +27,9 @@ def test_offline_html_runtime_dependency_includes_python_markdown():
def test_opentelemetry_dependencies_exclude_protobuf4_only_proto_versions():
- pyproject = tomllib.loads((PROJECT_ROOT / "pyproject.toml").read_text())
+ pyproject = tomllib.loads(
+ (PROJECT_ROOT / "pyproject.toml").read_text(encoding="utf-8")
+ )
requirements = {
Requirement(raw).name: Requirement(raw)
for raw in pyproject["project"]["dependencies"]