"""LoadSkillTool 路径改写测试。

docker backend 下 fs/shell/run_python 在容器里跑,skill 目录按来源 bind 到不同挂载点
(内置 → `/sandbox/skills:ro`,用户 → `/workspace/.skills`)。LoadSkillTool 返回头里的
`dir` 必须是容器路径而不是 host 绝对路径,否则 LLM 拿 host 路径调 read references 时
容器 namespace 不通。容器路径由 `SkillSource.container_root` 按 `skill.source` 决定。
"""
from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from core.skills import SkillRegistry, SkillSource
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",
        )

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

    def test_host_backend_returns_host_path(self):
        """container_root=None → header 用 host 绝对路径(原行为)。"""
        registry = SkillRegistry(self.skills_dir)  # 单 Path → builtin, container_root=None
        tool = LoadSkillTool(registry=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_root=/sandbox/skills → header 用容器路径,且不漏 host 路径。"""
        registry = SkillRegistry(
            SkillSource(self.skills_dir, "builtin", "/sandbox/skills")
        )
        tool = LoadSkillTool(registry=registry)
        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)
        self.assertIn("# Demo body", out)

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

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


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