tauri-plugin-typegen 0.1.6

DEPRECATED - This crate has been renamed. Please use the new crate: **[tauri-typegen](https://crates.io/crates/tauri-typegen)**
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use std::collections::HashMap;
use std::fs;
use tauri_plugin_typegen::analysis::CommandAnalyzer;
use tauri_plugin_typegen::generators::generator::BindingsGenerator;
use tauri_plugin_typegen::models::{CommandInfo, ParameterInfo, StructInfo};
use tempfile::TempDir;

fn create_sample_commands() -> Vec<CommandInfo> {
    vec![
        CommandInfo {
            name: "greet".to_string(),
            file_path: "test_file.rs".to_string(),
            line_number: 10,
            parameters: vec![ParameterInfo {
                name: "name".to_string(),
                rust_type: "String".to_string(),
                typescript_type: "string".to_string(),
                is_optional: false,
            }],
            return_type: "string".to_string(),
            is_async: true,
        },
        CommandInfo {
            name: "get_user_count".to_string(),
            file_path: "test_file.rs".to_string(),
            line_number: 15,
            parameters: vec![],
            return_type: "number".to_string(),
            is_async: false,
        },
    ]
}

fn create_empty_structs() -> HashMap<String, StructInfo> {
    HashMap::new() // Empty struct map for basic tests
}

#[test]
fn test_generator_creates_all_files() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = create_sample_commands();
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(Some("zod".to_string()));
    let generated_files = generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    assert_eq!(generated_files.len(), 3);
    assert!(generated_files.contains(&"types.ts".to_string()));
    assert!(generated_files.contains(&"commands.ts".to_string()));
    assert!(generated_files.contains(&"index.ts".to_string()));

    // Verify files exist
    assert!(temp_dir.path().join("types.ts").exists());
    assert!(temp_dir.path().join("commands.ts").exists());
    assert!(temp_dir.path().join("index.ts").exists());
}

#[test]
fn test_generator_without_validation_library() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = create_sample_commands();
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(Some("none".to_string()));
    let generated_files = generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    // Should generate 3 files (no schemas.ts)
    assert_eq!(generated_files.len(), 3);
    assert!(!generated_files.contains(&"schemas.ts".to_string()));
    assert!(!temp_dir.path().join("schemas.ts").exists());
}

#[test]
fn test_types_file_generation() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = create_sample_commands();
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(None);
    generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    let types_content = fs::read_to_string(temp_dir.path().join("types.ts")).unwrap();

    // Should contain parameter interfaces for commands with parameters (vanilla TypeScript)
    assert!(types_content.contains("export interface GreetParams"));
    assert!(types_content.contains("name: string;"));

    // Should NOT contain params interface for commands without parameters
    assert!(!types_content.contains("GetUserCountParams"));
}

#[test]
fn test_zod_schemas_in_types_file() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = create_sample_commands();
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(Some("zod".to_string()));
    generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    let types_content = fs::read_to_string(temp_dir.path().join("types.ts")).unwrap();

    // Schemas are now embedded in types.ts file
    assert!(types_content.contains("import { z } from 'zod';"));
    assert!(types_content.contains("GreetParamsSchema"));
    assert!(types_content.contains("z.object({"));
    assert!(types_content.contains("name: z.string()"));

    // Should not generate schema for commands without parameters
    assert!(!types_content.contains("GetUserCountParamsSchema"));
}

#[test]
fn test_yup_schemas_generation() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = create_sample_commands();
    let discovered_structs = create_empty_structs();

    // Yup support removed - should fall back to vanilla generator
    let mut generator = BindingsGenerator::new(Some("yup".to_string()));
    let generated_files = generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    // Should generate vanilla files (no schemas.ts for yup)
    assert_eq!(generated_files.len(), 3);
    assert!(generated_files.contains(&"types.ts".to_string()));
    assert!(generated_files.contains(&"commands.ts".to_string()));
    assert!(generated_files.contains(&"index.ts".to_string()));
    assert!(!generated_files.contains(&"schemas.ts".to_string()));
}

#[test]
fn test_commands_file_generation() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = create_sample_commands();
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(Some("zod".to_string()));
    generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    let commands_content = fs::read_to_string(temp_dir.path().join("commands.ts")).unwrap();

    assert!(commands_content.contains("import { invoke } from '@tauri-apps/api/core';"));
    assert!(commands_content.contains("import * as types from './types';"));

    // Check specific command functions
    assert!(commands_content.contains("export async function greet"));
    assert!(commands_content.contains("params: types.GreetParams"));
    assert!(commands_content.contains("Promise<string>"));
    assert!(commands_content.contains("types.GreetParamsSchema.parse(params)"));
    assert!(commands_content.contains("invoke('greet'"));

    // Check command without parameters
    assert!(commands_content.contains("export async function getUserCount(): Promise<number>"));
    assert!(commands_content.contains("return invoke('get_user_count');"));
}

#[test]
fn test_commands_without_validation() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = create_sample_commands();
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(Some("none".to_string()));
    generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    let commands_content = fs::read_to_string(temp_dir.path().join("commands.ts")).unwrap();

    // Should not import schemas
    assert!(!commands_content.contains("import * as schemas"));
    assert!(commands_content.contains("return invoke('greet', params);"));
    assert!(!commands_content.contains("parse(params)"));
}

#[test]
fn test_index_file_generation() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = create_sample_commands();
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(None);
    generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    let index_content = fs::read_to_string(temp_dir.path().join("index.ts")).unwrap();

    assert!(index_content.contains("export * from './types';"));
    assert!(index_content.contains("export * from './commands';"));
}

#[test]
fn test_pascal_case_conversion() {
    let generator = BindingsGenerator::new(None);

    assert_eq!(generator.to_pascal_case("hello_world"), "HelloWorld");
    assert_eq!(generator.to_pascal_case("get_user_count"), "GetUserCount");
    assert_eq!(generator.to_pascal_case("simple"), "Simple");
    assert_eq!(generator.to_pascal_case(""), "");
}

#[test]
fn test_typescript_to_zod_type_conversion() {
    let generator = BindingsGenerator::new(None);

    assert_eq!(generator.typescript_to_zod_type("string"), "z.string()");
    assert_eq!(generator.typescript_to_zod_type("number"), "z.number()");
    assert_eq!(generator.typescript_to_zod_type("boolean"), "z.boolean()");
    assert_eq!(
        generator.typescript_to_zod_type("string[]"),
        "z.array(z.string())"
    );
    assert_eq!(
        generator.typescript_to_zod_type("string | null"),
        "z.string().nullable()"
    );
    assert_eq!(
        generator.typescript_to_zod_type("CustomType"),
        "z.lazy(() => z.any()) /* CustomType - define schema separately if needed */"
    );
}

#[test]
fn test_typescript_to_yup_type_conversion() {
    let generator = BindingsGenerator::new(None);

    // Yup support has been removed - all types return the removed message
    assert!(generator
        .typescript_to_yup_type("string")
        .contains("yup support removed"));
    assert!(generator
        .typescript_to_yup_type("number")
        .contains("yup support removed"));
    assert!(generator
        .typescript_to_yup_type("boolean")
        .contains("yup support removed"));
    assert!(generator
        .typescript_to_yup_type("string[]")
        .contains("yup support removed"));
    assert!(generator
        .typescript_to_yup_type("string | null")
        .contains("yup support removed"));
    assert!(generator
        .typescript_to_yup_type("CustomType")
        .contains("yup support removed"));
}

#[test]
fn test_custom_type_detection() {
    let generator = BindingsGenerator::new(None);

    assert!(!generator.is_custom_type("string"));
    assert!(!generator.is_custom_type("number"));
    assert!(!generator.is_custom_type("boolean"));
    assert!(!generator.is_custom_type("void"));
    assert!(!generator.is_custom_type("string[]"));
    assert!(!generator.is_custom_type("string | null"));

    assert!(generator.is_custom_type("User"));
    assert!(generator.is_custom_type("CreateUserRequest"));
}

#[test]
fn test_generator_with_void_return() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = vec![CommandInfo {
        name: "delete_item".to_string(),
        file_path: "test_file.rs".to_string(),
        line_number: 10,
        parameters: vec![ParameterInfo {
            name: "id".to_string(),
            rust_type: "i32".to_string(),
            typescript_type: "number".to_string(),
            is_optional: false,
        }],
        return_type: "void".to_string(),
        is_async: true,
    }];
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(None);
    generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    let commands_content = fs::read_to_string(temp_dir.path().join("commands.ts")).unwrap();
    assert!(commands_content.contains("Promise<void>"));
}

#[test]
fn test_generator_empty_commands_list() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = vec![];
    let discovered_structs = create_empty_structs();

    let mut generator = BindingsGenerator::new(None);
    let generated_files = generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    // Should still generate files, just with empty content
    assert_eq!(generated_files.len(), 3);

    let types_content = fs::read_to_string(temp_dir.path().join("types.ts")).unwrap();
    // Should contain header but no interfaces
    assert!(types_content.contains("Auto-generated TypeScript types"));
    assert!(!types_content.contains("export interface"));
}

#[test]
fn test_primitive_arrays_and_optional_custom_types() {
    // Regression test for issue where Vec<String> became types.string[]
    // and Option<CustomType> didn't get types. prefix
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().to_str().unwrap();

    let commands = vec![
        CommandInfo {
            name: "get_dates".to_string(),
            file_path: "test_file.rs".to_string(),
            line_number: 10,
            parameters: vec![],
            return_type: "string[]".to_string(), // Already converted from Vec<String>
            is_async: true,
        },
        CommandInfo {
            name: "get_user".to_string(),
            file_path: "test_file.rs".to_string(),
            line_number: 20,
            parameters: vec![],
            return_type: "User | null".to_string(), // Already converted from Option<User>
            is_async: true,
        },
        CommandInfo {
            name: "get_items".to_string(),
            file_path: "test_file.rs".to_string(),
            line_number: 30,
            parameters: vec![],
            return_type: "Item[]".to_string(), // Already converted from Vec<Item>
            is_async: true,
        },
    ];

    let mut discovered_structs = HashMap::new();
    discovered_structs.insert(
        "User".to_string(),
        StructInfo {
            name: "User".to_string(),
            fields: vec![],
            is_enum: false,
            file_path: "test_file.rs".to_string(),
        },
    );
    discovered_structs.insert(
        "Item".to_string(),
        StructInfo {
            name: "Item".to_string(),
            fields: vec![],
            is_enum: false,
            file_path: "test_file.rs".to_string(),
        },
    );

    let mut generator = BindingsGenerator::new(Some("zod".to_string()));
    generator
        .generate_models(
            &commands,
            &discovered_structs,
            output_path,
            &CommandAnalyzer::new(),
        )
        .unwrap();

    let commands_content = fs::read_to_string(temp_dir.path().join("commands.ts")).unwrap();

    // Primitive arrays should NOT have types. prefix
    assert!(commands_content.contains("Promise<string[]>"));
    assert!(!commands_content.contains("types.string[]"));

    // Custom types should have types. prefix
    assert!(commands_content.contains("Promise<types.User | null>"));
    assert!(!commands_content.contains("Promise<User | null>")); // Raw User should not appear

    // Arrays of custom types should have types. prefix on the base type
    assert!(commands_content.contains("Promise<types.Item[]>"));
    assert!(!commands_content.contains("Promise<Item[]>")); // Raw Item should not appear
}