11package app
22
33import (
4- "context"
5- "net/http"
6- "net/http/httptest"
7- "os"
8- "testing"
9- "time"
4+ "context"
5+ "net/http"
6+ "net/http/httptest"
7+ "testing"
8+ "time"
109
11- "strings"
10+ "strings"
1211
13- "github.com/capymind/internal/database"
12+ "github.com/capymind/internal/database"
1413)
1514
1615func TestStartTherapySession (t * testing.T ) {
@@ -56,11 +55,6 @@ func TestEndTherapySession(t *testing.T) {
5655func TestRelayTherapyMessage (t * testing.T ) {
5756 // Create a fake therapy session backend implementing both init and run endpoints
5857 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
59- token := r .Header .Get ("Authorization" )
60- if token != "Bearer test-token" {
61- http .Error (w , "unauthorized" , http .StatusUnauthorized )
62- return
63- }
6458 switch {
6559 case r .Method == http .MethodPost && strings .HasPrefix (r .URL .Path , "/apps/capymind_agent/users/u1/sessions/" ):
6660 // Session init endpoint
@@ -79,10 +73,14 @@ func TestRelayTherapyMessage(t *testing.T) {
7973 }
8074 }))
8175 defer ts .Close ()
82- os .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
83- os .Setenv ("CAPY_AGENT_TOKEN" , "test-token" )
84- defer os .Unsetenv ("CAPY_THERAPY_SESSION_URL" )
85- defer os .Unsetenv ("CAPY_AGENT_TOKEN" )
76+ t .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
77+
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 }()
8684
8785 ctx := context .Background ()
8886 locale := "en"
@@ -91,7 +89,7 @@ func TestRelayTherapyMessage(t *testing.T) {
9189
9290 relayTherapyMessage ("hi" , session )
9391
94- if len (session .Job .Output ) == 0 {
92+ if len (session .Job .Output ) == 0 {
9593 t .Fatalf ("expected at least one output" )
9694 }
9795 if session .Job .Output [0 ].TextID != "Hello, I'm here for you." {
@@ -116,11 +114,6 @@ func TestHandleSession_AutoEndWhenExpired(t *testing.T) {
116114func TestHandleSession_ForwardDuringActive (t * testing.T ) {
117115 // Fake backend implementing both init and run endpoints
118116 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
119- token := r .Header .Get ("Authorization" )
120- if token != "Bearer test-token" {
121- http .Error (w , "unauthorized" , http .StatusUnauthorized )
122- return
123- }
124117 switch {
125118 case r .Method == http .MethodPost && strings .HasPrefix (r .URL .Path , "/apps/capymind_agent/users/u1/sessions/" ):
126119 w .WriteHeader (http .StatusOK )
@@ -137,10 +130,13 @@ func TestHandleSession_ForwardDuringActive(t *testing.T) {
137130 }
138131 }))
139132 defer ts .Close ()
140- os .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
141- os .Setenv ("CAPY_AGENT_TOKEN" , "test-token" )
142- defer os .Unsetenv ("CAPY_THERAPY_SESSION_URL" )
143- defer os .Unsetenv ("CAPY_AGENT_TOKEN" )
133+ t .Setenv ("CAPY_THERAPY_SESSION_URL" , ts .URL )
134+
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 }()
144140
145141 ctx := context .Background ()
146142 future := time .Now ().Add (5 * time .Minute )
@@ -160,11 +156,6 @@ func TestHandleSession_ForwardDuringActive(t *testing.T) {
160156func TestRelayTherapyMessage_ExistingSessionContinues (t * testing.T ) {
161157 // Fake backend: init returns 400 Session already exists; run_sse returns a reply
162158 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- }
168159 switch {
169160 case r .Method == http .MethodPost && strings .HasPrefix (r .URL .Path , "/apps/capymind_agent/users/u1/sessions/" ):
170161 w .WriteHeader (http .StatusBadRequest )
@@ -181,10 +172,13 @@ func TestRelayTherapyMessage_ExistingSessionContinues(t *testing.T) {
181172 }
182173 }))
183174 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" )
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 }()
188182
189183 ctx := context .Background ()
190184 locale := "en"
0 commit comments