84 lines
2.4 KiB
Rust
84 lines
2.4 KiB
Rust
//! 系统提示模板的单元测试。
|
||
//! Unit tests for system prompt templates.
|
||
|
||
use focus_core::model::Usage;
|
||
use focus_harness::prompt::{
|
||
days_to_ymd, default_context, ContextUsage, PromptContext, SystemPromptTemplate,
|
||
};
|
||
use std::path::PathBuf;
|
||
|
||
#[test]
|
||
fn days_to_ymd_known_dates() {
|
||
// 1970-01-01 → 0。
|
||
assert_eq!(days_to_ymd(0), (1970, 1, 1));
|
||
// 2000-01-01。
|
||
// 30 年 * 365.25 ≈ 10957 天。
|
||
assert_eq!(days_to_ymd(10_957), (2000, 1, 1));
|
||
// 2024-02-29(闰年)。
|
||
// 2024-02-29 (leap year).
|
||
let days = (2024 - 1970) * 365 + (2024 - 1969) / 4 + 59;
|
||
assert_eq!(days_to_ymd(days), (2024, 2, 29));
|
||
}
|
||
|
||
#[test]
|
||
fn renders_default_template() {
|
||
let tpl = SystemPromptTemplate::default();
|
||
let mut ctx = default_context();
|
||
ctx.context_usage = Some(ContextUsage {
|
||
estimated_tokens: 12_345,
|
||
context_window: 128_000,
|
||
window_known: true,
|
||
});
|
||
ctx.last_usage = Some(Usage {
|
||
input_tokens: 100,
|
||
output_tokens: 50,
|
||
cache_read_tokens: 5,
|
||
cache_write_tokens: 0,
|
||
});
|
||
ctx.extra.push("remember: rust edition 2021".to_string());
|
||
let out = tpl.render(&ctx);
|
||
assert!(out.contains(&ctx.cwd.display().to_string()));
|
||
assert!(
|
||
out.contains("Context usage: 12,345 / 128,000 tokens (9.6%)."),
|
||
"got: {}",
|
||
out
|
||
);
|
||
assert!(out.contains("Last turn usage: input 100, output 50, cache-read 5, cache-write 0."));
|
||
assert!(out.contains("remember: rust edition 2021"));
|
||
assert!(out.contains(ctx.os));
|
||
}
|
||
|
||
#[test]
|
||
fn renders_unknown_window_note() {
|
||
let tpl = SystemPromptTemplate::default();
|
||
let mut ctx = default_context();
|
||
ctx.context_usage = Some(ContextUsage {
|
||
estimated_tokens: 50_000,
|
||
context_window: 128_000,
|
||
window_known: false,
|
||
});
|
||
let out = tpl.render(&ctx);
|
||
assert!(
|
||
out.contains("window unknown; assuming 128,000"),
|
||
"got: {}",
|
||
out
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn missing_fields_render_empty() {
|
||
let tpl = SystemPromptTemplate::default();
|
||
let ctx = PromptContext {
|
||
cwd: PathBuf::from("/tmp"),
|
||
os: "linux",
|
||
date: "2025-01-01".into(),
|
||
shell: "bash".into(),
|
||
context_usage: None,
|
||
last_usage: None,
|
||
extra: Vec::new(),
|
||
};
|
||
let out = tpl.render(&ctx);
|
||
assert!(!out.contains("{context_usage}"));
|
||
assert!(!out.contains("{last_usage}"));
|
||
}
|