11package app
22
33import (
4- "context"
5- "net/http"
6- "net/http/httptest"
7- "testing"
8- "time"
4+ "context"
5+ "net/http"
6+ "net/http/httptest"
7+ "testing"
8+ "time"
99
10- "strings"
10+ "strings"
1111
12- "github.com/capymind/internal/database"
12+ "github.com/capymind/internal/database"
1313)
1414
1515func TestStartTherapySession (t * testing.T ) {
@@ -54,7 +54,7 @@ func TestEndTherapySession(t *testing.T) {
5454
5555func TestRelayTherapyMessage (t * testing.T ) {
5656 // Create a fake therapy session backend implementing both init and run endpoints
57- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
57+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
5858 switch {
5959 case r .Method == http .MethodPost && strings .HasPrefix (r .URL .Path , "/apps/capymind_agent/users/u1/sessions/" ):
6060 // Session init endpoint
@@ -63,24 +63,24 @@ func TestRelayTherapyMessage(t *testing.T) {
6363 return
6464 case r .Method == http .MethodPost && r .URL .Path == "/run_sse" :
6565 // Message sending endpoint
66- w .Header ().Set ("Content-Type" , "text/event-stream" )
67- w .WriteHeader (http .StatusOK )
68- _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Hello, I'm here for you.\" }],\" role\" :\" model\" }}\n \n " ))
66+ w .Header ().Set ("Content-Type" , "text/event-stream" )
67+ w .WriteHeader (http .StatusOK )
68+ _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Hello, I'm here for you.\" }],\" role\" :\" model\" }}\n \n " ))
6969 return
7070 default :
7171 http .NotFound (w , r )
7272 return
7373 }
7474 }))
7575 defer ts .Close ()
76- t .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
76+ t .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
7777
78- // Inject simple HTTP client without Google auth for tests
79- originalBuilder := newTherapyHTTPClient
80- newTherapyHTTPClient = func (ctx context.Context , targetURL string ) (* http.Client , error ) {
81- return & http.Client {Timeout : 5 * time .Second }, nil
82- }
83- defer func () { newTherapyHTTPClient = originalBuilder }()
78+ // Inject simple HTTP client without Google auth for tests
79+ originalBuilder := newTherapyHTTPClient
80+ newTherapyHTTPClient = func (ctx context.Context , targetURL string ) (* http.Client , error ) {
81+ return & http.Client {Timeout : 5 * time .Second }, nil
82+ }
83+ defer func () { newTherapyHTTPClient = originalBuilder }()
8484
8585 ctx := context .Background ()
8686 locale := "en"
@@ -89,7 +89,7 @@ func TestRelayTherapyMessage(t *testing.T) {
8989
9090 relayTherapyMessage ("hi" , session )
9191
92- if len (session .Job .Output ) == 0 {
92+ if len (session .Job .Output ) == 0 {
9393 t .Fatalf ("expected at least one output" )
9494 }
9595 if session .Job .Output [0 ].TextID != "Hello, I'm here for you." {
@@ -113,30 +113,30 @@ func TestHandleSession_AutoEndWhenExpired(t *testing.T) {
113113
114114func TestHandleSession_ForwardDuringActive (t * testing.T ) {
115115 // Fake backend implementing both init and run endpoints
116- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
116+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
117117 switch {
118118 case r .Method == http .MethodPost && strings .HasPrefix (r .URL .Path , "/apps/capymind_agent/users/u1/sessions/" ):
119119 w .WriteHeader (http .StatusOK )
120120 _ , _ = w .Write ([]byte (`{"ok":true}` ))
121121 return
122122 case r .Method == http .MethodPost && r .URL .Path == "/run_sse" :
123- w .Header ().Set ("Content-Type" , "text/event-stream" )
124- w .WriteHeader (http .StatusOK )
125- _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Therapist reply\" }],\" role\" :\" model\" }}\n \n " ))
123+ w .Header ().Set ("Content-Type" , "text/event-stream" )
124+ w .WriteHeader (http .StatusOK )
125+ _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Therapist reply\" }],\" role\" :\" model\" }}\n \n " ))
126126 return
127127 default :
128128 http .NotFound (w , r )
129129 return
130130 }
131131 }))
132132 defer ts .Close ()
133- t .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
133+ t .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
134134
135- originalBuilder := newTherapyHTTPClient
136- newTherapyHTTPClient = func (ctx context.Context , targetURL string ) (* http.Client , error ) {
137- return & http.Client {Timeout : 5 * time .Second }, nil
138- }
139- defer func () { newTherapyHTTPClient = originalBuilder }()
135+ originalBuilder := newTherapyHTTPClient
136+ newTherapyHTTPClient = func (ctx context.Context , targetURL string ) (* http.Client , error ) {
137+ return & http.Client {Timeout : 5 * time .Second }, nil
138+ }
139+ defer func () { newTherapyHTTPClient = originalBuilder }()
140140
141141 ctx := context .Background ()
142142 future := time .Now ().Add (5 * time .Minute )
@@ -154,45 +154,45 @@ func TestHandleSession_ForwardDuringActive(t *testing.T) {
154154}
155155
156156func TestRelayTherapyMessage_ExistingSessionContinues (t * testing.T ) {
157- // Fake backend: init returns 400 Session already exists; run_sse returns a reply
158- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
159- switch {
160- case r .Method == http .MethodPost && strings .HasPrefix (r .URL .Path , "/apps/capymind_agent/users/u1/sessions/" ):
161- w .WriteHeader (http .StatusBadRequest )
162- _ , _ = w .Write ([]byte (`{"detail":"Session already exists: abc-123"}` ))
163- return
164- case r .Method == http .MethodPost && r .URL .Path == "/run_sse" :
165- w .Header ().Set ("Content-Type" , "text/event-stream" )
166- w .WriteHeader (http .StatusOK )
167- _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Hello again\" }],\" role\" :\" model\" }}\n \n " ))
168- return
169- default :
170- http .NotFound (w , r )
171- return
172- }
173- }))
174- defer ts .Close ()
175- t .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
176-
177- originalBuilder := newTherapyHTTPClient
178- newTherapyHTTPClient = func (ctx context.Context , targetURL string ) (* http.Client , error ) {
179- return & http.Client {Timeout : 5 * time .Second }, nil
180- }
181- defer func () { newTherapyHTTPClient = originalBuilder }()
182-
183- ctx := context .Background ()
184- locale := "en"
185- user := & database.User {ID : "u1" , Locale : & locale }
186- session := createSession (& Job {Command : None }, user , nil , & ctx )
187-
188- relayTherapyMessage ("hi" , session )
189-
190- if len (session .Job .Output ) == 0 {
191- t .Fatalf ("expected at least one output" )
192- }
193- if session .Job .Output [0 ].TextID != "Hello again" {
194- t .Fatalf ("unexpected relay text: %s" , session .Job .Output [0 ].TextID )
195- }
157+ // Fake backend: init returns 400 Session already exists; run_sse returns a reply
158+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
159+ switch {
160+ case r .Method == http .MethodPost && strings .HasPrefix (r .URL .Path , "/apps/capymind_agent/users/u1/sessions/" ):
161+ w .WriteHeader (http .StatusBadRequest )
162+ _ , _ = w .Write ([]byte (`{"detail":"Session already exists: abc-123"}` ))
163+ return
164+ case r .Method == http .MethodPost && r .URL .Path == "/run_sse" :
165+ w .Header ().Set ("Content-Type" , "text/event-stream" )
166+ w .WriteHeader (http .StatusOK )
167+ _ , _ = w .Write ([]byte ("data: {\" content\" :{\" parts\" :[{\" text\" :\" Hello again\" }],\" role\" :\" model\" }}\n \n " ))
168+ return
169+ default :
170+ http .NotFound (w , r )
171+ return
172+ }
173+ }))
174+ defer ts .Close ()
175+ t .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
176+
177+ originalBuilder := newTherapyHTTPClient
178+ newTherapyHTTPClient = func (ctx context.Context , targetURL string ) (* http.Client , error ) {
179+ return & http.Client {Timeout : 5 * time .Second }, nil
180+ }
181+ defer func () { newTherapyHTTPClient = originalBuilder }()
182+
183+ ctx := context .Background ()
184+ locale := "en"
185+ user := & database.User {ID : "u1" , Locale : & locale }
186+ session := createSession (& Job {Command : None }, user , nil , & ctx )
187+
188+ relayTherapyMessage ("hi" , session )
189+
190+ if len (session .Job .Output ) == 0 {
191+ t .Fatalf ("expected at least one output" )
192+ }
193+ if session .Job .Output [0 ].TextID != "Hello again" {
194+ t .Fatalf ("unexpected relay text: %s" , session .Job .Output [0 ].TextID )
195+ }
196196}
197197
198198func TestHandleSession_EndOnOtherCommand (t * testing.T ) {
0 commit comments