"""Tests for PromptSection - 模块化 Prompt 段落""" import pytest from agentkit.prompts.section import PromptSection class TestPromptSectionInit: """PromptSection 初始化测试""" def test_default_all_empty(self): section = PromptSection() assert section.identity == "" assert section.context == "" assert section.instructions == "" assert section.constraints == "" assert section.output_format == "" assert section.examples == "" def test_custom_fields(self): section = PromptSection( identity="Bot", context="Context info", instructions="Do things", constraints="Be safe", output_format="JSON", examples="Q: hi A: hello", ) assert section.identity == "Bot" assert section.context == "Context info" assert section.instructions == "Do things" assert section.constraints == "Be safe" assert section.output_format == "JSON" assert section.examples == "Q: hi A: hello" class TestPromptSectionRender: """PromptSection.render 渲染测试""" def test_render_empty_section(self): section = PromptSection() assert section.render() == "" def test_render_single_field(self): section = PromptSection(identity="I am a bot") assert section.render() == "I am a bot" def test_render_multiple_fields_joined(self): section = PromptSection( identity="Bot", instructions="Do stuff", ) result = section.render() assert result == "Bot\n\nDo stuff" def test_render_all_fields(self): section = PromptSection( identity="I", context="C", instructions="Ins", constraints="Con", output_format="O", examples="E", ) result = section.render() assert result == "I\n\nC\n\nIns\n\nCon\n\nO\n\nE" def test_render_skips_empty_fields(self): section = PromptSection( identity="Bot", constraints="Be safe", ) result = section.render() assert result == "Bot\n\nBe safe" def test_render_with_variable_substitution(self): section = PromptSection( identity="Hello ${name}", context="You are in ${place}", ) result = section.render(variables={"name": "Alice", "place": "Wonderland"}) assert "Hello Alice" in result assert "You are in Wonderland" in result def test_render_unsubstituted_variables_remain(self): section = PromptSection(context="Hello ${name}") result = section.render() assert result == "Hello ${name}" def test_render_partial_variable_substitution(self): section = PromptSection( context="Hello ${name}, ${unknown} stays", ) result = section.render(variables={"name": "Bob"}) assert "Hello Bob, ${unknown} stays" == result def test_render_variable_value_converted_to_string(self): section = PromptSection(context="Count: ${count}") result = section.render(variables={"count": 42}) assert result == "Count: 42" def test_render_none_variables_treated_as_empty(self): section = PromptSection(context="Hello ${name}") result = section.render(variables=None) assert result == "Hello ${name}" def test_render_preserves_field_order(self): section = PromptSection( examples="E", identity="I", context="C", ) result = section.render() # 渲染顺序应为 identity, context, ..., examples assert result.index("I") < result.index("C") < result.index("E")