//! `cargo test -p openidea --test export_bindings` regenerates `src/bindings.ts` and //! `src-tauri/tests/commands.snap` without launching the app (used by `pnpm gen:bindings` //! and CI drift checks). use ide_host::trust::{projectless_command_allowed, restricted_command_allowed}; use std::collections::{BTreeMap, BTreeSet}; use std::path::Path; use tauri_specta::{BuilderConfiguration, LanguageExt}; #[test] fn export_bindings() { openidea_lib::export_bindings(&openidea_lib::specta_builder()); } /// Commands registered outside tauri-specta (`src-tauri/src/lib.rs`), with how Tauri runs them. const RAW_COMMANDS: &[(&str, &str)] = &[("user_font_data", "async")]; /// Every command, one line each: its trust class, where a remote window runs it, whether it is /// in `bindings.ts`, and how Tauri runs it. A change to any of these shows up in this file's diff. #[test] fn export_command_snapshot() { let target = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/commands.snap"); let fresh = std::env::temp_dir().join(format!("openidea-commands-{}.snap", std::process::id())); openidea_lib::specta_builder().export(CommandSnapshot, &fresh).expect("command snapshot"); let text = std::fs::read(&fresh).expect("exported command snapshot"); let _ = std::fs::remove_file(&fresh); if std::fs::read(&target).ok().as_deref() != Some(text.as_slice()) { std::fs::write(&target, text).expect("failed to write the command snapshot"); } } struct CommandSnapshot; impl LanguageExt for CommandSnapshot { type Error = std::io::Error; fn export(self, cfg: &BuilderConfiguration, path: &Path) -> std::io::Result<()> { let spawned = spawned_commands(); let mut modes: BTreeMap<&str, &str> = cfg .commands .iter() .map(|f| { let mode = if spawned.contains(f.name()) { "spawn" } else if f.asyncness() { "async" } else { "sync" }; (f.name(), mode) }) .collect(); let in_bindings: BTreeSet<&str> = modes.keys().copied().collect(); modes.extend(RAW_COMMANDS.iter().copied()); let routed: BTreeSet<&str> = ide_host::rpc::client_commands().into_iter().collect(); let names: BTreeSet<&str> = modes.keys().chain(&routed).copied().collect(); let mut out = String::from( "# Generated by `pnpm gen:bindings`; do not edit. One line per command:\n\ # name, trust class, placement in a remote window, in bindings.ts, how Tauri runs it.\n", ); for name in names { let trust = if restricted_command_allowed(name) { "always" } else if projectless_command_allowed(name) { "outside_restricted" } else { "trusted" }; let placement = if routed.contains(name) { "project_host" } else { "desktop" }; let bindings = if in_bindings.contains(name) { "bindings" } else { "-" }; let mode = modes.get(name).copied().unwrap_or("-"); out.push_str(&format!("{name:<44} {trust:<18} {placement:<12} {bindings:<8} {mode}\n")); } std::fs::write(path, out) } } /// Sync commands declared `#[tauri::command(async)]`, which Tauri runs off the main thread. fn spawned_commands() -> BTreeSet { let mut out = BTreeSet::new(); let mut dirs = vec![Path::new(env!("CARGO_MANIFEST_DIR")).join("src")]; while let Some(dir) = dirs.pop() { for entry in std::fs::read_dir(&dir).unwrap() { let path = entry.unwrap().path(); if path.is_dir() { dirs.push(path); continue; } let source = std::fs::read_to_string(&path).unwrap_or_default(); for (_, rest) in source.match_indices("#[tauri::command(async)]").map(|(i, _)| source.split_at(i)) { let name = rest.split("fn ").nth(1).and_then(|s| s.split('(').next()); out.insert(name.expect("a function after #[tauri::command(async)]").trim().to_owned()); } } } out }