fix tests

This commit is contained in:
Jochen
2025-11-19 12:42:37 +11:00
parent 9bffd8b1bf
commit 3f21bdc7b2
4 changed files with 73 additions and 116 deletions

View File

@@ -6,14 +6,10 @@ use std::sync::Arc;
fn test_task_result_basic_functionality() { fn test_task_result_basic_functionality() {
// Create a context window with some messages // Create a context window with some messages
let mut context = ContextWindow::new(10000); let mut context = ContextWindow::new(10000);
context.add_message(Message { context.add_message(Message::new(MessageRole::User, "Test message 1".to_string())
role: MessageRole::User, );
content: "Test message 1".to_string(), context.add_message(Message::new(MessageRole::Assistant, "Response 1".to_string())
}); );
context.add_message(Message {
role: MessageRole::Assistant,
content: "Response 1".to_string(),
});
// Create a TaskResult // Create a TaskResult
let response = "This is the response\n\nFinal output block".to_string(); let response = "This is the response\n\nFinal output block".to_string();
@@ -100,10 +96,7 @@ fn test_context_window_preservation() {
// Add some messages // Add some messages
for i in 0..5 { for i in 0..5 {
context.add_message(Message { context.add_message(Message::new(if i % 2 == 0 { MessageRole::User } else { MessageRole::Assistant }, format!("Message {}", i)));
role: if i % 2 == 0 { MessageRole::User } else { MessageRole::Assistant },
content: format!("Message {}", i),
});
} }
// Create TaskResult // Create TaskResult

View File

@@ -46,10 +46,10 @@ fn test_thin_context_basic() {
// Add some messages to the first third // Add some messages to the first third
for i in 0..9 { for i in 0..9 {
if i % 2 == 0 { if i % 2 == 0 {
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::Assistant, MessageRole::Assistant,
content: format!("Assistant message {}", i), format!("Assistant message {}", i),
}); ));
} else { } else {
// Add tool results with varying sizes // Add tool results with varying sizes
let content = if i == 1 { let content = if i == 1 {
@@ -63,10 +63,10 @@ fn test_thin_context_basic() {
format!("Tool result: small result {}", i) format!("Tool result: small result {}", i)
}; };
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::User, MessageRole::User,
content, content,
}); ));
} }
} }
@@ -98,10 +98,10 @@ fn test_thin_write_file_tool_calls() {
let mut context = ContextWindow::new(10000); let mut context = ContextWindow::new(10000);
// Add some messages including a write_file tool call with large content // Add some messages including a write_file tool call with large content
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::User, MessageRole::User,
content: "Please create a large file".to_string(), "Please create a large file".to_string(),
}); ));
// Add an assistant message with a write_file tool call containing large content // Add an assistant message with a write_file tool call containing large content
let large_content = "x".repeat(1500); let large_content = "x".repeat(1500);
@@ -109,22 +109,22 @@ fn test_thin_write_file_tool_calls() {
r#"{{"tool": "write_file", "args": {{"file_path": "test.txt", "content": "{}"}}}}"#, r#"{{"tool": "write_file", "args": {{"file_path": "test.txt", "content": "{}"}}}}"#,
large_content large_content
); );
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::Assistant, MessageRole::Assistant,
content: format!("I'll create that file.\n\n{}", tool_call_json), format!("I'll create that file.\n\n{}", tool_call_json),
}); ));
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::User, MessageRole::User,
content: "Tool result: ✅ Successfully wrote 1500 lines".to_string(), "Tool result: ✅ Successfully wrote 1500 lines".to_string(),
}); ));
// Add more messages to ensure we have enough for "first third" logic // Add more messages to ensure we have enough for "first third" logic
for i in 0..6 { for i in 0..6 {
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::Assistant, MessageRole::Assistant,
content: format!("Response {}", i), format!("Response {}", i),
}); ));
} }
// Trigger thinning at 50% // Trigger thinning at 50%
@@ -154,10 +154,10 @@ fn test_thin_str_replace_tool_calls() {
let mut context = ContextWindow::new(10000); let mut context = ContextWindow::new(10000);
// Add some messages including a str_replace tool call with large diff // Add some messages including a str_replace tool call with large diff
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::User, MessageRole::User,
content: "Please update the file".to_string(), "Please update the file".to_string(),
}); ));
// Add an assistant message with a str_replace tool call containing large diff // Add an assistant message with a str_replace tool call containing large diff
let large_diff = format!("--- old\n{}\n+++ new\n{}", "-old line\n".repeat(100), "+new line\n".repeat(100)); let large_diff = format!("--- old\n{}\n+++ new\n{}", "-old line\n".repeat(100), "+new line\n".repeat(100));
@@ -165,22 +165,22 @@ fn test_thin_str_replace_tool_calls() {
r#"{{"tool": "str_replace", "args": {{"file_path": "test.txt", "diff": "{}"}}}}"#, r#"{{"tool": "str_replace", "args": {{"file_path": "test.txt", "diff": "{}"}}}}"#,
large_diff.replace('\n', "\\n") large_diff.replace('\n', "\\n")
); );
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::Assistant, MessageRole::Assistant,
content: format!("I'll update that file.\n\n{}", tool_call_json), format!("I'll update that file.\n\n{}", tool_call_json),
}); ));
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::User, MessageRole::User,
content: "Tool result: ✅ applied unified diff".to_string(), "Tool result: ✅ applied unified diff".to_string(),
}); ));
// Add more messages to ensure we have enough for "first third" logic // Add more messages to ensure we have enough for "first third" logic
for i in 0..6 { for i in 0..6 {
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::Assistant, MessageRole::Assistant,
content: format!("Response {}", i), format!("Response {}", i),
}); ));
} }
// Trigger thinning at 50% // Trigger thinning at 50%
@@ -212,10 +212,10 @@ fn test_thin_context_no_large_results() {
// Add only small messages // Add only small messages
for i in 0..9 { for i in 0..9 {
context.add_message(Message { context.add_message(Message::new(
role: MessageRole::User, MessageRole::User,
content: format!("Tool result: small {}", i), format!("Tool result: small {}", i),
}); ));
} }
context.used_tokens = 5000; context.used_tokens = 5000;
@@ -244,7 +244,7 @@ fn test_thin_context_only_affects_first_third() {
MessageRole::Assistant MessageRole::Assistant
}; };
context.add_message(Message { role, content }); context.add_message(Message::new(role, content));
} }
context.used_tokens = 5000; context.used_tokens = 5000;

View File

@@ -8,27 +8,18 @@ fn test_todo_read_results_not_thinned() {
let mut context = ContextWindow::new(10000); let mut context = ContextWindow::new(10000);
// Add a todo_read tool call // Add a todo_read tool call
context.add_message(Message { context.add_message(Message::new(MessageRole::Assistant, r#"{"tool": "todo_read", "args": {}}"#.to_string()));
role: MessageRole::Assistant,
content: r#"{"tool": "todo_read", "args": {}}"#.to_string(),
});
// Add a large TODO result (> 500 chars) // Add a large TODO result (> 500 chars)
let large_todo_result = format!( let large_todo_result = format!(
"Tool result: 📝 TODO list:\n{}", "Tool result: 📝 TODO list:\n{}",
"- [ ] Task with long description\n".repeat(50) "- [ ] Task with long description\n".repeat(50)
); );
context.add_message(Message { context.add_message(Message::new(MessageRole::User, large_todo_result.clone()));
role: MessageRole::User,
content: large_todo_result.clone(),
});
// Add more messages to ensure we have enough for "first third" logic // Add more messages to ensure we have enough for "first third" logic
for i in 0..6 { for i in 0..6 {
context.add_message(Message { context.add_message(Message::new(MessageRole::Assistant, format!("Response {}", i)))
role: MessageRole::Assistant,
content: format!("Response {}", i),
});
} }
// Trigger thinning at 50% // Trigger thinning at 50%
@@ -65,27 +56,18 @@ fn test_todo_write_results_not_thinned() {
// Add a todo_write tool call // Add a todo_write tool call
let large_content = "- [ ] Task\n".repeat(100); let large_content = "- [ ] Task\n".repeat(100);
context.add_message(Message { context.add_message(Message::new(MessageRole::Assistant, format!(r#"{{"tool": "todo_write", "args": {{"content": "{}"}}}}"#, large_content)));
role: MessageRole::Assistant,
content: format!(r#"{{"tool": "todo_write", "args": {{"content": "{}"}}}}"#, large_content),
});
// Add a large TODO write result // Add a large TODO write result
let large_todo_result = format!( let large_todo_result = format!(
"Tool result: ✅ TODO list updated ({} chars) and saved to todo.g3.md", "Tool result: ✅ TODO list updated ({} chars) and saved to todo.g3.md",
large_content.len() large_content.len()
); );
context.add_message(Message { context.add_message(Message::new(MessageRole::User, large_todo_result.clone()));
role: MessageRole::User,
content: large_todo_result.clone(),
});
// Add more messages // Add more messages
for i in 0..6 { for i in 0..6 {
context.add_message(Message { context.add_message(Message::new(MessageRole::Assistant, format!("Response {}", i)))
role: MessageRole::Assistant,
content: format!("Response {}", i),
});
} }
// Trigger thinning at 50% // Trigger thinning at 50%
@@ -119,24 +101,15 @@ fn test_non_todo_results_still_thinned() {
let mut context = ContextWindow::new(10000); let mut context = ContextWindow::new(10000);
// Add a non-TODO tool call (e.g., read_file) // Add a non-TODO tool call (e.g., read_file)
context.add_message(Message { context.add_message(Message::new(MessageRole::Assistant, r#"{"tool": "read_file", "args": {"file_path": "test.txt"}}"#.to_string()));
role: MessageRole::Assistant,
content: r#"{"tool": "read_file", "args": {"file_path": "test.txt"}}"#.to_string(),
});
// Add a large read_file result (> 500 chars) // Add a large read_file result (> 500 chars)
let large_result = format!("Tool result: {}", "x".repeat(1500)); let large_result = format!("Tool result: {}", "x".repeat(1500));
context.add_message(Message { context.add_message(Message::new(MessageRole::User, large_result));
role: MessageRole::User,
content: large_result,
});
// Add more messages // Add more messages
for i in 0..6 { for i in 0..6 {
context.add_message(Message { context.add_message(Message::new(MessageRole::Assistant, format!("Response {}", i)))
role: MessageRole::Assistant,
content: format!("Response {}", i),
});
} }
// Trigger thinning at 50% // Trigger thinning at 50%
@@ -172,27 +145,18 @@ fn test_todo_read_with_spaces_in_tool_name() {
let mut context = ContextWindow::new(10000); let mut context = ContextWindow::new(10000);
// Add a todo_read tool call with spaces (JSON formatting variation) // Add a todo_read tool call with spaces (JSON formatting variation)
context.add_message(Message { context.add_message(Message::new(MessageRole::Assistant, r#"{"tool": "todo_read", "args": {}}"#.to_string()));
role: MessageRole::Assistant,
content: r#"{"tool": "todo_read", "args": {}}"#.to_string(),
});
// Add a large TODO result // Add a large TODO result
let large_todo_result = format!( let large_todo_result = format!(
"Tool result: 📝 TODO list:\n{}", "Tool result: 📝 TODO list:\n{}",
"- [ ] Task\n".repeat(50) "- [ ] Task\n".repeat(50)
); );
context.add_message(Message { context.add_message(Message::new(MessageRole::User, large_todo_result.clone()));
role: MessageRole::User,
content: large_todo_result.clone(),
});
// Add more messages // Add more messages
for i in 0..6 { for i in 0..6 {
context.add_message(Message { context.add_message(Message::new(MessageRole::Assistant, format!("Response {}", i)))
role: MessageRole::Assistant,
content: format!("Response {}", i),
});
} }
// Trigger thinning // Trigger thinning

View File

@@ -27,7 +27,7 @@ fn get_todo_path(temp_dir: &TempDir) -> PathBuf {
#[serial] #[serial]
async fn test_todo_write_creates_file() { async fn test_todo_write_creates_file() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
let todo_path = get_todo_path(&temp_dir); let todo_path = get_todo_path(&temp_dir);
// Initially, todo.g3.md should not exist // Initially, todo.g3.md should not exist
@@ -67,7 +67,7 @@ async fn test_todo_read_from_file() {
fs::write(&todo_path, test_content).unwrap(); fs::write(&todo_path, test_content).unwrap();
// Create agent (should load from file) // Create agent (should load from file)
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
// Create a tool call to read TODO // Create a tool call to read TODO
let tool_call = g3_core::ToolCall { let tool_call = g3_core::ToolCall {
@@ -88,7 +88,7 @@ async fn test_todo_read_from_file() {
#[serial] #[serial]
async fn test_todo_read_empty_file() { async fn test_todo_read_empty_file() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
// Create a tool call to read TODO (file doesn't exist) // Create a tool call to read TODO (file doesn't exist)
let tool_call = g3_core::ToolCall { let tool_call = g3_core::ToolCall {
@@ -111,7 +111,7 @@ async fn test_todo_persistence_across_agents() {
// Agent 1: Write TODO // Agent 1: Write TODO
{ {
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
let tool_call = g3_core::ToolCall { let tool_call = g3_core::ToolCall {
tool: "todo_write".to_string(), tool: "todo_write".to_string(),
args: serde_json::json!({ args: serde_json::json!({
@@ -126,7 +126,7 @@ async fn test_todo_persistence_across_agents() {
// Agent 2: Read TODO (new agent instance) // Agent 2: Read TODO (new agent instance)
{ {
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
let tool_call = g3_core::ToolCall { let tool_call = g3_core::ToolCall {
tool: "todo_read".to_string(), tool: "todo_read".to_string(),
args: serde_json::json!({}), args: serde_json::json!({}),
@@ -143,7 +143,7 @@ async fn test_todo_persistence_across_agents() {
#[serial] #[serial]
async fn test_todo_update_preserves_file() { async fn test_todo_update_preserves_file() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
let todo_path = get_todo_path(&temp_dir); let todo_path = get_todo_path(&temp_dir);
// Write initial TODO // Write initial TODO
@@ -173,7 +173,7 @@ async fn test_todo_update_preserves_file() {
#[serial] #[serial]
async fn test_todo_handles_large_content() { async fn test_todo_handles_large_content() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
let todo_path = get_todo_path(&temp_dir); let todo_path = get_todo_path(&temp_dir);
// Create a large TODO (but under the 50k limit) // Create a large TODO (but under the 50k limit)
@@ -202,7 +202,7 @@ async fn test_todo_handles_large_content() {
#[serial] #[serial]
async fn test_todo_respects_size_limit() { async fn test_todo_respects_size_limit() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
// Create content that exceeds the default 50k limit // Create content that exceeds the default 50k limit
let huge_content = "x".repeat(60_000); let huge_content = "x".repeat(60_000);
@@ -232,7 +232,7 @@ async fn test_todo_agent_initialization_loads_file() {
fs::write(&todo_path, initial_content).unwrap(); fs::write(&todo_path, initial_content).unwrap();
// Create agent - should load the file during initialization // Create agent - should load the file during initialization
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
// Read TODO - should return the pre-existing content // Read TODO - should return the pre-existing content
let tool_call = g3_core::ToolCall { let tool_call = g3_core::ToolCall {
@@ -248,7 +248,7 @@ async fn test_todo_agent_initialization_loads_file() {
#[serial] #[serial]
async fn test_todo_handles_unicode_content() { async fn test_todo_handles_unicode_content() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
let todo_path = get_todo_path(&temp_dir); let todo_path = get_todo_path(&temp_dir);
// Create TODO with unicode characters // Create TODO with unicode characters
@@ -283,7 +283,7 @@ async fn test_todo_handles_unicode_content() {
#[serial] #[serial]
async fn test_todo_empty_content_creates_empty_file() { async fn test_todo_empty_content_creates_empty_file() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
let todo_path = get_todo_path(&temp_dir); let todo_path = get_todo_path(&temp_dir);
// Write empty TODO // Write empty TODO
@@ -306,7 +306,7 @@ async fn test_todo_empty_content_creates_empty_file() {
#[serial] #[serial]
async fn test_todo_whitespace_only_content() { async fn test_todo_whitespace_only_content() {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let agent = create_test_agent_in_dir(&temp_dir).await; let mut agent = create_test_agent_in_dir(&temp_dir).await;
// Write whitespace-only TODO // Write whitespace-only TODO
let tool_call = g3_core::ToolCall { let tool_call = g3_core::ToolCall {