codex-mobile-bridge 0.2.6

Remote bridge and service manager for codex-mobile.
Documentation
use super::*;

#[test]
fn decode_env_value_restores_single_quotes() {
    let raw = "'a'\"'\"'b'";
    assert_eq!(environment::decode_env_value(raw), "a'b");
}

#[test]
fn merge_env_values_prefers_overrides_and_defaults() {
    let home_dir = PathBuf::from("/home/demo");
    let paths = ManagedPaths::new(home_dir);
    let existing = HashMap::from([
        (
            "CODEX_MOBILE_TOKEN".to_string(),
            "persisted-token".to_string(),
        ),
        ("CODEX_BINARY".to_string(), "/usr/bin/codex".to_string()),
        ("PATH".to_string(), "/usr/bin:/bin".to_string()),
    ]);
    let overrides = EnvOverrides {
        bridge_token: Some("override-token".to_string()),
        runtime_limit: Some(9),
        db_path: Some(PathBuf::from("/tmp/bridge.db")),
        ..EnvOverrides::default()
    };

    let merged = environment::merge_env_values(&paths, &existing, &overrides).expect("merge failed");

    assert_eq!(
        merged,
        BridgeEnvValues {
            bridge_token: "override-token".to_string(),
            listen_addr: DEFAULT_LISTEN_ADDR.to_string(),
            runtime_limit: 9,
            db_path: PathBuf::from("/tmp/bridge.db"),
            codex_home: None,
            codex_binary: "/usr/bin/codex".to_string(),
            launch_path: "/usr/bin:/bin".to_string(),
        }
    );
}

#[test]
fn build_activate_record_promotes_previous_release() {
    let existing = InstallRecord {
        install_state: "installed".to_string(),
        current_artifact_id: Some("codex-mobile-bridge-0.1.0".to_string()),
        current_version: Some("0.1.0".to_string()),
        current_build_hash: Some("build-1".to_string()),
        current_sha256: Some("sha-1".to_string()),
        current_protocol_version: Some(1),
        current_release_path: Some("/releases/0.1.0".to_string()),
        installed_at_ms: 10,
        ..InstallRecord::default()
    };
    let metadata = ReleaseMetadata {
        artifact_id: "codex-mobile-bridge-0.2.0".to_string(),
        version: "0.2.0".to_string(),
        build_hash: "build-2".to_string(),
        sha256: "sha-2".to_string(),
        protocol_version: 2,
        release_root: "/releases/0.2.0".to_string(),
        executable_path: "/releases/0.2.0/bin/codex-mobile-bridge".to_string(),
    };

    let next = records::build_activate_record(Some(&existing), &metadata, "update");

    assert_eq!(next.current_version.as_deref(), Some("0.2.0"));
    assert_eq!(next.previous_version.as_deref(), Some("0.1.0"));
    assert_eq!(next.previous_release_path.as_deref(), Some("/releases/0.1.0"));
    assert_eq!(next.installed_at_ms, 10);
    assert_eq!(next.last_operation.as_deref(), Some("update"));
}

#[test]
fn build_rollback_record_swaps_current_and_previous() {
    let existing = InstallRecord {
        install_state: "installed".to_string(),
        current_artifact_id: Some("codex-mobile-bridge-0.2.0".to_string()),
        current_version: Some("0.2.0".to_string()),
        current_build_hash: Some("build-2".to_string()),
        current_sha256: Some("sha-2".to_string()),
        current_protocol_version: Some(2),
        current_release_path: Some("/releases/0.2.0".to_string()),
        previous_artifact_id: Some("codex-mobile-bridge-0.1.0".to_string()),
        previous_version: Some("0.1.0".to_string()),
        previous_build_hash: Some("build-1".to_string()),
        previous_sha256: Some("sha-1".to_string()),
        previous_protocol_version: Some(1),
        previous_release_path: Some("/releases/0.1.0".to_string()),
        installed_at_ms: 10,
        ..InstallRecord::default()
    };
    let metadata = ReleaseMetadata {
        artifact_id: "codex-mobile-bridge-0.1.0".to_string(),
        version: "0.1.0".to_string(),
        build_hash: "build-1".to_string(),
        sha256: "sha-1".to_string(),
        protocol_version: 1,
        release_root: "/releases/0.1.0".to_string(),
        executable_path: "/releases/0.1.0/bin/codex-mobile-bridge".to_string(),
    };

    let next = records::build_rollback_record(&existing, &metadata);

    assert_eq!(next.current_version.as_deref(), Some("0.1.0"));
    assert_eq!(next.previous_version.as_deref(), Some("0.2.0"));
    assert_eq!(next.last_operation.as_deref(), Some("rollback"));
}