subx-cli 1.7.4

AI subtitle processing CLI tool, which automatically matches, renames, and converts subtitle files.
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
//! Test macros for convenient configuration service testing.
//!
//! This module provides convenient macros for creating test configurations
//! and running tests with specific configuration services.

/// Run a test with a custom configuration builder.
///
/// This macro takes a configuration builder and a test closure,
/// creates a configuration service, and runs the test with it.
///
/// # Examples
///
/// ```rust
/// use subx_cli::{test_with_config, config::{TestConfigBuilder, ConfigService}};
///
/// test_with_config!(
///     TestConfigBuilder::new().with_ai_provider("openai"),
///     |config_service: &dyn ConfigService| {
///         let config = config_service.get_config().unwrap();
///         assert_eq!(config.ai.provider, "openai");
///     }
/// );
/// ```
#[macro_export]
macro_rules! test_with_config {
    ($config_builder:expr, $test:expr) => {{
        let config_service = $config_builder.build_service();
        $test(&config_service)
    }};
}

/// Execute ProductionConfigService tests with specified environment variable mapping.
///
/// This macro creates a TestEnvironmentProvider, sets the specified environment variables,
/// then uses that provider to create a ProductionConfigService for testing.
///
/// # Arguments
/// * `$env_vars` - Environment variable mapping expression (HashMap<&str, &str>)
/// * `$test` - Test closure that receives a ProductionConfigService reference
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::{test_production_config_with_env, std::collections::HashMap};
///
/// let env_vars = [
///     ("OPENAI_API_KEY", "sk-test-key"),
///     ("OPENAI_BASE_URL", "https://test.api.com/v1")
/// ].iter().cloned().collect::<HashMap<_, _>>();
///
/// test_production_config_with_env!(env_vars, |service| {
///     let config = service.get_config().unwrap();
///     assert_eq!(config.ai.api_key, Some("sk-test-key".to_string()));
///     assert_eq!(config.ai.base_url, "https://test.api.com/v1");
/// });
/// ```
#[macro_export]
macro_rules! test_production_config_with_env {
    ($env_vars:expr, $test:expr) => {{
        use std::sync::Arc;

        let mut env_provider = $crate::config::TestEnvironmentProvider::new();

        // Set a non-existent config path to avoid interference from existing config files
        env_provider.set_var(
            "SUBX_CONFIG_PATH",
            "/tmp/test_config_macro_does_not_exist.toml",
        );

        // Convert environment variable mapping to strings and set them in the provider
        for (key, value) in $env_vars {
            env_provider.set_var(key, value);
        }

        let service =
            $crate::config::ProductionConfigService::with_env_provider(Arc::new(env_provider))
                .expect("Failed to create ProductionConfigService with environment provider");

        $test(&service)
    }};
}

/// Execute ProductionConfigService tests with OPENAI environment variables.
///
/// This macro is a convenience version of test_production_config_with_env!,
/// specifically designed for testing OPENAI_API_KEY and OPENAI_BASE_URL environment variables.
///
/// # Arguments
/// * `$api_key` - OPENAI_API_KEY value (Option<&str>)
/// * `$base_url` - OPENAI_BASE_URL value (Option<&str>)  
/// * `$test` - Test closure that receives a ProductionConfigService reference
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::test_production_config_with_openai_env;
///
/// test_production_config_with_openai_env!(
///     Some("sk-test-key"),
///     Some("https://test.api.com/v1"),
///     |service| {
///         let config = service.get_config().unwrap();
///         assert_eq!(config.ai.api_key, Some("sk-test-key".to_string()));
///         assert_eq!(config.ai.base_url, "https://test.api.com/v1");
///     }
/// );
/// ```
#[macro_export]
macro_rules! test_production_config_with_openai_env {
    ($api_key:expr, $base_url:expr, $test:expr) => {{
        use std::sync::Arc;

        let mut env_provider = $crate::config::TestEnvironmentProvider::new();

        // Set a non-existent config path to avoid interference from existing config files
        env_provider.set_var(
            "SUBX_CONFIG_PATH",
            "/tmp/test_config_openai_macro_does_not_exist.toml",
        );

        // Set OPENAI_API_KEY (if provided)
        if let Some(api_key) = $api_key {
            env_provider.set_var("OPENAI_API_KEY", api_key);
        }

        // Set OPENAI_BASE_URL (if provided)
        if let Some(base_url) = $base_url {
            env_provider.set_var("OPENAI_BASE_URL", base_url);
        }

        let service =
            $crate::config::ProductionConfigService::with_env_provider(Arc::new(env_provider))
                .expect(
                    "Failed to create ProductionConfigService with OPENAI environment variables",
                );

        $test(&service)
    }};
}

/// Create a temporary ProductionConfigService with environment variable provider for test functions.
///
/// This macro creates a ProductionConfigService variable with specified environment variables
/// that can be used throughout the entire test function.
///
/// # Arguments
/// * `$service_name` - Service variable name
/// * `$env_vars` - Environment variable mapping expression (HashMap<&str, &str>)
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::create_production_config_service_with_env;
///
/// fn my_test() {
///     let env_vars = [("OPENAI_API_KEY", "sk-test")].iter().cloned().collect();
///     create_production_config_service_with_env!(service, env_vars);
///
///     let config = service.get_config().unwrap();
///     assert_eq!(config.ai.api_key, Some("sk-test".to_string()));
/// }
/// ```
#[macro_export]
macro_rules! create_production_config_service_with_env {
    ($service_name:ident, $env_vars:expr) => {
        use std::sync::Arc;

        let mut env_provider = $crate::config::TestEnvironmentProvider::new();

        // Set a non-existent config path to avoid interference from existing config files
        env_provider.set_var(
            "SUBX_CONFIG_PATH",
            "/tmp/test_config_create_macro_does_not_exist.toml",
        );

        for (key, value) in $env_vars {
            env_provider.set_var(key, value);
        }

        let $service_name =
            $crate::config::ProductionConfigService::with_env_provider(Arc::new(env_provider))
                .expect("Failed to create ProductionConfigService with environment provider");
    };
}

/// Create a ProductionConfigService with empty environment variables for testing.
///
/// This macro creates a ProductionConfigService without any environment variables,
/// used for testing default behavior.
///
/// # Arguments
/// * `$service_name` - Service variable name
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::create_production_config_service_with_empty_env;
///
/// fn my_test() {
///     create_production_config_service_with_empty_env!(service);
///
///     let config = service.get_config().unwrap();
///     assert_eq!(config.ai.api_key, None); // Expected no API key
/// }
/// ```
#[macro_export]
macro_rules! create_production_config_service_with_empty_env {
    ($service_name:ident) => {
        create_production_config_service_with_env!($service_name, std::collections::HashMap::new())
    };
}

#[cfg(test)]
mod env_macro_tests {
    use crate::config::service::ConfigService;
    use std::collections::HashMap;

    #[test]
    fn test_production_config_with_env_macro() {
        let env_vars: HashMap<&str, &str> = [
            ("OPENAI_API_KEY", "sk-macro-test"),
            ("OPENAI_BASE_URL", "https://macro.test.com/v1"),
        ]
        .iter()
        .cloned()
        .collect();

        test_production_config_with_env!(
            env_vars,
            |service: &crate::config::ProductionConfigService| {
                let config = service.get_config().unwrap();
                assert_eq!(config.ai.api_key, Some("sk-macro-test".to_string()));
                assert_eq!(config.ai.base_url, "https://macro.test.com/v1");
            }
        );
    }

    #[test]
    fn test_production_config_with_openai_env_macro_both() {
        test_production_config_with_openai_env!(
            Some("sk-openai-macro"),
            Some("https://openai.macro.com/v1"),
            |service: &crate::config::ProductionConfigService| {
                let config = service.get_config().unwrap();
                assert_eq!(config.ai.api_key, Some("sk-openai-macro".to_string()));
                assert_eq!(config.ai.base_url, "https://openai.macro.com/v1");
            }
        );
    }

    #[test]
    fn test_production_config_with_openai_env_macro_api_key_only() {
        test_production_config_with_openai_env!(
            Some("sk-only-key"),
            None,
            |service: &crate::config::ProductionConfigService| {
                let config = service.get_config().unwrap();
                assert_eq!(config.ai.api_key, Some("sk-only-key".to_string()));
                // base_url should use default value
                assert_eq!(config.ai.base_url, "https://api.openai.com/v1");
            }
        );
    }

    #[test]
    fn test_production_config_with_openai_env_macro_base_url_only() {
        test_production_config_with_openai_env!(
            None,
            Some("https://only-url.com/v1"),
            |service: &crate::config::ProductionConfigService| {
                let config = service.get_config().unwrap();
                assert_eq!(config.ai.api_key, None);
                assert_eq!(config.ai.base_url, "https://only-url.com/v1");
            }
        );
    }

    #[test]
    fn test_production_config_with_openai_env_macro_empty() {
        test_production_config_with_openai_env!(
            None,
            None,
            |service: &crate::config::ProductionConfigService| {
                let config = service.get_config().unwrap();
                assert_eq!(config.ai.api_key, None);
                assert_eq!(config.ai.base_url, "https://api.openai.com/v1");
            }
        );
    }

    #[test]
    fn test_create_production_config_service_with_env_macro() {
        let env_vars: HashMap<&str, &str> = [("OPENAI_API_KEY", "sk-create-macro")]
            .iter()
            .cloned()
            .collect();

        create_production_config_service_with_env!(service, env_vars);

        let config = service.get_config().unwrap();
        assert_eq!(config.ai.api_key, Some("sk-create-macro".to_string()));
    }

    #[test]
    fn test_create_production_config_service_with_empty_env_macro() {
        create_production_config_service_with_empty_env!(service);

        let config = service.get_config().unwrap();
        assert_eq!(config.ai.api_key, None);
        assert_eq!(config.ai.base_url, "https://api.openai.com/v1");
    }
}

/// Run a test with the default configuration.
///
/// This macro creates a test configuration service with default settings
/// and runs the provided test closure with it.
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::{test_with_default_config, config::ConfigService};
///
/// test_with_default_config!(|config_service: &dyn ConfigService| {
///     let config = config_service.get_config().unwrap();
///     assert_eq!(config.ai.provider, "openai");
/// });
/// ```
#[macro_export]
macro_rules! test_with_default_config {
    ($test:expr) => {
        test_with_config!($crate::config::TestConfigBuilder::new(), $test)
    };
}

/// Run a test with specific AI configuration.
///
/// This macro creates a test configuration service with the specified
/// AI provider and model, then runs the test closure.
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::{test_with_ai_config, config::ConfigService};
///
/// test_with_ai_config!("anthropic", "claude-3", |config_service: &dyn ConfigService| {
///     let config = config_service.get_config().unwrap();
///     assert_eq!(config.ai.provider, "anthropic");
///     assert_eq!(config.ai.model, "claude-3");
/// });
/// ```
#[macro_export]
macro_rules! test_with_ai_config {
    ($provider:expr, $model:expr, $test:expr) => {
        test_with_config!(
            $crate::config::TestConfigBuilder::new()
                .with_ai_provider($provider)
                .with_ai_model($model),
            $test
        )
    };
}

/// Run a test with specific AI configuration including API key.
///
/// This macro creates a test configuration service with the specified
/// AI provider, model, and API key, then runs the test closure.
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::{test_with_ai_config_and_key, config::ConfigService};
///
/// test_with_ai_config_and_key!("openai", "gpt-4.1", "test-key", |config_service: &dyn ConfigService| {
///     let config = config_service.get_config().unwrap();
///     assert_eq!(config.ai.provider, "openai");
///     assert_eq!(config.ai.model, "gpt-4.1");
///     assert_eq!(config.ai.api_key, Some("test-key".to_string()));
/// });
/// ```
#[macro_export]
macro_rules! test_with_ai_config_and_key {
    ($provider:expr, $model:expr, $api_key:expr, $test:expr) => {
        test_with_config!(
            $crate::config::TestConfigBuilder::new()
                .with_ai_provider($provider)
                .with_ai_model($model)
                .with_ai_api_key($api_key),
            $test
        )
    };
}

/// Run a test with specific sync configuration.
///
/// This macro creates a test configuration service with the specified
/// synchronization parameters, then runs the test closure.
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::{test_with_sync_config, config::ConfigService};
///
/// test_with_sync_config!(0.8, 45.0, |config_service: &dyn ConfigService| {
///     let config = config_service.get_config().unwrap();
///     assert_eq!(config.sync.correlation_threshold, 0.8);
///     assert_eq!(config.sync.max_offset_seconds, 45.0);
/// });
/// ```
#[macro_export]
macro_rules! test_with_sync_config {
    ($threshold:expr, $max_offset:expr, $test:expr) => {
        test_with_config!(
            $crate::config::TestConfigBuilder::new()
                .with_vad_sensitivity($threshold)
                .with_sync_method("vad"),
            $test
        )
    };
}

/// Run a test with specific parallel processing configuration.
///
/// This macro creates a test configuration service with the specified
/// parallel processing parameters, then runs the test closure.
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::{test_with_parallel_config, config::ConfigService};
///
/// test_with_parallel_config!(8, 200, |config_service: &dyn ConfigService| {
///     let config = config_service.get_config().unwrap();
///     assert_eq!(config.general.max_concurrent_jobs, 8);
///     assert_eq!(config.parallel.task_queue_size, 200);
/// });
/// ```
#[macro_export]
macro_rules! test_with_parallel_config {
    ($max_jobs:expr, $queue_size:expr, $test:expr) => {
        test_with_config!(
            $crate::config::TestConfigBuilder::new()
                .with_max_concurrent_jobs($max_jobs)
                .with_task_queue_size($queue_size),
            $test
        )
    };
}

/// Create a temporary test configuration service for use in test functions.
///
/// This macro creates a configuration service variable that can be used
/// throughout a test function.
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::create_test_config_service;
///
/// fn my_test() {
///     create_test_config_service!(service, TestConfigBuilder::new().with_ai_provider("openai"));
///     
///     let config = service.get_config().unwrap();
///     assert_eq!(config.ai.provider, "openai");
/// }
/// ```
#[macro_export]
macro_rules! create_test_config_service {
    ($service_name:ident, $config_builder:expr) => {
        let $service_name = $config_builder.build_service();
    };
}

/// Create a temporary test configuration service with default settings.
///
/// This macro creates a configuration service variable with default settings
/// that can be used throughout a test function.
///
/// # Examples
///
/// ```rust,ignore
/// use subx_cli::create_default_test_config_service;
///
/// fn my_test() {
///     create_default_test_config_service!(service);
///     
///     let config = service.get_config().unwrap();
///     assert_eq!(config.ai.provider, "openai");
/// }
/// ```
#[macro_export]
macro_rules! create_default_test_config_service {
    ($service_name:ident) => {
        create_test_config_service!($service_name, $crate::config::TestConfigBuilder::new());
    };
}

#[cfg(test)]
mod tests {
    use crate::config::{ConfigService, TestConfigBuilder};

    #[test]
    fn test_macro_with_config() {
        test_with_config!(
            TestConfigBuilder::new().with_ai_provider("test_provider"),
            |config_service: &crate::config::TestConfigService| {
                let config = config_service.get_config().unwrap();
                assert_eq!(config.ai.provider, "test_provider");
            }
        );
    }

    #[test]
    fn test_macro_with_default_config() {
        test_with_default_config!(|config_service: &crate::config::TestConfigService| {
            let config = config_service.get_config().unwrap();
            assert_eq!(config.ai.provider, "openai");
        });
    }

    #[test]
    fn test_macro_with_ai_config() {
        test_with_ai_config!(
            "anthropic",
            "claude-3",
            |config_service: &crate::config::TestConfigService| {
                let config = config_service.get_config().unwrap();
                assert_eq!(config.ai.provider, "anthropic");
                assert_eq!(config.ai.model, "claude-3");
            }
        );
    }

    #[test]
    fn test_macro_with_ai_config_and_key() {
        test_with_ai_config_and_key!(
            "openai",
            "gpt-4.1",
            "test-key",
            |config_service: &crate::config::TestConfigService| {
                let config = config_service.get_config().unwrap();
                assert_eq!(config.ai.provider, "openai");
                assert_eq!(config.ai.model, "gpt-4.1");
                assert_eq!(config.ai.api_key, Some("test-key".to_string()));
            }
        );
    }

    #[test]
    fn test_macro_with_sync_config() {
        test_with_sync_config!(
            0.9,
            60.0,
            |config_service: &crate::config::TestConfigService| {
                let config = config_service.config();
                // Test new sync configuration structure
                assert_eq!(config.sync.vad.sensitivity, 0.9);
                assert_eq!(config.sync.default_method, "vad");
            }
        );
    }

    #[test]
    fn test_macro_with_parallel_config() {
        test_with_parallel_config!(
            16,
            500,
            |config_service: &crate::config::TestConfigService| {
                let config = config_service.get_config().unwrap();
                assert_eq!(config.general.max_concurrent_jobs, 16);
                assert_eq!(config.parallel.task_queue_size, 500);
            }
        );
    }

    #[test]
    fn test_create_test_config_service_macro() {
        create_test_config_service!(
            service,
            TestConfigBuilder::new().with_ai_provider("macro_test")
        );

        let config = service.get_config().unwrap();
        assert_eq!(config.ai.provider, "macro_test");
    }

    #[test]
    fn test_create_default_test_config_service_macro() {
        create_default_test_config_service!(service);

        let config = service.get_config().unwrap();
        assert_eq!(config.ai.provider, "openai");
    }
}