"""回归:docker backend 下 system prompt 必须注入容器路径,绝不泄漏宿主绝对路径 / uid。

背景(2026-06-03 修复 + 06-09 复核):fs/shell/run_python 在 docker backend 跑在容器里
(`<workspace>/users/<uid>` bind 到 `/workspace`)。早期 prompt 焊死宿主绝对路径
(`/home/ubuntu/zcbot/workspace/users/<uid>/<wd>`),LLM 据此 glob,容器内无此路径 →
`[Error] base path not found` → 重试风暴(实测单 task 51 次)。本测试锁住修复:docker 下
prompt 只出现 `/workspace/<wd>`,不出现宿主路径 / uid。host 下仍用本地绝对路径(不回归)。
"""
from __future__ import annotations

import sys
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from uuid import UUID

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from core.agent_builder import _build_system_prompt, load_config, user_root  # noqa: E402
from core.paths import ROOT  # noqa: E402
from core.skills import SkillRegistry  # noqa: E402

_UID = UUID("6b14d2ab-7a6e-4d0b-8562-ea62a801e32c")
_TID = UUID("ab063233-7e90-4f57-9afe-3f7e47b13522")
_HOST_CWD = Path("/home/ubuntu/zcbot")  # 模拟 prod 宿主启动目录


def _build(backend: str) -> tuple[str, Path, Path]:
    cfg = load_config()
    skills = SkillRegistry(ROOT / cfg.get("skills_dir", "skills"))
    tmp = tempfile.mkdtemp()
    ws = Path(tmp) / "workspace"
    wd = user_root(ws, _UID) / "数据资源展示"
    wd.mkdir(parents=True, exist_ok=True)
    with patch.dict("os.environ", {"ZCBOT_SANDBOX_BACKEND": backend}, clear=False):
        prompt = _build_system_prompt(
            cfg, skills, ws, _HOST_CWD, wd, _UID, _TID, "数据资源展示", "documents"
        )
    return prompt, ws, wd


class TestSystemPromptPaths(unittest.TestCase):
    def test_docker_prompt_uses_container_path_no_host_leak(self):
        prompt, ws, wd = _build("docker")
        # 容器路径在
        self.assertIn("/workspace/数据资源展示", prompt)
        # 宿主路径 / tmp / uid 一律不泄漏
        self.assertNotIn("/home/ubuntu/zcbot", prompt)
        self.assertNotIn(str(ws), prompt)
        self.assertNotIn(str(wd), prompt)
        self.assertNotIn(str(_UID), prompt)
        # docker 下不应再出现「cwd: <宿主路径>」那一行
        self.assertNotIn(f"`{_HOST_CWD}`", prompt)

    def test_host_prompt_keeps_local_absolute_path(self):
        prompt, ws, wd = _build("host")
        # host backend 用本地绝对 task_dir(Windows dev / dogfood),不容器化
        self.assertIn(str(wd), prompt)
        self.assertNotIn("/workspace/数据资源展示", prompt)


if __name__ == "__main__":
    unittest.main()
