11mod common;
22
3- use agent:: agent_config:: definitions:: * ;
4- use common:: * ;
53use std:: collections:: HashMap ;
64use std:: time:: Duration ;
5+
6+ use agent:: agent_config:: definitions:: * ;
7+ use common:: * ;
78use tracing:: info;
89
910fn create_spawn_hook_single_shell_config ( ) -> AgentConfig {
10- let hooks = HashMap :: from ( [
11- (
12- HookTrigger :: AgentSpawn ,
13- vec ! [ HookConfig :: ShellCommand ( CommandHook {
14- command: "echo 'Agent initialized'" . to_string( ) ,
15- opts: BaseHookConfig {
16- timeout_ms: 5000 ,
17- max_output_size: 1024 ,
18- cache_ttl_seconds: 0 ,
19- matcher: None ,
20- } ,
21- } ) ] ,
22- ) ,
23- ] ) ;
11+ let hooks = HashMap :: from ( [ ( HookTrigger :: AgentSpawn , vec ! [ HookConfig :: ShellCommand ( CommandHook {
12+ command: "echo 'Agent initialized'" . to_string( ) ,
13+ opts: BaseHookConfig {
14+ timeout_ms: 5000 ,
15+ max_output_size: 1024 ,
16+ cache_ttl_seconds: 0 ,
17+ matcher: None ,
18+ } ,
19+ } ) ] ) ] ) ;
2420
2521 AgentConfig :: V2025_08_22 ( AgentConfigV2025_08_22 {
2622 hooks,
2723 ..Default :: default ( )
2824 } )
2925}
3026
31-
3227#[ tokio:: test]
3328async fn test_agent_spawn_hook_single ( ) {
3429 let _ = tracing_subscriber:: fmt:: try_init ( ) ;
35-
30+
3631 let mut test: TestCase = TestCase :: builder ( )
3732 . test_name ( "agent spawn hook behavior for single shell command and one turn" )
3833 . with_agent_config ( create_spawn_hook_single_shell_config ( ) )
@@ -44,19 +39,19 @@ async fn test_agent_spawn_hook_single() {
4439 . build ( )
4540 . await
4641 . unwrap ( ) ;
47-
42+
4843 test. wait_until_agent_initializes ( Duration :: from_millis ( 100 ) ) . await ;
4944 test. send_prompt ( "hello" . to_string ( ) ) . await ;
5045 test. wait_until_agent_stop ( Duration :: from_millis ( 100 ) ) . await ;
5146 let req = test. requests ( ) . first ( ) . expect ( "should have one request" ) ;
52- let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
47+ let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
5348 assert_contains ( & first_msg, "Agent initialized" ) ;
5449}
5550
5651#[ tokio:: test]
5752async fn test_agent_spawn_hook_persistence ( ) {
5853 let _ = tracing_subscriber:: fmt:: try_init ( ) ;
59-
54+
6055 let mut test: TestCase = TestCase :: builder ( )
6156 . test_name ( "agent spawn hook behavior for single shell command and two turns" )
6257 . with_agent_config ( create_spawn_hook_single_shell_config ( ) )
@@ -68,35 +63,32 @@ async fn test_agent_spawn_hook_persistence() {
6863 . build ( )
6964 . await
7065 . unwrap ( ) ;
71-
66+
7267 test. wait_until_agent_initializes ( Duration :: from_millis ( 100 ) ) . await ;
7368 test. send_prompt ( "hello" . to_string ( ) ) . await ;
7469 test. wait_until_agent_stop ( Duration :: from_millis ( 100 ) ) . await ;
7570 test. send_prompt ( "bye" . to_string ( ) ) . await ;
7671 test. wait_until_agent_stop ( Duration :: from_millis ( 100 ) ) . await ;
7772 for req in test. requests ( ) {
78- let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
73+ let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
7974 assert_contains ( & first_msg, "Agent initialized" ) ;
8075 }
8176}
8277
8378#[ tokio:: test]
8479async fn test_user_prompt_hook_single ( ) {
8580 let _ = tracing_subscriber:: fmt:: try_init ( ) ;
86- let hooks = HashMap :: from ( [
87- (
88- HookTrigger :: UserPromptSubmit ,
89- vec ! [ HookConfig :: ShellCommand ( CommandHook {
90- command: "echo 'submitted!'" . to_string( ) ,
91- opts: BaseHookConfig {
92- timeout_ms: 5000 ,
93- max_output_size: 1024 ,
94- cache_ttl_seconds: 0 ,
95- matcher: None ,
96- } ,
97- } ) ] ,
98- ) ,
99- ] ) ;
81+ let hooks = HashMap :: from ( [ ( HookTrigger :: UserPromptSubmit , vec ! [ HookConfig :: ShellCommand (
82+ CommandHook {
83+ command: "echo 'submitted!'" . to_string( ) ,
84+ opts: BaseHookConfig {
85+ timeout_ms: 5000 ,
86+ max_output_size: 1024 ,
87+ cache_ttl_seconds: 0 ,
88+ matcher: None ,
89+ } ,
90+ } ,
91+ ) ] ) ] ) ;
10092
10193 let agent_config = AgentConfig :: V2025_08_22 ( AgentConfigV2025_08_22 {
10294 hooks,
@@ -113,33 +105,30 @@ async fn test_user_prompt_hook_single() {
113105 . build ( )
114106 . await
115107 . unwrap ( ) ;
116-
108+
117109 test. wait_until_agent_initializes ( Duration :: from_millis ( 100 ) ) . await ;
118110 test. send_prompt ( "hello" . to_string ( ) ) . await ;
119111 test. wait_until_agent_stop ( Duration :: from_millis ( 100 ) ) . await ;
120112 let req = test. requests ( ) . first ( ) . expect ( "should have one request" ) ;
121- let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
113+ let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
122114 assert_contains ( & first_msg, "submitted!" ) ;
123115}
124116
125117#[ tokio:: test]
126118async fn test_user_prompt_hook_persistence ( ) {
127119 let _ = tracing_subscriber:: fmt:: try_init ( ) ;
128-
129- let hooks = HashMap :: from ( [
130- (
131- HookTrigger :: UserPromptSubmit ,
132- vec ! [ HookConfig :: ShellCommand ( CommandHook {
133- command: "printf 'a' >> turns.txt; printf \" char count: $(wc -c < turns.txt | tr -d ' ')\" " . to_string( ) ,
134- opts: BaseHookConfig {
135- timeout_ms: 5000 ,
136- max_output_size: 1024 ,
137- cache_ttl_seconds: 0 ,
138- matcher: None ,
139- } ,
140- } ) ] ,
141- ) ,
142- ] ) ;
120+
121+ let hooks = HashMap :: from ( [ ( HookTrigger :: UserPromptSubmit , vec ! [ HookConfig :: ShellCommand (
122+ CommandHook {
123+ command: "printf 'a' >> turns.txt; printf \" char count: $(wc -c < turns.txt | tr -d ' ')\" " . to_string( ) ,
124+ opts: BaseHookConfig {
125+ timeout_ms: 5000 ,
126+ max_output_size: 1024 ,
127+ cache_ttl_seconds: 0 ,
128+ matcher: None ,
129+ } ,
130+ } ,
131+ ) ] ) ] ) ;
143132
144133 let agent_config = AgentConfig :: V2025_08_22 ( AgentConfigV2025_08_22 {
145134 hooks,
@@ -158,16 +147,16 @@ async fn test_user_prompt_hook_persistence() {
158147 . build ( )
159148 . await
160149 . unwrap ( ) ;
161-
150+
162151 test. wait_until_agent_initializes ( Duration :: from_millis ( 100 ) ) . await ;
163152 test. send_prompt ( "hello" . to_string ( ) ) . await ;
164153 test. wait_until_agent_stop ( Duration :: from_millis ( 100 ) ) . await ;
165154 test. send_prompt ( "bye" . to_string ( ) ) . await ;
166155 test. wait_until_agent_stop ( Duration :: from_millis ( 100 ) ) . await ;
167156 let req = test. requests ( ) . first ( ) . expect ( "first request should exist" ) ;
168- let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
169- assert_contains ( & first_msg, "char count: 1" ) ;
170- let req = test. requests ( ) . get ( 1 ) . expect ( "second request should exist" ) ;
171- let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
172- assert_contains ( & first_msg, "char count: 2" ) ;
157+ let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
158+ assert_contains ( & first_msg, "char count: 1" ) ;
159+ let req = test. requests ( ) . get ( 1 ) . expect ( "second request should exist" ) ;
160+ let first_msg = req. messages ( ) . first ( ) . expect ( "first message should exist" ) . text ( ) ;
161+ assert_contains ( & first_msg, "char count: 2" ) ;
173162}
0 commit comments