use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::cli::harness::HarnessId;
use crate::cli::setup::InstallationType;
pub const OPERATOR_SETUP_PROFILE_FILE: &str = "operator-setup.json";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthPosture {
#[serde(rename = "loopback-no-key")]
LoopbackNoKey,
#[serde(rename = "network-keyed")]
NetworkKeyed,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OperatorSetupProfile {
pub installation_type: InstallationType,
pub port: u16,
pub auth_posture: AuthPosture,
pub harnesses: Vec<String>,
pub updated_ms: i64,
}
impl OperatorSetupProfile {
pub fn new(
installation_type: InstallationType,
port: u16,
auth_posture: AuthPosture,
harnesses: &[HarnessId],
updated_ms: i64,
) -> Self {
Self {
installation_type,
port,
auth_posture,
harnesses: harnesses.iter().map(|h| h.slug().to_string()).collect(),
updated_ms,
}
}
pub fn path(base: &Path) -> PathBuf {
crate::paths::resolve_symforge_dir(base).join(OPERATOR_SETUP_PROFILE_FILE)
}
pub fn load(base: &Path) -> Option<Self> {
let text = std::fs::read_to_string(Self::path(base)).ok()?;
serde_json::from_str(&text).ok()
}
pub fn save(&self, base: &Path) -> std::io::Result<()> {
let path = Self::path(base);
let dir = crate::paths::ensure_symforge_dir(base)?;
let json = serde_json::to_string_pretty(self).map_err(std::io::Error::other)?;
atomic_write_in(&dir, &path, json.as_bytes())
}
}
fn atomic_write_in(dir: &Path, path: &Path, content: &[u8]) -> std::io::Result<()> {
use std::io::Write;
let mut tmp = tempfile::NamedTempFile::new_in(dir)?;
tmp.write_all(content)?;
tmp.flush()?;
tmp.as_file().sync_all()?;
tmp.persist(path).map_err(|e| e.error)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> OperatorSetupProfile {
OperatorSetupProfile::new(
InstallationType::Both,
8787,
AuthPosture::LoopbackNoKey,
&[HarnessId::ClaudeCode, HarnessId::Cursor],
1_750_000_000_000,
)
}
#[test]
fn load_missing_returns_none() {
let dir = tempfile::tempdir().unwrap();
assert_eq!(OperatorSetupProfile::load(dir.path()), None);
}
#[test]
fn save_then_load_roundtrips() {
let dir = tempfile::tempdir().unwrap();
let profile = sample();
profile.save(dir.path()).expect("save profile");
let loaded = OperatorSetupProfile::load(dir.path()).expect("load saved profile");
assert_eq!(loaded, profile);
assert_eq!(loaded.harnesses, vec!["claude", "cursor"]);
assert_eq!(loaded.auth_posture, AuthPosture::LoopbackNoKey);
}
#[test]
fn malformed_file_returns_none() {
let dir = tempfile::tempdir().unwrap();
crate::paths::ensure_symforge_dir(dir.path()).unwrap();
std::fs::write(
OperatorSetupProfile::path(dir.path()),
b"{ this is not valid json",
)
.unwrap();
assert_eq!(OperatorSetupProfile::load(dir.path()), None);
}
#[test]
fn network_keyed_posture_persists_without_key_bytes() {
let dir = tempfile::tempdir().unwrap();
let profile = OperatorSetupProfile::new(
InstallationType::Server,
9000,
AuthPosture::NetworkKeyed,
&[HarnessId::Codex],
1,
);
profile.save(dir.path()).expect("save");
let text = std::fs::read_to_string(OperatorSetupProfile::path(dir.path())).unwrap();
assert!(text.contains("network-keyed"));
let loaded = OperatorSetupProfile::load(dir.path()).expect("load");
assert_eq!(loaded.auth_posture, AuthPosture::NetworkKeyed);
}
}