sz-rust-cli 0.6.3

SZ-Rust 命令行工具:项目脚手架、数据库迁移、调度器管理
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
//! `make:*` 代码生成命令 — 对齐 PHP `think\console\command\make\*`
//!
//! ## PHP 对齐
//!
//! PHP `Make::execute()` 流程:
//! 1. `getArgument('name')` 获取类名
//! 2. `getClassName(name)` 处理 `@` 分隔应用名 + `/` 转 `\`
//! 3. `getPathName(className)` 剥离 `app\` 前缀 + `/` 替换 + `.php` 后缀
//! 4. 检查文件存在 → `mkdir` → `file_put_contents(buildClass())`
//! 5. `buildClass(name)` 读取 stub,替换占位符
//!
//! Rust 端对齐上述流程,但生成 `.rs` 文件而非 `.php` 文件。

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

use clap::Subcommand;

use crate::error::CliError;
use crate::stubs::{self, render_template};

/// `make` 子命令枚举
///
/// 对齐 PHP `think make:*` 命令组。
#[derive(Subcommand, Debug)]
pub enum MakeCommand {
    /// 生成 Model(对齐 `php think make:model User`)
    #[command(name = "model")]
    Model {
        /// 类名(如 `User` 或 `admin/User`)
        name: String,
    },

    /// 生成 Controller(对齐 `php think make:controller User`)
    #[command(name = "controller")]
    Controller {
        /// 类名
        name: String,
        /// 生成 API 风格控制器(5 方法,无 create/edit)
        #[arg(long)]
        api: bool,
        /// 生成空控制器
        #[arg(long)]
        plain: bool,
    },

    /// 生成迁移文件(对齐 Phinx `make:migration`)
    #[command(name = "migration")]
    Migration {
        /// 迁移名称(如 `create_users`)
        name: String,
        /// 迁移目录(默认 `migrations`)
        #[arg(short = 'p', long, default_value = "migrations")]
        path: String,
    },

    /// 生成填充文件(对齐 PHP `make:seeder`)
    ///
    /// 在 `seeds/` 目录下生成 `<name>.sql` 文件骨架。
    #[command(name = "seeder")]
    Seeder {
        /// 填充文件名称(如 `001_users_seed`)
        name: String,
        /// 填充目录(默认 `seeds`)
        #[arg(short = 'p', long, default_value = "seeds")]
        path: String,
    },

    /// 生成 Guard(sz-rust 自研,无 PHP 对应)
    #[command(name = "guard")]
    Guard {
        /// Guard 名称(如 `Admin`)
        name: String,
    },

    /// 生成验证器(对齐 PHP `make:validate`)
    ///
    /// 在 `app/validate/` 目录下生成 `<Name>.rs` 验证器骨架。
    #[command(name = "validate")]
    Validate {
        /// 验证器类名(如 `User` 或 `admin/User`)
        name: String,
    },

    /// 生成事件类(对齐 PHP `make:event`)
    ///
    /// 在 `app/event/` 目录下生成 `<Name>.rs` 事件骨架,包含事件名与负载构造方法。
    #[command(name = "event")]
    Event {
        /// 事件类名(如 `UserLogin` 或 `admin/UserLogin`)
        name: String,
    },

    /// 生成监听器(对齐 PHP `make:listener`)
    ///
    /// 在 `app/listener/` 目录下生成 `<Name>.rs` 监听器骨架,实现 `Listener` trait。
    #[command(name = "listener")]
    Listener {
        /// 监听器类名(如 `SendWelcomeEmail`)
        name: String,
        /// 监听的事件名(可选,默认使用类名)
        #[arg(long)]
        event: Option<String>,
    },

    /// 生成自定义命令(对齐 PHP `make:command`)
    ///
    /// 在 `app/command/` 目录下生成 `<Name>.rs` 命令骨架,实现 `Command` trait。
    #[command(name = "command")]
    Command {
        /// 命令类名(如 `SyncData`)
        name: String,
    },

    /// 生成服务类(对齐 PHP `make:service`)
    ///
    /// 在 `app/service/` 目录下生成 `<Name>.rs` 服务骨架,可注入容器。
    #[command(name = "service")]
    Service {
        /// 服务类名(如 `UserService`)
        name: String,
    },

    /// 生成中间件(sz-rust 自研,对齐 NestJS `make:middleware`)
    ///
    /// 在 `app/middleware/` 目录下生成 `<Name>.rs` 中间件骨架,
    /// 实现 `SzMiddleware` trait,可注册到中间件链。
    #[command(name = "middleware")]
    Middleware {
        /// 中间件类名(如 `CorsMiddleware` 或 `admin/RateLimit`)
        name: String,
    },

    /// 生成脚手架(Model + Controller + Migration)
    #[command(name = "scaffold")]
    Scaffold {
        /// 资源名称(如 `User`)
        name: String,
    },
}

/// 执行 make 子命令
pub fn execute(cmd: &MakeCommand) -> Result<(), CliError> {
    match cmd {
        MakeCommand::Model { name } => execute_make_model(name),
        MakeCommand::Controller { name, api, plain } => execute_make_controller(name, *api, *plain),
        MakeCommand::Migration { name, path } => execute_make_migration(name, path),
        MakeCommand::Seeder { name, path } => execute_make_seeder(name, path),
        MakeCommand::Guard { name } => execute_make_guard(name),
        MakeCommand::Validate { name } => execute_make_validate(name),
        MakeCommand::Event { name } => execute_make_event(name),
        MakeCommand::Listener { name, event } => execute_make_listener(name, event.as_deref()),
        MakeCommand::Command { name } => execute_make_command(name),
        MakeCommand::Service { name } => execute_make_service(name),
        MakeCommand::Middleware { name } => execute_make_middleware(name),
        MakeCommand::Scaffold { name } => execute_make_scaffold(name),
    }
}

/// 生成 Model 文件
///
/// 对齐 PHP `make:model`:读取 `model.stub`,替换占位符,写入 `app/model/{name}.rs`。
fn execute_make_model(name: &str) -> Result<(), CliError> {
    let (class_name, module_path, file_path) = resolve_target(name, "model");
    check_file_exists(&file_path)?;

    let namespace = format!("app::{}", module_path);
    let table_name = class_to_snake(&class_name);
    let content = render_template(
        stubs::MODEL_STUB,
        &[
            ("{%className%}", &class_name),
            ("{%namespace%}", &namespace),
            ("{%table_name%}", &table_name),
        ],
    );

    write_file(&file_path, &content)?;
    println!("Model created: {}", file_path.display());
    Ok(())
}

/// 生成 Controller 文件
///
/// 对齐 PHP `make:controller`:根据 `--api` / `--plain` 选择不同 stub。
fn execute_make_controller(name: &str, api: bool, plain: bool) -> Result<(), CliError> {
    let (class_name, module_path, file_path) = resolve_target(name, "controller");
    check_file_exists(&file_path)?;

    let namespace = format!("app::{}", module_path);
    let route = class_to_snake(&class_name);

    let template = if plain {
        stubs::CONTROLLER_PLAIN_STUB
    } else if api {
        stubs::CONTROLLER_API_STUB
    } else {
        stubs::CONTROLLER_STUB
    };

    let content = render_template(
        template,
        &[
            ("{%className%}", &class_name),
            ("{%namespace%}", &namespace),
            ("{%route%}", &route),
        ],
    );

    write_file(&file_path, &content)?;
    println!("Controller created: {}", file_path.display());
    Ok(())
}

/// 生成迁移文件
///
/// 对齐 Phinx 风格:生成 `{timestamp}_{name}_up.sql` 和 `{timestamp}_{name}_down.sql`。
fn execute_make_migration(name: &str, path: &str) -> Result<(), CliError> {
    let dir = Path::new(path);
    std::fs::create_dir_all(dir)?;

    let timestamp = chrono::Utc::now().format("%Y%m%d%H%M%S").to_string();
    let table_name = name_to_table(name);

    let up_file = dir.join(format!("{}_{}_up.sql", timestamp, name));
    let down_file = dir.join(format!("{}_{}_down.sql", timestamp, name));

    check_file_exists(&up_file)?;
    check_file_exists(&down_file)?;

    let up_content = render_template(
        stubs::MIGRATION_UP_STUB,
        &[
            ("{%name%}", name),
            ("{%timestamp%}", &timestamp),
            ("{%table_name%}", &table_name),
        ],
    );
    let down_content = render_template(
        stubs::MIGRATION_DOWN_STUB,
        &[
            ("{%name%}", name),
            ("{%timestamp%}", &timestamp),
            ("{%table_name%}", &table_name),
        ],
    );

    write_file(&up_file, &up_content)?;
    write_file(&down_file, &down_content)?;
    println!(
        "Migration created: {} & {}",
        up_file.display(),
        down_file.display()
    );
    Ok(())
}

/// 生成填充文件
///
/// 对齐 PHP `make:seeder`:在 `seeds/` 目录下生成 `<name>.sql` 文件骨架。
fn execute_make_seeder(name: &str, path: &str) -> Result<(), CliError> {
    let dir = Path::new(path);
    std::fs::create_dir_all(dir)?;

    let file_path = dir.join(format!("{}.sql", name));
    check_file_exists(&file_path)?;

    let timestamp = chrono::Utc::now()
        .format("%Y-%m-%d %H:%M:%S UTC")
        .to_string();
    let content = render_template(
        stubs::SEED_STUB,
        &[("{%name%}", name), ("{%timestamp%}", &timestamp)],
    );

    write_file(&file_path, &content)?;
    println!("Seeder created: {}", file_path.display());
    Ok(())
}

/// 生成 Guard 文件(sz-rust 自研)
fn execute_make_guard(name: &str) -> Result<(), CliError> {
    let (class_name, _module_path, file_path) = resolve_target(name, "guard");
    check_file_exists(&file_path)?;

    let content = format!(
        "//! Guard: {class_name}\n//!\n//! 由 `sz-rust make:guard` 生成。\n//!\n//! 对齐 NestJS Guard + Spring Security 模式。\n\nuse sz_rust_core::guard::Guard;\nuse sz_rust_core::request::Request;\n\n/// {class_name} Guard\npub struct {class_name};\n\nimpl Guard for {class_name} {{\n    async fn can_activate(&self, _req: &Request) -> bool {{\n        // 在此实现鉴权逻辑\n        true\n    }}\n}}\n"
    );

    write_file(&file_path, &content)?;
    println!("Guard created: {}", file_path.display());
    Ok(())
}

/// 生成验证器文件
///
/// 对齐 PHP `make:validate`:在 `app/validate/` 目录下生成 `<Name>.rs` 验证器骨架,
/// 包含 `Validate` 结构体初始化与常见规则示例。
fn execute_make_validate(name: &str) -> Result<(), CliError> {
    let (class_name, module_path, file_path) = resolve_target(name, "validate");
    check_file_exists(&file_path)?;

    let namespace = format!("app::{}", module_path);
    let content = render_template(
        stubs::VALIDATE_STUB,
        &[
            ("{%className%}", &class_name),
            ("{%namespace%}", &namespace),
        ],
    );

    write_file(&file_path, &content)?;
    println!("Validator created: {}", file_path.display());
    Ok(())
}

/// 生成事件类文件
///
/// 对齐 PHP `make:event`:读取 `event.stub`,替换占位符,写入 `app/event/{name}.rs`。
fn execute_make_event(name: &str) -> Result<(), CliError> {
    let (class_name, module_path, file_path) = resolve_target(name, "event");
    check_file_exists(&file_path)?;

    let namespace = format!("app::{}", module_path);
    // 事件名默认使用类名(对齐 PHP `make:event` 默认行为)
    let event_name = class_name.clone();
    let content = render_template(
        stubs::EVENT_STUB,
        &[
            ("{%className%}", &class_name),
            ("{%namespace%}", &namespace),
            ("{%event_name%}", &event_name),
        ],
    );

    write_file(&file_path, &content)?;
    println!("Event created: {}", file_path.display());
    Ok(())
}

/// 生成监听器文件
///
/// 对齐 PHP `make:listener`:读取 `listener.stub`,替换占位符,写入 `app/listener/{name}.rs`。
/// 若未指定 `--event`,事件名默认使用监听器类名。
fn execute_make_listener(name: &str, event: Option<&str>) -> Result<(), CliError> {
    let (class_name, module_path, file_path) = resolve_target(name, "listener");
    check_file_exists(&file_path)?;

    let namespace = format!("app::{}", module_path);
    // 事件名:优先使用 --event 参数,否则默认使用类名
    let event_name = event.unwrap_or(&class_name).to_string();
    let content = render_template(
        stubs::LISTENER_STUB,
        &[
            ("{%className%}", &class_name),
            ("{%namespace%}", &namespace),
            ("{%event_name%}", &event_name),
        ],
    );

    write_file(&file_path, &content)?;
    println!("Listener created: {}", file_path.display());
    Ok(())
}

/// 生成自定义命令文件
///
/// 对齐 PHP `make:command`:读取 `command.stub`,替换占位符,写入 `app/command/{name}.rs`。
/// 命令名默认为类名的 snake_case 形式(如 `SyncData` → `sync_data`)。
fn execute_make_command(name: &str) -> Result<(), CliError> {
    let (class_name, module_path, file_path) = resolve_target(name, "command");
    check_file_exists(&file_path)?;

    let namespace = format!("app::{}", module_path);
    // 命令名:snake_case(类名)(对齐 PHP `make:command` 默认行为)
    let command_name = class_to_snake(&class_name);
    let content = render_template(
        stubs::COMMAND_STUB,
        &[
            ("{%className%}", &class_name),
            ("{%namespace%}", &namespace),
            ("{%command_name%}", &command_name),
        ],
    );

    write_file(&file_path, &content)?;
    println!("Command created: {}", file_path.display());
    Ok(())
}

/// 生成服务类文件
///
/// 对齐 PHP `make:service`:读取 `service.stub`,替换占位符,写入 `app/service/{name}.rs`。
fn execute_make_service(name: &str) -> Result<(), CliError> {
    let (class_name, module_path, file_path) = resolve_target(name, "service");
    check_file_exists(&file_path)?;

    let namespace = format!("app::{}", module_path);
    let content = render_template(
        stubs::SERVICE_STUB,
        &[
            ("{%className%}", &class_name),
            ("{%namespace%}", &namespace),
        ],
    );

    write_file(&file_path, &content)?;
    println!("Service created: {}", file_path.display());
    Ok(())
}

/// 生成中间件文件(sz-rust 自研)
///
/// 在 `app/middleware/` 目录下生成 `<Name>.rs` 中间件骨架,
/// 实现 `SzMiddleware` trait,可注册到中间件链。
fn execute_make_middleware(name: &str) -> Result<(), CliError> {
    let (class_name, module_path, file_path) = resolve_target(name, "middleware");
    check_file_exists(&file_path)?;

    let namespace = format!("app::{}", module_path);
    let content = render_template(
        stubs::MIDDLEWARE_STUB,
        &[
            ("{%className%}", &class_name),
            ("{%namespace%}", &namespace),
        ],
    );

    write_file(&file_path, &content)?;
    println!("Middleware created: {}", file_path.display());
    Ok(())
}

/// 生成脚手架(Model + Controller + Migration)
fn execute_make_scaffold(name: &str) -> Result<(), CliError> {
    println!("Scaffolding for: {}", name);
    execute_make_model(name)?;
    execute_make_controller(name, false, false)?;
    execute_make_migration(&class_to_snake(name), "migrations")?;
    println!("Scaffold complete.");
    Ok(())
}

// ============================================================================
// 辅助函数(对齐 PHP Make 基类方法)
// ============================================================================

/// 解析目标(类名 + 模块路径 + 文件路径)
///
/// 对齐 PHP `Make::getClassName()` + `getPathName()`:
///
/// - `User` → (`User`, `model`, `app/model/User.rs`)
/// - `admin/User` → (`User`, `controller::admin`, `app/controller/admin/User.rs`)
/// - `admin@User` → (`User`, `admin::model`, `app/admin/model/User.rs`)
fn resolve_target(name: &str, layer: &str) -> (String, String, PathBuf) {
    // 处理 @ 分隔应用名(对齐 PHP getClassName)
    let (app, class_part) = if let Some(idx) = name.find('@') {
        (&name[..idx], &name[idx + 1..])
    } else {
        ("", name)
    };

    // 处理 / 分隔子目录(对齐 PHP / → \)
    let segments: Vec<&str> = class_part.split('/').collect();
    let class_name = segments.last().unwrap_or(&"").to_string();

    // 构建模块路径
    let parent_segments: Vec<&str> = if segments.len() > 1 {
        segments[..segments.len() - 1].to_vec()
    } else {
        Vec::new()
    };

    let module_path = if app.is_empty() {
        if parent_segments.is_empty() {
            layer.to_string()
        } else {
            format!("{}::{}", layer, parent_segments.join("::"))
        }
    } else if parent_segments.is_empty() {
        format!("{}::{}", app, layer)
    } else {
        format!("{}::{}::{}", app, layer, parent_segments.join("::"))
    };

    // 构建文件路径(对齐 PHP getPathName:app\ → app/ + / 替换)
    let mut path = PathBuf::from("app");
    if !app.is_empty() {
        path.push(app);
    }
    // 添加层目录
    path.push(layer);
    // 添加子目录
    for seg in &parent_segments {
        path.push(seg);
    }
    // 添加文件名
    path.push(format!("{}.rs", class_name));

    (class_name, module_path, path)
}

/// 检查文件是否已存在
///
/// 对齐 PHP `Make::execute()` 中 `already exists!` 提示。
fn check_file_exists(path: &Path) -> Result<(), CliError> {
    if path.exists() {
        return Err(CliError::FileExists(path.display().to_string()));
    }
    Ok(())
}

/// 写入文件(自动创建父目录)
///
/// 对齐 PHP `mkdir` + `file_put_contents`。
fn write_file(path: &Path, content: &str) -> Result<(), CliError> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(path, content)?;
    Ok(())
}

/// 类名转 snake_case 表名
///
/// `User` → `user`,`OrderItem` → `order_item`
fn class_to_snake(s: &str) -> String {
    let mut result = String::new();
    for (i, ch) in s.chars().enumerate() {
        if ch.is_uppercase() && i > 0 {
            result.push('_');
        }
        result.push(ch.to_lowercase().next().unwrap_or(ch));
    }
    result
}

/// 迁移名转表名
///
/// `create_users` → `users`,`add_index_to_orders` → `orders`
fn name_to_table(name: &str) -> String {
    // 简单提取:create_xxx → xxx
    if let Some(rest) = name.strip_prefix("create_") {
        return rest.to_string();
    }
    if let Some(rest) = name.strip_prefix("add_") {
        // add_index_to_orders → orders
        if let Some(to_pos) = rest.find("_to_") {
            return rest[to_pos + 4..].to_string();
        }
        return rest.to_string();
    }
    name.to_string()
}

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

    /// RAII 守卫:在作用域结束时恢复原始工作目录。
    ///
    /// 即使测试 panic 也能保证恢复,避免污染后续测试。
    /// 配合 `super::super::test_support::acquire_global_lock()` 使用,
    /// 确保与 optimize 模块测试的 set_current_dir 互斥。
    struct CwdGuard {
        original: Option<PathBuf>,
    }

    impl CwdGuard {
        /// 切换到 `new_dir` 并返回守卫。守卫 drop 时恢复原目录。
        fn switch(new_dir: &Path) -> std::io::Result<Self> {
            let original = std::env::current_dir().ok();
            std::env::set_current_dir(new_dir)?;
            Ok(Self { original })
        }
    }

    impl Drop for CwdGuard {
        fn drop(&mut self) {
            if let Some(ref orig) = self.original {
                let _ = std::env::set_current_dir(orig);
            }
        }
    }

    #[test]
    fn test_resolve_target_simple_model() {
        let (class, module, path) = resolve_target("User", "model");
        assert_eq!(class, "User");
        assert_eq!(module, "model");
        assert_eq!(path, PathBuf::from("app/model/User.rs"));
    }

    #[test]
    fn test_resolve_target_nested_controller() {
        let (class, module, path) = resolve_target("admin/User", "controller");
        assert_eq!(class, "User");
        assert_eq!(module, "controller::admin");
        assert_eq!(path, PathBuf::from("app/controller/admin/User.rs"));
    }

    #[test]
    fn test_resolve_target_with_app() {
        let (class, module, _path) = resolve_target("admin@User", "model");
        assert_eq!(class, "User");
        assert_eq!(module, "admin::model");
    }

    #[test]
    fn test_class_to_snake() {
        assert_eq!(class_to_snake("User"), "user");
        assert_eq!(class_to_snake("OrderItem"), "order_item");
        assert_eq!(class_to_snake("API"), "a_p_i");
    }

    #[test]
    fn test_name_to_table_create() {
        assert_eq!(name_to_table("create_users"), "users");
        assert_eq!(name_to_table("create_orders"), "orders");
    }

    #[test]
    fn test_name_to_table_add() {
        assert_eq!(name_to_table("add_index_to_orders"), "orders");
        assert_eq!(name_to_table("add_status"), "status");
    }

    #[test]
    fn test_name_to_table_other() {
        assert_eq!(name_to_table("custom_migration"), "custom_migration");
    }

    #[test]
    fn test_check_file_exists_nonexistent() {
        let result = check_file_exists(Path::new("/nonexistent/path/file.txt"));
        assert!(result.is_ok());
    }

    #[test]
    fn test_check_file_exists_existing() {
        // 使用临时文件确保文件确实存在
        let temp = tempfile::NamedTempFile::new().unwrap();
        let result = check_file_exists(temp.path());
        assert!(matches!(result, Err(CliError::FileExists(_))));
    }

    #[test]
    fn test_write_and_read_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");

        write_file(&file_path, "test content").unwrap();
        assert_eq!(std::fs::read_to_string(&file_path).unwrap(), "test content");
    }

    #[test]
    fn test_execute_make_migration_creates_files() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().to_str().unwrap();

        execute_make_migration("create_test_table", path).unwrap();

        let entries: Vec<_> = std::fs::read_dir(path).unwrap().collect();
        assert_eq!(entries.len(), 2); // up + down

        let mut has_up = false;
        let mut has_down = false;
        for entry in entries {
            let name = entry.unwrap().file_name();
            let name = name.to_string_lossy();
            if name.ends_with("_up.sql") {
                has_up = true;
            }
            if name.ends_with("_down.sql") {
                has_down = true;
            }
        }
        assert!(has_up);
        assert!(has_down);
    }

    #[test]
    fn test_execute_make_model_in_temp() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_model("TestUser").unwrap();

        let model_path = temp_dir.path().join("app/model/TestUser.rs");
        assert!(model_path.exists());

        let content = std::fs::read_to_string(&model_path).unwrap();
        assert!(content.contains("TestUser"));
        assert!(content.contains("test_user"));
    }

    #[test]
    fn test_execute_make_seeder_creates_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().to_str().unwrap();

        execute_make_seeder("001_test_seed", path).unwrap();

        let seed_path = Path::new(path).join("001_test_seed.sql");
        assert!(seed_path.exists());

        let content = std::fs::read_to_string(&seed_path).unwrap();
        assert!(content.contains("001_test_seed"));
        assert!(content.contains("-- Seed:"));
        // 渲染后不应残留占位符
        assert!(!content.contains("{%"));
    }

    #[test]
    fn test_execute_make_seeder_file_already_exists() {
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().to_str().unwrap();

        // 第一次创建应成功
        execute_make_seeder("001_dup_seed", path).unwrap();
        // 第二次创建同名文件应失败
        let result = execute_make_seeder("001_dup_seed", path);
        assert!(matches!(result, Err(CliError::FileExists(_))));
    }

    #[test]
    fn test_execute_make_seeder_creates_directory() {
        let temp_dir = tempfile::tempdir().unwrap();
        let nested = temp_dir.path().join("nested").join("seeds");
        let path = nested.to_str().unwrap();

        // 目录不存在时应自动创建
        execute_make_seeder("001_seed", path).unwrap();
        assert!(nested.exists());
    }

    #[test]
    fn test_make_validate_creates_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        // 通过顶层 execute 入口调用 make validate(验证命令分发正常)
        let cmd = MakeCommand::Validate {
            name: "Order".to_string(),
        };
        execute(&cmd).unwrap();

        let validate_path = temp_dir.path().join("app/validate/Order.rs");
        assert!(validate_path.exists());

        let content = std::fs::read_to_string(&validate_path).unwrap();
        // 验证器 struct 与 Validate 引用
        assert!(content.contains("pub struct OrderValidate;"));
        assert!(content.contains("use sz_rust_core::validate::Validate"));
        assert!(content.contains("app::validate"));
        // 渲染后不应残留占位符
        assert!(!content.contains("{%"));
    }

    #[test]
    fn test_validate_stub_contains_required_elements() {
        // 验证 stub 模板包含必要元素:struct 定义、Validate 引用、占位符
        assert!(stubs::VALIDATE_STUB.contains("pub struct {%className%}Validate;"));
        assert!(stubs::VALIDATE_STUB.contains("use sz_rust_core::validate::Validate"));
        assert!(stubs::VALIDATE_STUB.contains("impl {%className%}Validate"));
        assert!(stubs::VALIDATE_STUB.contains("pub fn new() -> Validate"));
        assert!(stubs::VALIDATE_STUB.contains("{%className%}"));
        assert!(stubs::VALIDATE_STUB.contains("{%namespace%}"));
    }

    #[test]
    fn test_execute_make_validate_creates_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_validate("User").unwrap();

        let validate_path = temp_dir.path().join("app/validate/User.rs");
        assert!(validate_path.exists());

        let content = std::fs::read_to_string(&validate_path).unwrap();
        assert!(content.contains("UserValidate"));
        assert!(content.contains("use sz_rust_core::validate::Validate"));
        assert!(content.contains("app::validate"));
        // 渲染后不应残留占位符
        assert!(!content.contains("{%"));
    }

    #[test]
    fn test_execute_make_validate_file_already_exists() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        // 第一次创建应成功
        execute_make_validate("User").unwrap();
        // 第二次创建同名文件应失败
        let result = execute_make_validate("User");
        assert!(matches!(result, Err(CliError::FileExists(_))));
    }

    #[test]
    fn test_execute_make_validate_nested_path() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        // 嵌套路径:admin/User → app/validate/admin/User.rs
        execute_make_validate("admin/User").unwrap();

        let validate_path = temp_dir.path().join("app/validate/admin/User.rs");
        assert!(validate_path.exists());

        let content = std::fs::read_to_string(&validate_path).unwrap();
        assert!(content.contains("UserValidate"));
        assert!(content.contains("app::validate::admin"));
    }

    // ---------- make event/listener/command/service 测试 ----------

    #[test]
    fn test_execute_make_event_creates_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_event("UserLogin").unwrap();

        let event_path = temp_dir.path().join("app/event/UserLogin.rs");
        assert!(event_path.exists());

        let content = std::fs::read_to_string(&event_path).unwrap();
        assert!(content.contains("pub struct UserLogin;"));
        assert!(content.contains("app::event"));
        assert!(content.contains("UserLogin"));
        assert!(content.contains("use serde_json::Value"));
        // 渲染后不应残留占位符
        assert!(!content.contains("{%"));
    }

    #[test]
    fn test_execute_make_event_file_already_exists() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_event("UserLogin").unwrap();
        let result = execute_make_event("UserLogin");
        assert!(matches!(result, Err(CliError::FileExists(_))));
    }

    #[test]
    fn test_execute_make_event_nested_path() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_event("admin/UserLogin").unwrap();

        let event_path = temp_dir.path().join("app/event/admin/UserLogin.rs");
        assert!(event_path.exists());

        let content = std::fs::read_to_string(&event_path).unwrap();
        assert!(content.contains("app::event::admin"));
    }

    #[test]
    fn test_execute_make_listener_default_event_name() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        // 未指定 --event,事件名默认使用类名
        execute_make_listener("SendWelcomeEmail", None).unwrap();

        let listener_path = temp_dir.path().join("app/listener/SendWelcomeEmail.rs");
        assert!(listener_path.exists());

        let content = std::fs::read_to_string(&listener_path).unwrap();
        assert!(content.contains("pub struct SendWelcomeEmail;"));
        assert!(content.contains("app::listener"));
        assert!(content.contains("use sz_rust_core::event::{EventError, Listener}"));
        assert!(content.contains("impl Listener for SendWelcomeEmail"));
        // 默认事件名 = 类名
        assert!(content.contains(r#""SendWelcomeEmail""#));
        assert!(!content.contains("{%"));
    }

    #[test]
    fn test_execute_make_listener_custom_event_name() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        // 指定 --event UserLogin
        execute_make_listener("SendWelcomeEmail", Some("UserLogin")).unwrap();

        let listener_path = temp_dir.path().join("app/listener/SendWelcomeEmail.rs");
        assert!(listener_path.exists());

        let content = std::fs::read_to_string(&listener_path).unwrap();
        assert!(content.contains(r#""UserLogin""#));
        assert!(!content.contains(r#""SendWelcomeEmail""#));
        assert!(!content.contains("{%"));
    }

    #[test]
    fn test_execute_make_listener_file_already_exists() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_listener("SendWelcomeEmail", None).unwrap();
        let result = execute_make_listener("SendWelcomeEmail", None);
        assert!(matches!(result, Err(CliError::FileExists(_))));
    }

    #[test]
    fn test_execute_make_command_creates_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_command("SyncData").unwrap();

        let command_path = temp_dir.path().join("app/command/SyncData.rs");
        assert!(command_path.exists());

        let content = std::fs::read_to_string(&command_path).unwrap();
        assert!(content.contains("pub struct SyncData;"));
        assert!(content.contains("app::command"));
        assert!(content.contains("use sz_rust_cli::console::{Command, CommandSignature}"));
        assert!(content.contains("impl Command for SyncData"));
        // 命令名应为 snake_case:sync_data
        assert!(content.contains(r#""sync_data""#));
        assert!(!content.contains("{%"));
    }

    #[test]
    fn test_execute_make_command_file_already_exists() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_command("SyncData").unwrap();
        let result = execute_make_command("SyncData");
        assert!(matches!(result, Err(CliError::FileExists(_))));
    }

    #[test]
    fn test_execute_make_command_nested_path() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_command("admin/SyncData").unwrap();

        let command_path = temp_dir.path().join("app/command/admin/SyncData.rs");
        assert!(command_path.exists());

        let content = std::fs::read_to_string(&command_path).unwrap();
        assert!(content.contains("app::command::admin"));
    }

    #[test]
    fn test_execute_make_service_creates_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_service("UserService").unwrap();

        let service_path = temp_dir.path().join("app/service/UserService.rs");
        assert!(service_path.exists());

        let content = std::fs::read_to_string(&service_path).unwrap();
        assert!(content.contains("pub struct UserService;"));
        assert!(content.contains("app::service"));
        assert!(content.contains("impl Default for UserService"));
        assert!(content.contains("pub fn new() -> Self"));
        assert!(!content.contains("{%"));
    }

    #[test]
    fn test_execute_make_service_file_already_exists() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_service("UserService").unwrap();
        let result = execute_make_service("UserService");
        assert!(matches!(result, Err(CliError::FileExists(_))));
    }

    #[test]
    fn test_execute_make_service_nested_path() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _lock = super::super::test_support::acquire_global_lock();
        let _guard = CwdGuard::switch(temp_dir.path()).unwrap();

        execute_make_service("admin/UserService").unwrap();

        let service_path = temp_dir.path().join("app/service/admin/UserService.rs");
        assert!(service_path.exists());

        let content = std::fs::read_to_string(&service_path).unwrap();
        assert!(content.contains("app::service::admin"));
    }
}