tauri-typegen 0.5.1

A rust crate that automatically generates TypeScript models and bindings from your Tauri commands
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
use crate::interface::config::GenerateConfig;
use crate::models::{CommandInfo, EventInfo, StructInfo};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum CacheError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
    #[error("Hash generation error: {0}")]
    HashError(String),
}

/// Cache file name stored in the output directory
const CACHE_FILE_NAME: &str = ".typecache";

/// Represents the cached state of a generation run
#[derive(Debug, Serialize, Deserialize)]
pub struct GenerationCache {
    /// Version of the cache format for future compatibility
    version: u32,
    /// Hash of all discovered commands
    commands_hash: String,
    /// Hash of all discovered structs
    structs_hash: String,
    /// Hash of all discovered events
    events_hash: String,
    /// Hash of configuration settings that affect output
    config_hash: String,
    /// Combined hash for quick comparison
    combined_hash: String,
}

impl GenerationCache {
    const CURRENT_VERSION: u32 = 2;

    /// Create a new cache from current generation state
    pub fn new(
        commands: &[CommandInfo],
        structs: &HashMap<String, StructInfo>,
        events: &[EventInfo],
        config: &GenerateConfig,
    ) -> Result<Self, CacheError> {
        let commands_hash = Self::hash_commands(commands)?;
        let structs_hash = Self::hash_structs(structs)?;
        let events_hash = Self::hash_events(events)?;
        let config_hash = Self::hash_config(config)?;
        let combined_hash =
            Self::combine_hashes(&commands_hash, &structs_hash, &events_hash, &config_hash)?;

        Ok(Self {
            version: Self::CURRENT_VERSION,
            commands_hash,
            structs_hash,
            events_hash,
            config_hash,
            combined_hash,
        })
    }

    /// Load cache from file
    pub fn load<P: AsRef<Path>>(output_dir: P) -> Result<Self, CacheError> {
        let cache_path = Self::cache_path(output_dir);
        let content = fs::read_to_string(cache_path)?;
        let cache: Self = serde_json::from_str(&content)?;
        Ok(cache)
    }

    /// Save cache to file
    pub fn save<P: AsRef<Path>>(&self, output_dir: P) -> Result<(), CacheError> {
        let cache_path = Self::cache_path(output_dir);

        // Ensure output directory exists
        if let Some(parent) = cache_path.parent() {
            fs::create_dir_all(parent)?;
        }

        let content = serde_json::to_string_pretty(self)?;
        fs::write(cache_path, content)?;
        Ok(())
    }

    /// Check if generation is needed by comparing with previous cache
    pub fn needs_regeneration<P: AsRef<Path>>(
        output_dir: P,
        commands: &[CommandInfo],
        structs: &HashMap<String, StructInfo>,
        events: &[EventInfo],
        config: &GenerateConfig,
    ) -> Result<bool, CacheError> {
        // Try to load previous cache
        let previous_cache = match Self::load(&output_dir) {
            Ok(cache) => cache,
            Err(_) => {
                // No cache file or error reading it - needs regeneration
                return Ok(true);
            }
        };

        // Check version compatibility
        if previous_cache.version != Self::CURRENT_VERSION {
            return Ok(true);
        }

        // Generate current cache
        let current_cache = Self::new(commands, structs, events, config)?;

        // Compare combined hashes
        Ok(previous_cache.combined_hash != current_cache.combined_hash)
    }

    /// Get the cache file path
    fn cache_path<P: AsRef<Path>>(output_dir: P) -> PathBuf {
        output_dir.as_ref().join(CACHE_FILE_NAME)
    }

    /// Generate a deterministic hash of commands
    fn hash_commands(commands: &[CommandInfo]) -> Result<String, CacheError> {
        // Create a serializable representation
        #[derive(Serialize)]
        struct CommandHashData<'a> {
            name: &'a str,
            serde_rename_all: Option<&'a str>,
            parameters: Vec<ParameterHashData<'a>>,
            return_type: &'a str,
            is_async: bool,
            channels: Vec<ChannelHashData<'a>>,
        }

        #[derive(Serialize)]
        struct ParameterHashData<'a> {
            name: &'a str,
            rust_type: &'a str,
            is_optional: bool,
            serde_rename: Option<&'a str>,
        }

        #[derive(Serialize)]
        struct ChannelHashData<'a> {
            parameter_name: &'a str,
            message_type: &'a str,
            serde_rename: Option<&'a str>,
        }

        let mut serialized_commands: Vec<String> = commands
            .iter()
            .map(|cmd| {
                serde_json::to_string(&CommandHashData {
                    name: &cmd.name,
                    serde_rename_all: cmd
                        .serde_rename_all
                        .as_ref()
                        .map(|rule| rule.to_rename_all_str()),
                    parameters: cmd
                        .parameters
                        .iter()
                        .map(|p| ParameterHashData {
                            name: &p.name,
                            rust_type: &p.rust_type,
                            is_optional: p.is_optional,
                            serde_rename: p.serde_rename.as_deref(),
                        })
                        .collect(),
                    return_type: &cmd.return_type,
                    is_async: cmd.is_async,
                    channels: cmd
                        .channels
                        .iter()
                        .map(|c| ChannelHashData {
                            parameter_name: &c.parameter_name,
                            message_type: &c.message_type,
                            serde_rename: c.serde_rename.as_deref(),
                        })
                        .collect(),
                })
            })
            .collect::<Result<_, _>>()?;
        serialized_commands.sort_unstable();

        let json = serde_json::to_string(&serialized_commands)?;
        Ok(Self::compute_hash(&json))
    }

    /// Generate a deterministic hash of events
    fn hash_events(events: &[EventInfo]) -> Result<String, CacheError> {
        #[derive(Serialize)]
        struct EventHashData<'a> {
            event_name: &'a str,
            payload_type: &'a str,
        }

        let mut serialized_events: Vec<String> = events
            .iter()
            .map(|event| {
                serde_json::to_string(&EventHashData {
                    event_name: &event.event_name,
                    payload_type: &event.payload_type,
                })
            })
            .collect::<Result<_, _>>()?;
        serialized_events.sort_unstable();

        let json = serde_json::to_string(&serialized_events)?;
        Ok(Self::compute_hash(&json))
    }

    /// Generate a deterministic hash of structs
    fn hash_structs(structs: &HashMap<String, StructInfo>) -> Result<String, CacheError> {
        #[derive(Serialize)]
        struct StructHashData<'a> {
            name: &'a str,
            is_enum: bool,
            serde_rename_all: Option<&'a str>,
            serde_tag: Option<&'a str>,
            fields: Vec<FieldHashData<'a>>,
            enum_variants: Vec<EnumVariantHashData<'a>>,
        }

        #[derive(Serialize)]
        struct FieldHashData<'a> {
            name: &'a str,
            rust_type: &'a str,
            is_optional: bool,
            is_public: bool,
            validator_attributes: Option<&'a crate::models::ValidatorAttributes>,
            serde_rename: Option<&'a str>,
            type_structure: &'a crate::models::TypeStructure,
        }

        #[derive(Serialize)]
        struct EnumVariantHashData<'a> {
            name: &'a str,
            serde_rename: Option<&'a str>,
            kind: &'a crate::models::EnumVariantKind,
        }

        let mut serialized_structs: Vec<String> = structs
            .values()
            .map(|s| {
                serde_json::to_string(&StructHashData {
                    name: &s.name,
                    is_enum: s.is_enum,
                    serde_rename_all: s
                        .serde_rename_all
                        .as_ref()
                        .map(|rule| rule.to_rename_all_str()),
                    serde_tag: s.serde_tag.as_deref(),
                    fields: s
                        .fields
                        .iter()
                        .map(|f| FieldHashData {
                            name: &f.name,
                            rust_type: &f.rust_type,
                            is_optional: f.is_optional,
                            is_public: f.is_public,
                            validator_attributes: f.validator_attributes.as_ref(),
                            serde_rename: f.serde_rename.as_deref(),
                            type_structure: &f.type_structure,
                        })
                        .collect(),
                    enum_variants: s
                        .enum_variants
                        .as_ref()
                        .map(|variants| {
                            variants
                                .iter()
                                .map(|variant| EnumVariantHashData {
                                    name: &variant.name,
                                    serde_rename: variant.serde_rename.as_deref(),
                                    kind: &variant.kind,
                                })
                                .collect()
                        })
                        .unwrap_or_default(),
                })
            })
            .collect::<Result<_, _>>()?;
        serialized_structs.sort_unstable();

        let json = serde_json::to_string(&serialized_structs)?;
        Ok(Self::compute_hash(&json))
    }

    /// Generate a hash of configuration settings that affect output
    fn hash_config(config: &GenerateConfig) -> Result<String, CacheError> {
        #[derive(Serialize)]
        struct ConfigHashData<'a> {
            validation_library: &'a str,
            include_private: bool,
            type_mappings: Option<Vec<(&'a str, &'a str)>>,
            default_parameter_case: &'a str,
            default_field_case: &'a str,
        }

        let type_mappings = config.type_mappings.as_ref().map(|mappings| {
            let mut canonical: Vec<_> = mappings
                .iter()
                .map(|(key, value)| (key.as_str(), value.as_str()))
                .collect();
            canonical.sort_unstable();
            canonical
        });

        let hash_data = ConfigHashData {
            validation_library: &config.validation_library,
            include_private: config.include_private.unwrap_or(false),
            type_mappings,
            default_parameter_case: &config.default_parameter_case,
            default_field_case: &config.default_field_case,
        };

        let json = serde_json::to_string(&hash_data)?;
        Ok(Self::compute_hash(&json))
    }

    /// Combine multiple hashes into a single hash
    fn combine_hashes(
        commands: &str,
        structs: &str,
        events: &str,
        config: &str,
    ) -> Result<String, CacheError> {
        let combined = format!("{}{}{}{}", commands, structs, events, config);
        Ok(Self::compute_hash(&combined))
    }

    /// Compute SHA-256 hash of a string
    fn compute_hash(data: &str) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        data.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{
        EnumVariantInfo, EnumVariantKind, FieldInfo, LengthConstraint, ParameterInfo,
        TypeStructure, ValidatorAttributes,
    };
    use serde_rename_rule::RenameRule;
    // Test utilities already imported from parent module
    use tempfile::TempDir;

    fn create_test_config() -> GenerateConfig {
        GenerateConfig {
            project_path: "./src-tauri".to_string(),
            output_path: "./src/generated".to_string(),
            validation_library: "none".to_string(),
            verbose: Some(false),
            visualize_deps: Some(false),
            include_private: Some(false),
            type_mappings: None,
            exclude_patterns: None,
            include_patterns: None,
            default_parameter_case: "camelCase".to_string(),
            default_field_case: "snake_case".to_string(),
            force: Some(false),
        }
    }

    fn create_test_command(name: &str) -> CommandInfo {
        CommandInfo::new_for_test(name, "test.rs", 1, vec![], "String", false, vec![])
    }

    fn create_test_event(name: &str) -> EventInfo {
        EventInfo {
            event_name: name.to_string(),
            payload_type: "String".to_string(),
            payload_type_structure: crate::models::TypeStructure::Primitive("string".to_string()),
            file_path: "events.rs".to_string(),
            line_number: 1,
        }
    }

    #[test]
    fn test_cache_creation() {
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        let cache = GenerationCache::new(&commands, &structs, &[], &config).unwrap();

        assert_eq!(cache.version, GenerationCache::CURRENT_VERSION);
        assert!(!cache.commands_hash.is_empty());
        assert!(!cache.structs_hash.is_empty());
        assert!(!cache.config_hash.is_empty());
        assert!(!cache.combined_hash.is_empty());
    }

    #[test]
    fn test_cache_save_and_load() {
        let temp_dir = TempDir::new().unwrap();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        let cache = GenerationCache::new(&commands, &structs, &[], &config).unwrap();
        cache.save(temp_dir.path()).unwrap();

        let loaded_cache = GenerationCache::load(temp_dir.path()).unwrap();

        assert_eq!(cache.combined_hash, loaded_cache.combined_hash);
        assert_eq!(cache.commands_hash, loaded_cache.commands_hash);
        assert_eq!(cache.structs_hash, loaded_cache.structs_hash);
    }

    #[test]
    fn test_needs_regeneration_no_cache() {
        let temp_dir = TempDir::new().unwrap();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        let needs_regen =
            GenerationCache::needs_regeneration(temp_dir.path(), &commands, &structs, &[], &config)
                .unwrap();

        assert!(needs_regen);
    }

    #[test]
    fn test_needs_regeneration_same_state() {
        let temp_dir = TempDir::new().unwrap();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        // Save initial cache
        let cache = GenerationCache::new(&commands, &structs, &[], &config).unwrap();
        cache.save(temp_dir.path()).unwrap();

        // Check if regeneration needed with same data
        let needs_regen =
            GenerationCache::needs_regeneration(temp_dir.path(), &commands, &structs, &[], &config)
                .unwrap();

        assert!(!needs_regen);
    }

    #[test]
    fn test_needs_regeneration_command_changed() {
        let temp_dir = TempDir::new().unwrap();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        // Save initial cache
        let cache = GenerationCache::new(&commands, &structs, &[], &config).unwrap();
        cache.save(temp_dir.path()).unwrap();

        // Change commands
        let new_commands = vec![create_test_command("different_command")];

        let needs_regen = GenerationCache::needs_regeneration(
            temp_dir.path(),
            &new_commands,
            &structs,
            &[],
            &config,
        )
        .unwrap();

        assert!(needs_regen);
    }

    #[test]
    fn test_needs_regeneration_config_changed() {
        let temp_dir = TempDir::new().unwrap();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        // Save initial cache
        let cache = GenerationCache::new(&commands, &structs, &[], &config).unwrap();
        cache.save(temp_dir.path()).unwrap();

        // Change config
        let mut new_config = config;
        new_config.validation_library = "zod".to_string();

        let needs_regen = GenerationCache::needs_regeneration(
            temp_dir.path(),
            &commands,
            &structs,
            &[],
            &new_config,
        )
        .unwrap();

        assert!(needs_regen);
    }

    #[test]
    fn test_hash_determinism() {
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        let cache1 = GenerationCache::new(&commands, &structs, &[], &config).unwrap();
        let cache2 = GenerationCache::new(&commands, &structs, &[], &config).unwrap();

        assert_eq!(cache1.combined_hash, cache2.combined_hash);
        assert_eq!(cache1.commands_hash, cache2.commands_hash);
        assert_eq!(cache1.structs_hash, cache2.structs_hash);
        assert_eq!(cache1.config_hash, cache2.config_hash);
    }

    #[test]
    fn test_needs_regeneration_version_mismatch() {
        let temp_dir = TempDir::new().unwrap();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        // Create a cache with a different version
        let old_cache_content = r#"{
            "version": 0,
            "commands_hash": "abc123",
            "structs_hash": "def456",
            "config_hash": "ghi789",
            "combined_hash": "xyz000"
        }"#;
        let cache_path = temp_dir.path().join(".typecache");
        std::fs::write(&cache_path, old_cache_content).unwrap();

        // Should need regeneration due to version mismatch
        let needs_regen =
            GenerationCache::needs_regeneration(temp_dir.path(), &commands, &structs, &[], &config)
                .unwrap();

        assert!(needs_regen);
    }

    #[test]
    fn test_empty_commands_and_structs() {
        let commands: Vec<CommandInfo> = vec![];
        let structs: HashMap<String, crate::models::StructInfo> = HashMap::new();
        let config = create_test_config();

        let cache = GenerationCache::new(&commands, &structs, &[], &config).unwrap();

        // Should still create valid hashes even with empty data
        assert!(!cache.commands_hash.is_empty());
        assert!(!cache.structs_hash.is_empty());
        assert!(!cache.combined_hash.is_empty());
    }

    #[test]
    fn test_struct_hash_order_independence() {
        use crate::models::{FieldInfo, StructInfo, TypeStructure};

        let config = create_test_config();
        let commands = vec![create_test_command("test_command")];

        // Create two structs
        let struct_a = StructInfo {
            name: "StructA".to_string(),
            fields: vec![FieldInfo {
                name: "field_a".to_string(),
                rust_type: "String".to_string(),
                is_optional: false,
                is_public: true,
                validator_attributes: None,
                serde_rename: None,
                type_structure: TypeStructure::Primitive("string".to_string()),
            }],
            file_path: "test.rs".to_string(),
            is_enum: false,
            serde_rename_all: None,
            serde_tag: None,
            enum_variants: None,
        };

        let struct_b = StructInfo {
            name: "StructB".to_string(),
            fields: vec![FieldInfo {
                name: "field_b".to_string(),
                rust_type: "i32".to_string(),
                is_optional: false,
                is_public: true,
                validator_attributes: None,
                serde_rename: None,
                type_structure: TypeStructure::Primitive("number".to_string()),
            }],
            file_path: "test.rs".to_string(),
            is_enum: false,
            serde_rename_all: None,
            serde_tag: None,
            enum_variants: None,
        };

        // Insert in order A, B
        let mut structs1 = HashMap::new();
        structs1.insert("StructA".to_string(), struct_a.clone());
        structs1.insert("StructB".to_string(), struct_b.clone());

        // Insert in order B, A (reverse)
        let mut structs2 = HashMap::new();
        structs2.insert("StructB".to_string(), struct_b);
        structs2.insert("StructA".to_string(), struct_a);

        let cache1 = GenerationCache::new(&commands, &structs1, &[], &config).unwrap();
        let cache2 = GenerationCache::new(&commands, &structs2, &[], &config).unwrap();

        // Hash should be the same regardless of insertion order
        assert_eq!(cache1.structs_hash, cache2.structs_hash);
        assert_eq!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn command_hash_order_independence() {
        let config = create_test_config();
        let structs = HashMap::new();

        let commands1 = vec![
            create_test_command("alpha_command"),
            create_test_command("beta_command"),
        ];
        let commands2 = vec![
            create_test_command("beta_command"),
            create_test_command("alpha_command"),
        ];

        let cache1 = GenerationCache::new(&commands1, &structs, &[], &config).unwrap();
        let cache2 = GenerationCache::new(&commands2, &structs, &[], &config).unwrap();

        assert_eq!(cache1.commands_hash, cache2.commands_hash);
        assert_eq!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn command_hash_ignores_source_location() {
        let config = create_test_config();
        let structs = HashMap::new();

        let command1 = CommandInfo::new_for_test(
            "test_command",
            "src/alpha.rs",
            10,
            vec![],
            "String",
            false,
            vec![],
        );
        let command2 = CommandInfo::new_for_test(
            "test_command",
            "src/beta.rs",
            200,
            vec![],
            "String",
            false,
            vec![],
        );

        let cache1 = GenerationCache::new(&[command1], &structs, &[], &config).unwrap();
        let cache2 = GenerationCache::new(&[command2], &structs, &[], &config).unwrap();

        assert_eq!(cache1.commands_hash, cache2.commands_hash);
        assert_eq!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn event_hash_ignores_source_location() {
        let config = create_test_config();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();

        let event1 = EventInfo {
            event_name: "alpha-ready".to_string(),
            payload_type: "String".to_string(),
            payload_type_structure: crate::models::TypeStructure::Primitive("string".to_string()),
            file_path: "src/alpha.rs".to_string(),
            line_number: 10,
        };
        let event2 = EventInfo {
            event_name: "alpha-ready".to_string(),
            payload_type: "String".to_string(),
            payload_type_structure: crate::models::TypeStructure::Primitive("string".to_string()),
            file_path: "src/beta.rs".to_string(),
            line_number: 200,
        };

        let cache1 = GenerationCache::new(&commands, &structs, &[event1], &config).unwrap();
        let cache2 = GenerationCache::new(&commands, &structs, &[event2], &config).unwrap();

        assert_eq!(cache1.events_hash, cache2.events_hash);
        assert_eq!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn struct_hash_ignores_source_location() {
        let config = create_test_config();
        let commands = vec![create_test_command("test_command")];

        let struct1 = StructInfo {
            name: "Payload".to_string(),
            fields: vec![FieldInfo {
                name: "value".to_string(),
                rust_type: "String".to_string(),
                is_optional: false,
                is_public: true,
                validator_attributes: None,
                serde_rename: None,
                type_structure: TypeStructure::Primitive("string".to_string()),
            }],
            file_path: "src/alpha.rs".to_string(),
            is_enum: false,
            serde_rename_all: None,
            serde_tag: None,
            enum_variants: None,
        };
        let struct2 = StructInfo {
            file_path: "src/beta.rs".to_string(),
            ..struct1.clone()
        };

        let mut structs1 = HashMap::new();
        structs1.insert("Payload".to_string(), struct1);

        let mut structs2 = HashMap::new();
        structs2.insert("Payload".to_string(), struct2);

        let cache1 = GenerationCache::new(&commands, &structs1, &[], &config).unwrap();
        let cache2 = GenerationCache::new(&commands, &structs2, &[], &config).unwrap();

        assert_eq!(cache1.structs_hash, cache2.structs_hash);
        assert_eq!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn command_hash_changes_with_serde_metadata() {
        let config = create_test_config();
        let structs = HashMap::new();

        let mut command1 = CommandInfo::new_for_test(
            "test_command",
            "src/test.rs",
            10,
            vec![ParameterInfo {
                name: "user_id".to_string(),
                rust_type: "String".to_string(),
                is_optional: false,
                type_structure: TypeStructure::Primitive("string".to_string()),
                serde_rename: None,
            }],
            "String",
            false,
            vec![crate::models::ChannelInfo::new_for_test(
                "progress_updates",
                "String",
                "test_command",
                "src/test.rs",
                10,
            )],
        );
        let mut command2 = CommandInfo::new_for_test(
            "test_command",
            "src/test.rs",
            10,
            vec![ParameterInfo {
                name: "user_id".to_string(),
                rust_type: "String".to_string(),
                is_optional: false,
                type_structure: TypeStructure::Primitive("string".to_string()),
                serde_rename: Some("userIdExplicit".to_string()),
            }],
            "String",
            false,
            vec![crate::models::ChannelInfo::new_for_test(
                "progress_updates",
                "String",
                "test_command",
                "src/test.rs",
                10,
            )],
        );
        command1.serde_rename_all = Some(RenameRule::SnakeCase);
        command2.channels[0].serde_rename = Some("progressUpdates".to_string());

        let cache1 = GenerationCache::new(&[command1], &structs, &[], &config).unwrap();
        let cache2 = GenerationCache::new(&[command2], &structs, &[], &config).unwrap();

        assert_ne!(cache1.commands_hash, cache2.commands_hash);
        assert_ne!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn struct_hash_changes_with_field_metadata() {
        let config = create_test_config();
        let commands = vec![create_test_command("test_command")];

        let struct1 = StructInfo {
            name: "Payload".to_string(),
            fields: vec![FieldInfo {
                name: "created_at".to_string(),
                rust_type: "String".to_string(),
                is_optional: false,
                is_public: true,
                validator_attributes: None,
                serde_rename: None,
                type_structure: TypeStructure::Primitive("string".to_string()),
            }],
            file_path: "src/payload.rs".to_string(),
            is_enum: false,
            serde_rename_all: None,
            serde_tag: None,
            enum_variants: None,
        };
        let struct2 = StructInfo {
            fields: vec![FieldInfo {
                name: "created_at".to_string(),
                rust_type: "String".to_string(),
                is_optional: false,
                is_public: true,
                validator_attributes: Some(ValidatorAttributes {
                    length: Some(LengthConstraint {
                        min: Some(1),
                        max: None,
                        message: Some("required".to_string()),
                    }),
                    range: None,
                    email: false,
                    url: false,
                    custom_message: Some("required".to_string()),
                }),
                serde_rename: Some("createdAt".to_string()),
                type_structure: TypeStructure::Primitive("string".to_string()),
            }],
            serde_rename_all: Some(RenameRule::CamelCase),
            ..struct1.clone()
        };

        let mut structs1 = HashMap::new();
        structs1.insert("Payload".to_string(), struct1);

        let mut structs2 = HashMap::new();
        structs2.insert("Payload".to_string(), struct2);

        let cache1 = GenerationCache::new(&commands, &structs1, &[], &config).unwrap();
        let cache2 = GenerationCache::new(&commands, &structs2, &[], &config).unwrap();

        assert_ne!(cache1.structs_hash, cache2.structs_hash);
        assert_ne!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn struct_hash_changes_with_enum_metadata() {
        let config = create_test_config();
        let commands = vec![create_test_command("test_command")];

        let base_variant = EnumVariantInfo {
            name: "ReadyState".to_string(),
            kind: EnumVariantKind::Struct(vec![FieldInfo {
                name: "event_id".to_string(),
                rust_type: "String".to_string(),
                is_optional: false,
                is_public: true,
                validator_attributes: None,
                serde_rename: None,
                type_structure: TypeStructure::Primitive("string".to_string()),
            }]),
            serde_rename: None,
        };
        let renamed_variant = EnumVariantInfo {
            serde_rename: Some("ready_state".to_string()),
            ..base_variant.clone()
        };

        let enum1 = StructInfo {
            name: "StatusEvent".to_string(),
            fields: vec![],
            file_path: "src/status.rs".to_string(),
            is_enum: true,
            serde_rename_all: None,
            serde_tag: None,
            enum_variants: Some(vec![base_variant]),
        };
        let enum2 = StructInfo {
            serde_rename_all: Some(RenameRule::SnakeCase),
            serde_tag: Some("kind".to_string()),
            enum_variants: Some(vec![renamed_variant]),
            ..enum1.clone()
        };

        let mut structs1 = HashMap::new();
        structs1.insert("StatusEvent".to_string(), enum1);

        let mut structs2 = HashMap::new();
        structs2.insert("StatusEvent".to_string(), enum2);

        let cache1 = GenerationCache::new(&commands, &structs1, &[], &config).unwrap();
        let cache2 = GenerationCache::new(&commands, &structs2, &[], &config).unwrap();

        assert_ne!(cache1.structs_hash, cache2.structs_hash);
        assert_ne!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn test_needs_regeneration_with_corrupted_cache_file() {
        let temp_dir = TempDir::new().unwrap();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        // Create a corrupted cache file
        let cache_path = temp_dir.path().join(".typecache");
        std::fs::write(&cache_path, "not valid json").unwrap();

        // Should need regeneration because cache is unreadable
        let needs_regen =
            GenerationCache::needs_regeneration(temp_dir.path(), &commands, &structs, &[], &config)
                .unwrap();

        assert!(needs_regen);
    }

    #[test]
    fn test_cache_with_type_mappings_config() {
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();

        let mut config1 = create_test_config();
        let mut type_mappings = std::collections::HashMap::new();
        type_mappings.insert("CustomType".to_string(), "string".to_string());
        config1.type_mappings = Some(type_mappings);

        let config2 = create_test_config(); // No type mappings

        let cache1 = GenerationCache::new(&commands, &structs, &[], &config1).unwrap();
        let cache2 = GenerationCache::new(&commands, &structs, &[], &config2).unwrap();

        // Config hash should differ when type_mappings differ
        assert_ne!(cache1.config_hash, cache2.config_hash);
        assert_ne!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn config_hash_type_mappings_order_independence() {
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();

        let mut config1 = create_test_config();
        let mut mappings1 = HashMap::new();
        mappings1.insert("First".to_string(), "string".to_string());
        mappings1.insert("Second".to_string(), "number".to_string());
        config1.type_mappings = Some(mappings1);

        let mut config2 = create_test_config();
        let mut mappings2 = HashMap::new();
        mappings2.insert("Second".to_string(), "number".to_string());
        mappings2.insert("First".to_string(), "string".to_string());
        config2.type_mappings = Some(mappings2);

        let cache1 = GenerationCache::new(&commands, &structs, &[], &config1).unwrap();
        let cache2 = GenerationCache::new(&commands, &structs, &[], &config2).unwrap();

        assert_eq!(cache1.config_hash, cache2.config_hash);
        assert_eq!(cache1.combined_hash, cache2.combined_hash);
    }

    #[test]
    fn events_change_requires_regeneration() {
        let temp_dir = TempDir::new().unwrap();
        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();
        let initial_events = vec![create_test_event("alpha-ready")];
        let changed_events = vec![create_test_event("beta-ready")];

        let cache = GenerationCache::new(&commands, &structs, &initial_events, &config).unwrap();
        cache.save(temp_dir.path()).unwrap();

        let needs_regen = GenerationCache::needs_regeneration(
            temp_dir.path(),
            &commands,
            &structs,
            &changed_events,
            &config,
        )
        .unwrap();

        assert!(needs_regen);
    }

    #[test]
    fn test_cache_with_channels() {
        use crate::models::ChannelInfo;

        let structs = HashMap::new();
        let config = create_test_config();

        let channel = ChannelInfo::new_for_test("progress", "u32", "test_command", "test.rs", 1);

        let cmd_with_channel = CommandInfo::new_for_test(
            "test_command",
            "test.rs",
            1,
            vec![],
            "String",
            false,
            vec![channel],
        );

        let cmd_without_channel = create_test_command("test_command");

        let cache_with = GenerationCache::new(&[cmd_with_channel], &structs, &[], &config).unwrap();
        let cache_without =
            GenerationCache::new(&[cmd_without_channel], &structs, &[], &config).unwrap();

        // Commands hash should differ when channels differ
        assert_ne!(cache_with.commands_hash, cache_without.commands_hash);
    }

    #[test]
    fn test_save_creates_output_directory() {
        let temp_dir = TempDir::new().unwrap();
        let nested_output = temp_dir.path().join("nested").join("output").join("dir");

        let commands = vec![create_test_command("test_command")];
        let structs = HashMap::new();
        let config = create_test_config();

        let cache = GenerationCache::new(&commands, &structs, &[], &config).unwrap();

        // Should create nested directories
        cache.save(&nested_output).unwrap();

        assert!(nested_output.join(".typecache").exists());
    }

    #[test]
    fn test_load_nonexistent_cache() {
        let temp_dir = TempDir::new().unwrap();

        // Should return an error when cache doesn't exist
        let result = GenerationCache::load(temp_dir.path());
        assert!(result.is_err());
    }
}