132 lines
4.7 KiB
Rust
132 lines
4.7 KiB
Rust
//! World-loaded dream built-ins and messages survive a harness reopen.
|
|
use sausage::{harness::run_module, world::World};
|
|
use std::{path::Path, process::Command};
|
|
const BUNDLE: &str = include_str!("../js/dream-test-bundle.js");
|
|
fn git(repo: &Path, args: &[&str]) -> anyhow::Result<String> {
|
|
let output = Command::new("git")
|
|
.args(args)
|
|
.current_dir(repo)
|
|
.env("GIT_AUTHOR_NAME", "Test")
|
|
.env("GIT_AUTHOR_EMAIL", "test@example.com")
|
|
.env("GIT_COMMITTER_NAME", "Test")
|
|
.env("GIT_COMMITTER_EMAIL", "test@example.com")
|
|
.output()?;
|
|
anyhow::ensure!(
|
|
output.status.success(),
|
|
"git {args:?}: {}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
Ok(String::from_utf8(output.stdout)?.trim().into())
|
|
}
|
|
fn copy_tree(source: &Path, destination: &Path) -> anyhow::Result<()> {
|
|
std::fs::create_dir_all(destination)?;
|
|
for entry in std::fs::read_dir(source)? {
|
|
let entry = entry?;
|
|
let target = destination.join(entry.file_name());
|
|
if entry.file_type()?.is_dir() {
|
|
copy_tree(&entry.path(), &target)?;
|
|
} else {
|
|
std::fs::copy(entry.path(), target)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
fn prepare(dir: &Path) -> anyhow::Result<()> {
|
|
World::open(dir)?;
|
|
let disk = dir.join("world");
|
|
let repo = disk.join("src/sausage");
|
|
let source = Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
|
|
let target = Path::new(env!("CARGO_TARGET_TMPDIR")).parent().unwrap();
|
|
let userland = source.join("userland");
|
|
if !userland.join("cwd-init/init.o").exists() {
|
|
anyhow::ensure!(
|
|
Command::new("nu")
|
|
.args(["--no-config-file", "cwd-init/build.nu"])
|
|
.current_dir(&userland)
|
|
.status()?
|
|
.success(),
|
|
"cwd-init build failed"
|
|
);
|
|
}
|
|
anyhow::ensure!(
|
|
Command::new("cargo")
|
|
.args(["build", "--release", "-p", "git", "--manifest-path"])
|
|
.arg(userland.join("Cargo.toml"))
|
|
.args(["--target", "wasm32-wasip2", "--target-dir"])
|
|
.arg(target)
|
|
.args(["-j", "2", "--locked"])
|
|
.current_dir(&userland)
|
|
.status()?
|
|
.success(),
|
|
"world git build failed"
|
|
);
|
|
std::fs::create_dir_all(disk.join("bin"))?;
|
|
std::fs::copy(
|
|
target.join("wasm32-wasip2/release/git.wasm"),
|
|
disk.join("bin/git.wasm"),
|
|
)?;
|
|
copy_tree(&source.join("extensions"), &repo.join("extensions"))?;
|
|
std::fs::create_dir_all(repo.join("extensions/probe"))?;
|
|
std::fs::write(
|
|
repo.join("extensions/probe/server.ts"),
|
|
"export default host => { globalThis.dreamHost=host; globalThis.dreamService=host.need({name:'dream'}); globalThis.dreamSubagents=host.need({name:'subagents'}); globalThis.dreamAutomations=host.need({name:'automations'}); return {name:'probe'}; }",
|
|
)?;
|
|
std::fs::create_dir_all(repo.join("agents"))?;
|
|
std::fs::write(
|
|
repo.join("agents/models.json"),
|
|
r#"{"default":{"model":"faux/faux-1"},"fast":{"model":"faux/faux-1"}}"#,
|
|
)?;
|
|
for role in ["ultimate", "coordinator", "worker", "reviewer"] {
|
|
let denial = if role == "worker" || role == "reviewer" {
|
|
"denyTools: [subagent]\n"
|
|
} else {
|
|
""
|
|
};
|
|
std::fs::write(
|
|
repo.join(format!("agents/{role}.md")),
|
|
format!(
|
|
"---\nname: {role}\ndescription: P3 {role}\nisolate: none\n{denial}{}---\nP3 role:{role}\n",
|
|
if role == "ultimate" {
|
|
"resident: true\n"
|
|
} else {
|
|
""
|
|
}
|
|
),
|
|
)?;
|
|
}
|
|
std::fs::copy(
|
|
source.join("agents/dream-pass.md"),
|
|
repo.join("agents/dream-pass.md"),
|
|
)?;
|
|
let demo = disk.join("src/demo");
|
|
std::fs::create_dir_all(&demo)?;
|
|
std::fs::write(demo.join("README.md"), "P3 demo\n")?;
|
|
for repo in [&repo, &demo] {
|
|
git(repo, &["init", "-b", "main"])?;
|
|
git(repo, &["add", "."])?;
|
|
git(repo, &["commit", "--no-gpg-sign", "-m", "P3 fixture"])?;
|
|
}
|
|
Ok(())
|
|
}
|
|
async fn run(dir: &Path, mode: &str) -> anyhow::Result<()> {
|
|
let world = World::open(dir)?;
|
|
let exec = sausage::process::exec(World {
|
|
root: world.root.clone(),
|
|
host_dir: world.host_dir.clone(),
|
|
processes: world.processes.clone(),
|
|
})?;
|
|
run_module(
|
|
world,
|
|
exec,
|
|
&format!("globalThis.dreamMode={mode:?};\n{BUNDLE}"),
|
|
)
|
|
.await
|
|
}
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn builtins_fire_and_report_across_reopen() -> anyhow::Result<()> {
|
|
let dir = tempfile::tempdir()?;
|
|
prepare(dir.path())?;
|
|
run(dir.path(), "first").await?;
|
|
run(dir.path(), "reopen").await
|
|
}
|