subx-cli 1.6.0

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
//! Configuration builder for fluent test configuration creation.
//!
//! This module provides a fluent API for building test configurations,
//! making it easy to create specific configuration scenarios for testing.

use crate::config::test_service::TestConfigService;
use crate::config::{Config, OverflowStrategy};

/// Fluent builder for creating test configurations.
///
/// This builder provides a convenient way to create configurations
/// for testing with specific settings, using method chaining for clarity.
///
/// # Examples
///
/// ```rust
/// use subx_cli::config::TestConfigBuilder;
///
/// let config = TestConfigBuilder::new()
///     .with_ai_provider("openai")
///     .with_ai_model("gpt-4.1")
///     .with_vad_enabled(true)
///     .build_config();
/// ```
pub struct TestConfigBuilder {
    config: Config,
}

impl TestConfigBuilder {
    /// Create a new configuration builder with default values.
    pub fn new() -> Self {
        Self {
            config: Config::default(),
        }
    }

    // AI Configuration Methods

    /// Set the AI provider.
    ///
    /// # Arguments
    ///
    /// * `provider` - The AI provider name (e.g., "openai", "anthropic")
    pub fn with_ai_provider(mut self, provider: &str) -> Self {
        self.config.ai.provider = provider.to_string();
        self
    }

    /// Set the AI model.
    ///
    /// # Arguments
    ///
    /// * `model` - The AI model name (e.g., "gpt-4.1", "claude-3")
    pub fn with_ai_model(mut self, model: &str) -> Self {
        self.config.ai.model = model.to_string();
        self
    }

    /// Set the AI API key.
    ///
    /// # Arguments
    ///
    /// * `api_key` - The API key for authentication
    pub fn with_ai_api_key(mut self, api_key: &str) -> Self {
        self.config.ai.api_key = Some(api_key.to_string());
        self
    }

    /// Set the AI base URL.
    ///
    /// # Arguments
    ///
    /// * `base_url` - The base URL for the AI service
    pub fn with_ai_base_url(mut self, base_url: &str) -> Self {
        self.config.ai.base_url = base_url.to_string();
        self
    }

    /// Set the maximum sample length for AI requests.
    ///
    /// # Arguments
    ///
    /// * `length` - Maximum sample length in characters
    pub fn with_max_sample_length(mut self, length: usize) -> Self {
        self.config.ai.max_sample_length = length;
        self
    }

    /// Set the AI temperature parameter.
    ///
    /// # Arguments
    ///
    /// * `temperature` - Temperature value (0.0-1.0)
    pub fn with_ai_temperature(mut self, temperature: f32) -> Self {
        self.config.ai.temperature = temperature;
        self
    }

    /// Set the AI max tokens parameter.
    ///
    /// # Arguments
    ///
    /// * `max_tokens` - Maximum tokens in response (1-100000)
    pub fn with_ai_max_tokens(mut self, max_tokens: u32) -> Self {
        self.config.ai.max_tokens = max_tokens;
        self
    }

    /// Set the AI retry parameters.
    ///
    /// # Arguments
    ///
    /// * `attempts` - Number of retry attempts
    /// * `delay_ms` - Delay between retries in milliseconds
    pub fn with_ai_retry(mut self, attempts: u32, delay_ms: u64) -> Self {
        self.config.ai.retry_attempts = attempts;
        self.config.ai.retry_delay_ms = delay_ms;
        self
    }

    /// Set the AI request timeout.
    ///
    /// # Arguments
    ///
    /// * `timeout_seconds` - Request timeout in seconds
    pub fn with_ai_request_timeout(mut self, timeout_seconds: u64) -> Self {
        self.config.ai.request_timeout_seconds = timeout_seconds;
        self
    }

    // Sync Configuration Methods

    /// Set the synchronization method.
    ///
    /// # Arguments
    ///
    /// * `method` - The sync method to use ("vad", "auto", "manual")
    pub fn with_sync_method(mut self, method: &str) -> Self {
        self.config.sync.default_method = method.to_string();
        self
    }

    /// Enable or disable local VAD.
    ///
    /// # Arguments
    ///
    /// * `enabled` - Whether to enable local VAD processing
    pub fn with_vad_enabled(mut self, enabled: bool) -> Self {
        self.config.sync.vad.enabled = enabled;
        self
    }

    /// Set VAD sensitivity.
    ///
    /// # Arguments
    ///
    /// * `sensitivity` - VAD sensitivity value (0.0-1.0)
    pub fn with_vad_sensitivity(mut self, sensitivity: f32) -> Self {
        self.config.sync.vad.sensitivity = sensitivity;
        self
    }

    // Formats Configuration Methods

    /// Set the default output format.
    ///
    /// # Arguments
    ///
    /// * `format` - The output format (e.g., "srt", "ass", "vtt")
    pub fn with_default_output_format(mut self, format: &str) -> Self {
        self.config.formats.default_output = format.to_string();
        self
    }

    /// Enable or disable style preservation.
    ///
    /// # Arguments
    ///
    /// * `preserve` - Whether to preserve styling
    pub fn with_preserve_styling(mut self, preserve: bool) -> Self {
        self.config.formats.preserve_styling = preserve;
        self
    }

    /// Set the default encoding.
    ///
    /// # Arguments
    ///
    /// * `encoding` - The default encoding (e.g., "utf-8", "gbk")
    pub fn with_default_encoding(mut self, encoding: &str) -> Self {
        self.config.formats.default_encoding = encoding.to_string();
        self
    }

    /// Set the encoding detection confidence threshold.
    ///
    /// # Arguments
    ///
    /// * `confidence` - Confidence threshold (0.0-1.0)
    pub fn with_encoding_detection_confidence(mut self, confidence: f32) -> Self {
        self.config.formats.encoding_detection_confidence = confidence;
        self
    }

    // General Configuration Methods

    /// Enable or disable backup.
    ///
    /// # Arguments
    ///
    /// * `enabled` - Whether to enable backup
    pub fn with_backup_enabled(mut self, enabled: bool) -> Self {
        self.config.general.backup_enabled = enabled;
        self
    }

    /// Set the maximum number of concurrent jobs.
    ///
    /// # Arguments
    ///
    /// * `jobs` - Maximum concurrent jobs
    pub fn with_max_concurrent_jobs(mut self, jobs: usize) -> Self {
        self.config.general.max_concurrent_jobs = jobs;
        self
    }

    /// Set the task timeout.
    ///
    /// # Arguments
    ///
    /// * `timeout_seconds` - Timeout in seconds
    pub fn with_task_timeout(mut self, timeout_seconds: u64) -> Self {
        self.config.general.task_timeout_seconds = timeout_seconds;
        self
    }

    /// Enable or disable progress bar.
    ///
    /// # Arguments
    ///
    /// * `enabled` - Whether to enable progress bar
    pub fn with_progress_bar(mut self, enabled: bool) -> Self {
        self.config.general.enable_progress_bar = enabled;
        self
    }

    /// Set the worker idle timeout.
    ///
    /// # Arguments
    ///
    /// * `timeout_seconds` - Idle timeout in seconds
    pub fn with_worker_idle_timeout(mut self, timeout_seconds: u64) -> Self {
        self.config.general.worker_idle_timeout_seconds = timeout_seconds;
        self
    }

    // Parallel Configuration Methods

    /// Set the task queue size.
    ///
    /// # Arguments
    ///
    /// * `size` - Queue size
    pub fn with_task_queue_size(mut self, size: usize) -> Self {
        self.config.parallel.task_queue_size = size;
        self
    }

    /// Enable or disable task priorities.
    ///
    /// # Arguments
    ///
    /// * `enabled` - Whether to enable task priorities
    pub fn with_task_priorities(mut self, enabled: bool) -> Self {
        self.config.parallel.enable_task_priorities = enabled;
        self
    }

    /// Enable or disable auto-balancing of workers.
    ///
    /// # Arguments
    ///
    /// * `enabled` - Whether to enable auto-balancing
    pub fn with_auto_balance_workers(mut self, enabled: bool) -> Self {
        self.config.parallel.auto_balance_workers = enabled;
        self
    }

    /// Set the queue overflow strategy.
    ///
    /// # Arguments
    ///
    /// * `strategy` - Overflow strategy
    pub fn with_queue_overflow_strategy(mut self, strategy: OverflowStrategy) -> Self {
        self.config.parallel.overflow_strategy = strategy;
        self
    }

    /// Set the number of parallel workers and queue size, used for integration testing.
    pub fn with_parallel_settings(mut self, max_workers: usize, queue_size: usize) -> Self {
        self.config.general.max_concurrent_jobs = max_workers;
        self.config.parallel.task_queue_size = queue_size;
        self
    }

    // Builder Methods

    /// Build a test configuration service with the configured settings.
    pub fn build_service(self) -> TestConfigService {
        TestConfigService::new(self.config)
    }

    /// Build a configuration object with the configured settings.
    pub fn build_config(self) -> Config {
        self.config
    }

    /// Get a reference to the current configuration being built.
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Get a mutable reference to the current configuration being built.
    pub fn config_mut(&mut self) -> &mut Config {
        &mut self.config
    }
    /// Configure AI base URL for mock server integration testing.
    ///
    /// Sets up the configuration to use a mock AI server for testing purposes.
    /// This is primarily used in integration tests to avoid making real API calls.
    ///
    /// # Arguments
    ///
    /// - `mock_url`: The URL of the mock server to use for AI API calls
    ///
    /// # Examples
    ///
    /// ```rust
    /// use subx_cli::config::TestConfigBuilder;
    ///
    /// let config = TestConfigBuilder::new()
    ///     .with_mock_ai_server("http://localhost:3000")
    ///     .build_config();
    /// ```
    pub fn with_mock_ai_server(mut self, mock_url: &str) -> Self {
        self.config.ai.base_url = mock_url.to_string();
        self.config.ai.api_key = Some("mock-api-key".to_string());
        self
    }
}

impl Default for TestConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::service::ConfigService;

    #[test]
    fn test_builder_default() {
        let config = TestConfigBuilder::new().build_config();
        let default_config = Config::default();

        assert_eq!(config.ai.provider, default_config.ai.provider);
        assert_eq!(config.ai.model, default_config.ai.model);
    }

    #[test]
    fn test_builder_ai_configuration() {
        let config = TestConfigBuilder::new()
            .with_ai_provider("anthropic")
            .with_ai_model("claude-3")
            .with_ai_api_key("test-key")
            .with_max_sample_length(5000)
            .with_ai_temperature(0.7)
            .build_config();

        assert_eq!(config.ai.provider, "anthropic");
        assert_eq!(config.ai.model, "claude-3");
        assert_eq!(config.ai.api_key, Some("test-key".to_string()));
        assert_eq!(config.ai.max_sample_length, 5000);
        assert_eq!(config.ai.temperature, 0.7);
    }

    #[test]
    fn test_builder_sync_configuration() {
        let config = TestConfigBuilder::new()
            .with_sync_method("vad")
            .with_vad_enabled(true)
            .with_vad_sensitivity(0.8)
            .build_config();

        assert_eq!(config.sync.default_method, "vad");
        assert!(config.sync.vad.enabled);
        assert_eq!(config.sync.vad.sensitivity, 0.8);
    }

    #[test]
    fn test_builder_service_creation() {
        let service = TestConfigBuilder::new()
            .with_ai_provider("test-provider")
            .build_service();

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

    #[test]
    fn test_builder_chaining() {
        let config = TestConfigBuilder::new()
            .with_ai_provider("openai")
            .with_ai_model("gpt-4.1")
            .with_sync_method("vad")
            .with_vad_sensitivity(0.5)
            .with_max_concurrent_jobs(8)
            .with_task_queue_size(200)
            .build_config();

        assert_eq!(config.ai.provider, "openai");
        assert_eq!(config.ai.model, "gpt-4.1");
        assert_eq!(config.sync.default_method, "vad");
        assert_eq!(config.sync.vad.sensitivity, 0.5);
        assert_eq!(config.general.max_concurrent_jobs, 8);
        assert_eq!(config.parallel.task_queue_size, 200);
    }

    #[test]
    fn test_builder_ai_configuration_openrouter() {
        let config = TestConfigBuilder::new()
            .with_ai_provider("openrouter")
            .with_ai_model("deepseek/deepseek-r1-0528:free")
            .with_ai_api_key("test-openrouter-key")
            .build_config();
        assert_eq!(config.ai.provider, "openrouter");
        assert_eq!(config.ai.model, "deepseek/deepseek-r1-0528:free");
        assert_eq!(config.ai.api_key, Some("test-openrouter-key".to_string()));
    }
}