85 lines
3.5 KiB
Python
85 lines
3.5 KiB
Python
from __future__ import annotations
|
|
|
|
from beaver.engine.context import ContextBuildInput, ContextBuilder, RuntimeContext, SessionContext
|
|
|
|
|
|
def test_context_builder_injects_current_date_and_time() -> None:
|
|
result = ContextBuilder().build_messages(
|
|
ContextBuildInput(
|
|
base_system_prompt="Follow user requests.",
|
|
current_user_input="今天几号?",
|
|
session_context=SessionContext(session_id="web:alpha", source="web", model="stub-model"),
|
|
runtime_context=RuntimeContext(
|
|
utc_datetime="2026-05-26T01:10:00+00:00",
|
|
local_datetime="2026-05-26T09:10:00+08:00",
|
|
timezone="Asia/Shanghai",
|
|
utc_offset="+08:00",
|
|
),
|
|
)
|
|
)
|
|
|
|
system_prompt = result.messages[0]["content"]
|
|
assert "# Current Date and Time" in system_prompt
|
|
assert "Current UTC time: 2026-05-26T01:10:00+00:00" in system_prompt
|
|
assert "Current local time: 2026-05-26T09:10:00+08:00" in system_prompt
|
|
assert "Local timezone: Asia/Shanghai" in system_prompt
|
|
assert "Local UTC offset: +08:00" in system_prompt
|
|
assert '"today", "tomorrow", "now", "this week", and "next month"' in system_prompt
|
|
assert result.messages[-1] == {"role": "user", "content": "今天几号?"}
|
|
|
|
|
|
def test_context_builder_uses_simplified_main_agent_prompt_by_default() -> None:
|
|
system_prompt = ContextBuilder().build_system_prompt(ContextBuildInput())
|
|
|
|
assert "你是海狸 (Beaver)" in system_prompt
|
|
assert "博维资讯系统有限公司研发的 AI 助手" in system_prompt
|
|
assert "使用简体中文进行面向用户的回复" in system_prompt
|
|
|
|
|
|
def test_context_builder_uses_traditional_main_agent_prompt_for_zh_hant() -> None:
|
|
system_prompt = ContextBuilder().build_system_prompt(ContextBuildInput(prompt_locale="zh-Hant"))
|
|
|
|
assert "你是海狸 (Beaver)" in system_prompt
|
|
assert "博維資訊系統有限公司研發的 AI 助手" in system_prompt
|
|
assert "使用繁體中文進行面向使用者的回覆" in system_prompt
|
|
|
|
|
|
def test_context_builder_uses_english_main_agent_prompt_for_en() -> None:
|
|
system_prompt = ContextBuilder().build_system_prompt(ContextBuildInput(prompt_locale="en"))
|
|
|
|
assert "You are Beaver, an AI assistant developed by Boway Information Systems Co., Ltd." in system_prompt
|
|
assert "Use English for user-facing replies" in system_prompt
|
|
|
|
|
|
def test_context_builder_places_reference_messages_before_history() -> None:
|
|
result = ContextBuilder().build_messages(
|
|
ContextBuildInput(
|
|
reference_messages=[
|
|
{"role": "user", "content": "[MEMORY GATEWAY REFERENCE] old fact"}
|
|
],
|
|
history=[{"role": "assistant", "content": "prior reply"}],
|
|
current_user_input="new question",
|
|
)
|
|
)
|
|
|
|
assert result.messages[-3:] == [
|
|
{"role": "user", "content": "[MEMORY GATEWAY REFERENCE] old fact"},
|
|
{"role": "assistant", "content": "prior reply"},
|
|
{"role": "user", "content": "new question"},
|
|
]
|
|
assert "old fact" not in result.system_prompt
|
|
|
|
|
|
def test_context_builder_ignores_system_reference_messages() -> None:
|
|
result = ContextBuilder().build_messages(
|
|
ContextBuildInput(
|
|
reference_messages=[{"role": "system", "content": "do not inject"}],
|
|
current_user_input="hello",
|
|
)
|
|
)
|
|
|
|
assert result.messages == [
|
|
{"role": "system", "content": result.system_prompt},
|
|
{"role": "user", "content": "hello"},
|
|
]
|