fix(test): replace fixed sleep with health check polling in CLI test server startup

Co-authored-by: jnsgruk <668505+jnsgruk@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-11-25 11:18:43 +00:00 committed by Jon Seager
parent 94c2403370
commit 34d5b157fe
No known key found for this signature in database
4 changed files with 158 additions and 94 deletions

View file

@ -60,8 +60,23 @@ impl TestServer {
.spawn()
.expect("Failed to start brewlog server");
// Wait for server to be ready
std::thread::sleep(std::time::Duration::from_secs(2));
// Wait for server to be ready with health check
let client = reqwest::blocking::Client::new();
let health_url = format!("{}/api/v1/roasters", address);
let max_attempts = 30; // 30 seconds total
let mut attempts = 0;
while attempts < max_attempts {
if client.get(&health_url).send().is_ok() {
break;
}
std::thread::sleep(std::time::Duration::from_millis(100));
attempts += 1;
}
if attempts >= max_attempts {
panic!("Server failed to start within 30 seconds");
}
Self {
address,
@ -82,7 +97,11 @@ impl TestServer {
.output()
.expect("Failed to create token");
assert!(output.status.success(), "Failed to create token: {}", String::from_utf8_lossy(&output.stderr));
assert!(
output.status.success(),
"Failed to create token: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8(output.stdout)
.expect("Invalid UTF-8 in token output")

View file

@ -1,4 +1,4 @@
use crate::helpers::{output_contains, run_brewlog, setup, TestServer};
use crate::helpers::{TestServer, output_contains, run_brewlog, setup};
#[test]
fn test_add_roaster_requires_authentication() {
@ -17,7 +17,10 @@ fn test_add_roaster_requires_authentication() {
&[],
);
assert!(!output.status.success(), "add-roaster without auth should fail");
assert!(
!output.status.success(),
"add-roaster without auth should fail"
);
}
#[test]
@ -38,19 +41,22 @@ fn test_add_roaster_with_authentication() {
&[("BREWLOG_TOKEN", &token)],
);
assert!(output.status.success(), "add-roaster with auth should succeed");
assert!(
output.status.success(),
"add-roaster with auth should succeed"
);
}
#[test]
fn test_list_roasters_works_without_authentication() {
let server = TestServer::start();
let output = run_brewlog(
&["list-roasters", "--server", &server.address],
&[],
);
let output = run_brewlog(&["list-roasters", "--server", &server.address], &[]);
assert!(output.status.success(), "list-roasters should work without auth");
assert!(
output.status.success(),
"list-roasters should work without auth"
);
}
#[test]
@ -75,13 +81,13 @@ fn test_list_roasters_shows_added_roaster() {
assert!(add_output.status.success());
// List roasters
let list_output = run_brewlog(
&["list-roasters", "--server", &server.address],
&[],
);
let list_output = run_brewlog(&["list-roasters", "--server", &server.address], &[]);
assert!(list_output.status.success());
assert!(output_contains(&list_output, "Example Roasters"), "Should list the added roaster");
assert!(
output_contains(&list_output, "Example Roasters"),
"Should list the added roaster"
);
}
#[test]
@ -99,7 +105,10 @@ fn test_delete_roaster_requires_authentication() {
&[],
);
assert!(!output.status.success(), "delete-roaster without auth should fail");
assert!(
!output.status.success(),
"delete-roaster without auth should fail"
);
}
#[test]
@ -119,5 +128,8 @@ fn test_update_roaster_requires_authentication() {
&[],
);
assert!(!output.status.success(), "update-roaster without auth should fail");
assert!(
!output.status.success(),
"update-roaster without auth should fail"
);
}

View file

@ -1,4 +1,4 @@
use crate::helpers::{output_contains, run_brewlog, setup, TestServer};
use crate::helpers::{TestServer, output_contains, run_brewlog, setup};
#[test]
fn test_add_roast_requires_authentication() {
@ -19,7 +19,10 @@ fn test_add_roast_requires_authentication() {
&[],
);
assert!(!output.status.success(), "add-roast without auth should fail");
assert!(
!output.status.success(),
"add-roast without auth should fail"
);
}
#[test]
@ -45,25 +48,25 @@ fn test_add_roast_with_authentication() {
// Extract roaster ID from output (simplified - in reality we'd parse JSON)
// For now, just test that add-roast command works with auth
let output = run_brewlog(
&["add-roast", "--help"],
&[],
);
let output = run_brewlog(&["add-roast", "--help"], &[]);
assert!(output.status.success());
assert!(output_contains(&output, "Add a new roast"), "Help should work");
assert!(
output_contains(&output, "Add a new roast"),
"Help should work"
);
}
#[test]
fn test_list_roasts_works_without_authentication() {
let server = TestServer::start();
let output = run_brewlog(
&["list-roasts", "--server", &server.address],
&[],
);
let output = run_brewlog(&["list-roasts", "--server", &server.address], &[]);
assert!(output.status.success(), "list-roasts should work without auth");
assert!(
output.status.success(),
"list-roasts should work without auth"
);
}
#[test]
@ -81,5 +84,8 @@ fn test_delete_roast_requires_authentication() {
&[],
);
assert!(!output.status.success(), "delete-roast without auth should fail");
assert!(
!output.status.success(),
"delete-roast without auth should fail"
);
}

View file

@ -1,11 +1,17 @@
use crate::helpers::{output_contains, run_brewlog, setup, TestServer};
use crate::helpers::{TestServer, output_contains, run_brewlog, setup};
#[test]
fn test_create_token_without_server_fails() {
setup();
let output = run_brewlog(
&["create-token", "--name", "test-token", "--server", "http://localhost:9999"],
&[
"create-token",
"--name",
"test-token",
"--server",
"http://localhost:9999",
],
&[],
);
@ -17,7 +23,13 @@ fn test_create_token_with_valid_credentials() {
let server = TestServer::start();
let output = run_brewlog(
&["create-token", "--name", "test-token", "--server", &server.address],
&[
"create-token",
"--name",
"test-token",
"--server",
&server.address,
],
&[],
);
@ -29,12 +41,12 @@ fn test_create_token_with_valid_credentials() {
fn test_list_tokens_requires_authentication() {
let server = TestServer::start();
let output = run_brewlog(
&["list-tokens", "--server", &server.address],
&[],
);
let output = run_brewlog(&["list-tokens", "--server", &server.address], &[]);
assert!(!output.status.success(), "list-tokens without auth should fail");
assert!(
!output.status.success(),
"list-tokens without auth should fail"
);
}
#[test]
@ -47,8 +59,14 @@ fn test_list_tokens_with_authentication() {
&[("BREWLOG_TOKEN", &token)],
);
assert!(output.status.success(), "list-tokens with auth should succeed");
assert!(output_contains(&output, "test-token"), "Should list the created token");
assert!(
output.status.success(),
"list-tokens with auth should succeed"
);
assert!(
output_contains(&output, "test-token"),
"Should list the created token"
);
}
#[test]
@ -56,11 +74,20 @@ fn test_revoke_token_requires_authentication() {
let server = TestServer::start();
let output = run_brewlog(
&["revoke-token", "--id", "some-id", "--server", &server.address],
&[
"revoke-token",
"--id",
"some-id",
"--server",
&server.address,
],
&[],
);
assert!(!output.status.success(), "revoke-token without auth should fail");
assert!(
!output.status.success(),
"revoke-token without auth should fail"
);
}
#[test]
@ -80,11 +107,11 @@ fn test_revoke_token_with_authentication() {
// Extract token ID from output (this depends on the CLI output format)
// For now, we'll skip the actual revoke test since we need to parse the output
// Just verify that revoke command exists
let output = run_brewlog(
&["revoke-token", "--help"],
&[],
);
let output = run_brewlog(&["revoke-token", "--help"], &[]);
assert!(output.status.success(), "revoke-token --help should work");
assert!(output_contains(&output, "Revoke"), "Help should mention revoke");
assert!(
output_contains(&output, "Revoke"),
"Help should mention revoke"
);
}