use crate::boundary::answer::answer;
use crate::boundary::config::ConfigFile;
use crate::boundary::dispatch::{Deps, dispatch};
use crate::boundary::reply::Reply;
use crate::boundary::tests::snapshot;
use crate::boundary::{Action, Query};
use crate::cli_outbound::Cli;
use crate::test_support::world_under;
use crate::ui_state::UiState;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
mod browse;
mod files;
mod knobs;
mod reads;
pub(super) const ACME: &str = "[[provider]]\nname = \"acme\"\nprotocol = \"openai_chat\"\n\
base_url = \"https://acme.test\"\nauth = \"none\"\n";
pub(super) fn deps_at(root: &Path, lernie: &Path, bl: &Path) -> Deps {
let world = world_under(root);
fs::create_dir_all(root.join("lernie/workflows")).unwrap();
fs::create_dir_all(world.yog_state_root()).unwrap();
let brazen = crate::test_support::wall_paths(root).config;
fs::create_dir_all(brazen.parent().expect("the wall's brazen dir")).unwrap();
fs::write(&brazen, ACME).unwrap();
Deps {
lernie: Cli::new(lernie),
bl: Cli::new(bl),
state_root: world.yog_state_root(),
yog_binary: root.join("yog"),
world,
home: root.join("home"),
yog_data_root: root.join("data/yog"),
balls_state_root: root.join("state/balls"),
snapshot: Arc::new(snapshot(Path::new("/ws"), "alba", vec![], vec![])),
caller: crate::boundary::dispatch::Caller::default(),
}
}
pub(super) fn seeing(deps: &Deps, workspaces: &[&Path]) -> Deps {
let mut snap = (*deps.snapshot).clone();
snap.workspaces
.extend(workspaces.iter().map(|path| crate::binding::Workspace {
path: (*path).to_path_buf(),
kind: crate::binding::WorkspaceKind::Named {
name: crate::naming::leaf(path),
},
}));
Deps {
snapshot: Arc::new(snap),
..deps.clone()
}
}
pub(super) fn quiet(root: &Path) -> Deps {
deps_at(
root,
Path::new("/definitely/not/a/lernie-xyz"),
Path::new("/definitely/not/a/bl-xyz"),
)
}
pub(super) fn script(dir: &Path, name: &str, body: &str) -> PathBuf {
let path = dir.join(name);
fs::write(&path, format!("#!/bin/sh\n{body}")).unwrap();
let mut perms = fs::metadata(&path).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&path, perms).unwrap();
path
}
pub(super) fn fire(deps: &Deps, action: &Action) -> Result<Reply, String> {
let mut ui = UiState::open(PathBuf::from("/nonexistent/ui.json"));
dispatch(deps, &mut ui, "T0", action)
}
pub(super) fn ask(deps: &Deps, query: &Query) -> Result<Reply, String> {
let ui = UiState::open(PathBuf::from("/nonexistent/ui.json"));
answer(query, deps, &ui, 0)
}
pub(super) fn seed_wall(deps: &Deps, workspace: &Path, text: &str) {
let config = crate::config_edit::brazen::BrazenPaths::in_wall(&crate::world::wall::root_of(
&deps.world,
workspace,
))
.config;
fs::create_dir_all(config.parent().expect("the wall's brazen dir")).unwrap();
fs::write(&config, text).unwrap();
}
pub(super) fn brazen_file() -> ConfigFile {
ConfigFile::Brazen {
workspace: crate::naming::leaf(&(crate::test_support::fixture_workspace())),
}
}
pub(super) fn applying(file: ConfigFile, text: &str) -> Action {
Action::ApplyConfig {
file,
text: text.to_owned(),
}
}