host: review fixes for cargo.build, the test runner, the console, and main; tests for every PROTOCOL.md rule

cargo.build and the runner (review T22R): builds use the network again;
core.unlock and core.notify in order under the runner; quiet test
output; manifest preparation keeps [workspace], target tables, renamed
and optional entries; cargo.rpc.discover; -32004 with data.output;
retention of 10 builds; light, empty test worlds with an in-memory
journal; only CARGO_/RUST_/SAUSAGE_ variables; stdin is null; the runner
through --config. A compile cache that cannot be created is not fatal.
Activation replaces plugin.wasm without a gap.

Console and main: init ships the workspace Cargo.lock and creates the
world atomically; each console command runs in its own task, so ps,
stop, and kill work during a slow call; SAUSAGE_HEADLESS; the terminal
mode is restored; signals during boot; chat text is not taken as a
command.

Tests: 20 more tests for PROTOCOL.md sections 2 to 10; the cargo tests
choose the libtest binary from cargo's JSON output and time out.
This commit is contained in:
2026-10-23 07:47:39 +01:00
parent 61d5d5f817
commit a090e1fd57
24 changed files with 1889 additions and 242 deletions
+182 -2
View File
@@ -313,6 +313,24 @@ async fn review_retirement_checks_live_version() {
marker(world.0.join("state/notes/retired"), &previous).await;
host.stop().await;
}
/// T23: an unwritable compile cache does not prevent a world from running.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn unwritable_compile_cache_is_nonfatal() {
use std::os::unix::fs::PermissionsExt;
let world = TestWorld::new();
world.plugin("notes", document("add"), document("add"));
let parent = world.0.join("unwritable");
std::fs::create_dir(&parent).unwrap();
std::fs::set_permissions(&parent, std::fs::Permissions::from_mode(0o500)).unwrap();
let result = Host::start_with_cache(&world.0, &parent.join("cache")).await;
std::fs::set_permissions(&parent, std::fs::Permissions::from_mode(0o700)).unwrap();
let host = result.unwrap();
assert_eq!(
call(&host, "notes.version", json!({})).await["version"],
"1"
);
host.stop().await;
}
/// L13: source subtrees require no watches, even when the host cannot read them.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn review_watcher_is_shallow() {
@@ -322,7 +340,7 @@ async fn review_watcher_is_shallow() {
let source = world.0.join("plugins/notes/source");
std::fs::create_dir_all(&source).unwrap();
std::fs::set_permissions(&source, std::fs::Permissions::from_mode(0o0)).unwrap();
let result = Host::start(&world.0).await;
let result = Host::start_with_cache(&world.0, &world.0.join("compile-cache")).await;
std::fs::set_permissions(&source, std::fs::Permissions::from_mode(0o700)).unwrap();
let host = result.unwrap();
activate(&host, "notes").await.unwrap();
@@ -337,7 +355,7 @@ async fn review_watcher_failure_is_nonfatal() {
let inaccessible = world.0.join("plugins/inaccessible");
std::fs::create_dir_all(&inaccessible).unwrap();
std::fs::set_permissions(&inaccessible, std::fs::Permissions::from_mode(0o0)).unwrap();
let result = Host::start(&world.0).await;
let result = Host::start_with_cache(&world.0, &world.0.join("compile-cache")).await;
std::fs::set_permissions(&inaccessible, std::fs::Permissions::from_mode(0o700)).unwrap();
let host = result.unwrap();
assert!(
@@ -785,3 +803,165 @@ async fn does_not_restart_detached_call() {
);
host.stop().await;
}
/// PROTOCOL 7: activation records the plugin's upgrade grace period in the journal.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn upgrades_enter_the_journal() {
let world = TestWorld::new();
let mut old = document("hold");
old.as_object_mut().unwrap().remove("x-sausage-grace");
world.plugin("notes", old, document("hold"));
let host = world.start().await;
call(&host, "notes.hold", json!({})).await;
activate(&host, "notes").await.unwrap();
let entries = tokio::time::timeout(Duration::from_secs(10), async {
loop {
let entries = call(
&host,
"core.log",
json!({"plugin":"notes","grep":"upgrade grace"}),
)
.await;
if entries
.as_array()
.unwrap()
.iter()
.any(|e| e["line"] == "upgrade grace 60 seconds")
{
break entries;
}
tokio::task::yield_now().await;
}
})
.await
.expect("the upgrade journal entry must arrive within 10 seconds");
assert!(
entries
.as_array()
.unwrap()
.iter()
.all(|e| e["stream"] == "core")
);
host.stop().await;
}
/// PROTOCOL 10.2 and 10.3: breaking endpoint methods reject activation with counterexamples where possible.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn endpoint_contracts_and_activation_counterexamples() {
let method = json!({"name":"stats","params":[{"name":"value","required":true,"schema":{"type":"integer","minimum":0}}],"result":{"schema":{"type":"string"}}});
let expected = json!({"methods":[],"x-sausage-endpoints":{"control":{"methods":[method]}}});
for case in ["missing", "params", "result", "unsupported"] {
let world = TestWorld::new();
let mut next = expected.clone();
match case {
"missing" => next["x-sausage-endpoints"]["control"]["methods"] = json!([]),
"params" => {
next["x-sausage-endpoints"]["control"]["methods"][0]["params"][0]["schema"]["minimum"] =
json!(1)
}
"result" => {
next["x-sausage-endpoints"]["control"]["methods"][0]["result"]["schema"] =
json!({"type":"integer"})
}
_ => {
next["x-sausage-endpoints"]["control"]["methods"][0]["result"]["schema"]["pattern"] =
json!("[a-z]+")
}
}
world.plugin("notes", expected.clone(), next);
world.plugin(
"agent",
consumer("notes", expected.clone()),
document("consume"),
);
let host = world.start().await;
let error = activate(&host, "notes").await.unwrap_err();
assert_eq!(error["code"], -32003, "{case}: {error}");
assert!(!error["data"].as_array().unwrap().is_empty());
if ["params", "result"].contains(&case) {
assert!(
error["data"]
.as_array()
.unwrap()
.iter()
.any(|p| !p["counterexample"].is_null()),
"{error}"
);
}
if case == "unsupported" {
assert!(error["data"].to_string().contains("unsupported"), "{error}");
}
assert_eq!(
std::fs::read(world.0.join("plugins/notes/plugin.wasm")).unwrap(),
std::fs::read(fixture(1)).unwrap()
);
host.stop().await;
}
}
/// PROTOCOL 10.2: providers can add methods, optional params, and result fields without breaking consumers.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn additive_contract_changes_activate() {
let old = json!({"methods":[{"name":"read","params":[],"result":{"schema":{"type":"object","properties":{"text":{"type":"string"}},"required":["text"]}}}]});
let mut next = old.clone();
next["methods"][0]["params"] =
json!([{"name":"limit","required":false,"schema":{"type":"integer"}}]);
next["methods"][0]["result"]["schema"]["properties"]["extra"] = json!({"type":"boolean"});
next["methods"]
.as_array_mut()
.unwrap()
.push(json!({"name":"new","params":[],"result":{"schema":{"type":"null"}}}));
let world = TestWorld::new();
world.plugin("notes", old.clone(), next);
world.plugin("agent", consumer("notes", old), document("consume"));
let host = world.start().await;
activate(&host, "notes").await.unwrap();
host.stop().await;
}
/// PROTOCOL 3.7: an old consumer can still send its old import during grace, even after the provider removes it.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn old_consumer_can_call_removed_provider_method() {
let world = TestWorld::new();
world.plugin("notes", document("legacy"), document("new"));
let mut old = consumer("notes", document("legacy"));
old["x-sausage-grace"] = json!(60);
world.plugin("agent", old, document("consume"));
let host = world.start().await;
let pid = call(&host, "agent.deferred", json!({})).await;
activate(&host, "agent").await.unwrap();
activate(&host, "notes").await.unwrap();
assert_eq!(host.processes()[pid.as_str().unwrap()].state, "running");
std::fs::write(world.0.join("state/agent/release-call"), "release").unwrap();
let response: Value = tokio::time::timeout(Duration::from_secs(10), async {
loop {
if let Ok(bytes) = std::fs::read(world.0.join("state/agent/call-result"))
&& let Ok(value) = serde_json::from_slice(&bytes)
{
break value;
}
tokio::task::yield_now().await;
}
})
.await
.unwrap();
assert_eq!(response["error"]["code"], -32601);
assert_eq!(response["error"]["data"]["version"], "2");
host.stop().await;
}
/// PROTOCOL 10.2: activation and revert check interfaces but leave persistent state unchanged.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn state_is_outside_interface_checks() {
let world = TestWorld::new();
world.plugin("notes", document("add"), document("add"));
let state = world.0.join("state/notes/user-state");
let contents = b"a state format neither version understands\0";
std::fs::write(&state, contents).unwrap();
let host = world.start().await;
activate(&host, "notes").await.unwrap();
assert_eq!(std::fs::read(&state).unwrap(), contents);
call(&host, "core.revert", json!({"plugin":"notes"})).await;
assert_eq!(std::fs::read(state).unwrap(), contents);
host.stop().await;
}
+315 -20
View File
@@ -46,6 +46,11 @@ fn fixture(name: &str) -> PathBuf {
/// Builds through the real registered native plugin.
async fn build(world: &TestWorld, name: &str) -> Result<Value, Value> {
let host = Host::start(&world.0).await.unwrap();
let runner = world.0.join("state/runner with spaces");
std::os::unix::fs::symlink(env!("CARGO_BIN_EXE_sausage"), &runner).unwrap();
sausage::cargo::Cargo::with_runner(World::open(&world.0).unwrap(), runner)
.unwrap()
.register(&host);
let result = host.call("cargo.build", json!({"plugin":name})).await;
host.stop().await;
result
@@ -68,22 +73,62 @@ async fn valid() -> &'static Valid {
"exclude",
)
.unwrap();
let nested = world.0.join("plugins/valid/source/src/target");
std::fs::create_dir_all(&nested).unwrap();
std::fs::write(nested.join("data"), "nested target").unwrap();
let git = world.0.join("plugins/valid/source/src/.git");
std::fs::create_dir_all(&git).unwrap();
std::fs::write(git.join("data"), "nested git").unwrap();
for number in 1..=20 {
std::fs::create_dir_all(world.0.join(format!("state/cargo/builds/{number}")))
.unwrap();
}
std::fs::create_dir_all(world.0.join("state/cargo/tmp/stale")).unwrap();
std::fs::write(world.0.join("state/cargo/tmp/stale/file"), "stale").unwrap();
let result = build(&world, "valid")
.await
.unwrap_or_else(|error| panic!("build failed: {error}"));
let test =
std::fs::read_dir(world.0.join("state/cargo/target/wasm32-wasip2/debug/deps"))
.unwrap()
.filter_map(|entry| {
let path = entry.ok()?.path();
(path
.extension()
.is_some_and(|extension| extension == "wasm")
&& path.file_name()?.to_str()?.starts_with("cargo_valid-"))
.then_some(path)
})
.next()
.unwrap();
// Integration tests also build a normal binary. Cargo identifies the libtest artifact.
let artifacts = tokio::process::Command::new("cargo")
.args([
"test",
"--no-run",
"--message-format=json",
"--bin",
"cargo-valid",
"--target",
"wasm32-wasip2",
"--target-dir",
])
.arg(world.0.join("state/cargo/target"))
.current_dir(world.0.join("state/cargo/work"))
.stdin(std::process::Stdio::null())
.output()
.await
.unwrap();
assert!(
artifacts.status.success(),
"{}",
String::from_utf8_lossy(&artifacts.stderr)
);
let mut tests: Vec<PathBuf> = String::from_utf8_lossy(&artifacts.stdout)
.lines()
.filter_map(|line| serde_json::from_str::<Value>(line).ok())
.filter(|message| {
message["reason"] == "compiler-artifact"
&& message["profile"]["test"] == true
&& message["target"]["kind"]
.as_array()
.is_some_and(|kinds| kinds.iter().any(|kind| kind == "bin"))
})
.filter_map(|message| message["executable"].as_str().map(PathBuf::from))
.collect();
assert_eq!(
tests.len(),
1,
"expected exactly one libtest binary: {tests:?}"
);
let test = tests.pop().unwrap();
Valid {
world,
result,
@@ -94,16 +139,215 @@ async fn valid() -> &'static Valid {
}
/// Invokes the same runner subcommand that cargo uses.
async fn runner(valid: &Valid, args: &[&str]) -> std::process::Output {
tokio::process::Command::new(env!("CARGO_BIN_EXE_sausage"))
let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_sausage"));
command
.arg("test-runner")
.arg(&valid.test)
.args(args)
.current_dir(valid.world.0.join("state/cargo/work"))
.output()
.kill_on_drop(true);
tokio::time::timeout(std::time::Duration::from_secs(120), command.output())
.await
.expect("test runner exceeded 120 seconds")
.unwrap()
}
/// T23: unlocks stay ordered before the next lock over 300 acquisitions.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn unlock_order() {
assert!(
runner(valid().await, &["unlock_order"])
.await
.status
.success()
);
}
/// T23: each runner world is empty and has independent proc, self, and run mounts.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn empty_and_isolated_worlds() {
for name in ["empty_world", "isolation_", "environment", "late_response"] {
let output = runner(valid().await, &[name]).await;
assert!(
output.status.success(),
"{name}: {} {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
}
/// T23: successful builds preserve warnings and integration output without cargo progress noise.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn build_output_and_warnings() {
let valid = valid().await;
assert!(
valid.result["warnings"]
.as_str()
.unwrap()
.contains("fixture build warning")
);
let output = valid.result["tests"]["output"].as_str().unwrap();
assert!(output.contains("INTEGRATION_CASE"));
assert!(!output.contains("Compiling "));
assert!(!output.contains(" running 1 test"));
assert!(
!output
.lines()
.any(|line| line.starts_with(" ") && line.trim().is_empty())
);
}
/// T23: publication retains ten builds and the next build removes abandoned test worlds.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn build_retention_and_temporary_cleanup() {
let valid = valid().await;
assert_eq!(
std::fs::read_dir(valid.world.0.join("state/cargo/builds"))
.unwrap()
.count(),
10
);
assert!(!valid.world.0.join("state/cargo/tmp/stale").exists());
assert_eq!(
std::fs::read_dir(valid.world.0.join("state/cargo/tmp"))
.unwrap()
.count(),
0
);
}
/// T23: manifest preparation preserves workspaces, renamed target dependencies, and optional flags.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn manifest_and_nested_sources() {
let valid = valid().await;
let root = valid.world.0.join("state/cargo/work");
let manifest = std::fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(manifest.contains("workspace.package"));
assert!(manifest.contains("optional = true"));
assert!(manifest.contains("/plugins/cargo/sdk"));
assert_eq!(
std::fs::read_to_string(root.join("src/target/data")).unwrap(),
"nested target"
);
assert_eq!(
std::fs::read_to_string(root.join("src/.git/data")).unwrap(),
"nested git"
);
}
/// T23: discovery describes the native build method and preparation errors name their paths.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn native_discovery_and_error_paths() {
let world = TestWorld::new();
let host = Host::start(&world.0).await.unwrap();
assert_eq!(
host.call("cargo.rpc.discover", json!({})).await.unwrap()["methods"][0]["name"],
"build"
);
let error = host
.call("cargo.build", json!({"plugin":"missing"}))
.await
.unwrap_err();
assert_eq!(error["code"], -32004);
assert!(
error["data"]["output"]
.as_str()
.unwrap()
.contains("plugins/missing/source")
);
host.stop().await;
}
/// T23: release-only failures use data.output and retain successful WASI totals.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn release_only_failure() {
let world = TestWorld::new();
let root = world.0.join("plugins/release/source/src");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.parent().unwrap().join("Cargo.toml"),
"[package]\nname='release-only'\nversion='0.1.0'\nedition='2024'\n",
)
.unwrap();
std::fs::write(root.join("main.rs"), "#[cfg(not(debug_assertions))] compile_error!(\"release-only error\"); fn main() {} #[test] fn passes() {}\n").unwrap();
let error = build(&world, "release").await.unwrap_err();
assert!(
error["data"]["output"]
.as_str()
.unwrap()
.contains("release-only error")
);
assert_eq!(error["data"]["tests"]["passed"], 1);
}
/// T23: cancellation retains serialization until a blocking source copy completes.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn cancelled_preparation_is_serialized() {
let world = TestWorld::new();
let first = world.0.join("first");
let second = world.0.join("second");
for (root, name) in [(&first, "first"), (&second, "second")] {
std::fs::create_dir_all(root.join("src")).unwrap();
std::fs::write(
root.join("Cargo.toml"),
format!("[package]\nname='{name}'\nversion='0.1.0'\nedition='2024'\n"),
)
.unwrap();
std::fs::write(root.join("src/main.rs"), "fn main() {}\n").unwrap();
}
for number in 0..10000 {
std::fs::write(first.join(format!("old-{number}")), "copy").unwrap();
}
let builder = std::sync::Arc::new(
sausage::cargo::Cargo::with_runner(
World::open(&world.0).unwrap(),
PathBuf::from(env!("CARGO_BIN_EXE_sausage")),
)
.unwrap(),
);
let copying = builder.clone();
let task = tokio::spawn(async move { copying.build(&first).await });
tokio::time::timeout(std::time::Duration::from_secs(30), async {
loop {
if std::fs::read_dir(world.0.join("state/cargo/work"))
.is_ok_and(|entries| entries.count() > 10)
{
break;
}
tokio::task::yield_now().await;
}
})
.await
.unwrap();
task.abort();
let _ = task.await;
builder.build(&second).await.unwrap();
assert!(
!std::fs::read_dir(world.0.join("state/cargo/work"))
.unwrap()
.any(|entry| entry
.unwrap()
.file_name()
.to_string_lossy()
.starts_with("old-"))
);
}
/// T23: multiple binary artifacts fail clearly instead of publishing an arbitrary binary.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn multiple_binaries() {
let world = TestWorld::new();
let root = world.0.join("plugins/multi/source/src/bin");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.parent().unwrap().parent().unwrap().join("Cargo.toml"),
"[package]\nname='multiple'\nversion='0.1.0'\nedition='2024'\n",
)
.unwrap();
for name in ["one", "two"] {
std::fs::write(root.join(format!("{name}.rs")), "fn main() {}\n").unwrap();
}
let error = build(&world, "multi").await.unwrap_err();
assert!(
error["data"]["output"]
.as_str()
.unwrap()
.contains("exactly one binary")
);
}
/// PROTOCOL 11: a valid SDK plugin builds and publishes a release component without activation.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn valid_build() {
@@ -111,7 +355,7 @@ async fn valid_build() {
let wasm = valid.result["wasm"].as_str().unwrap();
assert!(wasm.starts_with("/state/cargo/builds/"));
assert!(valid.world.0.join(wasm.trim_start_matches('/')).is_file());
assert_eq!(valid.result["tests"]["passed"], 5);
assert_eq!(valid.result["tests"]["passed"], 12);
assert_eq!(valid.result["tests"]["failed"], 0);
assert!(!valid.world.0.join("plugins/valid/plugin.wasm").exists());
}
@@ -305,6 +549,10 @@ async fn standalone_builder() {
/// PROTOCOL 11: killing a native build also kills its build-script descendants.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn kill_process_group() {
assert!(
Path::new("/proc/self/stat").is_file(),
"process-group checks require /proc"
);
let world = TestWorld::new();
world.source("hang", &fixture("cargo-hang"));
let host = Host::start(&world.0).await.unwrap();
@@ -323,21 +571,68 @@ async fn kill_process_group() {
})
.await
.unwrap();
let mut ancestor = child;
loop {
let command = std::fs::read(format!("/proc/{ancestor}/cmdline")).unwrap();
let arguments: Vec<_> = command
.split(|byte| *byte == 0)
.map(|part| String::from_utf8_lossy(part).to_string())
.collect();
if arguments[0].ends_with("/cargo") || arguments[0] == "cargo" {
assert!(!arguments.iter().any(|arg| arg == "--offline"));
assert!(
arguments
.iter()
.any(|arg| arg.starts_with("target.wasm32-wasip2.runner=["))
);
assert_eq!(
std::fs::read_link(format!("/proc/{ancestor}/fd/0")).unwrap(),
PathBuf::from("/dev/null")
);
break;
}
let status = std::fs::read_to_string(format!("/proc/{ancestor}/status")).unwrap();
ancestor = status
.lines()
.find_map(|line| line.strip_prefix("PPid:"))
.unwrap()
.trim()
.parse()
.unwrap();
assert!(ancestor > 1, "cargo ancestor not found");
}
let pid = host
.processes()
.into_iter()
.find(|(_, process)| process.plugin == "cargo")
.unwrap()
.0;
let fs = host.filesystem().await.unwrap();
use sausage_vfs::Layer;
assert!(
fs.stat(&format!("/proc/{pid}/info.json"), true)
.await
.is_ok()
);
assert!(
!host
.call("core.log", json!({"pid":pid,"grep":"start build"}))
.await
.unwrap()
.as_array()
.unwrap()
.is_empty()
);
host.call("core.kill", json!({"pid":pid})).await.unwrap();
assert!(task.await.unwrap().is_err());
tokio::time::timeout(std::time::Duration::from_secs(10), async {
loop {
let status = std::fs::read_to_string(format!("/proc/{child}/stat"));
if status
.as_ref()
.map_or(true, |status| status.split_whitespace().nth(2) == Some("Z"))
{
if match status {
Ok(status) => status.split_whitespace().nth(2) == Some("Z"),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => true,
Err(error) => panic!("read /proc/{child}/stat: {error}"),
} {
break;
}
tokio::task::yield_now().await;
+303 -21
View File
@@ -1,39 +1,120 @@
//! Initialize the embedded agent and start the resulting world through the CLI.
use sausage::Host;
//! Binary contracts for atomic init, environment flags, signals, and the piped console.
use sausage::{Host, world::World};
use serde_json::{Value, json};
use std::process::{Command, Stdio};
use std::{
path::{Path, PathBuf},
process::Stdio,
sync::OnceLock,
time::Duration,
};
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
process::{Child, Command},
};
/// Environment configuration creates a complete world with a callable agent.
fn binary() -> Command {
let mut command = Command::new(env!("CARGO_BIN_EXE_sausage"));
command.env_remove("SAUSAGE_HEADLESS").kill_on_drop(true);
command
}
fn world() -> (tempfile::TempDir, PathBuf) {
let folder = tempfile::tempdir().unwrap();
let world = World::create(&folder.path().join("world")).unwrap();
(folder, world.root)
}
async fn status(child: &mut Child) -> std::process::ExitStatus {
tokio::time::timeout(Duration::from_secs(15), child.wait())
.await
.expect("CLI did not exit")
.unwrap()
}
async fn journal_exists(world: &Path) {
tokio::time::timeout(Duration::from_secs(15), async {
while !world.join("state/core/journal.sqlite").exists() {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
}
fn signal(child: &Child, signal: nix::sys::signal::Signal) {
nix::sys::signal::kill(
nix::unistd::Pid::from_raw(child.id().unwrap() as i32),
signal,
)
.unwrap();
}
fn probe() -> PathBuf {
static WASM: OnceLock<PathBuf> = OnceLock::new();
WASM.get_or_init(|| {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
let target = root.join("target/core/console-guests");
assert!(
std::process::Command::new("cargo")
.args([
"build",
"--offline",
"--target",
"wasm32-wasip2",
"--manifest-path"
])
.arg(root.join("cli/tests/plugins/probe/Cargo.toml"))
.arg("--target-dir")
.arg(&target)
.status()
.unwrap()
.success()
);
target.join("wasm32-wasip2/debug/probe.wasm")
})
.clone()
}
fn install_probe(world: &Path) {
std::fs::create_dir_all(world.join("plugins/probe")).unwrap();
std::fs::copy(probe(), world.join("plugins/probe/plugin.wasm")).unwrap();
}
/// A failed agent build leaves no world. Bare sausage retries and publishes a complete world.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn init_builds_agent() {
let temporary = std::env::temp_dir().join(format!("sausage-cli-{}", uuid::Uuid::now_v7()));
std::fs::create_dir_all(&temporary).unwrap();
let world = temporary.join("world");
let output = Command::new(env!("CARGO_BIN_EXE_sausage"))
let temporary = tempfile::tempdir().unwrap();
let world = temporary.path().join("world");
let configured = || {
let mut command = binary();
command
.current_dir(temporary.path())
.env("SAUSAGE_API", "anthropic-messages")
.env("SAUSAGE_BASE_URL", "http://127.0.0.1:1/")
.env("SAUSAGE_MODEL", "init-test")
.env("SAUSAGE_API_TOKEN", "fake-token")
.stdin(Stdio::null());
command
};
let failed = configured()
.arg("init")
.arg(&world)
.env("SAUSAGE_API", "anthropic-messages")
.env("SAUSAGE_BASE_URL", "http://127.0.0.1:1/")
.env("SAUSAGE_MODEL", "init-test")
.env("SAUSAGE_API_TOKEN", "fake-token")
.stdin(Stdio::null())
.env("RUSTC", "/nonexistent-sausage-rustc")
.output()
.await
.unwrap();
assert!(!failed.status.success());
assert!(!world.exists());
let output = configured().output().await.unwrap();
assert!(
output.status.success(),
"stdout: {}\nstderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8(output.stdout).unwrap();
assert!(stdout.contains("first agent build takes a few minutes"));
assert!(stdout.contains("sausage run"));
assert!(
String::from_utf8_lossy(&output.stdout).contains("first agent build takes a few minutes")
);
let config: Value =
serde_json::from_slice(&std::fs::read(world.join("config/agent.json")).unwrap()).unwrap();
assert_eq!(
config,
json!({"api":"anthropic-messages","base_url":"http://127.0.0.1:1","model":"init-test","token":"fake-token"})
);
assert!(String::from_utf8_lossy(&output.stdout).contains("sausage run"));
assert_eq!(
serde_json::from_slice::<Value>(&std::fs::read(world.join("config/boot.json")).unwrap())
.unwrap(),
@@ -48,9 +129,16 @@ async fn init_builds_agent() {
] {
assert!(world.join(file).is_file(), "missing {file}");
}
let cache =
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../target/sdk/cli-cache");
let host = Host::start_with_cache(&world, &cache).await.unwrap();
assert_eq!(
std::fs::read(world.join("plugins/agent/source/Cargo.lock")).unwrap(),
include_bytes!("../../Cargo.lock")
);
let host = Host::start_with_cache(
&world,
&Path::new(env!("CARGO_MANIFEST_DIR")).join("../target/core/cli-cache"),
)
.await
.unwrap();
let doc = host.call("agent.rpc.discover", json!({})).await.unwrap();
assert_eq!(doc["x-sausage-grace"], 600);
assert!(
@@ -61,6 +149,200 @@ async fn init_builds_agent() {
.any(|method| method["name"] == "chat")
);
host.stop().await;
}
std::fs::remove_dir_all(temporary).unwrap();
/// Both headless forms ignore EOF. False and empty values preserve the plain console.
#[tokio::test]
async fn headless_flag_and_environment() {
for value in [None, Some("1"), Some("true")] {
let (_folder, world) = world();
let mut command = binary();
command.arg("run").arg(&world).stdin(Stdio::null());
if let Some(value) = value {
command.env("SAUSAGE_HEADLESS", value);
} else {
command.arg("--headless");
}
let mut child = command.spawn().unwrap();
journal_exists(&world).await;
assert!(
tokio::time::timeout(Duration::from_millis(300), child.wait())
.await
.is_err()
);
signal(&child, nix::sys::signal::Signal::SIGTERM);
assert_eq!(status(&mut child).await.code(), Some(0));
}
for value in ["0", ""] {
let (_folder, world) = world();
let mut child = binary()
.arg("run")
.arg(world)
.env("SAUSAGE_HEADLESS", value)
.stdin(Stdio::null())
.spawn()
.unwrap();
assert_eq!(status(&mut child).await.code(), Some(0));
}
}
/// Signals interrupt an unanswered boot service instead of waiting forever.
#[tokio::test]
async fn signals_work_during_boot() {
for sig in [
nix::sys::signal::Signal::SIGTERM,
nix::sys::signal::Signal::SIGINT,
] {
let (_folder, world) = world();
std::fs::write(
world.join("config/boot.json"),
json!([
{"method":"core.lock","params":{"path":"boot"}},
{"method":"core.lock","params":{"path":"boot"}}
])
.to_string(),
)
.unwrap();
let mut child = binary()
.arg("run")
.arg(&world)
.arg("--headless")
.stdin(Stdio::null())
.spawn()
.unwrap();
journal_exists(&world).await;
tokio::time::timeout(Duration::from_secs(15), async {
loop {
let db =
rusqlite::Connection::open(world.join("state/core/journal.sqlite")).unwrap();
if let Ok(2) = db.query_row(
"SELECT count(*) FROM journal WHERE line='service start core.lock'",
[],
|row| row.get::<_, i64>(0),
) {
break;
}
tokio::task::yield_now().await;
}
})
.await
.unwrap();
signal(&child, sig);
assert_eq!(status(&mut child).await.code(), Some(0));
}
}
/// Piped input supports commands, quit, and the restart exit code.
#[tokio::test]
async fn piped_console_and_exit_codes() {
for (line, expected) in [("help\n", 0), ("quit\n", 0), ("core.exit\n", 75)] {
let (_folder, world) = world();
let mut child = binary()
.arg("run")
.arg(world)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
child
.stdin
.take()
.unwrap()
.write_all(line.as_bytes())
.await
.unwrap();
let output = tokio::time::timeout(Duration::from_secs(15), child.wait_with_output())
.await
.unwrap()
.unwrap();
assert_eq!(output.status.code(), Some(expected));
if expected == 0 {
assert!(!output.stdout.is_empty());
}
}
}
/// A console input error still stops live actors and flushes the journal.
#[tokio::test]
async fn console_error_stops_host() {
let (_folder, world) = world();
install_probe(&world);
std::fs::write(
world.join("config/boot.json"),
json!([{"method":"probe.serve","params":{}}]).to_string(),
)
.unwrap();
let mut child = binary()
.arg("run")
.arg(&world)
.stdin(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
child
.stdin
.take()
.unwrap()
.write_all(&[255, 10])
.await
.unwrap();
assert_eq!(status(&mut child).await.code(), Some(1));
let db = rusqlite::Connection::open(world.join("state/core/journal.sqlite")).unwrap();
let stopped: i64 = db
.query_row(
"SELECT count(*) FROM journal WHERE plugin='probe' AND line='stop'",
[],
|row| row.get(0),
)
.unwrap();
assert!(stopped > 0);
}
/// Ps and kill remain available before a slow command returns.
#[tokio::test]
async fn piped_console_accepts_commands_during_a_call() {
let (_folder, world) = world();
install_probe(&world);
let mut child = binary()
.arg("run")
.arg(world)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
let mut input = child.stdin.take().unwrap();
let mut output = BufReader::new(child.stdout.take().unwrap()).lines();
input.write_all(b"probe.busy\n").await.unwrap();
let mut pid = None;
tokio::time::timeout(Duration::from_secs(15), async {
while pid.is_none() {
input.write_all(b"ps\n").await.unwrap();
while let Some(line) = output.next_line().await.unwrap() {
if line.contains("probe busy console") {
pid = line.split_whitespace().next().map(str::to_owned);
break;
}
if line.contains("PID PLUGIN METHOD") {
break;
}
}
}
})
.await
.unwrap();
input
.write_all(format!("kill {}\n", pid.unwrap()).as_bytes())
.await
.unwrap();
tokio::time::timeout(Duration::from_secs(15), async {
while let Some(line) = output.next_line().await.unwrap() {
if line.starts_with("> kill ") {
break;
}
}
})
.await
.unwrap();
input.write_all(b"quit\n").await.unwrap();
assert_eq!(status(&mut child).await.code(), Some(0));
}
+29 -4
View File
@@ -14,7 +14,7 @@ fn fixture() -> PathBuf {
.parent()
.unwrap()
.to_owned();
let target = root.join("target/vfs/console-guests");
let target = root.join("target/core/console-guests");
let status = std::process::Command::new("cargo")
.args([
"build",
@@ -54,7 +54,7 @@ impl TestWorld {
let cache = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("target/vfs/console-cache");
.join("target/core/console-cache");
Host::start_with_cache(&self.0, &cache).await.unwrap()
}
}
@@ -78,7 +78,10 @@ async fn rpc_call() {
let result = command(&host, "probe.echo {\"answer\":42}").await;
assert!(result.contains("\n \"answer\": 42\n"));
assert!(command(&host, "absent.echo {}").await.contains("error:"));
assert!(execute_line(&host, "probe.echo not-json").await.is_err());
assert_eq!(
command(&host, "probe.echo not-json").await,
"probe.echo not-json"
);
host.stop().await;
}
/// Plain text calls agent.chat and its interim notification uses dim styling.
@@ -96,6 +99,28 @@ async fn chat() {
);
host.stop().await;
}
/// Invalid command arguments and dotted prose remain agent chat.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn prose_is_not_a_command() {
let world = TestWorld::new();
let host = world.start().await;
for line in [
"help me write a notes plugin",
"ps is useful",
"stop it",
"kill that",
"cat",
"methods probe extra",
"ls too many words",
"log -n nope",
"README.md says hello",
"e.g. write notes",
] {
assert_eq!(command(&host, line).await, line);
}
host.stop().await;
}
/// Folder listings include the live mounts and accept an explicit folder.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ls() {
@@ -215,7 +240,7 @@ async fn log() {
}
}
assert_eq!(command(&host, "log absent-plugin").await, "");
assert!(execute_line(&host, "log -n").await.is_err());
assert_eq!(command(&host, "log -n").await, "log -n");
host.stop().await;
}
/// Activate installs a candidate through core.activate.
+252 -1
View File
@@ -1438,7 +1438,7 @@ async fn core_lifetime_events_enter_the_journal() {
let h = w.start().await;
let stopped = call(&h, "probe.serve", json!({"n":1})).await;
call(&h, "core.stop", json!({"pid":stopped})).await;
let killed = call(&h, "probe.serve", json!({"n":2})).await;
let killed = call(&h, "probe.tree", json!({"depth":0})).await;
call(&h, "core.kill", json!({"pid":killed})).await;
let entries = call(&h, "core.log", json!({"plugin":"probe","limit":100})).await;
let lines: Vec<_> = entries
@@ -1706,3 +1706,254 @@ async fn filesystem_native_processes_and_lock_snapshot() {
assert!(view.stat(&format!("/proc/{pid}"), true).await.is_err());
h.stop().await;
}
/// PROTOCOL 2: invalid plugin names cannot route calls, and core cannot be shadowed.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn plugin_names_and_core_reservation() {
let w = TestWorld::new();
let h = w.start().await;
for name in ["Upper", "bad!", "with space", "é", "", "../probe"] {
h.register_native(
name,
std::sync::Arc::new(|_, _, _| Box::pin(async { Ok(json!("bad")) })),
);
assert_eq!(
h.call(&format!("{name}.echo"), json!({}))
.await
.unwrap_err()["code"],
-32601
);
}
h.register_native(
"core",
std::sync::Arc::new(|_, _, _| Box::pin(async { Ok(json!("bad")) })),
);
assert_eq!(
h.call("core.unknown", json!({})).await.unwrap_err()["code"],
-32601
);
for name in ["a-z_09", "0"] {
h.register_native(
name,
std::sync::Arc::new(|_, _, _| Box::pin(async { Ok(json!(true)) })),
);
assert_eq!(call(&h, &format!("{name}.echo"), json!({})).await, true);
}
h.stop().await;
}
/// PROTOCOL 3.1: a batch on guest stdout does not execute any of its requests.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn batches_are_discarded() {
let w = TestWorld::new();
let h = w.start().await;
assert_eq!(call(&h, "probe.batch", json!({})).await, true);
h.stop().await;
assert!(!w.0.join("state/batch").exists());
assert_eq!(
call(&h, "core.log", json!({"grep":"discarded stdout"}))
.await
.as_array()
.unwrap()
.len(),
1
);
}
/// PROTOCOL 3.3: a second response to an answered call cannot replace its result.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn late_responses_are_dropped() {
let w = TestWorld::new();
let h = w.start().await;
assert_eq!(call(&h, "probe.late_answer", json!({})).await, 42);
assert_eq!(
call(&h, "probe.echo", json!({"next":true})).await,
json!({"next":true})
);
h.stop().await;
}
/// PROTOCOL 3.4: core.call notifications forget detached children after the caller ends.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn notification_core_call_forgets_detached_child() {
let w = TestWorld::new();
let h = w.start().await;
let parent = call(&h, "probe.forget", json!({})).await;
let child: Value = tokio::time::timeout(std::time::Duration::from_secs(10), async {
loop {
if let Ok(bytes) = std::fs::read(w.0.join("state/forgotten"))
&& let Ok(value) = serde_json::from_slice::<Value>(&bytes)
{
break value;
}
tokio::task::yield_now().await;
}
})
.await
.unwrap();
tokio::time::timeout(std::time::Duration::from_secs(10), async {
while h.processes().contains_key(parent["pid"].as_str().unwrap()) {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
assert!(h.processes()[child["pid"].as_str().unwrap()].detached);
assert!(
h.processes()[child["pid"].as_str().unwrap()]
.parent
.is_none()
);
call(&h, "core.stop", json!({"pid":child["pid"]})).await;
assert!(!h.processes().contains_key(child["pid"].as_str().unwrap()));
h.stop().await;
}
/// PROTOCOL 3.6: three generations that ignore EOF stop together within one five-second deadline.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn attached_tree_stops_in_parallel() {
let w = TestWorld::new();
let h = w.start().await;
let root = call(&h, "probe.tree", json!({"depth":2})).await;
assert_eq!(
h.processes()
.values()
.filter(|p| p.method == "tree")
.count(),
3
);
let before = std::time::Instant::now();
call(&h, "core.stop", json!({"pid":root})).await;
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while h.processes().values().any(|p| p.method == "tree") {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
assert!(before.elapsed() >= std::time::Duration::from_millis(4900));
assert!(before.elapsed() < std::time::Duration::from_secs(7));
h.stop().await;
}
/// PROTOCOL 3.8: a nested call receives its caller's process ID as SAUSAGE_PARENT.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn nested_parent_environment() {
let w = TestWorld::new();
let h = w.start().await;
let result = call(&h, "probe.nested_environment", json!({})).await;
assert_eq!(result["child"]["parent"], result["caller"]);
assert_ne!(result["child"]["pid"], result["caller"]);
h.stop().await;
}
/// PROTOCOL 8 and 3.8: boot services receive core as SAUSAGE_PARENT.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn service_parent_environment() {
let w = TestWorld::new();
std::fs::write(
w.0.join("config/boot.json"),
br#"[{"method":"probe.forgotten","params":{}}]"#,
)
.unwrap();
let h = w.start().await;
let env: Value =
serde_json::from_slice(&std::fs::read(w.0.join("state/forgotten")).unwrap()).unwrap();
assert_eq!(env["parent"], "core");
assert_eq!(env["plugin"], "probe");
assert!(h.processes()[env["pid"].as_str().unwrap()].detached);
h.stop().await;
}
/// PROTOCOL 4: info.json exposes every field of the live process snapshot.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn info_json_has_all_process_fields() {
let w = TestWorld::new();
let h = w.start().await;
let params = json!({"marker":"complete"});
let pid = call(&h, "probe.serve", params.clone()).await;
let text = call(
&h,
"probe.read",
json!({"path":format!("/proc/{}/info.json", pid.as_str().unwrap())}),
)
.await;
let info: Value = serde_json::from_str(text.as_str().unwrap()).unwrap();
let expected = serde_json::to_value(&h.processes()[pid.as_str().unwrap()]).unwrap();
assert_eq!(info, expected);
for field in [
"plugin",
"version",
"method",
"params",
"parent",
"detached",
"start_time",
"state",
] {
assert!(info.get(field).is_some(), "{field}");
}
assert_eq!(info["params"], params);
assert_eq!(info["parent"], "console");
assert_eq!(info["state"], "running");
h.stop().await;
}
/// PROTOCOL 3.6: detached processes persist until stop, kill, or host shutdown explicitly ends them.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn detached_processes_need_explicit_termination() {
let w = TestWorld::new();
let h = w.start().await;
let mut pids = Vec::new();
for _ in 0..3 {
let pid = call(
&h,
"core.call",
json!({"method":"probe.serve","params":{},"detach":true}),
)
.await;
assert!(h.processes()[pid.as_str().unwrap()].parent.is_none());
pids.push(pid);
}
call(&h, "probe.echo", json!({})).await;
for pid in &pids {
assert!(h.processes().contains_key(pid.as_str().unwrap()));
}
call(&h, "core.stop", json!({"pid":pids[0]})).await;
call(&h, "core.kill", json!({"pid":pids[1]})).await;
assert!(h.processes().contains_key(pids[2].as_str().unwrap()));
h.stop().await;
for pid in pids {
assert!(!h.processes().contains_key(pid.as_str().unwrap()));
}
}
/// PROTOCOL 9: core.exit from boot ends the host executable with status 75.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn core_exit_sets_executable_status_75() {
let w = TestWorld::new();
std::fs::write(
w.0.join("config/boot.json"),
br#"[{"method":"core.exit","params":{}}]"#,
)
.unwrap();
let output = tokio::time::timeout(
std::time::Duration::from_secs(30),
tokio::process::Command::new(env!("CARGO_BIN_EXE_sausage"))
.arg("--headless")
.arg("run")
.arg(&w.0)
.stdin(std::process::Stdio::null())
.kill_on_drop(true)
.output(),
)
.await
.unwrap()
.unwrap();
assert_eq!(
output.status.code(),
Some(75),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}
+9 -3
View File
@@ -1,9 +1,15 @@
[package]
name = "cargo-valid"
version = "0.1.0"
edition = "2024"
edition.workspace = true
[dependencies]
sausage-plugin = "0.1"
[target.'cfg(target_arch = "wasm32")'.dependencies]
sausage_plugin = { package = "sausage-plugin", version = "0.1", optional = true }
[features]
default = ["dep:sausage_plugin"]
[workspace]
[workspace.package]
edition = "2024"
+4
View File
@@ -0,0 +1,4 @@
//! Produces a warning so the build response can retain diagnostics.
fn main() {
println!("cargo:warning=fixture build warning");
}
+80
View File
@@ -34,6 +34,12 @@ mod tests {
async fn sdk_lock() {
let guard = sausage_plugin::lock("/state/lock").await.unwrap();
assert!(guard.held());
let locks: sausage_plugin::serde_json::Value = sausage_plugin::serde_json::from_str(
&std::fs::read_to_string("/proc/core/locks.json").unwrap(),
)
.unwrap();
assert_eq!(locks.as_object().unwrap().len(), 1);
assert!(locks.get("/state/lock").is_some());
let mut guard = Some(guard);
let mut waiting = Box::pin(std::future::IntoFuture::into_future(sausage_plugin::lock(
"/state/lock",
@@ -66,6 +72,80 @@ mod tests {
.unwrap_err();
assert!(error.to_string().contains("a test has no other plugins"));
}
#[sausage_plugin::test]
async fn unlock_order() {
for iteration in 0..300 {
let guard = sausage_plugin::lock("/state/order").await.unwrap();
assert!(guard.held());
drop(guard);
let guard = sausage_plugin::lock("/state/order")
.try_now()
.await
.unwrap();
assert!(guard.held(), "iteration {iteration}");
}
}
#[test]
fn empty_world() {
for path in [
"/plugins",
"/config",
"/state",
"/state/core/journal.sqlite",
] {
assert!(!std::path::Path::new(path).exists(), "{path}");
}
let names: Vec<_> = std::fs::read_dir("/")
.unwrap()
.map(|entry| entry.unwrap().file_name())
.collect();
assert!(names.iter().all(|name| {
["proc", "self", "run", "tmp"]
.iter()
.any(|allowed| name == allowed)
}));
for path in ["/proc", "/self", "/run"] {
assert!(std::path::Path::new(path).is_dir());
}
assert!(std::fs::write("/proc/forbidden", "no").is_err());
}
fn isolated() {
assert!(!std::path::Path::new("/self/own").exists());
assert!(!std::path::Path::new("/run/own").exists());
std::fs::write("/self/own", "self").unwrap();
std::fs::write("/run/own", "run").unwrap();
assert_eq!(std::fs::read_to_string("/self/own").unwrap(), "self");
}
#[test]
fn isolation_one() {
isolated();
}
#[test]
fn isolation_two() {
isolated();
}
#[test]
fn environment() {
for (key, _) in std::env::vars() {
assert!(
["CARGO_", "RUST_", "SAUSAGE_"]
.iter()
.any(|prefix| key.starts_with(prefix)),
"{key}"
);
}
assert!(std::env::var("PWD").is_err());
}
#[test]
fn late_response() {
use std::io::Write;
for id in 0..100 {
println!(
"\n{{\"jsonrpc\":\"2.0\",\"id\":{id},\"method\":\"core.log\",\"params\":{{}}}}"
);
}
std::io::stdout().flush().unwrap();
}
#[test]
#[ignore]
fn ignored_case() {
@@ -0,0 +1,5 @@
//! Checks that cargo invokes integration test binaries through the WASI runner.
#[test]
fn integration_case() {
println!("INTEGRATION_CASE");
}
+40
View File
@@ -6,6 +6,38 @@ fn send(value: Value) {
println!("{value}");
io::stdout().flush().unwrap();
}
/// Calls a provider after activation releases an old consumer process.
fn deferred(request: &Value, plugin: &str, lines: &mut impl Iterator<Item = io::Result<String>>) {
send(
json!({"jsonrpc":"2.0","id":request["id"],"result":std::env::var("SAUSAGE_PID").unwrap()}),
);
while !std::path::Path::new(&format!("/state/{plugin}/release-call")).exists() {
std::thread::sleep(std::time::Duration::from_millis(1));
}
send(json!({"jsonrpc":"2.0","id":"deferred","method":"notes.legacy","params":{}}));
for line in lines {
let response: Value = serde_json::from_str(&line.unwrap()).unwrap();
if response["id"] == "deferred" {
std::fs::write(format!("/state/{plugin}/call-result"), response.to_string()).unwrap();
break;
}
}
}
/// Answers a method that version two removes from its interface.
fn legacy(request: &Value, document: &Value, version: &str) {
if document["methods"]
.as_array()
.unwrap()
.iter()
.any(|m| m["name"] == "legacy")
{
send(json!({"jsonrpc":"2.0","id":request["id"],"result":version}));
} else {
send(
json!({"jsonrpc":"2.0","id":request["id"],"error":{"code":-32601,"message":"legacy removed","data":{"version":version}}}),
);
}
}
/// Reads a version-specific discovery document without the SDK.
fn main() {
let version = env!("FIXTURE_VERSION");
@@ -16,6 +48,14 @@ fn main() {
serde_json::from_slice(&std::fs::read(format!("/state/{plugin}/{version}.json")).unwrap())
.unwrap();
let result = match request["method"].as_str().unwrap() {
"deferred" => {
deferred(&request, &plugin, &mut lines);
return;
}
"legacy" => {
legacy(&request, &document, version);
return;
}
"rpc.discover" => {
if document["x-test-wait"] == true {
std::fs::write(format!("/state/{plugin}/discover-started"), "ready").unwrap();
+66
View File
@@ -324,9 +324,75 @@ fn notification_child(lines: &mut Lines) -> Value {
}
json!(count)
}
fn batch(lines: &mut Lines) -> Value {
send(
json!([{"jsonrpc":"2.0","method":"probe.write","params":{"path":"/state/batch","text":"bad"}}]),
);
outgoing_call(
&json!({"method":"probe.echo","params":{"barrier":true}}),
lines,
);
json!(!std::path::Path::new("/state/batch").exists())
}
fn late_answer(request: &Value, lines: &mut Lines) {
send(json!({"jsonrpc":"2.0","id":request["id"],"result":42}));
send(json!({"jsonrpc":"2.0","id":request["id"],"result":99}));
for _ in lines {}
}
fn nested_environment(lines: &mut Lines) -> Value {
let child = outgoing_call(&json!({"method":"probe.env","params":{}}), lines);
json!({"caller":std::env::var("SAUSAGE_PID").unwrap(),"child":child})
}
fn forgotten(request: &Value, lines: &mut Lines) {
std::fs::write("/state/forgotten", environment().to_string()).unwrap();
serve(request, lines);
}
fn forget(lines: &mut Lines) -> Value {
send(
json!({"jsonrpc":"2.0","method":"core.call","params":{"method":"probe.forgotten","params":{},"detach":true}}),
);
while !std::path::Path::new("/state/forgotten").exists() {
std::thread::sleep(std::time::Duration::from_millis(1));
}
send(json!({"jsonrpc":"2.0","id":"barrier","method":"probe.echo","params":{}}));
let response = receive(lines);
assert_eq!(
response["id"], "barrier",
"a forgotten call must not send a result"
);
environment()
}
fn tree(request: &Value, lines: &mut Lines) {
let depth = request["params"]["depth"].as_u64().unwrap();
if depth > 0 {
outgoing_call(
&json!({"method":"probe.tree","params":{"depth":depth-1}}),
lines,
);
}
send(
json!({"jsonrpc":"2.0","id":request["id"],"result":std::env::var("SAUSAGE_PID").unwrap()}),
);
busy();
}
fn dispatch(request: &Value, lines: &mut Lines) -> Option<Value> {
let params = &request["params"];
let result = match request["method"].as_str().unwrap() {
"batch" => batch(lines),
"nested_environment" => nested_environment(lines),
"forget" => forget(lines),
"forgotten" => {
forgotten(request, lines);
return None;
}
"tree" => {
tree(request, lines);
return None;
}
"late_answer" => {
late_answer(request, lines);
return None;
}
"invalid_stderr" => {
io::stderr().write_all(&[255, 10]).unwrap();
eprintln!("after invalid UTF-8");
+119
View File
@@ -246,3 +246,122 @@ async fn secrets_fixed_endpoint() {
assert_eq!(call(&host, "consumer.vault", json!({})).await, "hello");
host.stop().await;
}
/// PROTOCOL 3.6: a background task retains its SDK process until the task completes.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn background_task_retains_process() {
let world = TestWorld::new();
let host = world.start().await;
let pid = call(&host, "probe.background_hold", json!({})).await;
assert!(host.processes().contains_key(pid.as_str().unwrap()));
assert_eq!(call(&host, "notes.list", json!({})).await, json!([]));
assert!(host.processes().contains_key(pid.as_str().unwrap()));
std::fs::write(world.0.join("release-background"), "release").unwrap();
tokio::time::timeout(Duration::from_secs(10), async {
while host.processes().contains_key(pid.as_str().unwrap()) {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
assert_eq!(
std::fs::read_to_string(world.0.join("background-ended")).unwrap(),
"done"
);
host.stop().await;
}
/// PROTOCOL 5: stale endpoint identities and interfaces fail through genuine SDK processes.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn endpoint_handshake_rejects_mismatch() {
let world = TestWorld::new();
let host = world.start().await;
let pid = call(&host, "probe.service", json!({})).await;
let path = world.0.join("state/control");
let original = std::fs::read_to_string(&path).unwrap();
let words: Vec<_> = original.split_whitespace().collect();
for changed in [
format!("{} wrong-owner {}\n", words[0], words[2]),
format!("{} {} wrong-interface\n", words[0], words[1]),
] {
std::fs::write(&path, changed).unwrap();
let error = host
.call("typed.world_endpoint", json!({}))
.await
.unwrap_err();
assert_eq!(error["code"], -32000);
assert!(error["message"].as_str().unwrap().contains("mismatch"));
assert!(host.processes().contains_key(pid.as_str().unwrap()));
}
std::fs::write(path, original).unwrap();
assert_eq!(call(&host, "typed.world_endpoint", json!({})).await, 7);
host.stop().await;
}
/// PROTOCOL 10.1 and 5: discovery through core retains params, endpoints, imports, and grace.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn discovery_extensions_through_core() {
let world = TestWorld::new();
let host = world.start().await;
let provider = call(&host, "probe.rpc.discover", json!({})).await;
assert_eq!(provider["openrpc"], "1.2.6");
assert_eq!(provider["x-sausage-grace"], 12);
assert!(
provider["x-sausage-endpoints"]["control"]["methods"]
.as_array()
.unwrap()
.iter()
.any(|m| m["name"] == "stats")
);
let add = provider["methods"]
.as_array()
.unwrap()
.iter()
.find(|m| m["name"] == "add")
.unwrap();
assert_eq!(add["paramStructure"], "by-name");
let reference = add["x-sausage-params"]["$ref"].as_str().unwrap();
assert_eq!(
provider.pointer(reference.trim_start_matches('#')).unwrap()["type"],
"object"
);
let consumer = call(&host, "typed.rpc.discover", json!({})).await;
assert!(
consumer["x-sausage-imports"]["probe"]["methods"]
.as_array()
.unwrap()
.iter()
.any(|m| m["name"] == "add")
);
host.stop().await;
}
/// PROTOCOL 3.2: rpc.discover is SDK control traffic and endpoint control is not a main method.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn reserved_rpc_control_names() {
let world = TestWorld::new();
let host = world.start().await;
assert!(call(&host, "probe.rpc.discover", json!({})).await["methods"].is_array());
for method in ["probe.rpc.endpoint", "probe.rpc.unknown"] {
assert_eq!(
host.call(method, json!({})).await.unwrap_err()["code"],
-32601
);
}
host.stop().await;
}
/// PROTOCOL 3.1: the SDK's leading newline isolates an unfinished diagnostic from its response.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn leading_newline_protects_sdk_response() {
let world = TestWorld::new();
let host = world.start().await;
assert_eq!(
call(&host, "probe.framed_output", json!({})).await,
"intact"
);
host.stop().await;
let warnings = call(&host, "core.log", json!({"grep":"discarded stdout"})).await;
assert_eq!(warnings.as_array().unwrap().len(), 1);
assert!(!warnings.to_string().contains("partial diagnostic"));
}