@@ -22,7 +22,7 @@ import (
2222
2323const (
2424 testTimeout = 30 * time .Second
25- operationTimeout = 5 * time .Second
25+ operationTimeout = 10 * time .Second
2626 healthCheckTimeout = 10 * time .Second
2727)
2828
@@ -40,15 +40,14 @@ func TestE2E(t *testing.T) {
4040 t .Run ("basic" , func (t * testing.T ) {
4141 ctx , cancel := context .WithTimeout (context .Background (), testTimeout )
4242 defer cancel ()
43- script , apiClient := setup (ctx , t )
44- require .NoError (t , waitAgentAPIStable (ctx , apiClient , operationTimeout ))
43+ script , apiClient := setup (ctx , t , nil )
4544 messageReq := agentapisdk.PostMessageParams {
4645 Content : "This is a test message." ,
4746 Type : agentapisdk .MessageTypeUser ,
4847 }
4948 _ , err := apiClient .PostMessage (ctx , messageReq )
5049 require .NoError (t , err , "Failed to send message via SDK" )
51- require .NoError (t , waitAgentAPIStable (ctx , apiClient , operationTimeout ))
50+ require .NoError (t , waitAgentAPIStable (ctx , t , apiClient , operationTimeout , "post message" ))
5251 msgResp , err := apiClient .GetMessages (ctx )
5352 require .NoError (t , err , "Failed to get messages via SDK" )
5453 require .Len (t , msgResp .Messages , 3 )
@@ -61,7 +60,7 @@ func TestE2E(t *testing.T) {
6160 ctx , cancel := context .WithTimeout (context .Background (), testTimeout )
6261 defer cancel ()
6362
64- script , apiClient := setup (ctx , t )
63+ script , apiClient := setup (ctx , t , nil )
6564 messageReq := agentapisdk.PostMessageParams {
6665 Content : "What is the answer to life, the universe, and everything?" ,
6766 Type : agentapisdk .MessageTypeUser ,
@@ -71,7 +70,7 @@ func TestE2E(t *testing.T) {
7170 statusResp , err := apiClient .GetStatus (ctx )
7271 require .NoError (t , err )
7372 require .Equal (t , agentapisdk .StatusRunning , statusResp .Status )
74- require .NoError (t , waitAgentAPIStable (ctx , apiClient , 5 * time .Second ))
73+ require .NoError (t , waitAgentAPIStable (ctx , t , apiClient , 5 * time .Second , "post message" ))
7574 msgResp , err := apiClient .GetMessages (ctx )
7675 require .NoError (t , err , "Failed to get messages via SDK" )
7776 require .Len (t , msgResp .Messages , 3 )
@@ -82,11 +81,49 @@ func TestE2E(t *testing.T) {
8281 require .Equal (t , script [1 ].ResponseMessage , strings .TrimSpace (parts [0 ]))
8382 require .Equal (t , script [2 ].ResponseMessage , strings .TrimSpace (parts [1 ]))
8483 })
84+
85+ t .Run ("stdin" , func (t * testing.T ) {
86+ ctx , cancel := context .WithTimeout (context .Background (), testTimeout )
87+ defer cancel ()
88+
89+ script , apiClient := setup (ctx , t , & params {
90+ cmdFn : func (ctx context.Context , t testing.TB , serverPort int , binaryPath , cwd , scriptFilePath string ) (string , []string ) {
91+ script := fmt .Sprintf (`echo "hello agent" | %s server --port=%d -- go run %s %s` ,
92+ binaryPath ,
93+ serverPort ,
94+ filepath .Join (cwd , "echo.go" ),
95+ scriptFilePath ,
96+ )
97+ return "/bin/sh" , []string {"-c" , script }
98+ },
99+ })
100+ require .NoError (t , waitAgentAPIStable (ctx , t , apiClient , 5 * time .Second , "stdin" ))
101+ msgResp , err := apiClient .GetMessages (ctx )
102+ require .NoError (t , err , "Failed to get messages via SDK" )
103+ require .Len (t , msgResp .Messages , 3 )
104+ require .Equal (t , script [0 ].ExpectMessage , strings .TrimSpace (msgResp .Messages [1 ].Content ))
105+ require .Equal (t , script [0 ].ResponseMessage , strings .TrimSpace (msgResp .Messages [2 ].Content ))
106+ })
85107}
86108
87- func setup (ctx context.Context , t testing.TB ) ([]ScriptEntry , * agentapisdk.Client ) {
109+ type params struct {
110+ cmdFn func (ctx context.Context , t testing.TB , serverPort int , binaryPath , cwd , scriptFilePath string ) (string , []string )
111+ }
112+
113+ func defaultCmdFn (ctx context.Context , t testing.TB , serverPort int , binaryPath , cwd , scriptFilePath string ) (string , []string ) {
114+ return binaryPath , []string {"server" , fmt .Sprintf ("--port=%d" , serverPort ), "--" , "go" , "run" , filepath .Join (cwd , "echo.go" ), scriptFilePath }
115+ }
116+
117+ func setup (ctx context.Context , t testing.TB , p * params ) ([]ScriptEntry , * agentapisdk.Client ) {
88118 t .Helper ()
89119
120+ if p == nil {
121+ p = & params {}
122+ }
123+ if p .cmdFn == nil {
124+ p .cmdFn = defaultCmdFn
125+ }
126+
90127 scriptFilePath := filepath .Join ("testdata" , filepath .Base (t .Name ())+ ".json" )
91128 data , err := os .ReadFile (scriptFilePath )
92129 require .NoError (t , err , "Failed to read test script file: %s" , scriptFilePath )
@@ -116,10 +153,8 @@ func setup(ctx context.Context, t testing.TB) ([]ScriptEntry, *agentapisdk.Clien
116153 cwd , err := os .Getwd ()
117154 require .NoError (t , err , "Failed to get current working directory" )
118155
119- cmd := exec .CommandContext (ctx , binaryPath , "server" ,
120- fmt .Sprintf ("--port=%d" , serverPort ),
121- "--" ,
122- "go" , "run" , filepath .Join (cwd , "echo.go" ), scriptFilePath )
156+ bin , args := p .cmdFn (ctx , t , serverPort , binaryPath , cwd , scriptFilePath )
157+ cmd := exec .CommandContext (ctx , bin , args ... )
123158
124159 // Capture output for debugging
125160 stdout , err := cmd .StdoutPipe ()
@@ -160,7 +195,7 @@ func setup(ctx context.Context, t testing.TB) ([]ScriptEntry, *agentapisdk.Clien
160195 apiClient , err := agentapisdk .NewClient (serverURL )
161196 require .NoError (t , err , "Failed to create agentapi SDK client" )
162197
163- require .NoError (t , waitAgentAPIStable (ctx , apiClient , operationTimeout ))
198+ require .NoError (t , waitAgentAPIStable (ctx , t , apiClient , operationTimeout , "setup" ))
164199 return script , apiClient
165200}
166201
@@ -198,21 +233,30 @@ func waitForServer(ctx context.Context, t testing.TB, url string, timeout time.D
198233 }
199234}
200235
201- func waitAgentAPIStable (ctx context.Context , apiClient * agentapisdk.Client , waitFor time.Duration ) error {
236+ func waitAgentAPIStable (ctx context.Context , t testing.TB , apiClient * agentapisdk.Client , waitFor time.Duration , msg string ) error {
237+ t .Helper ()
202238 waitCtx , waitCancel := context .WithTimeout (ctx , waitFor )
203239 defer waitCancel ()
204240
205- tick := time .NewTicker (100 * time .Millisecond )
241+ start := time .Now ()
242+ tick := time .NewTicker (time .Millisecond )
206243 defer tick .Stop ()
244+ var prevStatus agentapisdk.AgentStatus
245+ defer func () {
246+ elapsed := time .Since (start )
247+ t .Logf ("%s: agent API status: %s (elapsed: %s)" , msg , prevStatus , elapsed .Round (100 * time .Millisecond ))
248+ }()
207249 for {
208250 select {
209251 case <- waitCtx .Done ():
210252 return waitCtx .Err ()
211253 case <- tick .C :
254+ tick .Reset (100 * time .Millisecond )
212255 sr , err := apiClient .GetStatus (ctx )
213256 if err != nil {
214257 continue
215258 }
259+ prevStatus = sr .Status
216260 if sr .Status == agentapisdk .StatusStable {
217261 return nil
218262 }
0 commit comments