"""LoadSkillTool 路径改写测试。

docker backend 下 fs/shell/run_python 在容器里跑,skills/ bind mount 到
`/sandbox/skills:ro`。LoadSkillTool 返回头里的 `dir` 必须是容器路径而不是 host
绝对路径,否则 LLM 拿 host 路径调 read references 时容器 namespace 不通。
"""
from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from core.skills import SkillRegistry
from tools.skill_tool import LoadSkillTool


class TestLoadSkillToolPathRewrite(unittest.TestCase):
    def setUp(self):
        self.tmpdir = tempfile.TemporaryDirectory()
        self.skills_dir = Path(self.tmpdir.name)
        skill_dir = self.skills_dir / "demo"
        skill_dir.mkdir()
        (skill_dir / "SKILL.md").write_text(
            "---\nname: demo\ndescription: 测试用\n---\n\n# Demo body\n",
            encoding="utf-8",
        )
        self.registry = SkillRegistry(self.skills_dir)

    def tearDown(self):
        self.tmpdir.cleanup()

    def test_host_backend_returns_host_path(self):
        """没传 container_skills_dir → header 用 host 绝对路径(原行为)。"""
        tool = LoadSkillTool(registry=self.registry)
        out = tool.execute(name="demo")
        host_path = str((self.skills_dir / "demo"))
        self.assertIn(f"dir={host_path}", out)
        self.assertIn("# Demo body", out)

    def test_docker_backend_rewrites_to_sandbox_path(self):
        """传 container_skills_dir=/sandbox/skills → header 用容器路径,且不漏 host 路径。"""
        tool = LoadSkillTool(
            registry=self.registry,
            container_skills_dir="/sandbox/skills",
        )
        out = tool.execute(name="demo")
        self.assertIn("dir=/sandbox/skills/demo", out)
        # host 临时目录路径不应出现在 header(防止改写不彻底)
        host_path = str((self.skills_dir / "demo"))
        self.assertNotIn(host_path, out)
        # body 不变
        self.assertIn("# Demo body", out)

    def test_docker_backend_strips_trailing_slash(self):
        """container_skills_dir 带末尾斜杠 → 拼接路径不应出现双斜杠。"""
        tool = LoadSkillTool(
            registry=self.registry,
            container_skills_dir="/sandbox/skills/",
        )
        out = tool.execute(name="demo")
        self.assertIn("dir=/sandbox/skills/demo", out)
        self.assertNotIn("//demo", out)

    def test_unknown_skill_returns_error(self):
        tool = LoadSkillTool(registry=self.registry)
        out = tool.execute(name="nonexistent")
        self.assertIn("not found", out)
        self.assertIn("demo", out)  # available list


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