|
| 1 | +#![cfg(all(feature = "typescript", feature = "derive"))] |
| 2 | +#![allow(missing_docs, clippy::unwrap_used, clippy::panic)] |
| 3 | + |
| 4 | +use std::fs; |
| 5 | + |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use specta::Type; |
| 8 | +use specta_typescript::Typescript; |
| 9 | +use tauri::WebviewWindowBuilder; |
| 10 | +use tauri::ipc::{CallbackFn, InvokeBody}; |
| 11 | +use tauri::test::{INVOKE_KEY, MockRuntime, get_ipc_response, mock_builder, mock_context, noop_assets}; |
| 12 | +use tauri::webview::InvokeRequest; |
| 13 | +use tauri_specta::{Builder, Casing, Event, collect_commands, collect_events}; |
| 14 | + |
| 15 | +#[tauri::command] |
| 16 | +#[specta::specta] |
| 17 | +fn hello_world(my_name: String) -> String { |
| 18 | + format!("Hello, {my_name}!") |
| 19 | +} |
| 20 | + |
| 21 | +// `rename_all = "snake_case"` makes Tauri expect snake_case argument keys from the frontend. |
| 22 | +#[tauri::command(rename_all = "snake_case")] |
| 23 | +#[specta::specta] |
| 24 | +fn greet_user(user_name: String) -> String { |
| 25 | + format!("Hello, {user_name}, from Rust!") |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Clone, Serialize, Deserialize, Type, Event)] |
| 29 | +struct MyDemoEvent(String); |
| 30 | + |
| 31 | +fn export_to_string(builder: &Builder<MockRuntime>) -> String { |
| 32 | + let dir = std::env::temp_dir().join(format!( |
| 33 | + "tauri_specta_casing_{}_{:?}", |
| 34 | + std::process::id(), |
| 35 | + std::thread::current().id() |
| 36 | + )); |
| 37 | + fs::create_dir_all(&dir).unwrap(); |
| 38 | + let path = dir.join("bindings.ts"); |
| 39 | + builder |
| 40 | + .export(Typescript::default(), &path) |
| 41 | + .expect("failed to export bindings"); |
| 42 | + fs::read_to_string(&path).unwrap() |
| 43 | +} |
| 44 | + |
| 45 | +fn invoke_request(cmd: &str, body: serde_json::Value) -> InvokeRequest { |
| 46 | + InvokeRequest { |
| 47 | + cmd: cmd.into(), |
| 48 | + callback: CallbackFn(0), |
| 49 | + error: CallbackFn(1), |
| 50 | + url: if cfg!(any(windows, target_os = "android")) { |
| 51 | + "http://tauri.localhost" |
| 52 | + } else { |
| 53 | + "tauri://localhost" |
| 54 | + } |
| 55 | + .parse() |
| 56 | + .unwrap(), |
| 57 | + body: InvokeBody::from(body), |
| 58 | + headers: Default::default(), |
| 59 | + invoke_key: INVOKE_KEY.to_string(), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn default_casing_is_camel_case() { |
| 65 | + let builder = Builder::<MockRuntime>::new().commands(collect_commands![hello_world]); |
| 66 | + let out = export_to_string(&builder); |
| 67 | + |
| 68 | + assert!( |
| 69 | + out.contains( |
| 70 | + r#"helloWorld: (myName: string) => __TAURI_INVOKE<string>("hello_world", { myName })"# |
| 71 | + ), |
| 72 | + "expected camelCase accessor and argument by default, got:\n{out}" |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +fn snake_case_function_and_argument_casing() { |
| 78 | + let builder = Builder::<MockRuntime>::new() |
| 79 | + .commands(collect_commands![hello_world]) |
| 80 | + .function_casing(Casing::SnakeCase) |
| 81 | + .argument_casing(Casing::SnakeCase); |
| 82 | + let out = export_to_string(&builder); |
| 83 | + |
| 84 | + assert!( |
| 85 | + out.contains( |
| 86 | + r#"hello_world: (my_name: string) => __TAURI_INVOKE<string>("hello_world", { my_name })"# |
| 87 | + ), |
| 88 | + "expected snake_case accessor and argument, got:\n{out}" |
| 89 | + ); |
| 90 | + // The underlying Tauri command string must be unchanged. |
| 91 | + assert!(out.contains(r#""hello_world""#)); |
| 92 | +} |
| 93 | + |
| 94 | +#[test] |
| 95 | +fn function_casing_applies_to_events() { |
| 96 | + let builder = Builder::<MockRuntime>::new() |
| 97 | + .events(collect_events![MyDemoEvent]) |
| 98 | + .function_casing(Casing::SnakeCase); |
| 99 | + let out = export_to_string(&builder); |
| 100 | + |
| 101 | + assert!( |
| 102 | + out.contains("my_demo_event: makeEvent"), |
| 103 | + "expected snake_case event accessor, got:\n{out}" |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +// The argument keys emitted by the bindings must match what Tauri actually accepts. |
| 108 | +// With `rename_all = "snake_case"`, Tauri expects snake_case keys, so the camelCase keys |
| 109 | +// emitted by the default behavior would fail. This proves `argument_casing` fixes that. |
| 110 | +#[test] |
| 111 | +fn snake_case_arguments_match_tauri_rename_all() { |
| 112 | + let ts_builder = Builder::<MockRuntime>::new() |
| 113 | + .commands(collect_commands![greet_user]) |
| 114 | + .argument_casing(Casing::SnakeCase); |
| 115 | + let out = export_to_string(&ts_builder); |
| 116 | + assert!( |
| 117 | + out.contains(r#"__TAURI_INVOKE<string>("greet_user", { user_name })"#), |
| 118 | + "expected snake_case argument key, got:\n{out}" |
| 119 | + ); |
| 120 | + |
| 121 | + let app_builder = Builder::<MockRuntime>::new().commands(collect_commands![greet_user]); |
| 122 | + let app = mock_builder() |
| 123 | + .invoke_handler(app_builder.invoke_handler()) |
| 124 | + .build(mock_context(noop_assets())) |
| 125 | + .expect("failed to build mock app"); |
| 126 | + let webview = WebviewWindowBuilder::new(&app, "main", Default::default()) |
| 127 | + .build() |
| 128 | + .expect("failed to build webview"); |
| 129 | + |
| 130 | + // snake_case key (what the new bindings emit) succeeds. |
| 131 | + let ok = get_ipc_response( |
| 132 | + &webview, |
| 133 | + invoke_request("greet_user", serde_json::json!({ "user_name": "Cursor" })), |
| 134 | + ); |
| 135 | + assert_eq!( |
| 136 | + ok.expect("snake_case invoke should succeed") |
| 137 | + .deserialize::<String>() |
| 138 | + .unwrap(), |
| 139 | + "Hello, Cursor, from Rust!" |
| 140 | + ); |
| 141 | + |
| 142 | + // camelCase key (what the old default bindings emit) fails against a snake_case command. |
| 143 | + let err = get_ipc_response( |
| 144 | + &webview, |
| 145 | + invoke_request("greet_user", serde_json::json!({ "userName": "Cursor" })), |
| 146 | + ); |
| 147 | + assert!( |
| 148 | + err.is_err(), |
| 149 | + "camelCase key should not satisfy a `rename_all = \"snake_case\"` command" |
| 150 | + ); |
| 151 | +} |
0 commit comments