ui-cli 0.3.13

A CLI to add components to your app.
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
use std::path::{Path, PathBuf};

use cargo_toml::{Dependency, Manifest};

use crate::shared::cli_error::{CliError, CliResult};

/// Information about the workspace and target crate
#[derive(Debug, Clone, PartialEq)]
pub struct WorkspaceInfo {
    /// Whether we're in a workspace
    pub is_workspace: bool,
    /// The workspace root directory (if in a workspace)
    pub workspace_root: Option<PathBuf>,
    /// The target crate name where components should be installed
    pub target_crate: Option<String>,
    /// The path to the target crate directory
    pub target_crate_path: Option<PathBuf>,
    /// The base path for components relative to current working directory
    pub components_base_path: String,
}

impl Default for WorkspaceInfo {
    fn default() -> Self {
        Self {
            is_workspace: false,
            workspace_root: None,
            target_crate: None,
            target_crate_path: None,
            components_base_path: "src/components".to_string(),
        }
    }
}

/// Analyzes the current directory to detect workspace structure and find the appropriate
/// crate for installing components.
pub fn analyze_workspace() -> CliResult<WorkspaceInfo> {
    let current_dir = std::env::current_dir()?;
    analyze_workspace_from_path(&current_dir)
}

/// Analyzes workspace from a specific path (useful for testing)
pub fn analyze_workspace_from_path(start_path: &Path) -> CliResult<WorkspaceInfo> {
    // First, check if we're in a workspace member directory
    let local_cargo_toml = start_path.join("Cargo.toml");

    if !local_cargo_toml.exists() {
        return Err(CliError::file_operation("Cargo.toml not found in current directory"));
    }

    let local_manifest = load_cargo_manifest(&local_cargo_toml)?
        .ok_or_else(|| CliError::file_operation("Failed to parse Cargo.toml"))?;

    // Check if this is a workspace root
    if local_manifest.workspace.is_some() {
        return analyze_from_workspace_root(start_path, &local_manifest);
    }

    // Check if we're in a workspace member by looking for workspace root
    if let Some(workspace_root) = find_workspace_root(start_path)? {
        return analyze_from_workspace_member(start_path, &workspace_root);
    }

    // Not in a workspace - simple single-crate project
    let has_leptos = check_leptos_in_manifest(&local_manifest);

    if !has_leptos {
        return Err(CliError::config("Leptos dependency not found in Cargo.toml"));
    }

    Ok(WorkspaceInfo {
        is_workspace: false,
        workspace_root: None,
        target_crate: local_manifest.package.as_ref().map(|p| p.name.clone()),
        target_crate_path: Some(start_path.to_path_buf()),
        components_base_path: "src/components".to_string(),
    })
}

/// Analyze when running from workspace root
fn analyze_from_workspace_root(workspace_root: &Path, manifest: &Manifest) -> CliResult<WorkspaceInfo> {
    let workspace =
        manifest.workspace.as_ref().ok_or_else(|| CliError::config("Expected workspace manifest"))?;

    // Find workspace member with Leptos
    let members = expand_workspace_members(workspace_root, &workspace.members)?;

    for member_path in &members {
        let member_cargo_toml = member_path.join("Cargo.toml");
        if let Some(member_manifest) = load_cargo_manifest(&member_cargo_toml)?
            && member_manifest.dependencies.contains_key("leptos")
        {
            let crate_name = member_manifest
                .package
                .as_ref()
                .map(|p| p.name.clone())
                .or_else(|| member_path.file_name().map(|n| n.to_string_lossy().to_string()))
                .unwrap_or_default();

            let relative_path = member_path.strip_prefix(workspace_root).unwrap_or(member_path);

            return Ok(WorkspaceInfo {
                is_workspace: true,
                workspace_root: Some(workspace_root.to_path_buf()),
                target_crate: Some(crate_name),
                target_crate_path: Some(member_path.clone()),
                components_base_path: format!("{}/src/components", relative_path.display()),
            });
        }
    }

    // Check workspace.dependencies for leptos
    if workspace.dependencies.contains_key("leptos") {
        // Leptos is in workspace deps, but we need to find which member uses it
        for member_path in &members {
            let member_cargo_toml = member_path.join("Cargo.toml");
            if let Some(member_manifest) = load_cargo_manifest(&member_cargo_toml)?
                && let Some(dep) = member_manifest.dependencies.get("leptos")
                && matches!(dep, Dependency::Inherited(_))
            {
                let crate_name = member_manifest
                    .package
                    .as_ref()
                    .map(|p| p.name.clone())
                    .or_else(|| member_path.file_name().map(|n| n.to_string_lossy().to_string()))
                    .unwrap_or_default();

                let relative_path = member_path.strip_prefix(workspace_root).unwrap_or(member_path);

                return Ok(WorkspaceInfo {
                    is_workspace: true,
                    workspace_root: Some(workspace_root.to_path_buf()),
                    target_crate: Some(crate_name),
                    target_crate_path: Some(member_path.clone()),
                    components_base_path: format!("{}/src/components", relative_path.display()),
                });
            }
        }
    }

    Err(CliError::config(
        "No workspace member with Leptos dependency found. Please run from a crate directory with Leptos installed.",
    ))
}

/// Analyze when running from a workspace member directory
fn analyze_from_workspace_member(member_path: &Path, workspace_root: &Path) -> CliResult<WorkspaceInfo> {
    let member_cargo_toml = member_path.join("Cargo.toml");
    let member_manifest = load_cargo_manifest(&member_cargo_toml)?
        .ok_or_else(|| CliError::file_operation("Failed to parse member Cargo.toml"))?;

    // Check if this member has leptos
    let has_leptos = check_leptos_in_manifest(&member_manifest);

    // Also check workspace.dependencies
    let workspace_cargo_toml = workspace_root.join("Cargo.toml");
    let workspace_has_leptos = if let Some(ws_manifest) = load_cargo_manifest(&workspace_cargo_toml)? {
        ws_manifest.workspace.as_ref().is_some_and(|ws| ws.dependencies.contains_key("leptos"))
    } else {
        false
    };

    if !has_leptos && !workspace_has_leptos {
        return Err(CliError::config("Leptos dependency not found in this crate or workspace"));
    }

    let crate_name = member_manifest
        .package
        .as_ref()
        .map(|p| p.name.clone())
        .or_else(|| member_path.file_name().map(|n| n.to_string_lossy().to_string()))
        .unwrap_or_default();

    Ok(WorkspaceInfo {
        is_workspace: true,
        workspace_root: Some(workspace_root.to_path_buf()),
        target_crate: Some(crate_name),
        target_crate_path: Some(member_path.to_path_buf()),
        // When running from member, components go in local src/components
        components_base_path: "src/components".to_string(),
    })
}

/// Find workspace root by walking up the directory tree
fn find_workspace_root(start_path: &Path) -> CliResult<Option<PathBuf>> {
    let mut current = start_path.parent();

    while let Some(dir) = current {
        let cargo_toml = dir.join("Cargo.toml");
        if cargo_toml.exists()
            && let Some(manifest) = load_cargo_manifest(&cargo_toml)?
            && manifest.workspace.is_some()
        {
            return Ok(Some(dir.to_path_buf()));
        }
        current = dir.parent();
    }

    Ok(None)
}

/// Expand workspace member patterns (handles globs like "crates/*")
fn expand_workspace_members(workspace_root: &Path, members: &[String]) -> CliResult<Vec<PathBuf>> {
    let mut result = Vec::new();

    for member in members {
        if member.contains('*') {
            // Handle glob pattern
            let pattern = workspace_root.join(member);
            let pattern_str = pattern.to_string_lossy();

            if let Ok(paths) = glob::glob(&pattern_str) {
                for path in paths.flatten() {
                    if path.is_dir() && path.join("Cargo.toml").exists() {
                        result.push(path);
                    }
                }
            }
        } else {
            let member_path = workspace_root.join(member);
            if member_path.is_dir() && member_path.join("Cargo.toml").exists() {
                result.push(member_path);
            }
        }
    }

    Ok(result)
}

/// Check if manifest has leptos dependency
fn check_leptos_in_manifest(manifest: &Manifest) -> bool {
    manifest.dependencies.contains_key("leptos")
}

/// Checks if Leptos is installed as a dependency in Cargo.toml
pub fn check_leptos_dependency() -> CliResult<bool> {
    // Use the workspace analysis which handles workspaces properly
    match analyze_workspace() {
        Ok(_) => Ok(true), // If analysis succeeds, leptos was found
        Err(e) => {
            // Check if it's specifically a "leptos not found" error
            let err_msg = format!("{e}");
            if err_msg.contains("Leptos") { Ok(false) } else { Err(e) }
        }
    }
}

/// Gets the tailwind input file path from Cargo.toml metadata.
/// Reads from `[[workspace.metadata.leptos]]` or `[package.metadata.leptos]`.
/// Returns an error if not found - user must add Leptos metadata to Cargo.toml.
pub fn get_tailwind_input_file() -> CliResult<String> {
    let current_dir = std::env::current_dir()?;
    get_tailwind_input_file_from_path(&current_dir)
}

/// Gets the tailwind input file from a specific path (useful for testing)
pub fn get_tailwind_input_file_from_path(start_path: &Path) -> CliResult<String> {
    // First try the local Cargo.toml
    let local_cargo_toml = start_path.join("Cargo.toml");
    if let Some(manifest) = load_cargo_manifest(&local_cargo_toml)?
        && let Some(tailwind_file) = extract_tailwind_from_manifest(&manifest)
    {
        return Ok(tailwind_file);
    }

    // If not found, try to find workspace root and read from there
    if let Some(workspace_root) = find_workspace_root(start_path)?
        && let Some(manifest) = load_cargo_manifest(&workspace_root.join("Cargo.toml"))?
        && let Some(tailwind_file) = extract_tailwind_from_manifest(&manifest)
    {
        return Ok(tailwind_file);
    }

    Err(CliError::config(
        "Missing `tailwind-input-file` in Cargo.toml. \
        Please add Leptos metadata to your Cargo.toml:\n\n\
        [package.metadata.leptos]\n\
        tailwind-input-file = \"style/tailwind.css\"\n\n\
        Or for workspaces:\n\n\
        [[workspace.metadata.leptos]]\n\
        tailwind-input-file = \"style/tailwind.css\"",
    ))
}

/// Extracts tailwind-input-file from a parsed Manifest
fn extract_tailwind_from_manifest(manifest: &Manifest) -> Option<String> {
    // Try workspace.metadata.leptos (array of tables stored as Value)
    if let Some(workspace) = &manifest.workspace
        && let Some(metadata) = &workspace.metadata
        && let Some(leptos_value) = metadata.get("leptos")
    {
        // [[workspace.metadata.leptos]] is an array
        if let Some(array) = leptos_value.as_array()
            && let Some(first) = array.first()
                && let Some(tailwind) = first.get("tailwind-input-file")
                    && let Some(value) = tailwind.as_str() {
                        return Some(value.to_string());
                    }
        // [workspace.metadata.leptos] could also be a table
        if let Some(tailwind) = leptos_value.get("tailwind-input-file")
            && let Some(value) = tailwind.as_str() {
                return Some(value.to_string());
            }
    }

    // Try package.metadata.leptos (single table)
    if let Some(package) = &manifest.package
        && let Some(metadata) = &package.metadata
        && let Some(leptos) = metadata.get("leptos")
        && let Some(tailwind) = leptos.get("tailwind-input-file")
        && let Some(value) = tailwind.as_str()
    {
        return Some(value.to_string());
    }

    None
}

/* ========================================================== */
/*                     ✨ HELPERS ✨                          */
/* ========================================================== */

/// Helper function to load a Cargo.toml manifest from a path
/// Loads and parses a Cargo.toml file into a Manifest struct.
/// Returns None if the file doesn't exist, Ok(Manifest) if parsed successfully.
pub fn load_cargo_manifest(cargo_toml_path: &Path) -> CliResult<Option<Manifest>> {
    if !cargo_toml_path.exists() {
        return Ok(None);
    }

    // Try to load with full workspace resolution first
    match Manifest::from_path(cargo_toml_path) {
        Ok(manifest) => Ok(Some(manifest)),
        Err(_) => {
            // If workspace resolution fails (e.g., in tests), try parsing without workspace resolution
            let contents = std::fs::read_to_string(cargo_toml_path)?;
            let manifest = Manifest::from_slice(contents.as_bytes())?;
            Ok(Some(manifest))
        }
    }
}

/* ========================================================== */
/*                        🧪 TESTS 🧪                         */
/* ========================================================== */

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::TempDir;

    use super::*;

    /// Helper to create a Cargo.toml with given content
    fn write_cargo_toml(dir: &Path, content: &str) {
        fs::write(dir.join("Cargo.toml"), content).unwrap();
    }

    /// Helper to create a minimal src directory
    fn create_src_dir(dir: &Path) {
        fs::create_dir_all(dir.join("src")).unwrap();
        fs::write(dir.join("src/lib.rs"), "").unwrap();
    }

    #[test]
    fn test_single_crate_with_leptos() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[package]
name = "my-app"
version = "0.1.0"
edition = "2021"

[dependencies]
leptos = "0.7"
"#,
        );
        create_src_dir(root);

        let info = analyze_workspace_from_path(root).unwrap();

        assert!(!info.is_workspace);
        assert_eq!(info.target_crate, Some("my-app".to_string()));
        assert_eq!(info.components_base_path, "src/components");
    }

    #[test]
    fn test_single_crate_without_leptos() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[package]
name = "my-app"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = "1"
"#,
        );
        create_src_dir(root);

        let result = analyze_workspace_from_path(root);

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Leptos"));
    }

    #[test]
    fn test_workspace_with_leptos_member() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Create workspace root
        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["app", "server"]
"#,
        );

        // Create app member with leptos
        let app_dir = root.join("app");
        fs::create_dir_all(&app_dir).unwrap();
        write_cargo_toml(
            &app_dir,
            r#"
[package]
name = "my-app"
version = "0.1.0"
edition = "2021"

[dependencies]
leptos = "0.7"
"#,
        );
        create_src_dir(&app_dir);

        // Create server member without leptos
        let server_dir = root.join("server");
        fs::create_dir_all(&server_dir).unwrap();
        write_cargo_toml(
            &server_dir,
            r#"
[package]
name = "my-server"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.7"
"#,
        );
        create_src_dir(&server_dir);

        // Test from workspace root
        let info = analyze_workspace_from_path(root).unwrap();

        assert!(info.is_workspace);
        assert_eq!(info.target_crate, Some("my-app".to_string()));
        assert_eq!(info.components_base_path, "app/src/components");
    }

    #[test]
    fn test_workspace_from_member_directory() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Create workspace root
        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["frontend"]
"#,
        );

        // Create frontend member with leptos
        let frontend_dir = root.join("frontend");
        fs::create_dir_all(&frontend_dir).unwrap();
        write_cargo_toml(
            &frontend_dir,
            r#"
[package]
name = "frontend"
version = "0.1.0"
edition = "2021"

[dependencies]
leptos = "0.7"
"#,
        );
        create_src_dir(&frontend_dir);

        // Test from member directory
        let info = analyze_workspace_from_path(&frontend_dir).unwrap();

        assert!(info.is_workspace);
        assert_eq!(info.target_crate, Some("frontend".to_string()));
        // When running from member, path is relative to member
        assert_eq!(info.components_base_path, "src/components");
    }

    #[test]
    fn test_workspace_with_workspace_dependencies() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Create workspace root with workspace.dependencies
        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["app"]

[workspace.dependencies]
leptos = "0.7"
"#,
        );

        // Create app member that inherits leptos
        let app_dir = root.join("app");
        fs::create_dir_all(&app_dir).unwrap();
        write_cargo_toml(
            &app_dir,
            r#"
[package]
name = "my-app"
version = "0.1.0"
edition = "2021"

[dependencies]
leptos.workspace = true
"#,
        );
        create_src_dir(&app_dir);

        let info = analyze_workspace_from_path(root).unwrap();

        assert!(info.is_workspace);
        assert_eq!(info.target_crate, Some("my-app".to_string()));
    }

    #[test]
    fn test_workspace_no_leptos_member() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["server"]
"#,
        );

        let server_dir = root.join("server");
        fs::create_dir_all(&server_dir).unwrap();
        write_cargo_toml(
            &server_dir,
            r#"
[package]
name = "server"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.7"
"#,
        );
        create_src_dir(&server_dir);

        let result = analyze_workspace_from_path(root);

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Leptos"));
    }

    #[test]
    fn test_no_cargo_toml() {
        let temp = TempDir::new().unwrap();
        let result = analyze_workspace_from_path(temp.path());

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Cargo.toml"));
    }

    #[test]
    fn test_workspace_info_default() {
        let info = WorkspaceInfo::default();

        assert!(!info.is_workspace);
        assert!(info.workspace_root.is_none());
        assert!(info.target_crate.is_none());
        assert_eq!(info.components_base_path, "src/components");
    }

    // ========== Tailwind Input File Tests ==========

    #[test]
    fn test_get_tailwind_from_workspace_metadata() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["app"]

[[workspace.metadata.leptos]]
name = "my-app"
tailwind-input-file = "style/main.css"
"#,
        );

        let result = get_tailwind_input_file_from_path(root).unwrap();
        assert_eq!(result, "style/main.css");
    }

    #[test]
    fn test_get_tailwind_from_package_metadata() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[package]
name = "my-app"
version = "0.1.0"

[package.metadata.leptos]
tailwind-input-file = "assets/tailwind.css"
"#,
        );

        let result = get_tailwind_input_file_from_path(root).unwrap();
        assert_eq!(result, "assets/tailwind.css");
    }

    #[test]
    fn test_get_tailwind_from_workspace_root_when_in_member() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Create workspace root with tailwind config
        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["app"]

[[workspace.metadata.leptos]]
name = "my-app"
tailwind-input-file = "style/global.css"
"#,
        );

        // Create member without tailwind config
        let app_dir = root.join("app");
        fs::create_dir_all(&app_dir).unwrap();
        write_cargo_toml(
            &app_dir,
            r#"
[package]
name = "app"
version = "0.1.0"

[dependencies]
leptos = "0.7"
"#,
        );

        // Should find tailwind from workspace root
        let result = get_tailwind_input_file_from_path(&app_dir).unwrap();
        assert_eq!(result, "style/global.css");
    }

    #[test]
    fn test_get_tailwind_missing_returns_error() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[package]
name = "my-app"
version = "0.1.0"

[dependencies]
leptos = "0.7"
"#,
        );

        let result = get_tailwind_input_file_from_path(root);
        assert!(result.is_err());

        let err_msg = result.unwrap_err().to_string();
        assert!(err_msg.contains("tailwind-input-file"));
        assert!(err_msg.contains("Cargo.toml"));
    }

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

        let result = get_tailwind_input_file_from_path(temp.path());
        assert!(result.is_err());
    }

    #[test]
    fn test_get_tailwind_prefers_local_over_workspace() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Create workspace root with one tailwind config
        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["app"]

[[workspace.metadata.leptos]]
name = "workspace-app"
tailwind-input-file = "style/workspace.css"
"#,
        );

        // Create member with its own tailwind config
        let app_dir = root.join("app");
        fs::create_dir_all(&app_dir).unwrap();
        write_cargo_toml(
            &app_dir,
            r#"
[package]
name = "app"
version = "0.1.0"

[package.metadata.leptos]
tailwind-input-file = "style/local.css"
"#,
        );

        // Should prefer local config
        let result = get_tailwind_input_file_from_path(&app_dir).unwrap();
        assert_eq!(result, "style/local.css");
    }

    #[test]
    fn test_get_tailwind_multiple_leptos_entries_uses_first() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // Multiple [[workspace.metadata.leptos]] entries - should use first
        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["app"]

[[workspace.metadata.leptos]]
name = "first-app"
tailwind-input-file = "style/first.css"

[[workspace.metadata.leptos]]
name = "second-app"
tailwind-input-file = "style/second.css"
"#,
        );

        let result = get_tailwind_input_file_from_path(root).unwrap();
        assert_eq!(result, "style/first.css");
    }

    #[test]
    fn test_get_tailwind_workspace_single_table_format() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        // [workspace.metadata.leptos] as single table (not array)
        write_cargo_toml(
            root,
            r#"
[workspace]
members = ["app"]

[workspace.metadata.leptos]
tailwind-input-file = "style/single.css"
"#,
        );

        let result = get_tailwind_input_file_from_path(root).unwrap();
        assert_eq!(result, "style/single.css");
    }

    #[test]
    fn test_get_tailwind_metadata_exists_but_no_leptos_key() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[package]
name = "my-app"
version = "0.1.0"

[package.metadata]
some-other-tool = { key = "value" }
"#,
        );

        let result = get_tailwind_input_file_from_path(root);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_tailwind_leptos_exists_but_no_tailwind_key() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[package]
name = "my-app"
version = "0.1.0"

[package.metadata.leptos]
name = "my-app"
site-root = "target/site"
"#,
        );

        let result = get_tailwind_input_file_from_path(root);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_tailwind_empty_value() {
        let temp = TempDir::new().unwrap();
        let root = temp.path();

        write_cargo_toml(
            root,
            r#"
[package]
name = "my-app"
version = "0.1.0"

[package.metadata.leptos]
tailwind-input-file = ""
"#,
        );

        // Empty string is technically valid - returns empty
        let result = get_tailwind_input_file_from_path(root).unwrap();
        assert_eq!(result, "");
    }
}