Skip to content

Commit 54303f0

Browse files
committed
refactor(sse-test-client): use existing coaching data or create if needed
Update test client to query for existing coaching relationships and sessions before attempting to create new ones. This prevents errors when using seeded database with pre-existing coaching relationships. Changes: - Add get_coaching_relationships() and get_coaching_sessions() methods - Update setup_test_environment() to check for existing data first - Fall back to creating new relationships/sessions only if needed - Update messaging to reflect "Using" instead of "Created" - Update README to clarify that seeded data is preferred and will be used
1 parent e931a5e commit 54303f0

File tree

3 files changed

+146
-33
lines changed

3 files changed

+146
-33
lines changed

sse-test-client/README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ A standalone Rust binary for testing Server-Sent Events (SSE) functionality with
77
This tool validates the SSE infrastructure by:
88
1. Authenticating two users (typically a coach and coachee)
99
2. Establishing SSE connections for both users
10-
3. Optionally creating a test coaching relationship and session (for action tests)
10+
3. Using existing coaching relationships/sessions or creating them if needed (for action tests)
1111
4. Triggering events (create/update/delete actions, force logout)
1212
5. Verifying that the correct SSE events are received by the appropriate users
1313

1414
## Prerequisites
1515

1616
- Backend server running (default: `http://localhost:4000`)
17-
- Two valid user accounts with credentials
18-
- **For action tests only**: Users must have admin permission to create coaching relationships
19-
- **For connection test**: No special permissions required
17+
- Two valid user accounts with credentials (seeded users recommended)
18+
- **For action tests**: An existing coaching relationship between the users (will be created if it doesn't exist)
19+
- **For connection test**: No special permissions or relationships required
2020

2121
## Usage
2222

@@ -82,12 +82,12 @@ cargo run -p sse-test-client -- \
8282

8383
## Available Scenarios
8484

85-
- `connection-test` - Tests basic SSE connectivity without creating any data (no admin permissions required)
86-
- `action-create` - Tests SSE events for action creation (requires admin permissions)
87-
- `action-update` - Tests SSE events for action updates (requires admin permissions)
88-
- `action-delete` - Tests SSE events for action deletion (requires admin permissions)
89-
- `force-logout-test` - Tests SSE events for force logout (requires admin permissions, NOT YET IMPLEMENTED)
90-
- `all` - Runs all test scenarios sequentially (requires admin permissions for action tests)
85+
- `connection-test` - Tests basic SSE connectivity without creating any data
86+
- `action-create` - Tests SSE events for action creation (uses existing coaching relationship or creates one)
87+
- `action-update` - Tests SSE events for action updates (uses existing coaching relationship or creates one)
88+
- `action-delete` - Tests SSE events for action deletion (uses existing coaching relationship or creates one)
89+
- `force-logout-test` - Tests SSE events for force logout (NOT YET IMPLEMENTED)
90+
- `all` - Runs all test scenarios sequentially
9191

9292
## Command-Line Arguments
9393

@@ -103,7 +103,7 @@ cargo run -p sse-test-client -- \
103103

104104
### Setup Phase
105105
1. Authenticates both users and obtains session cookies
106-
2. For action tests: Creates a coaching relationship and session between the users
106+
2. For action tests: Finds existing coaching relationship/session or creates new ones if needed
107107
3. For connection test: Skips coaching data setup
108108
4. Establishes SSE connections for both users
109109

@@ -153,16 +153,16 @@ Results: 1 passed, 0 failed
153153
All tests passed! ✓
154154
```
155155

156-
### Action Test (Requires Admin)
156+
### Action Test
157157
```
158158
=== SETUP PHASE ===
159159
→ Authenticating users...
160160
✓ User 1 authenticated (ID: 123e4567-e89b-12d3-a456-426614174000)
161161
✓ User 2 authenticated (ID: 234e5678-e89b-12d3-a456-426614174001)
162162
163-
Creating test coaching relationship and session...
164-
Coaching relationship created (ID: 345e6789-e89b-12d3-a456-426614174002)
165-
Coaching session created (ID: 456e789a-e89b-12d3-a456-426614174003)
163+
Setting up test coaching relationship and session...
164+
Using coaching relationship (ID: 345e6789-e89b-12d3-a456-426614174002)
165+
Using coaching session (ID: 456e789a-e89b-12d3-a456-426614174003)
166166
167167
→ Establishing SSE connections...
168168
✓ User 1 SSE connection established

sse-test-client/src/api_client.rs

Lines changed: 128 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,56 @@ impl ApiClient {
3131
.as_str()
3232
.context("No organization ID found")?;
3333

34-
// Create coaching relationship
35-
let relationship = self
36-
.create_coaching_relationship(coach_session, organization_id, coach_id, coachee_id)
34+
// Query for existing coaching relationships
35+
let relationships = self
36+
.get_coaching_relationships(coach_session, organization_id)
3737
.await?;
3838

39-
let relationship_id = relationship["id"]
40-
.as_str()
41-
.context("No relationship ID in response")?
42-
.to_string();
43-
44-
// Create coaching session
45-
let session = self
46-
.create_coaching_session(coach_session, &relationship_id)
39+
// Find existing relationship or create new one
40+
let relationship_id = if let Some(relationship) = relationships.as_array().and_then(|arr| {
41+
arr.iter().find(|rel| {
42+
rel["coach_id"].as_str() == Some(coach_id)
43+
&& rel["coachee_id"].as_str() == Some(coachee_id)
44+
})
45+
}) {
46+
relationship["id"]
47+
.as_str()
48+
.context("No relationship ID in existing relationship")?
49+
.to_string()
50+
} else {
51+
// Create new coaching relationship
52+
let relationship = self
53+
.create_coaching_relationship(coach_session, organization_id, coach_id, coachee_id)
54+
.await?;
55+
56+
relationship["id"]
57+
.as_str()
58+
.context("No relationship ID in response")?
59+
.to_string()
60+
};
61+
62+
// Query for existing coaching sessions
63+
let sessions = self
64+
.get_coaching_sessions(coach_session, &relationship_id)
4765
.await?;
4866

49-
let session_id = session["id"]
50-
.as_str()
51-
.context("No session ID in response")?
52-
.to_string();
67+
// Use existing session or create new one
68+
let session_id = if let Some(session) = sessions.as_array().and_then(|arr| arr.first()) {
69+
session["id"]
70+
.as_str()
71+
.context("No session ID in existing session")?
72+
.to_string()
73+
} else {
74+
// Create new coaching session
75+
let session = self
76+
.create_coaching_session(coach_session, &relationship_id)
77+
.await?;
78+
79+
session["id"]
80+
.as_str()
81+
.context("No session ID in response")?
82+
.to_string()
83+
};
5384

5485
Ok(TestEnvironment {
5586
relationship_id,
@@ -91,6 +122,88 @@ impl ApiClient {
91122
.map(|arr| Value::Array(arr.clone()))
92123
}
93124

125+
async fn get_coaching_relationships(
126+
&self,
127+
session_cookie: &str,
128+
organization_id: &str,
129+
) -> Result<Value> {
130+
let url = format!(
131+
"{}/organizations/{}/coaching_relationships",
132+
self.base_url, organization_id
133+
);
134+
135+
let response = self
136+
.client
137+
.get(&url)
138+
.header("Cookie", format!("id={}", session_cookie))
139+
.header("x-version", "1.0.0-beta1")
140+
.send()
141+
.await
142+
.context("Failed to get coaching relationships")?;
143+
144+
if !response.status().is_success() {
145+
let status = response.status();
146+
let body = response
147+
.text()
148+
.await
149+
.unwrap_or_else(|_| "Unable to read response body".to_string());
150+
anyhow::bail!(
151+
"Failed to get coaching relationships: {} - Response: {}",
152+
status,
153+
body
154+
);
155+
}
156+
157+
let api_response: Value = response.json().await.context("Failed to parse response")?;
158+
159+
// Extract the data array from ApiResponse wrapper
160+
api_response["data"]
161+
.as_array()
162+
.context("No data array in response")
163+
.map(|arr| Value::Array(arr.clone()))
164+
}
165+
166+
async fn get_coaching_sessions(
167+
&self,
168+
session_cookie: &str,
169+
relationship_id: &str,
170+
) -> Result<Value> {
171+
let url = format!(
172+
"{}/coaching_relationships/{}/coaching_sessions",
173+
self.base_url, relationship_id
174+
);
175+
176+
let response = self
177+
.client
178+
.get(&url)
179+
.header("Cookie", format!("id={}", session_cookie))
180+
.header("x-version", "1.0.0-beta1")
181+
.send()
182+
.await
183+
.context("Failed to get coaching sessions")?;
184+
185+
if !response.status().is_success() {
186+
let status = response.status();
187+
let body = response
188+
.text()
189+
.await
190+
.unwrap_or_else(|_| "Unable to read response body".to_string());
191+
anyhow::bail!(
192+
"Failed to get coaching sessions: {} - Response: {}",
193+
status,
194+
body
195+
);
196+
}
197+
198+
let api_response: Value = response.json().await.context("Failed to parse response")?;
199+
200+
// Extract the data array from ApiResponse wrapper
201+
api_response["data"]
202+
.as_array()
203+
.context("No data array in response")
204+
.map(|arr| Value::Array(arr.clone()))
205+
}
206+
94207
async fn create_coaching_relationship(
95208
&self,
96209
session_cookie: &str,

sse-test-client/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async fn main() -> Result<()> {
9999
}
100100
_ => {
101101
println!(
102-
"\n{} Creating test coaching relationship and session...",
102+
"\n{} Setting up test coaching relationship and session...",
103103
"→".blue()
104104
);
105105
let env = api_client
@@ -112,12 +112,12 @@ async fn main() -> Result<()> {
112112
.await?;
113113

114114
println!(
115-
"{} Coaching relationship created (ID: {})",
115+
"{} Using coaching relationship (ID: {})",
116116
"✓".green(),
117117
env.relationship_id
118118
);
119119
println!(
120-
"{} Coaching session created (ID: {})",
120+
"{} Using coaching session (ID: {})",
121121
"✓".green(),
122122
env.session_id
123123
);

0 commit comments

Comments
 (0)