Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

test(host): keep the end-to-end tests from failing at random

leepokaif5834de56cb0
diff --git a/host/tests/common/mod.rs b/host/tests/common/mod.rs
index d31800c0..5afd964f 100644
--- a/host/tests/common/mod.rs
+++ b/host/tests/common/mod.rs
@@ -1,6 +1,6 @@
//! Shared by the end-to-end tests.
-use std::process::{Child, Command};
+use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
/// Use the checksum-verified release fixture so isolated hosts don't each download it.
@@ -14,6 +14,33 @@ pub fn seed_memory_runtime(home: &std::path::Path) {
std::fs::write(dir.join(".installed"), "3.2.1").expect("mark fixture installed");
}
+/// Runs `codync-host serve` for `home` until it answers: the process and its base URL. A port
+/// picked here is only free for a moment; when something else takes it before the host binds, the
+/// host exits and this starts it again on another one.
+pub async fn serve(home: &std::path::Path) -> (Child, String) {
+ let deadline = Instant::now() + Duration::from_secs(30);
+ loop {
+ let port = std::net::TcpListener::bind("127.0.0.1:0").expect("free port").local_addr().expect("port").port();
+ let base = format!("http://127.0.0.1:{port}");
+ let mut child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
+ .args(["serve", "--bind", "127.0.0.1", "--port", &port.to_string()])
+ .env("CODYNC_HOME", home)
+ .env("CODYNC_CLOUD", "off")
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .spawn()
+ .expect("start the host");
+ while child.try_wait().expect("host state").is_none() {
+ if reqwest::get(format!("{base}/health")).await.is_ok() {
+ return (child, base);
+ }
+ assert!(Instant::now() < deadline, "host didn't start");
+ tokio::time::sleep(Duration::from_millis(50)).await;
+ }
+ assert!(Instant::now() < deadline, "host didn't start");
+ }
+}
+
/// Stops a test host the way launchd does (SIGTERM), so it shuts down its own
/// children: a SIGKILL would orphan whatever it had running, such as a harness
/// sign-in check (`cursor-agent status` then spins forever). Kills it after 5 s.
diff --git a/host/tests/e2e.rs b/host/tests/e2e.rs
index f4e90f40..7e7d04e1 100644
--- a/host/tests/e2e.rs
+++ b/host/tests/e2e.rs
@@ -14,7 +14,7 @@ use ed25519_dalek::SigningKey;
use futures::{SinkExt as _, StreamExt as _};
use serde_json::{Value, json};
use std::path::PathBuf;
-use std::process::{Child, Command, Stdio};
+use std::process::{Child, Command};
use std::time::{Duration, Instant};
struct Host {
@@ -31,29 +31,11 @@ impl Drop for Host {
}
}
-fn free_port() -> u16 {
- std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
-}
-
async fn start_host() -> Host {
let home = std::env::temp_dir().join(format!("codync-e2e-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&home).unwrap();
common::seed_memory_runtime(&home);
- let port = free_port();
- let child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
- .args(["serve", "--bind", "127.0.0.1", "--port", &port.to_string()])
- .env("CODYNC_HOME", &home)
- .env("CODYNC_CLOUD", "off")
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .spawn()
- .unwrap();
- let base = format!("http://127.0.0.1:{port}");
- let deadline = Instant::now() + Duration::from_secs(20);
- while reqwest::get(format!("{base}/health")).await.is_err() {
- assert!(Instant::now() < deadline, "host didn't start");
- tokio::time::sleep(Duration::from_millis(100)).await;
- }
+ let (child, base) = common::serve(&home).await;
let token = std::fs::read_to_string(home.join("token")).unwrap().trim().to_owned();
Host { child, base, token, home }
}
diff --git a/host/tests/files_e2e.rs b/host/tests/files_e2e.rs
index e61040c5..26418c64 100644
--- a/host/tests/files_e2e.rs
+++ b/host/tests/files_e2e.rs
@@ -6,7 +6,7 @@ mod common;
use serde_json::{Value, json};
use std::path::PathBuf;
-use std::process::{Child, Command, Stdio};
+use std::process::Child;
use std::time::Duration;
struct Host {
@@ -28,53 +28,15 @@ impl Host {
let home = std::env::temp_dir().join(format!("codync-files-e2e-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&home).unwrap();
common::seed_memory_runtime(&home);
- let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
- let child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
- .args(["serve", "--bind", "127.0.0.1", "--port", &port.to_string()])
- .env("CODYNC_CLOUD", "off")
- .env("CODYNC_HOME", &home)
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .spawn()
- .unwrap();
- let mut host = Self { child, home, base: format!("http://127.0.0.1:{port}"), token: String::new() };
- let waited = tokio::time::timeout(Duration::from_secs(20), async {
- loop {
- let response = reqwest::get(format!("{}/health", host.base)).await;
- if response.is_ok() {
- break;
- }
- tokio::time::sleep(Duration::from_millis(50)).await;
- }
- })
- .await;
- waited.unwrap();
+ let (child, base) = common::serve(&home).await;
+ let mut host = Self { child, home, base, token: String::new() };
std::fs::read_to_string(host.home.join("token")).unwrap().trim().clone_into(&mut host.token);
host
}
async fn restart(&mut self) {
common::stop(&mut self.child);
- let port = self.base.rsplit(':').next().unwrap();
- self.child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
- .args(["serve", "--bind", "127.0.0.1", "--port", port])
- .env("CODYNC_CLOUD", "off")
- .env("CODYNC_HOME", &self.home)
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .spawn()
- .unwrap();
- let waited = tokio::time::timeout(Duration::from_secs(20), async {
- loop {
- let response = reqwest::get(format!("{}/health", self.base)).await;
- if response.is_ok() {
- break;
- }
- tokio::time::sleep(Duration::from_millis(50)).await;
- }
- })
- .await;
- waited.unwrap();
+ (self.child, self.base) = common::serve(&self.home).await;
}
async fn call(&self, method: &str, body: Value) -> Value {
diff --git a/host/tests/memory_e2e.rs b/host/tests/memory_e2e.rs
index 686707f1..f70137bd 100644
--- a/host/tests/memory_e2e.rs
+++ b/host/tests/memory_e2e.rs
@@ -10,7 +10,7 @@ use serde_json::{Value, json};
use std::{
fmt::Write as _,
path::PathBuf,
- process::{Child, Command, Stdio},
+ process::Child,
time::{Duration, Instant},
};
@@ -33,21 +33,7 @@ impl Host {
assert!(std::env::var_os("CODYNC_TEST_ENGRAM").is_some(), "set CODYNC_TEST_ENGRAM to the pinned binary");
let home = std::env::temp_dir().join(format!("codync-memory-e2e-{}", uuid::Uuid::new_v4()));
common::seed_memory_runtime(&home);
- let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
- let child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
- .args(["serve", "--bind", "127.0.0.1", "--port", &port.to_string()])
- .env("CODYNC_HOME", &home)
- .env("CODYNC_CLOUD", "off")
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .spawn()
- .unwrap();
- let base = format!("http://127.0.0.1:{port}");
- let deadline = Instant::now() + Duration::from_secs(20);
- while reqwest::get(format!("{base}/health")).await.is_err() {
- assert!(Instant::now() < deadline, "host did not start");
- tokio::time::sleep(Duration::from_millis(50)).await;
- }
+ let (child, base) = common::serve(&home).await;
let token = std::fs::read_to_string(home.join("token")).unwrap().trim().to_owned();
Self { child, home, base, token }
}
diff --git a/host/tests/routines_e2e.rs b/host/tests/routines_e2e.rs
index 4deb8187..2395bdd2 100644
--- a/host/tests/routines_e2e.rs
+++ b/host/tests/routines_e2e.rs
@@ -30,45 +30,15 @@ impl Host {
let home = std::env::temp_dir().join(format!("codync-routines-e2e-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&home).unwrap();
common::seed_memory_runtime(&home);
- let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
- let child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
- .args(["serve", "--bind", "127.0.0.1", "--port", &port.to_string()])
- .env("CODYNC_CLOUD", "off")
- .env("CODYNC_HOME", &home)
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .spawn()
- .unwrap();
- let mut host = Self { child, home, base: format!("http://127.0.0.1:{port}"), token: String::new() };
- tokio::time::timeout(Duration::from_secs(20), async {
- while reqwest::get(format!("{}/health", host.base)).await.is_err() {
- tokio::time::sleep(Duration::from_millis(50)).await;
- }
- })
- .await
- .unwrap();
+ let (child, base) = common::serve(&home).await;
+ let mut host = Self { child, home, base, token: String::new() };
std::fs::read_to_string(host.home.join("token")).unwrap().trim().clone_into(&mut host.token);
host
}
async fn restart(&mut self) {
common::stop(&mut self.child);
- let port = self.base.rsplit(':').next().unwrap();
- self.child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
- .args(["serve", "--bind", "127.0.0.1", "--port", port])
- .env("CODYNC_CLOUD", "off")
- .env("CODYNC_HOME", &self.home)
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .spawn()
- .unwrap();
- tokio::time::timeout(Duration::from_secs(20), async {
- while reqwest::get(format!("{}/health", self.base)).await.is_err() {
- tokio::time::sleep(Duration::from_millis(50)).await;
- }
- })
- .await
- .unwrap();
+ (self.child, self.base) = common::serve(&self.home).await;
}
async fn call(&self, method: &str, body: Value) -> Value {
diff --git a/host/tests/team_e2e.rs b/host/tests/team_e2e.rs
index 77191c30..8dc285cf 100644
--- a/host/tests/team_e2e.rs
+++ b/host/tests/team_e2e.rs
@@ -7,7 +7,7 @@ mod common;
use serde_json::{Value, json};
use std::path::PathBuf;
-use std::process::{Child, Command, Stdio};
+use std::process::Child;
use std::time::Duration;
struct Host {
@@ -29,23 +29,8 @@ impl Host {
let home = std::env::temp_dir().join(format!("codync-team-e2e-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&home).unwrap();
common::seed_memory_runtime(&home);
- let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
- let child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
- .args(["serve", "--bind", "127.0.0.1", "--port", &port.to_string()])
- .env("CODYNC_CLOUD", "off")
- .env("CODYNC_HOME", &home)
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .spawn()
- .unwrap();
- let mut host = Self { child, home, base: format!("http://127.0.0.1:{port}"), token: String::new() };
- tokio::time::timeout(Duration::from_secs(20), async {
- while reqwest::get(format!("{}/health", host.base)).await.is_err() {
- tokio::time::sleep(Duration::from_millis(50)).await;
- }
- })
- .await
- .unwrap();
+ let (child, base) = common::serve(&home).await;
+ let mut host = Self { child, home, base, token: String::new() };
std::fs::read_to_string(host.home.join("token")).unwrap().trim().clone_into(&mut host.token);
host
}
diff --git a/host/tests/turns_e2e.rs b/host/tests/turns_e2e.rs
index 132b6b86..a0e754b7 100644
--- a/host/tests/turns_e2e.rs
+++ b/host/tests/turns_e2e.rs
@@ -7,7 +7,7 @@ mod common;
use serde_json::{Value, json};
use std::path::PathBuf;
-use std::process::{Child, Command, Stdio};
+use std::process::{Child, Command};
use std::time::{Duration, Instant};
struct Host {
@@ -29,21 +29,7 @@ impl Host {
let home = std::env::temp_dir().join(format!("codync-turns-e2e-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&home).unwrap();
common::seed_memory_runtime(&home);
- let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
- let child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
- .args(["serve", "--bind", "127.0.0.1", "--port", &port.to_string()])
- .env("CODYNC_HOME", &home)
- .env("CODYNC_CLOUD", "off")
- .stdout(Stdio::null())
- .stderr(Stdio::null())
- .spawn()
- .unwrap();
- let base = format!("http://127.0.0.1:{port}");
- let deadline = Instant::now() + Duration::from_secs(30);
- while reqwest::get(format!("{base}/health")).await.is_err() {
- assert!(Instant::now() < deadline, "host didn't start");
- tokio::time::sleep(Duration::from_millis(100)).await;
- }
+ let (child, base) = common::serve(&home).await;
let token = std::fs::read_to_string(home.join("token")).unwrap().trim().to_owned();
Self { child, base, token, home }
}
diff --git a/packaging/engram/test-runtime.py b/packaging/engram/test-runtime.py
index 9af9621f..aa31828e 100644
--- a/packaging/engram/test-runtime.py
+++ b/packaging/engram/test-runtime.py
@@ -21,23 +21,33 @@ extension = "zip" if system == "windows" else "tar.gz"
name = "engram.exe" if system == "windows" else "engram"
archive = f"engram_{version}_{target}.{extension}"
url = f"https://github.com/Gentleman-Programming/engram/releases/download/v{version}/{archive}"
-with urllib.request.urlopen(url, timeout=120) as response:
- data = response.read(128 * 1024 * 1024 + 1)
-if hashlib.sha256(data).hexdigest() != release["assets"][target]:
- raise SystemExit("Engram release checksum mismatch")
destination = root / "host" / "target" / "engram-test-runtime" / version / name
-destination.parent.mkdir(parents=True, exist_ok=True)
-if extension == "zip":
- with zipfile.ZipFile(io.BytesIO(data)) as files:
- binary = files.read(name)
-else:
- with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as files:
- member = files.getmember(name)
- if not member.isfile():
- raise SystemExit("Engram release does not contain a regular binary")
- binary = files.extractfile(member).read()
-destination.write_bytes(binary)
-destination.chmod(0o755)
+
+
+def fetch():
+ with urllib.request.urlopen(url, timeout=120) as response:
+ data = response.read(128 * 1024 * 1024 + 1)
+ if hashlib.sha256(data).hexdigest() != release["assets"][target]:
+ raise SystemExit("Engram release checksum mismatch")
+ if extension == "zip":
+ with zipfile.ZipFile(io.BytesIO(data)) as files:
+ binary = files.read(name)
+ else:
+ with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as files:
+ member = files.getmember(name)
+ if not member.isfile():
+ raise SystemExit("Engram release does not contain a regular binary")
+ binary = files.extractfile(member).read()
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ partial = destination.with_name(name + ".part")
+ partial.write_bytes(binary)
+ partial.chmod(0o755)
+ partial.replace(destination)
+
+
+# A copy fetched earlier is reused: the pre-commit hook asks for it on every host commit.
+if not destination.exists():
+ fetch()
if "GITHUB_ENV" in os.environ:
with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8") as env:
env.write(f"CODYNC_TEST_ENGRAM={destination}\n")