victauri-cli 0.2.0

CLI for Victauri — scaffold tests, check running apps, record sessions
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
//! CLI for Victauri — scaffold tests, diagnose running apps, and record sessions.

use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(
    name = "victauri",
    about = "Full-stack testing toolkit for Tauri apps",
    version,
    propagate_version = true
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Scaffold a Victauri test directory with starter tests
    Init {
        /// Path to the Tauri project root (default: current directory)
        #[arg(short, long)]
        path: Option<PathBuf>,
    },
    /// Connect to a running Tauri app and report status
    Check {
        /// Write `JUnit` XML report to this path
        #[arg(long)]
        junit: Option<PathBuf>,
    },
    /// Run the built-in smoke test suite against a running Tauri app
    Test {
        /// Maximum acceptable DOM complete time in milliseconds
        #[arg(long, default_value_t = 10_000)]
        max_load_ms: u64,
        /// Maximum acceptable JS heap usage in megabytes
        #[arg(long, default_value_t = 512.0)]
        max_heap_mb: f64,
        /// Write `JUnit` XML report to this path
        #[arg(long)]
        junit: Option<PathBuf>,
    },
    /// Record user interactions and generate a test file
    Record {
        /// Output file path for generated test
        #[arg(short, long, default_value = "tests/recorded_flow.rs")]
        output: PathBuf,
        /// Name of the generated test function
        #[arg(short = 'n', long, default_value = "recorded_flow")]
        test_name: String,
    },
    /// Watch test files and re-run on changes
    Watch {
        /// Directory to watch (default: tests/)
        #[arg(short, long, default_value = "tests")]
        dir: PathBuf,
        /// Only run tests matching this filter
        #[arg(short, long)]
        filter: Option<String>,
    },
    /// Report IPC command coverage from a running Tauri app
    Coverage {
        /// Minimum coverage percentage — exit with code 1 if below this value
        #[arg(long)]
        threshold: Option<f64>,
        /// Write coverage as `JUnit` XML report to this path
        #[arg(long)]
        junit: Option<PathBuf>,
        /// Allow empty registry (zero commands) without failing
        #[arg(long)]
        allow_empty_registry: bool,
    },
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Init { path } => {
            let root = path.unwrap_or_else(|| PathBuf::from("."));
            cmd_init(&root)?;
        }
        Commands::Check { junit } => {
            cmd_check(junit.as_deref()).await?;
        }
        Commands::Test {
            max_load_ms,
            max_heap_mb,
            junit,
        } => {
            cmd_test(max_load_ms, max_heap_mb, junit.as_deref()).await?;
        }
        Commands::Record { output, test_name } => {
            cmd_record(&output, &test_name).await?;
        }
        Commands::Watch { dir, filter } => {
            cmd_watch(&dir, filter.as_deref()).await?;
        }
        Commands::Coverage {
            threshold,
            junit,
            allow_empty_registry,
        } => {
            cmd_coverage(threshold, junit.as_deref(), allow_empty_registry).await?;
        }
    }

    Ok(())
}

fn cmd_init(root: &Path) -> Result<()> {
    let root = std::fs::canonicalize(root)
        .with_context(|| format!("directory not found: {}", root.display()))?;

    let (cargo_toml_path, is_tauri) = detect_project(&root)?;

    if !is_tauri {
        eprintln!(
            "Warning: no Tauri dependency detected in {}",
            cargo_toml_path.display()
        );
        eprintln!("         Victauri is designed for Tauri apps — tests may not connect.\n");
    }

    let added = add_dependencies(&cargo_toml_path)?;
    if added {
        eprintln!("  Added victauri-plugin and victauri-test to Cargo.toml");
    } else {
        eprintln!("  Dependencies already present in Cargo.toml");
    }

    let tests_dir = find_src_tauri(&root).map_or_else(|| root.join("tests"), |p| p.join("tests"));
    std::fs::create_dir_all(&tests_dir)
        .with_context(|| format!("failed to create {}", tests_dir.display()))?;

    let smoke_path = tests_dir.join("smoke.rs");
    if smoke_path.exists() {
        eprintln!("  tests/smoke.rs already exists — skipping");
    } else {
        std::fs::write(&smoke_path, generate_smoke_test())
            .with_context(|| format!("failed to write {}", smoke_path.display()))?;
        eprintln!("  Created {}", smoke_path.display());
    }

    eprintln!("\nVictauri initialized. Next steps:");
    eprintln!("  1. Add .plugin(victauri_plugin::init()) to your Tauri builder");
    eprintln!("     (or .plugin(victauri_plugin::init_auto_discover()) for auto-discovery)");
    eprintln!("  2. Start your app:  pnpm run tauri dev");
    eprintln!("  3. Run the tests:   VICTAURI_E2E=1 cargo test --test smoke");
    Ok(())
}

async fn cmd_check(junit_path: Option<&Path>) -> Result<()> {
    eprintln!("Connecting to running Victauri server...\n");

    let mut client = match victauri_test::VictauriClient::discover().await {
        Ok(c) => c,
        Err(e) => {
            bail!(
                "Could not connect to Victauri server: {e}\n\n\
                 Is your Tauri app running? Try:  pnpm run tauri dev\n\
                 The app must have victauri-plugin wired into its builder."
            );
        }
    };

    let info = client
        .get_plugin_info()
        .await
        .context("failed to get plugin info")?;

    let version = info
        .get("version")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("unknown");
    let tool_count = info
        .get("tool_count")
        .and_then(serde_json::Value::as_u64)
        .or_else(|| info.get("tools").and_then(serde_json::Value::as_u64))
        .map_or("?".to_string(), |n| n.to_string());
    let uptime = info
        .get("uptime_secs")
        .and_then(serde_json::Value::as_u64)
        .map_or("?".to_string(), |s| format!("{s}s"));

    eprintln!("  Victauri v{version}");
    eprintln!("  Tools:  {tool_count}");
    eprintln!("  Uptime: {uptime}");

    let registry = client
        .get_registry()
        .await
        .context("failed to get command registry")?;

    let cmd_count = registry
        .as_array()
        .map(Vec::len)
        .or_else(|| {
            registry
                .get("commands")
                .and_then(|c| c.as_array())
                .map(Vec::len)
        })
        .unwrap_or(0);
    eprintln!("  Registered commands: {cmd_count}");

    let health = client
        .check_ipc_integrity()
        .await
        .context("IPC integrity check failed")?;
    let healthy = health
        .get("healthy")
        .and_then(serde_json::Value::as_bool)
        .unwrap_or(false);
    let stale = health
        .get("stale_calls")
        .and_then(serde_json::Value::as_u64)
        .unwrap_or(0);
    let errors = health
        .get("error_count")
        .and_then(serde_json::Value::as_u64)
        .unwrap_or(0);

    if healthy {
        eprintln!("  IPC health: OK");
    } else {
        eprintln!("  IPC health: DEGRADED ({stale} stale, {errors} errors)");
    }

    let ghosts = client
        .detect_ghost_commands()
        .await
        .context("ghost command detection failed")?;
    let ghost_list = ghosts
        .get("ghost_commands")
        .and_then(|g| g.as_array())
        .or_else(|| ghosts.get("frontend_only").and_then(|f| f.as_array()));
    if let Some(list) = ghost_list {
        if list.is_empty() {
            eprintln!("  Ghost commands: none");
        } else {
            eprintln!("  Ghost commands: {} detected", list.len());
            for g in list.iter().take(5) {
                if let Some(name) = g.as_str() {
                    eprintln!("    - {name}");
                } else if let Some(name) = g.get("command").and_then(|c| c.as_str()) {
                    eprintln!("    - {name}");
                }
            }
            if list.len() > 5 {
                eprintln!("    ... and {} more", list.len() - 5);
            }
        }
    }

    let mem = client
        .get_memory_stats()
        .await
        .context("memory stats failed")?;
    let rss = mem
        .get("working_set_bytes")
        .or_else(|| mem.get("rss_bytes"))
        .or_else(|| mem.get("rss"))
        .and_then(serde_json::Value::as_u64);
    if let Some(bytes) = rss {
        let mb = bytes as f64 / 1_048_576.0;
        eprintln!("  Memory: {mb:.1} MB");
    }

    eprintln!("\nVictauri server is live and responding.");

    if let Some(path) = junit_path {
        let start = std::time::Instant::now();
        let report = client
            .verify()
            .ipc_healthy()
            .no_console_errors()
            .no_ghost_commands()
            .run()
            .await
            .context("verify checks failed")?;
        let duration = start.elapsed();

        let junit = report.to_junit("victauri-check", duration);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("failed to create directory {}", parent.display()))?;
        }
        victauri_test::reporting::write_junit_report(&junit, path)
            .with_context(|| format!("failed to write JUnit report to {}", path.display()))?;
        eprintln!("JUnit report written to {}", path.display());
    }

    Ok(())
}

async fn cmd_test(max_load_ms: u64, max_heap_mb: f64, junit_path: Option<&Path>) -> Result<()> {
    eprintln!("Connecting to running Victauri server...\n");

    let mut client = match victauri_test::VictauriClient::discover().await {
        Ok(c) => c,
        Err(e) => {
            bail!(
                "Could not connect to Victauri server: {e}\n\n\
                 Is your Tauri app running? Try:  pnpm run tauri dev\n\
                 The app must have victauri-plugin wired into its builder."
            );
        }
    };

    eprintln!("Running built-in smoke test suite (11 checks)...\n");

    let config = victauri_test::SmokeConfig {
        max_dom_complete_ms: max_load_ms,
        max_heap_mb,
    };

    let report = client
        .smoke_test_with_config(&config)
        .await
        .context("smoke test failed to complete")?;

    eprint!("{}", report.to_summary());

    if let Some(path) = junit_path {
        let verify = report.to_verify_report();
        let junit = verify.to_junit("victauri-smoke", report.duration);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("failed to create directory {}", parent.display()))?;
        }
        victauri_test::reporting::write_junit_report(&junit, path)
            .with_context(|| format!("failed to write JUnit report to {}", path.display()))?;
        eprintln!("JUnit report written to {}", path.display());
    }

    if !report.all_passed() {
        let fail_count = report.failures().len();
        eprintln!("\n{fail_count} of {} checks failed.", report.total_count());
        std::process::exit(1);
    }

    eprintln!("\nAll smoke checks passed.");
    Ok(())
}

async fn cmd_coverage(
    threshold: Option<f64>,
    junit_path: Option<&Path>,
    allow_empty_registry: bool,
) -> Result<()> {
    eprintln!("Connecting to running Victauri server...\n");

    let mut client = match victauri_test::VictauriClient::discover().await {
        Ok(c) => c,
        Err(e) => {
            bail!(
                "Could not connect to Victauri server: {e}\n\n\
                 Is your Tauri app running? Try:  pnpm run tauri dev\n\
                 The app must have victauri-plugin wired into its builder."
            );
        }
    };

    let report = victauri_test::coverage::coverage_report(&mut client)
        .await
        .context("failed to generate coverage report")?;

    if report.total_commands == 0 && !allow_empty_registry {
        eprintln!("WARNING: No commands registered. Coverage cannot be computed.");
        eprintln!(
            "Hint: Use #[inspectable] on your Tauri commands and call \
             .auto_discover() on VictauriBuilder."
        );
        eprintln!("\nPass --allow-empty-registry to suppress this error.");
        std::process::exit(1);
    }

    let summary = report.to_summary();
    eprintln!("{summary}");

    if let Some(path) = junit_path {
        let verify_report = victauri_test::VerifyReport {
            results: vec![victauri_test::CheckResult {
                description: format!(
                    "IPC coverage {:.1}% ({}/{})",
                    report.coverage_percentage, report.tested_commands, report.total_commands
                ),
                passed: threshold.is_none_or(|t| report.meets_threshold(t)),
                detail: if report.untested.is_empty() {
                    String::new()
                } else {
                    format!("untested: {}", report.untested.join(", "))
                },
            }],
        };
        let junit = verify_report.to_junit("victauri-coverage", std::time::Duration::from_secs(0));
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("failed to create directory {}", parent.display()))?;
        }
        victauri_test::reporting::write_junit_report(&junit, path)
            .with_context(|| format!("failed to write JUnit report to {}", path.display()))?;
        eprintln!("JUnit report written to {}", path.display());
    }

    if let Some(t) = threshold {
        if !report.meets_threshold(t) {
            eprintln!(
                "Coverage {:.1}% is below threshold {:.1}% — failing.",
                report.coverage_percentage, t
            );
            std::process::exit(1);
        }
        eprintln!(
            "Coverage {:.1}% meets threshold {:.1}%.",
            report.coverage_percentage, t
        );
    }

    Ok(())
}

async fn cmd_record(output: &Path, test_name: &str) -> Result<()> {
    eprintln!("Connecting to running Tauri app...\n");

    let mut client = match victauri_test::VictauriClient::discover().await {
        Ok(c) => c,
        Err(e) => {
            bail!(
                "Could not connect to Victauri server: {e}\n\n\
                 Is your Tauri app running? Try:  pnpm run tauri dev\n\
                 The app must have victauri-plugin wired into its builder."
            );
        }
    };

    let session_id = format!("record-{}", uuid::Uuid::new_v4());
    client
        .start_recording(Some(&session_id))
        .await
        .context("failed to start recording")?;

    eprintln!("Recording started (session: {session_id})");
    eprintln!("  Interact with your app — clicks, fills, and key presses are captured.");
    eprintln!("  Press Ctrl+C to stop recording and generate the test.\n");

    let (tx, rx) = tokio::sync::oneshot::channel::<()>();
    let tx = std::sync::Mutex::new(Some(tx));
    ctrlc::set_handler(move || {
        if let Some(tx) = victauri_core::acquire_lock(&tx, "ctrlc_handler").take() {
            let _ = tx.send(());
        }
    })
    .context("failed to register Ctrl+C handler")?;

    rx.await.ok();
    eprintln!("\nStopping recording...");

    let session_json = client
        .stop_recording()
        .await
        .context("failed to stop recording")?;

    let session: victauri_core::RecordedSession =
        serde_json::from_value(session_json).context("failed to parse recorded session")?;

    let event_count = session.events.len();
    let interaction_count = session
        .events
        .iter()
        .filter(|e| matches!(e.event, victauri_core::AppEvent::DomInteraction { .. }))
        .count();
    let ipc_count = session
        .events
        .iter()
        .filter(|e| matches!(e.event, victauri_core::AppEvent::Ipc(_)))
        .count();

    let options = victauri_core::CodegenOptions {
        test_name: test_name.to_string(),
        ..victauri_core::CodegenOptions::default()
    };
    let code = victauri_core::generate_test(&session, &options);

    if let Some(parent) = output.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("failed to create directory {}", parent.display()))?;
    }
    std::fs::write(output, &code)
        .with_context(|| format!("failed to write {}", output.display()))?;

    eprintln!(
        "  Captured {event_count} events ({interaction_count} interactions, {ipc_count} IPC calls)"
    );
    eprintln!("  Generated test: {}", output.display());
    eprintln!("\nRun your test:");
    eprintln!("  VICTAURI_E2E=1 cargo test --test {test_name}");
    Ok(())
}

async fn cmd_watch(dir: &Path, filter: Option<&str>) -> Result<()> {
    use notify::{RecursiveMode, Watcher};

    let watch_dir = if dir.is_absolute() {
        dir.to_path_buf()
    } else {
        std::env::current_dir()?.join(dir)
    };

    if !watch_dir.exists() {
        bail!("watch directory does not exist: {}", watch_dir.display());
    }

    eprintln!("Watching {} for changes...", watch_dir.display());
    if let Some(f) = filter {
        eprintln!("  Filter: {f}");
    }
    eprintln!("  Press Ctrl+C to stop.\n");

    let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(1);

    let mut watcher = notify::recommended_watcher(move |res: notify::Result<notify::Event>| {
        if let Ok(event) = res {
            let dominated_by_rs = event
                .paths
                .iter()
                .any(|p| p.extension().is_some_and(|ext| ext == "rs"));
            if dominated_by_rs {
                let _ = tx.blocking_send(());
            }
        }
    })
    .context("failed to create file watcher")?;

    watcher
        .watch(&watch_dir, RecursiveMode::Recursive)
        .with_context(|| format!("failed to watch {}", watch_dir.display()))?;

    run_tests(filter);

    while rx.recv().await.is_some() {
        // Debounce: drain any additional events that arrived during compilation
        tokio::time::sleep(std::time::Duration::from_millis(300)).await;
        while rx.try_recv().is_ok() {}

        eprintln!("\n--- File changed, re-running tests ---\n");
        run_tests(filter);
    }

    Ok(())
}

fn run_tests(filter: Option<&str>) {
    let start = std::time::Instant::now();
    let mut cmd = std::process::Command::new("cargo");
    cmd.arg("test");
    cmd.env("VICTAURI_E2E", "1");

    if let Some(f) = filter {
        cmd.arg("--test").arg(f);
    }

    let status = cmd.status();
    let elapsed = start.elapsed();

    match status {
        Ok(s) if s.success() => {
            eprintln!("\nAll tests passed ({:.1}s)", elapsed.as_secs_f64());
        }
        Ok(s) => {
            eprintln!(
                "\nTests failed (exit code: {}, {:.1}s)",
                s.code().unwrap_or(-1),
                elapsed.as_secs_f64()
            );
        }
        Err(e) => {
            eprintln!("\nFailed to run cargo test: {e}");
        }
    }

    if elapsed.as_secs() > 30 {
        eprintln!(
            "  Warning: test suite took >{:.0}s — consider splitting slow tests",
            elapsed.as_secs_f64()
        );
    }
}

fn detect_project(root: &Path) -> Result<(PathBuf, bool)> {
    let src_tauri = root.join("src-tauri");
    let cargo_toml = if src_tauri.join("Cargo.toml").exists() {
        src_tauri.join("Cargo.toml")
    } else if root.join("Cargo.toml").exists() {
        root.join("Cargo.toml")
    } else {
        bail!(
            "No Cargo.toml found in {} or {}/src-tauri/",
            root.display(),
            root.display()
        );
    };

    let content = std::fs::read_to_string(&cargo_toml).context("failed to read Cargo.toml")?;
    let is_tauri = content.contains("tauri");

    Ok((cargo_toml, is_tauri))
}

fn find_src_tauri(root: &Path) -> Option<PathBuf> {
    let p = root.join("src-tauri");
    if p.exists() { Some(p) } else { None }
}

fn add_dependencies(cargo_toml_path: &Path) -> Result<bool> {
    let content = std::fs::read_to_string(cargo_toml_path)?;
    let mut doc = content
        .parse::<toml_edit::DocumentMut>()
        .context("failed to parse Cargo.toml")?;

    let mut changed = false;

    if !has_dep(&doc, "dependencies", "victauri-plugin") {
        ensure_table(&mut doc, "dependencies");
        doc["dependencies"]["victauri-plugin"] = toml_edit::value(env!("CARGO_PKG_VERSION"));
        changed = true;
    }

    if !has_dep(&doc, "dev-dependencies", "victauri-test") {
        ensure_table(&mut doc, "dev-dependencies");
        doc["dev-dependencies"]["victauri-test"] = toml_edit::value(env!("CARGO_PKG_VERSION"));
        changed = true;
    }

    if changed {
        std::fs::write(cargo_toml_path, doc.to_string())?;
    }
    Ok(changed)
}

fn has_dep(doc: &toml_edit::DocumentMut, table: &str, dep: &str) -> bool {
    doc.get(table)
        .and_then(|t| t.as_table())
        .is_some_and(|t| t.contains_key(dep))
}

fn ensure_table(doc: &mut toml_edit::DocumentMut, key: &str) {
    if !doc.contains_key(key) {
        doc[key] = toml_edit::Item::Table(toml_edit::Table::new());
    }
}

fn generate_smoke_test() -> &'static str {
    r#"//! Victauri smoke tests — validates your Tauri app through the MCP bridge.
//!
//! Requires a running Tauri dev server.
//! Run with: VICTAURI_E2E=1 cargo test --test smoke

use victauri_test::VictauriClient;

fn skip_unless_e2e() -> bool {
    if !victauri_test::is_e2e() {
        eprintln!("Skipping: set VICTAURI_E2E=1 with your Tauri dev server running");
        return true;
    }
    false
}

#[tokio::test]
async fn connect_and_check_plugin_info() {
    if skip_unless_e2e() {
        return;
    }

    let mut client = VictauriClient::discover()
        .await
        .expect("Failed to connect — is your Tauri dev server running?");

    let info = client.get_plugin_info().await.unwrap();
    assert!(
        info.get("version").is_some(),
        "plugin_info should have version"
    );
    eprintln!("Connected to Victauri v{}", info["version"]);
}

#[tokio::test]
async fn screenshot_captures_window() {
    if skip_unless_e2e() {
        return;
    }

    let mut client = VictauriClient::discover().await.unwrap();
    let result = client.screenshot().await.unwrap();

    let has_image = result.get("image").is_some()
        || result.get("data").is_some()
        || result.get("base64").is_some()
        || result.pointer("/result/content/0/data").is_some();
    assert!(has_image, "screenshot should return image data");
    eprintln!("Screenshot captured successfully");
}

#[tokio::test]
async fn ipc_integrity_passes() {
    if skip_unless_e2e() {
        return;
    }

    let mut client = VictauriClient::discover().await.unwrap();
    let report = client
        .verify()
        .ipc_healthy()
        .no_console_errors()
        .run()
        .await
        .unwrap();

    for result in &report.results {
        eprintln!(
            "  [{}] {}",
            if result.passed { "PASS" } else { "FAIL" },
            result.description,
        );
    }
    assert!(
        report.all_passed(),
        "IPC integrity checks should pass: {:?}",
        report
            .failures()
            .iter()
            .map(|f| &f.description)
            .collect::<Vec<_>>()
    );
}
"#
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn smoke_test_content_compiles_as_valid_rust() {
        let content = generate_smoke_test();
        assert!(content.contains("VictauriClient"));
        assert!(content.contains("skip_unless_e2e"));
        assert!(content.contains("#[tokio::test]"));
        assert!(content.contains("VICTAURI_E2E=1"));
    }

    #[test]
    fn detect_project_finds_cargo_toml() {
        let dir = tempfile::tempdir().unwrap();
        let cargo = dir.path().join("Cargo.toml");
        let mut f = std::fs::File::create(&cargo).unwrap();
        writeln!(f, "[dependencies]\ntauri = \"2\"").unwrap();

        let (path, is_tauri) = detect_project(dir.path()).unwrap();
        assert_eq!(path, cargo);
        assert!(is_tauri);
    }

    #[test]
    fn detect_project_finds_src_tauri() {
        let dir = tempfile::tempdir().unwrap();
        let src_tauri = dir.path().join("src-tauri");
        std::fs::create_dir_all(&src_tauri).unwrap();
        let cargo = src_tauri.join("Cargo.toml");
        let mut f = std::fs::File::create(&cargo).unwrap();
        writeln!(f, "[dependencies]\ntauri = \"2\"").unwrap();

        let (path, is_tauri) = detect_project(dir.path()).unwrap();
        assert_eq!(path, cargo);
        assert!(is_tauri);
    }

    #[test]
    fn detect_project_not_tauri() {
        let dir = tempfile::tempdir().unwrap();
        let cargo = dir.path().join("Cargo.toml");
        let mut f = std::fs::File::create(&cargo).unwrap();
        writeln!(f, "[dependencies]\nserde = \"1\"").unwrap();

        let (_, is_tauri) = detect_project(dir.path()).unwrap();
        assert!(!is_tauri);
    }

    #[test]
    fn detect_project_missing_cargo_toml() {
        let dir = tempfile::tempdir().unwrap();
        assert!(detect_project(dir.path()).is_err());
    }

    #[test]
    fn add_deps_creates_sections() {
        let dir = tempfile::tempdir().unwrap();
        let cargo = dir.path().join("Cargo.toml");
        std::fs::write(
            &cargo,
            "[package]\nname = \"test-app\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();

        let added = add_dependencies(&cargo).unwrap();
        assert!(added);

        let content = std::fs::read_to_string(&cargo).unwrap();
        assert!(content.contains("victauri-plugin"));
        assert!(content.contains("victauri-test"));
    }

    #[test]
    fn add_deps_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        let cargo = dir.path().join("Cargo.toml");
        std::fs::write(
            &cargo,
            "[package]\nname = \"test-app\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();

        add_dependencies(&cargo).unwrap();
        let second = add_dependencies(&cargo).unwrap();
        assert!(!second, "second call should report no changes");
    }
}