alfred_workflow_rust_project/
lib.rs

1extern crate core;
2
3pub mod alfred;
4pub mod alfred_logger;
5pub mod alfred_web;
6pub mod common;
7pub mod icon;
8pub mod version;
9pub mod workflow;
10pub mod workflow_cache;
11pub mod workflow_config;
12pub mod workflow_item;
13pub mod workflow_keychain;
14pub mod workflow_updater;
15pub mod workflow_background;
16pub mod workflow_database;
17
18#[cfg(test)]
19mod tests {
20    #[test]
21    fn it_works() {
22        let result = 2 + 2;
23        assert_eq!(result, 4);
24    }
25}
26
27#[cfg(test)]
28mod workflow_tests {
29    use crate::workflow::AlfredWorkflow;
30
31    #[test]
32    fn test_workflow_init_ok() {
33        let workflow = AlfredWorkflow::init();
34    }
35
36    #[test]
37    fn test_workflow_send_feedback_ok() {
38        let workflow = AlfredWorkflow::init();
39        workflow.send_feedback();
40        assert!(true)
41    }
42}
43
44#[cfg(test)]
45mod workflow_item_tests {
46    use crate::icon::{BuiltinIcon, Icon};
47    use crate::workflow_item::{
48        Action, ActionItem, ItemText, ItemType, ModKey, Modifier, WorkflowItem,
49    };
50
51    #[test]
52    fn test_modifier_init_ok() {
53        let modifier = Modifier::new()
54            .subtitle(&"hello")
55            .valid(false)
56            .icon(Icon::new(&"test_path", None))
57            .args("kk")
58            .args("vv")
59            .vars("cmd", "just_ket")
60            .vars("cmd", "just_key_2")
61            .vars("cmd2", "hello_kitty");
62        let modifier_str = serde_json::to_string(&modifier).unwrap();
63
64        assert!(modifier_str.contains("hello"));
65        assert!(modifier_str.contains("kk"));
66        assert!(modifier_str.contains("vv"));
67        assert!(modifier_str.contains("cmd"));
68        assert!(modifier_str.contains("just_key_2"));
69        assert!(!modifier_str.contains("just_ket"));
70        assert!(modifier_str.contains("cmd2"));
71        assert!(modifier_str.contains("hello_kitty"));
72    }
73
74    #[test]
75    fn test_workflow_item_build() {
76        let item = WorkflowItem::new("this_is_title")
77            .actions(Action::SingleItem("single_action".to_string()))
78            .uid("uuid")
79            .item_type(ItemType::DEFAULT)
80            .subtitle("this_is_subtitle")
81            .icon(BuiltinIcon::ACCOUNT.get_icon())
82            .valid(true)
83            .matches("this_is_match")
84            .mods(vec![ModKey::CMD, ModKey::CTRL], Modifier::new())
85            .mods(vec![], Modifier::new().subtitle("invalid_subtitle"))
86            .quick_look("quick_look_uri")
87            .auto_complete("this_is_auto_complete_text")
88            .vars("key_1", "value_1")
89            .text(ItemText::new("copy_text"));
90
91        let item_str = serde_json::to_string(&item).unwrap();
92        assert!(item_str.contains("single_action"));
93        assert!(item_str.contains("uuid"));
94        assert!(item_str.contains("this_is_subtitle"));
95        assert!(item_str.contains("this_is_match"));
96        assert!(item_str.contains("quick_look_uri"));
97        assert!(item_str.contains("cmd+ctrl"));
98        assert!(item_str.contains("this_is_auto_complete_text"));
99        assert!(item_str.contains("copy_text"));
100        assert!(!item_str.contains("invalid_subtitle"));
101    }
102
103    #[test]
104    fn test_workflow_item_build_with_multi_item_action() {
105        let item = WorkflowItem::new("test")
106            .actions(Action::MultiItem(vec!["cc".to_string(), "bb".to_string()]));
107
108        let item_str = serde_json::to_string(&item).unwrap();
109        assert!(item_str.contains("[\"cc\",\"bb\"]"));
110    }
111
112    #[test]
113    fn test_workflow_item_build_with_object_item_action() {
114        let item = WorkflowItem::new("test").actions(Action::ObjectItem(
115            ActionItem::new()
116                .text("cc")
117                .texts(vec!["bb", "dd"])
118                .url("action_url")
119                .file("this_is_file")
120                .auto("use_auto"),
121        ));
122
123        let item_str = serde_json::to_string(&item).unwrap();
124        assert!(item_str.contains("[\"cc\",\"bb\",\"dd\"]"));
125        assert!(item_str.contains("text"));
126        assert!(item_str.contains("\"url\":"));
127        assert!(item_str.contains("\"file\":"));
128        assert!(item_str.contains("action_url"));
129        assert!(item_str.contains("this_is_file"));
130        assert!(item_str.contains("use_auto"));
131    }
132}
133
134#[cfg(test)]
135mod icon_tests {
136    use crate::common::EnumIdent;
137    use crate::icon::{BuiltinIcon, Icon, IconType};
138
139    #[test]
140    fn test_get_builtin_icon() {
141        let icon = BuiltinIcon::BURN.get_icon();
142        let icon_str = serde_json::to_string(&icon).unwrap();
143        assert!(icon_str.contains("BurningIcon.icns"));
144        assert!(icon_str.contains(IconType::IconFile.name()))
145    }
146
147    #[test]
148    fn test_new_customize_icon() {
149        let icon_1 = Icon::new(&"my_path", None);
150        let icon_1_str = serde_json::to_string(&icon_1).unwrap();
151        assert!(icon_1_str.contains("my_path"));
152        assert!(icon_1_str.contains(IconType::ImageSelf.name()))
153    }
154}
155
156#[cfg(test)]
157mod env_tests {
158    use crate::alfred::{Alfred, AlfredEnv};
159
160    #[test]
161    fn test_alfred_env_get_fine() {
162        dotenv::dotenv().ok();
163        let alfred = Alfred::init();
164        assert_eq!(alfred.get_preference_path(), "alfred_preferences");
165        assert_eq!(
166            alfred.get_preference_hash_path(),
167            "alfred_preferences_localhash"
168        );
169        assert_eq!(alfred.get_theme(), "alfred.theme.yosemite");
170        assert_eq!(alfred.get_theme_background(), "rgba(255,255,255,0.98)");
171        assert_eq!(alfred.get_theme_subtext(), "3");
172        assert_eq!(alfred.get_version_build(), "277");
173        assert_eq!(alfred.get_version(), "2.4");
174        assert_eq!(
175            alfred.get_workflow_bundle_id(),
176            "com.alfredapp.david.googlesuggest"
177        );
178        assert_eq!(alfred.get_workflow_cache_path(), "./");
179        assert_eq!(alfred.get_workflow_data_path(), "cache_path");
180        assert_eq!(alfred.get_workflow_name(), "GoogleSuggest");
181        assert_eq!(
182            alfred.get_workflow_uuid(),
183            "user.workflow.B0AC54EC-601C-479A-9428-01F9FD732959"
184        );
185        assert!(alfred.is_debug_mode());
186    }
187}