--- name: gpui-testing description: Testing patterns for GPUI applications tags: [rust, gpui, testing, unit-tests] --- # gpui-testing Comprehensive guide to testing GPUI applications based on patterns from script-kit-gpui. ## Overview GPUI applications require specific testing patterns because they deal with: - **UI state management** - Views, components, state machines - **Async operations** - Background tasks, channels, spawn - **Platform integration** - Hotkeys, system calls, OS-specific behavior - **Serialization** - Config files, theme parsing, protocol messages ## Test File Organization ### Convention: `*_tests.rs` Files Tests are organized in separate `*_tests.rs` files alongside their implementation: ``` src/ theme/ mod.rs # Theme types and logic theme_tests.rs # Theme tests config/ mod.rs # Config types config_tests.rs # Config tests components/ unified_list_item.rs unified_list_item_tests.rs ``` Import tests in the module: ```rust // In mod.rs #[cfg(test)] mod theme_tests; ``` ### Inline Test Modules For smaller modules, use inline test modules: ```rust #[cfg(test)] mod tests { use super::*; #[test] fn test_something() { // ... } } ``` ## Test Setup Patterns ### Helper Functions Create helpers to reduce test boilerplate: ```rust /// Helper to create a test Scriptlet with minimal required fields fn test_scriptlet(name: &str, tool: &str, code: &str) -> Scriptlet { Scriptlet { name: name.to_string(), description: None, code: code.to_string(), tool: tool.to_string(), shortcut: None, expand: None, group: None, file_path: None, command: None, alias: None, } } /// Helper to wrap Vec