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
2.9 KB
Raw Blame
# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.

"""SkillTurboPromptRail should not advertise a disabled acceleration tool."""

# pylint: disable=protected-access

from types import SimpleNamespace

import pytest

from jiuwenswarm.server.runtime.skill_turbo.rails.skill_prompt_rail import (
    SkillTurboPromptRail,
    _SECTION_NAME,
)


class _FakeBuilder:
    def __init__(self) -> None:
        self.sections: dict[str, object] = {}

    def add_section(self, section) -> None:
        self.sections[section.name] = section

    def remove_section(self, name: str) -> None:
        self.sections.pop(name, None)

    def get_section(self, name: str):
        return self.sections.get(name)


@pytest.mark.asyncio
async def test_skill_turbo_prompt_skipped_when_acceleration_disabled():
    """Explicit constructor flag must skip guide even on ReAct-like ctx.agent."""
    builder = _FakeBuilder()
    deep = SimpleNamespace(system_prompt_builder=builder)
    # Production BEFORE_MODEL_CALL bridge: ctx.agent is inner ReAct (no rails API).
    react_like = SimpleNamespace(system_prompt_builder=builder)

    rail = SkillTurboPromptRail(acceleration_disabled=True)
    rail.init(deep)

    await rail.before_model_call(SimpleNamespace(agent=react_like))

    assert _SECTION_NAME not in builder.sections
    assert rail._agent is deep


@pytest.mark.asyncio
async def test_skill_turbo_prompt_injected_when_tool_enabled(monkeypatch):
    builder = _FakeBuilder()
    deep = SimpleNamespace(system_prompt_builder=builder)
    react_like = SimpleNamespace(system_prompt_builder=builder)

    # 清单动态化后:仅当当前上下文可见加速技能时注入指南(空清单整段
    # 不注入,对齐 acceleration_disabled 先例)。CI 无 turbo 技能目录,
    # 须 stub 非空清单;rail 内部延迟 import 取模块属性,monkeypatch 生效。
    from jiuwenswarm.server.runtime.skill_turbo import skill_turbo_tools

    monkeypatch.setattr(
        skill_turbo_tools,
        "refresh_skill_acceleration_description",
        lambda: ["pptx-craft"],
    )

    rail = SkillTurboPromptRail(acceleration_disabled=False)
    rail.init(deep)

    await rail.before_model_call(SimpleNamespace(agent=react_like))

    assert _SECTION_NAME in builder.sections
    content = builder.sections[_SECTION_NAME].content["cn"]
    assert "skill_acceleration_exec" in content
    assert rail._agent is deep


@pytest.mark.asyncio
async def test_skill_turbo_prompt_removes_stale_section_when_disabled():
    builder = _FakeBuilder()
    builder.sections[_SECTION_NAME] = SimpleNamespace(name=_SECTION_NAME)
    deep = SimpleNamespace(system_prompt_builder=builder)
    react_like = SimpleNamespace(system_prompt_builder=builder)

    rail = SkillTurboPromptRail(acceleration_disabled=True)
    rail.init(deep)
    await rail.before_model_call(SimpleNamespace(agent=react_like))

    assert _SECTION_NAME not in builder.sections