@@ -55,7 +55,7 @@ func TestEndTherapySession(t *testing.T) {
5555
5656func TestRelayTherapyMessage (t * testing.T ) {
5757 // Create a fake therapy session backend implementing both init and run endpoints
58- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
58+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
5959 token := r .Header .Get ("Authorization" )
6060 if token != "Bearer test-token" {
6161 http .Error (w , "unauthorized" , http .StatusUnauthorized )
@@ -69,8 +69,9 @@ func TestRelayTherapyMessage(t *testing.T) {
6969 return
7070 case r .Method == http .MethodPost && r .URL .Path == "/run_sse" :
7171 // Message sending endpoint
72- w .WriteHeader (http .StatusOK )
73- _ , _ = w .Write ([]byte ("Hello, I'm here for you." ))
72+ w .Header ().Set ("Content-Type" , "text/event-stream" )
73+ w .WriteHeader (http .StatusOK )
74+ _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Hello, I'm here for you.\" }],\" role\" :\" model\" }}\n \n " ))
7475 return
7576 default :
7677 http .NotFound (w , r )
@@ -114,7 +115,7 @@ func TestHandleSession_AutoEndWhenExpired(t *testing.T) {
114115
115116func TestHandleSession_ForwardDuringActive (t * testing.T ) {
116117 // Fake backend implementing both init and run endpoints
117- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
118+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
118119 token := r .Header .Get ("Authorization" )
119120 if token != "Bearer test-token" {
120121 http .Error (w , "unauthorized" , http .StatusUnauthorized )
@@ -126,8 +127,9 @@ func TestHandleSession_ForwardDuringActive(t *testing.T) {
126127 _ , _ = w .Write ([]byte (`{"ok":true}` ))
127128 return
128129 case r .Method == http .MethodPost && r .URL .Path == "/run_sse" :
129- w .WriteHeader (http .StatusOK )
130- _ , _ = w .Write ([]byte ("Therapist reply" ))
130+ w .Header ().Set ("Content-Type" , "text/event-stream" )
131+ w .WriteHeader (http .StatusOK )
132+ _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Therapist reply\" }],\" role\" :\" model\" }}\n \n " ))
131133 return
132134 default :
133135 http .NotFound (w , r )
@@ -155,6 +157,50 @@ func TestHandleSession_ForwardDuringActive(t *testing.T) {
155157 }
156158}
157159
160+ func TestRelayTherapyMessage_ExistingSessionContinues (t * testing.T ) {
161+ // Fake backend: init returns 400 Session already exists; run_sse returns a reply
162+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
163+ token := r .Header .Get ("Authorization" )
164+ if token != "Bearer test-token" {
165+ http .Error (w , "unauthorized" , http .StatusUnauthorized )
166+ return
167+ }
168+ switch {
169+ case r .Method == http .MethodPost && strings .HasPrefix (r .URL .Path , "/apps/capymind_agent/users/u1/sessions/" ):
170+ w .WriteHeader (http .StatusBadRequest )
171+ _ , _ = w .Write ([]byte (`{"detail":"Session already exists: abc-123"}` ))
172+ return
173+ case r .Method == http .MethodPost && r .URL .Path == "/run_sse" :
174+ w .Header ().Set ("Content-Type" , "text/event-stream" )
175+ w .WriteHeader (http .StatusOK )
176+ _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Hello again\" }],\" role\" :\" model\" }}\n \n " ))
177+ return
178+ default :
179+ http .NotFound (w , r )
180+ return
181+ }
182+ }))
183+ defer ts .Close ()
184+ os .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
185+ os .Setenv ("CAPY_AGENT_TOKEN" , "test-token" )
186+ defer os .Unsetenv ("CAPY_THERAPY_SESSION_URL" )
187+ defer os .Unsetenv ("CAPY_AGENT_TOKEN" )
188+
189+ ctx := context .Background ()
190+ locale := "en"
191+ user := & database.User {ID : "u1" , Locale : & locale }
192+ session := createSession (& Job {Command : None }, user , nil , & ctx )
193+
194+ relayTherapyMessage ("hi" , session )
195+
196+ if len (session .Job .Output ) == 0 {
197+ t .Fatalf ("expected at least one output" )
198+ }
199+ if session .Job .Output [0 ].TextID != "Hello again" {
200+ t .Fatalf ("unexpected relay text: %s" , session .Job .Output [0 ].TextID )
201+ }
202+ }
203+
158204func TestHandleSession_EndOnOtherCommand (t * testing.T ) {
159205 ctx := context .Background ()
160206 future := time .Now ().Add (5 * time .Minute )
0 commit comments