Publish harness and TUI open-source

initial sync from the monorepo
This commit is contained in:
grokkybara[bot] 2026-07-16 06:46:02 +01:00
commit c68e39f604
2734 changed files with 1437016 additions and 0 deletions

View file

@ -0,0 +1,267 @@
//! Controllable raw HTTP/1.1 artifact server shared by the blitz
//! download/install tests and the concurrent-update convergence tests.
//!
//! Serves a real executable artifact and can truncate the body, close the
//! connection early, serve a right-length-but-garbage body, or hang
//! mid-transfer — for both the parallel byte-range path and the
//! single-connection path. It also counts body-serving GETs (HEAD probes are
//! excluded) so tests can assert how many downloads actually happened, and
//! supports a "slow" mode that widens the race window so concurrent
//! installers genuinely overlap in flight.
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
/// How the server corrupts (or doesn't) the next download.
#[derive(Clone, Copy, Debug)]
pub enum Mode {
/// Serve the real artifact correctly.
Full,
/// Serve a right-length body that exits non-zero (fails the smoke-test).
Garbage,
/// Advertise the full length but send only `k` bytes then close the socket
/// (silent truncation: premature EOF / short range chunk).
Truncate(usize),
/// Send `k` bytes then hang, so a client-side timeout cancels mid-transfer.
Hang(usize),
}
struct ServerState {
body: Arc<Vec<u8>>,
mode: Mode,
}
pub struct ArtifactServer {
addr: std::net::SocketAddr,
state: Arc<Mutex<ServerState>>,
shutdown: Arc<AtomicBool>,
gets: Arc<AtomicUsize>,
slow: Arc<AtomicBool>,
}
impl ArtifactServer {
pub fn start(body: Vec<u8>) -> Self {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
listener.set_nonblocking(true).unwrap();
let addr = listener.local_addr().unwrap();
let state = Arc::new(Mutex::new(ServerState {
body: Arc::new(body),
mode: Mode::Full,
}));
let shutdown = Arc::new(AtomicBool::new(false));
let gets = Arc::new(AtomicUsize::new(0));
let slow = Arc::new(AtomicBool::new(false));
let st = state.clone();
let sd = shutdown.clone();
let gc = gets.clone();
let sl = slow.clone();
std::thread::spawn(move || {
while !sd.load(Ordering::Relaxed) {
match listener.accept() {
Ok((stream, _)) => {
let st = st.clone();
let sd = sd.clone();
let gc = gc.clone();
let sl = sl.clone();
std::thread::spawn(move || handle_connection(stream, st, sd, gc, sl));
}
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
std::thread::sleep(Duration::from_millis(2));
}
Err(_) => break,
}
}
});
Self {
addr,
state,
shutdown,
gets,
slow,
}
}
pub fn uri(&self) -> String {
format!("http://{}", self.addr)
}
pub fn set_mode(&self, mode: Mode) {
self.state.lock().unwrap().mode = mode;
}
/// Number of body-serving GET requests handled so far (HEAD probes from
/// the parallel-download path are excluded). Tests use this to assert
/// how many downloads actually happened — e.g. that a sequential updater
/// converged onto an already-installed binary without re-downloading.
/// One download may span multiple GETs when the parallel byte-range path
/// splits it, so tests asserting exact counts use a small artifact
/// (single-connection path, 1 GET per download).
pub fn request_count(&self) -> usize {
self.gets.load(Ordering::Relaxed)
}
/// When enabled, hold each Full/Garbage response open ~500ms before
/// sending the body. This keeps an installer in flight long enough for
/// concurrent installers to genuinely overlap even on a heavily loaded
/// CI host — a too-short hold would let race tests run the installers
/// back-to-back and never exercise the concurrent window.
pub fn set_slow(&self, slow: bool) {
self.slow.store(slow, Ordering::Relaxed);
}
}
impl Drop for ArtifactServer {
fn drop(&mut self) {
self.shutdown.store(true, Ordering::Relaxed);
}
}
/// Parse `Range: bytes=a-b` from a raw request header block (case-insensitive).
fn parse_range(request: &str) -> Option<(usize, usize)> {
for line in request.lines() {
let lower = line.to_ascii_lowercase();
if let Some(rest) = lower.strip_prefix("range:") {
let spec = rest.trim().strip_prefix("bytes=")?;
let (a, b) = spec.split_once('-')?;
return Some((a.trim().parse().ok()?, b.trim().parse().ok()?));
}
}
None
}
fn handle_connection(
mut stream: TcpStream,
state: Arc<Mutex<ServerState>>,
shutdown: Arc<AtomicBool>,
gets: Arc<AtomicUsize>,
slow: Arc<AtomicBool>,
) {
// A stream accepted from a non-blocking listener can inherit non-blocking
// mode; force blocking so large `write_all`s don't short-write on WouldBlock.
let _ = stream.set_nonblocking(false);
// Avoid Nagle/delayed-ACK stalls on the header-then-body writes.
let _ = stream.set_nodelay(true);
// Read the request header block (until CRLFCRLF). Bodies are never sent by
// the client, so headers are all we need.
let mut buf = Vec::new();
let mut tmp = [0u8; 1024];
stream.set_read_timeout(Some(Duration::from_secs(5))).ok();
loop {
match stream.read(&mut tmp) {
Ok(0) => break,
Ok(n) => {
buf.extend_from_slice(&tmp[..n]);
if buf.windows(4).any(|w| w == b"\r\n\r\n") {
break;
}
if buf.len() > 64 * 1024 {
break;
}
}
Err(_) => return,
}
}
let request = String::from_utf8_lossy(&buf).to_string();
let is_head = request.starts_with("HEAD");
// Count only body-serving GETs; the parallel path's HEAD probe is excluded.
if !is_head {
gets.fetch_add(1, Ordering::Relaxed);
}
let range = parse_range(&request);
let (body, mode) = {
let st = state.lock().unwrap();
(st.body.clone(), st.mode)
};
let total = body.len();
let body: &[u8] = &body;
// Determine the byte slice this request is for, plus the length we will
// claim in Content-Length.
let (slice_start, slice_end_excl) = match range {
Some((a, b)) => (a.min(total), (b + 1).min(total)),
None => (0, total),
};
let claimed_len = slice_end_excl - slice_start;
// For truncation/hang, `k` is a GLOBAL cutoff across the whole artifact:
// a slice that reaches past byte `k` is sent short, so the parallel path's
// later chunk (or the single-connection body) is the one truncated.
let send_end = match mode {
Mode::Truncate(k) | Mode::Hang(k) => slice_end_excl.min(k).max(slice_start),
_ => slice_end_excl,
};
// `payload` is what we actually transmit before any early close; for the
// truncated modes it may be shorter than the advertised `claimed_len`.
let payload: Vec<u8> = match mode {
Mode::Garbage => {
let mut bad = b"#!/bin/sh\nexit 1\n".to_vec();
bad.resize(claimed_len, b'\n');
bad
}
_ => body[slice_start..send_end].to_vec(),
};
// Status line + headers. For range requests we answer 206; HEAD is 200.
let mut head = String::new();
if range.is_some() && !is_head {
head.push_str("HTTP/1.1 206 Partial Content\r\n");
head.push_str(&format!(
"Content-Range: bytes {}-{}/{}\r\n",
slice_start,
slice_end_excl.saturating_sub(1),
total
));
} else {
head.push_str("HTTP/1.1 200 OK\r\n");
head.push_str("Accept-Ranges: bytes\r\n");
}
// Always advertise the (claimed) full length so a truncated transfer is a
// genuine premature EOF rather than a short-but-consistent body.
head.push_str(&format!("Content-Length: {}\r\n", claimed_len));
head.push_str("Connection: close\r\n\r\n");
if stream.write_all(head.as_bytes()).is_err() {
return;
}
if is_head {
let _ = stream.flush();
return;
}
match mode {
Mode::Full | Mode::Garbage => {
// Hold the connection open longer so concurrent installers
// genuinely overlap mid-download (see `set_slow`).
if slow.load(Ordering::Relaxed) {
std::thread::sleep(Duration::from_millis(500));
}
let _ = stream.write_all(&payload);
}
Mode::Truncate(_) => {
// Send the (possibly short) payload then drop the connection without
// meeting Content-Length — the client sees a premature EOF.
let _ = stream.write_all(&payload);
}
Mode::Hang(_) => {
let _ = stream.write_all(&payload);
let _ = stream.flush();
// Hold the connection open longer than any client-side cancel
// timeout so the client times out and cancels (a genuine mid-flight
// cancel rather than a server-side close).
for _ in 0..30 {
if shutdown.load(Ordering::Relaxed) {
break;
}
std::thread::sleep(Duration::from_millis(20));
}
}
}
let _ = stream.flush();
}

View file

@ -0,0 +1,353 @@
//! Shared helpers for integration tests.
//!
//! Each `tests/*.rs` integration test is its own binary, so each binary has
//! its own `OnceLock<GROK_HOME>`. The helpers below ensure the per-binary
//! initialization is identical: same env-var set, same isolation guarantees,
//! same reset between tests.
//!
//! Mirrors the GROK_HOME isolation pattern used in other integration tests.
//!
//! ## Usage
//!
//! ```ignore
//! mod common;
//! use common::{test_home, reset_home};
//!
//! #[tokio::test]
//! #[serial_test::serial]
//! async fn my_test() {
//! let _ = test_home(); // initializes GROK_HOME once per binary
//! reset_home(); // wipes state between tests
//! // ...
//! }
//! ```
#![allow(dead_code)] // each test binary uses a different subset
#[cfg(unix)]
pub mod artifact_server;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
// ─────────────────────────────────────────────────────────────────────────────
// GROK_HOME isolation
// ─────────────────────────────────────────────────────────────────────────────
/// Returns a process-wide test `GROK_HOME`, initialized exactly once per test
/// binary. Once initialized, `xai_grok_config::grok_home()` will resolve to
/// this directory for the lifetime of the process.
///
/// Also clears env vars that the auto-update code consults so a parent shell's
/// values can't pollute the baseline (e.g. running tests from `npm run` would
/// otherwise inherit `npm_config_user_agent` and `NPM_TOKEN`).
pub fn test_home() -> &'static PathBuf {
static HOME: OnceLock<PathBuf> = OnceLock::new();
HOME.get_or_init(|| {
let dir = tempfile::TempDir::new().unwrap();
let path = dir.keep();
// SAFETY: called once at OnceLock init, before any other thread touches
// these env vars. Tests using this helper must be `#[serial]`.
unsafe {
std::env::set_var("GROK_HOME", &path);
std::env::remove_var("GROK_TEST_VERSION");
std::env::remove_var("NPM_TOKEN");
std::env::remove_var("GROK_INSTALLER");
std::env::remove_var("GROK_MANAGED_BY_NPM");
std::env::remove_var("GROK_MANAGED_BY_INTERNAL");
}
path
})
}
/// Wipe state in `GROK_HOME` between tests so each test sees a clean home.
/// Removes the well-known files and subdirectories the update path writes,
/// and clears env vars that individual tests may set.
pub fn reset_home() {
let home = test_home();
let _ = std::fs::remove_file(home.join("config.toml"));
let _ = std::fs::remove_file(home.join("version.json"));
let _ = std::fs::remove_file(home.join("version.json.tmp"));
let _ = std::fs::remove_dir_all(home.join("bin"));
let _ = std::fs::remove_dir_all(home.join("downloads"));
// SAFETY: tests using this helper must be `#[serial]`.
unsafe {
std::env::remove_var("GROK_TEST_VERSION");
std::env::remove_var("NPM_TOKEN");
std::env::remove_var("GROK_INSTALLER");
}
}
/// Override the version reported by `get_installed_grok_version()` for the
/// duration of the test (until [`reset_home`] or process exit).
pub fn set_test_version(v: &str) {
// SAFETY: tests using this helper must be `#[serial]`.
unsafe { std::env::set_var("GROK_TEST_VERSION", v) };
}
// ─────────────────────────────────────────────────────────────────────────────
// Install-test fixtures (shared by the blitz + convergence suites)
// ─────────────────────────────────────────────────────────────────────────────
/// Host `{os}-{arch}` string matching the versioned binary naming scheme
/// (`grok-{version}-{platform}`).
pub fn host_platform() -> String {
let os = if cfg!(target_os = "macos") {
"macos"
} else if cfg!(target_os = "linux") {
"linux"
} else {
panic!("unsupported test platform");
};
let arch = if cfg!(target_arch = "x86_64") {
"x86_64"
} else if cfg!(target_arch = "aarch64") {
"aarch64"
} else {
panic!("unsupported test arch");
};
format!("{os}-{arch}")
}
/// Minimal [`xai_grok_update::UpdateConfig`] for install tests.
pub fn make_update_config(channel: &str) -> xai_grok_update::UpdateConfig {
xai_grok_update::UpdateConfig {
proxy_base_url: "http://test.invalid/v1".to_string(),
auth_scope: "test".to_string(),
deployment_key: None,
alpha_test_key: None,
channel: channel.to_string(),
npm_registry: None,
}
}
/// True if shell-script artifacts can execute in this environment. False in
/// restricted sandboxes (e.g. hermetic remote execution) that lack /bin/sh.
#[cfg(unix)]
pub fn can_exec_shell_scripts() -> bool {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("probe");
std::fs::write(&p, b"#!/bin/sh\nexit 0\n").unwrap();
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
std::process::Command::new(&p)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
}
/// A small real executable: exits 0 for `--version`, so the smoke-test passes.
pub fn small_good_artifact() -> Vec<u8> {
b"#!/bin/sh\nexit 0\n".to_vec()
}
/// Backdate every file in `GROK_HOME/downloads` by ~2 hours.
///
/// `cleanup_old_downloads` deliberately never deletes a freshly-written
/// binary or temp file (it may belong to a concurrent in-flight install), so
/// tests asserting the retention policy must age their fixtures to look like
/// real leftovers from previous releases.
pub fn backdate_downloads() {
let downloads = test_home().join("downloads");
let Ok(entries) = std::fs::read_dir(&downloads) else {
return;
};
let old = std::time::SystemTime::now() - std::time::Duration::from_secs(2 * 60 * 60);
for entry in entries.flatten() {
let p = entry.path();
if p.is_file()
&& let Ok(f) = std::fs::File::options().write(true).open(&p)
{
let _ = f.set_times(std::fs::FileTimes::new().set_modified(old));
}
}
}
// ─────────────────────────────────────────────────────────────────────────────
// PATH-override fake binary
// ─────────────────────────────────────────────────────────────────────────────
/// RAII guard that places a sh-script with name `name` at the head of `PATH`.
/// Restores `PATH` on drop.
///
/// All tests using this MUST be `#[serial]` because `PATH` is process-global.
pub struct FakeBinGuard {
pub tmp: tempfile::TempDir,
pub name: String,
prev_path: OsString,
}
impl FakeBinGuard {
/// Install a fake binary at `<tmp>/<name>` whose body is produced by
/// `script_body(<tmp>)`, and prepend `<tmp>` to `PATH`.
pub fn install<F>(name: &str, script_body: F) -> Self
where
F: FnOnce(&Path) -> String,
{
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_path_buf();
let body = script_body(&dir);
let script_path = dir.join(name);
std::fs::write(&script_path, body).unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&script_path, std::fs::Permissions::from_mode(0o755)).unwrap();
}
let prev_path = std::env::var_os("PATH").unwrap_or_default();
let mut new_path = OsString::from(&dir);
new_path.push(":");
new_path.push(&prev_path);
// SAFETY: serial_test ensures no other thread races on PATH.
unsafe { std::env::set_var("PATH", &new_path) };
Self {
tmp,
name: name.to_string(),
prev_path,
}
}
/// Install a fake `npm` using the standard [`fake_npm_script`] template.
pub fn install_npm() -> Self {
Self::install("npm", fake_npm_script)
}
/// Install a fake `gh` using the standard [`fake_gh_script`] template.
pub fn install_gh() -> Self {
Self::install("gh", fake_gh_script)
}
/// The tempdir backing this guard (where canned stdout/stderr/exit files
/// can be written by tests, and where `<name>-args.log` is appended).
pub fn dir(&self) -> PathBuf {
self.tmp.path().to_path_buf()
}
/// Argv lines logged by the fake script — one line per invocation.
pub fn args_log(&self) -> Vec<String> {
std::fs::read_to_string(self.dir().join(format!("{}-args.log", self.name)))
.unwrap_or_default()
.lines()
.map(String::from)
.collect()
}
pub fn set_stdout(&self, content: &str) {
std::fs::write(self.dir().join(format!("{}-stdout", self.name)), content).unwrap();
}
pub fn set_stderr(&self, content: &str) {
std::fs::write(self.dir().join(format!("{}-stderr", self.name)), content).unwrap();
}
pub fn set_alpha_stdout(&self, content: &str) {
std::fs::write(
self.dir().join(format!("{}-alpha-stdout", self.name)),
content,
)
.unwrap();
}
pub fn set_stable_only_stdout(&self, content: &str) {
std::fs::write(
self.dir().join(format!("{}-stable-only-stdout", self.name)),
content,
)
.unwrap();
}
pub fn set_with_pre_stdout(&self, content: &str) {
std::fs::write(
self.dir().join(format!("{}-with-pre-stdout", self.name)),
content,
)
.unwrap();
}
pub fn set_exit_code(&self, code: i32) {
std::fs::write(
self.dir().join(format!("{}-exit", self.name)),
code.to_string(),
)
.unwrap();
}
}
impl Drop for FakeBinGuard {
fn drop(&mut self) {
// SAFETY: serial_test ensures no other thread races on PATH.
unsafe { std::env::set_var("PATH", &self.prev_path) };
}
}
/// Single-quote a path for safe substitution into a sh script.
fn single_quote_for_sh(p: &Path) -> String {
let s = p.to_string_lossy();
// Escape any embedded single quotes (paranoid — tempdir paths shouldn't
// contain them, but defensively quote).
let escaped = s.replace('\'', "'\\''");
format!("'{escaped}'")
}
/// sh script body for a fake `npm`. Logs argv to `<dir>/npm-args.log` and
/// dispatches stdout based on the first matching argv pattern:
///
/// - argv contains `@alpha` → cat `<dir>/npm-alpha-stdout`
/// - else → cat `<dir>/npm-stdout`
///
/// Always cats `<dir>/npm-stderr` to stderr (if exists). Exits with the integer
/// in `<dir>/npm-exit` (default 0).
pub fn fake_npm_script(dir: &Path) -> String {
let dq = single_quote_for_sh(dir);
format!(
r#"#!/bin/sh
echo "$@" >> {dq}/npm-args.log
if echo "$@" | grep -q '@alpha'; then
if [ -f {dq}/npm-alpha-stdout ]; then cat {dq}/npm-alpha-stdout; fi
elif [ -f {dq}/npm-stdout ]; then
cat {dq}/npm-stdout
fi
if [ -f {dq}/npm-stderr ]; then cat {dq}/npm-stderr >&2; fi
exit_code=0
if [ -f {dq}/npm-exit ]; then exit_code=$(cat {dq}/npm-exit); fi
exit "$exit_code"
"#
)
}
/// sh script body for a fake `gh`. Logs argv to `<dir>/gh-args.log` and
/// dispatches stdout based on `release list` argv:
///
/// - argv contains `release list --exclude-pre-releases` → `<dir>/gh-stable-only-stdout`
/// - argv contains `release list` (no exclude flag) → `<dir>/gh-with-pre-stdout`
/// - else → `<dir>/gh-stdout`
///
/// Exits with `<dir>/gh-exit` (default 0).
pub fn fake_gh_script(dir: &Path) -> String {
let dq = single_quote_for_sh(dir);
format!(
r#"#!/bin/sh
echo "$@" >> {dq}/gh-args.log
if echo "$@" | grep -q 'release list'; then
if echo "$@" | grep -q '\-\-exclude-pre-releases'; then
if [ -f {dq}/gh-stable-only-stdout ]; then cat {dq}/gh-stable-only-stdout; fi
else
if [ -f {dq}/gh-with-pre-stdout ]; then cat {dq}/gh-with-pre-stdout; fi
fi
elif [ -f {dq}/gh-stdout ]; then
cat {dq}/gh-stdout
fi
exit_code=0
if [ -f {dq}/gh-exit ]; then exit_code=$(cat {dq}/gh-exit); fi
exit "$exit_code"
"#
)
}