kopi 0.1.4

Kopi is a JDK version management tool
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
// Copyright 2025 dentsusoken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::indicator::{ProgressConfig, ProgressIndicator, SilentProgress};

pub struct SimpleProgress {}

impl SimpleProgress {
    pub fn new() -> Self {
        Self {}
    }
}

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

impl ProgressIndicator for SimpleProgress {
    fn start(&mut self, _config: ProgressConfig) {
        // Don't print on start to avoid duplication with StatusReporter
        // The complete() method will show the final status
    }

    fn update(&mut self, _current: u64, _total: Option<u64>) {
        // No update output in simple mode to avoid log spam
    }

    fn set_message(&mut self, message: String) {
        println!("{message}");
    }

    fn complete(&mut self, message: Option<String>) {
        let msg = message.unwrap_or_else(|| "Complete".to_string());
        println!("{msg}");
    }

    fn success(&self, message: &str) -> std::io::Result<()> {
        println!("[OK] {message}");
        Ok(())
    }

    fn error(&mut self, message: String) {
        eprintln!("[ERROR] {message}");
    }

    fn create_child(&mut self) -> Box<dyn ProgressIndicator> {
        // Return SilentProgress for child operations to keep output clean
        Box::new(SilentProgress::new())
    }

    fn suspend(&self, f: &mut dyn FnMut()) {
        // SimpleProgress doesn't use any terminal manipulation, just execute directly
        f();
    }

    fn println(&self, message: &str) -> std::io::Result<()> {
        // SimpleProgress can output directly without suspension
        println!("{message}");
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::indicator::ProgressStyle;
    use serial_test::serial;
    use std::sync::Mutex;

    // Helper to capture stdout/stderr for testing
    static OUTPUT: Mutex<Vec<String>> = Mutex::new(Vec::new());

    pub struct TestProgress {
        inner: SimpleProgress,
    }

    impl TestProgress {
        pub fn new() -> Self {
            Self {
                inner: SimpleProgress::new(),
            }
        }

        pub fn get_output() -> Vec<String> {
            OUTPUT.lock().unwrap().clone()
        }

        pub fn clear_output() {
            OUTPUT.lock().unwrap().clear();
        }
    }

    impl ProgressIndicator for TestProgress {
        fn start(&mut self, config: ProgressConfig) {
            let msg = "Starting...".to_string();
            OUTPUT.lock().unwrap().push(msg);
            self.inner.start(config);
        }

        fn update(&mut self, current: u64, total: Option<u64>) {
            self.inner.update(current, total);
        }

        fn set_message(&mut self, message: String) {
            OUTPUT.lock().unwrap().push(message.clone());
            self.inner.set_message(message);
        }

        fn complete(&mut self, message: Option<String>) {
            let msg = message.unwrap_or_else(|| "Complete".to_string());
            OUTPUT.lock().unwrap().push(msg);
        }

        fn success(&self, message: &str) -> std::io::Result<()> {
            let output = format!("[OK] {message}");
            OUTPUT.lock().unwrap().push(output);
            Ok(())
        }

        fn error(&mut self, message: String) {
            let output = format!("[ERROR] {message}");
            OUTPUT.lock().unwrap().push(output);
        }

        fn create_child(&mut self) -> Box<dyn ProgressIndicator> {
            self.inner.create_child()
        }

        fn suspend(&self, f: &mut dyn FnMut()) {
            self.inner.suspend(f)
        }

        fn println(&self, message: &str) -> std::io::Result<()> {
            self.inner.println(message)
        }
    }

    #[test]
    #[serial]
    fn test_message_output_format() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        let config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config);
        progress.complete(Some("Done".to_string()));

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 2);
        assert_eq!(output[0], "Starting...");
        assert_eq!(output[1], "Done");
    }

    #[test]
    #[serial]
    fn test_state_management() {
        let mut progress = SimpleProgress::new();

        // SimpleProgress no longer has state
        let config = ProgressConfig::new(ProgressStyle::Bytes);
        progress.start(config);

        // Just verify it doesn't panic
    }

    #[test]
    #[serial]
    fn test_error_handling() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        let config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config);
        progress.error("Failed to extract".to_string());

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 2);
        assert_eq!(output[1], "[ERROR] Failed to extract");
    }

    #[test]
    #[serial]
    fn test_complete_with_message() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        let config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config);
        progress.complete(Some("Successfully cached".to_string()));

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 2);
        assert_eq!(output[1], "Successfully cached");
    }

    #[test]
    #[serial]
    fn test_complete_without_message() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        let config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config);
        progress.complete(None);

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 2);
        assert_eq!(output[1], "Complete");
    }

    #[test]
    #[serial]
    fn test_update_no_output() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        let config = ProgressConfig::new(ProgressStyle::Count).with_total(100);
        progress.start(config);

        // Updates should not produce output
        for i in 0..10 {
            progress.update(i * 10, Some(100));
        }

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 1); // Only the start message
    }

    #[test]
    #[serial]
    fn test_set_message_output() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        let config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config);

        // Set message should now produce output
        progress.set_message("Processing file 1".to_string());
        progress.set_message("Processing file 2".to_string());

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 3); // Start message + 2 messages
        assert_eq!(output[1], "Processing file 1");
        assert_eq!(output[2], "Processing file 2");
    }

    #[test]
    #[serial]
    fn test_multiple_operations() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        // First operation
        let config1 = ProgressConfig::new(ProgressStyle::Bytes);
        progress.start(config1);
        progress.complete(None);

        // Second operation
        let config2 = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config2);
        progress.complete(Some("Done".to_string()));

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 4);
        assert_eq!(output[0], "Starting...");
        assert_eq!(output[1], "Complete");
        assert_eq!(output[2], "Starting...");
        assert_eq!(output[3], "Done");
    }

    #[test]
    fn test_create_child_returns_silent() {
        let mut progress = SimpleProgress::new();

        let mut child = progress.create_child();

        let config = ProgressConfig::new(ProgressStyle::Count);
        child.start(config);

        child.update(50, Some(100));
        child.set_message("Processing".to_string());
        child.complete(Some("Done".to_string()));

        child.error("Failed".to_string())
    }

    #[test]
    fn test_multiple_children() {
        let mut progress = SimpleProgress::new();

        let mut child1 = progress.create_child();
        let mut child2 = progress.create_child();
        let mut child3 = progress.create_child();

        let config1 = ProgressConfig::new(ProgressStyle::Count);
        child1.start(config1);
        child1.complete(None);

        let config2 = ProgressConfig::new(ProgressStyle::Bytes);
        child2.start(config2);
        child2.complete(Some("Success".to_string()));

        let config3 = ProgressConfig::new(ProgressStyle::Count);
        child3.start(config3);
        child3.error("Failed".to_string())
    }

    #[test]
    fn test_parent_child_interaction() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        let parent_config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(parent_config);

        let mut child = progress.create_child();
        let child_config = ProgressConfig::new(ProgressStyle::Bytes);
        child.start(child_config);
        child.update(100, Some(200));
        child.complete(Some("Child done".to_string()));

        progress.complete(Some("All done".to_string()));

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 2);
        assert_eq!(output[0], "Starting...");
        assert_eq!(output[1], "All done");
    }

    #[test]
    #[serial]
    fn test_ascii_only_output() {
        // Verify that SimpleProgress uses ASCII-only output for CI/NO_COLOR compatibility
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        // Test successful completion with ASCII [OK]
        let config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config);
        progress.complete(Some("Success".to_string()));

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 2);
        assert!(
            !output[1].starts_with("[OK]"),
            "Should NOT have [OK] prefix in complete()"
        );
        assert!(
            !output[1].contains(''),
            "Should not contain Unicode checkmark"
        );

        // Test error with ASCII [ERROR]
        TestProgress::clear_output();
        let mut progress = TestProgress::new();
        let config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config);
        progress.error("Failed".to_string());

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 2);
        assert!(
            output[1].starts_with("[ERROR]"),
            "Should use ASCII [ERROR] prefix"
        );
        assert!(
            !output[1].contains(''),
            "Should not contain Unicode cross mark"
        );
    }

    #[test]
    fn test_suspend_direct_execution() {
        let progress = SimpleProgress::new();
        let mut executed = false;

        progress.suspend(&mut || {
            executed = true;
        });

        assert!(executed, "suspend should execute the function directly");
    }

    #[test]
    fn test_println_output() {
        let progress = SimpleProgress::new();

        // This test just ensures println doesn't panic
        let result = progress.println("Test message");
        assert!(result.is_ok(), "println should return Ok");
    }

    #[test]
    #[serial]
    fn test_success_method() {
        TestProgress::clear_output();
        let mut progress = TestProgress::new();

        let config = ProgressConfig::new(ProgressStyle::Count);
        progress.start(config);
        progress.success("Operation succeeded").unwrap();

        let output = TestProgress::get_output();
        assert_eq!(output.len(), 2);
        assert_eq!(output[0], "Starting...");
        assert_eq!(output[1], "[OK] Operation succeeded");
    }
}