kopi 0.2.1

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
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
// 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.

//! Comprehensive integration tests for multi-progress functionality

use kopi::download::{DownloadProgressAdapter, ProgressReporter};
use kopi::indicator::{ProgressConfig, ProgressFactory, ProgressIndicator, ProgressStyle};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

mod common;
use common::progress_capture::TestProgressCapture;
use common::test_home::TestHomeGuard;

/// Test utility to capture multi-progress hierarchies
struct MultiProgressCapture {
    parent: TestProgressCapture,
    children: Arc<Mutex<Vec<TestProgressCapture>>>,
}

impl MultiProgressCapture {
    fn new() -> Self {
        Self {
            parent: TestProgressCapture::new(),
            children: Arc::new(Mutex::new(Vec::new())),
        }
    }

    fn get_parent(&mut self) -> &mut TestProgressCapture {
        &mut self.parent
    }

    fn add_child(&self) -> TestProgressCapture {
        let child = TestProgressCapture::new();
        self.children.lock().unwrap().push(child.clone());
        child
    }

    fn child_count(&self) -> usize {
        self.children.lock().unwrap().len()
    }

    fn verify_parent_child_relationship(&self) -> bool {
        // Parent should have messages
        self.parent.message_count() > 0
    }

    fn verify_indentation(&self) -> bool {
        // In real scenario, child bars would have "└─" prefix
        // For test capture, we verify child creation
        self.child_count() > 0
    }
}

// Test Scenarios

#[test]
fn test_parent_with_single_child() {
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(10);
    parent.start(config);

    // Create a child progress
    let mut child = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Bytes).with_total(1024 * 1024);
    child.start(child_config);

    // Update both progress bars
    for i in 0..5 {
        parent.update(i * 2, Some(10));
        child.update(i * 200_000, Some(1024 * 1024));
        thread::sleep(Duration::from_millis(5));
    }

    // Complete child first, then parent
    child.complete(Some("Child task complete".to_string()));
    parent.complete(Some("Parent task complete".to_string()));
}

#[test]
fn test_parent_with_multiple_children() {
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(3);
    parent.start(config);

    let mut children = Vec::new();

    // Create multiple children
    for i in 0..3 {
        let mut child = parent.create_child();
        let child_config = ProgressConfig::new(ProgressStyle::Count).with_total(100);
        child.start(child_config);
        let child_num = i + 1;
        child.set_message(format!("Processing child {child_num}"));
        children.push(child);
    }

    // Update all children
    for (i, child) in children.iter_mut().enumerate() {
        for j in 0..100 {
            if j % 20 == 0 {
                child.update(j, Some(100));
            }
        }
        let child_num = i + 1;
        child.complete(Some(format!("Child {child_num} complete")));
        parent.update(i as u64 + 1, Some(3));
    }

    parent.complete(Some("All tasks complete".to_string()));
}

#[test]
fn test_parent_no_children_threshold_not_met() {
    // Test case where threshold is not met, so no child is created
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(8);
    parent.start(config);

    // Create download adapter but simulate small file
    let mut adapter = DownloadProgressAdapter::for_jdk_download(
        "small-tool@1.0",
        Some(parent.create_child()),
        false,
    );

    // Small file (< 10MB threshold)
    adapter.on_start(5 * 1024 * 1024); // 5MB

    // Should update parent message instead of creating child
    adapter.on_progress(2 * 1024 * 1024);
    adapter.on_progress(5 * 1024 * 1024);

    adapter.on_complete();
    parent.complete(Some("Download complete".to_string()));
}

#[test]
fn test_error_handling_with_active_children() {
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(5);
    parent.start(config);

    // Create and start a child
    let mut child = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Bytes).with_total(1024 * 1024);
    child.start(child_config);

    // Update child partially
    child.update(500_000, Some(1024 * 1024));

    // Simulate error in child
    child.error("Network error occurred".to_string());

    // Parent should handle child error gracefully
    parent.error("Operation failed due to child error".to_string());
}

#[test]
fn test_finish_and_clear_removes_bars() {
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(2);
    parent.start(config);

    // Create children
    let mut child1 = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Count).with_total(50);
    child1.start(child_config.clone());

    let mut child2 = parent.create_child();
    child2.start(child_config);

    // Complete children
    child1.complete(Some("First complete".to_string()));
    child2.complete(Some("Second complete".to_string()));

    // Parent completion should clear all bars
    parent.complete(Some("All complete".to_string()));
}

// Command Integration Tests

#[test]
fn test_install_with_large_download() {
    let _test_home = TestHomeGuard::new();

    // Simulate install command with large JDK download
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(8);
    parent.start(config);
    parent.set_message("Installing temurin@21".to_string());

    // Simulate download phase
    parent.update(3, Some(8));
    parent.set_message("Downloading JDK...".to_string());

    // Create child for large download
    let mut download_child = parent.create_child();
    let download_config = ProgressConfig::new(ProgressStyle::Bytes).with_total(150 * 1024 * 1024); // 150MB
    download_child.start(download_config);
    download_child.set_message("temurin-21.0.1".to_string());

    // Simulate download progress
    for i in 0..10 {
        download_child.update(i * 15 * 1024 * 1024, Some(150 * 1024 * 1024));
        thread::sleep(Duration::from_millis(5));
    }

    download_child.complete(Some("Download complete".to_string()));

    // Continue with installation steps
    parent.update(5, Some(8));
    parent.set_message("Extracting archive...".to_string());
    thread::sleep(Duration::from_millis(10));

    parent.update(7, Some(8));
    parent.set_message("Creating shims...".to_string());
    thread::sleep(Duration::from_millis(10));

    parent.complete(Some("Installation complete".to_string()));
}

#[test]
fn test_install_with_small_download() {
    let _test_home = TestHomeGuard::new();

    // Simulate install with small tool download
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(8);
    parent.start(config);
    parent.set_message("Installing maven@3.9".to_string());

    parent.update(3, Some(8));

    // Small download - no child progress
    let mut adapter =
        DownloadProgressAdapter::for_jdk_download("maven@3.9", Some(parent.create_child()), false);

    adapter.on_start(8 * 1024 * 1024); // 8MB - below threshold

    // Updates go to parent message
    adapter.on_progress(4 * 1024 * 1024);
    adapter.on_progress(8 * 1024 * 1024);
    adapter.on_complete();

    parent.update(8, Some(8));
    parent.complete(Some("Installation complete".to_string()));
}

#[test]
fn test_cache_refresh_multiple_sources() {
    let _test_home = TestHomeGuard::new();

    // Simulate cache refresh with multiple metadata sources
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(5);
    parent.start(config);
    parent.set_message("Refreshing cache".to_string());

    // Source 1: Foojay (always creates child)
    parent.update(1, Some(5));
    parent.set_message("Fetching from Foojay API...".to_string());

    let mut foojay_child = parent.create_child();
    let foojay_config = ProgressConfig::new(ProgressStyle::Count).with_total(100);
    foojay_child.start(foojay_config);
    foojay_child.set_message("Foojay".to_string());

    for i in 0..100 {
        if i % 20 == 0 {
            foojay_child.update(i, Some(100));
        }
    }
    foojay_child.complete(Some("Fetched 100 packages".to_string()));

    // Source 2: HTTP metadata (large, creates child)
    parent.update(2, Some(5));
    parent.set_message("Fetching HTTP metadata...".to_string());

    let mut http_child = parent.create_child();
    let http_config = ProgressConfig::new(ProgressStyle::Bytes).with_total(12 * 1024 * 1024); // 12MB
    http_child.start(http_config);
    http_child.set_message("HTTP Source".to_string());

    http_child.update(12 * 1024 * 1024, Some(12 * 1024 * 1024));
    http_child.complete(Some("Metadata downloaded".to_string()));

    // Source 3: Local (no child)
    parent.update(3, Some(5));
    parent.set_message("Loading local metadata...".to_string());
    thread::sleep(Duration::from_millis(10));

    parent.update(5, Some(5));
    parent.complete(Some("Cache refresh complete".to_string()));
}

#[test]
fn test_cache_refresh_single_source() {
    let _test_home = TestHomeGuard::new();

    // Test with only Foojay source
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(3);
    parent.start(config);
    parent.set_message("Cache refresh".to_string());

    parent.update(1, Some(3));

    // Foojay always creates child
    let mut child = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Count);
    child.start(child_config);
    child.set_message("Foojay".to_string());

    child.set_message("Querying API...".to_string());
    thread::sleep(Duration::from_millis(10));
    child.set_message("Processing packages...".to_string());
    thread::sleep(Duration::from_millis(10));

    child.complete(Some("Foojay complete".to_string()));
    parent.complete(Some("Refresh complete".to_string()));
}

// Edge Case Tests

#[test]
fn test_concurrent_updates_thread_safety() {
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(100);
    parent.start(config);

    // Create shared parent progress
    let parent_arc = Arc::new(Mutex::new(parent));

    let mut handles = Vec::new();

    // Spawn multiple threads updating the same parent
    for i in 0..3 {
        let parent_clone = Arc::clone(&parent_arc);

        let handle = thread::spawn(move || {
            let mut local_parent = parent_clone.lock().unwrap();
            let mut child = local_parent.create_child();
            drop(local_parent); // Release lock

            let child_config =
                ProgressConfig::new(ProgressStyle::Count).with_total((i + 1) as u64 * 10);
            child.start(child_config);

            for j in 0..(i + 1) * 10 {
                child.update(j as u64, Some((i + 1) as u64 * 10));
                thread::sleep(Duration::from_millis(2));
            }

            child.complete(Some(format!("Thread {i} complete")));
        });

        handles.push(handle);
    }

    // Wait for all threads
    for handle in handles {
        handle.join().expect("Thread should complete");
    }

    parent_arc
        .lock()
        .unwrap()
        .complete(Some("All threads complete".to_string()));
}

#[test]
fn test_nested_progress_depth_limit() {
    // Test that we handle nested progress appropriately
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(3);
    parent.start(config);

    // Create first level child
    let mut child1 = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Count).with_total(2);
    child1.start(child_config);

    // Try to create second level child (should work but may be silent)
    let mut child2 = child1.create_child();
    let grandchild_config = ProgressConfig::new(ProgressStyle::Count).with_total(1);
    child2.start(grandchild_config);

    // Update all levels
    child2.update(1, Some(1));
    child2.complete(Some("Grandchild complete".to_string()));

    child1.update(2, Some(2));
    child1.complete(Some("Child complete".to_string()));

    parent.update(3, Some(3));
    parent.complete(Some("Parent complete".to_string()));
}

#[test]
fn test_spinner_parent_with_determinate_child() {
    // Parent as spinner (no total), child with progress bar
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count); // No total = spinner
    parent.start(config);

    parent.set_message("Processing...".to_string());

    // Create determinate child
    let mut child = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Bytes).with_total(1024 * 1024 * 50); // 50MB
    child.start(child_config);

    // Update child while parent spins
    for i in 0..10 {
        child.update(i * 5 * 1024 * 1024, Some(50 * 1024 * 1024));
        thread::sleep(Duration::from_millis(5));
    }

    child.complete(Some("Download complete".to_string()));
    parent.complete(Some("Processing complete".to_string()));
}

#[test]
fn test_rapid_create_destroy_cycles() {
    // Test rapid creation and destruction of progress bars
    for _ in 0..10 {
        let mut parent = ProgressFactory::create(false);
        let config = ProgressConfig::new(ProgressStyle::Count).with_total(2);
        parent.start(config);

        let mut child = parent.create_child();
        let child_config = ProgressConfig::new(ProgressStyle::Count).with_total(10);
        child.start(child_config);

        child.update(10, Some(10));
        child.complete(None);

        parent.update(2, Some(2));
        parent.complete(None);
    }
}

#[test]
fn test_suspend_and_println_with_children() {
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(5);
    parent.start(config);

    let mut child = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Count).with_total(10);
    child.start(child_config);

    // Test println - should work without corrupting progress bars
    parent.println("Parent message").unwrap();
    child.println("Child message").unwrap();

    // Test suspend - should temporarily hide all progress bars
    parent.suspend(&mut || {
        println!("Suspended output from parent");
    });

    child.suspend(&mut || {
        println!("Suspended output from child");
    });

    child.complete(Some("Child done".to_string()));
    parent.complete(Some("Parent done".to_string()));
}

#[test]
fn test_unknown_content_length_download() {
    // Test download with unknown content length
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(8);
    parent.start(config);

    let mut adapter = DownloadProgressAdapter::for_jdk_download(
        "unknown-size",
        Some(parent.create_child()),
        false,
    );

    // Unknown size (0 means unknown)
    adapter.on_start(0);

    // Should use spinner mode without creating child
    adapter.on_progress(1024 * 1024); // 1MB
    adapter.on_progress(5 * 1024 * 1024); // 5MB

    adapter.on_complete();
    parent.complete(Some("Download complete".to_string()));
}

#[test]
fn test_very_small_file_download() {
    // Test with very small file (< 1MB)
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(5);
    parent.start(config);

    let mut adapter = DownloadProgressAdapter::for_jdk_download(
        "config-file",
        Some(parent.create_child()),
        false,
    );

    // Very small file
    adapter.on_start(50 * 1024); // 50KB

    adapter.on_progress(25 * 1024);
    adapter.on_progress(50 * 1024);

    adapter.on_complete();
    parent.complete(Some("Configuration downloaded".to_string()));
}

#[test]
fn test_progress_with_zero_total() {
    // Test edge case with zero total
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(0);
    parent.start(config);

    let mut child = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Count).with_total(0);
    child.start(child_config);

    // Should handle gracefully
    child.update(0, Some(0));
    child.complete(Some("Complete".to_string()));

    parent.update(0, Some(0));
    parent.complete(Some("Complete".to_string()));
}

#[test]
fn test_overflow_protection_with_children() {
    // Test updating beyond total with children
    let mut parent = ProgressFactory::create(false);
    let config = ProgressConfig::new(ProgressStyle::Count).with_total(100);
    parent.start(config);

    let mut child = parent.create_child();
    let child_config = ProgressConfig::new(ProgressStyle::Count).with_total(50);
    child.start(child_config);

    // Try to exceed totals
    child.update(100, Some(50)); // 200% of child total
    parent.update(200, Some(100)); // 200% of parent total

    // Should handle gracefully
    child.complete(None);
    parent.complete(None);
}

// Helper assertion functions
fn assert_parent_child_relationship(capture: &MultiProgressCapture) {
    assert!(
        capture.verify_parent_child_relationship(),
        "Parent-child relationship not established"
    );
    assert!(
        capture.verify_indentation(),
        "Child indentation not correct"
    );
}

#[test]
fn test_multi_progress_capture_utility() {
    let mut capture = MultiProgressCapture::new();

    // Test parent operations
    capture
        .get_parent()
        .set_message("Parent message".to_string());
    assert_eq!(capture.get_parent().message_count(), 1);

    // Test child creation
    let mut child1 = capture.add_child();
    child1.set_message("Child 1".to_string());

    let mut child2 = capture.add_child();
    child2.set_message("Child 2".to_string());

    assert_eq!(capture.child_count(), 2);
    assert_parent_child_relationship(&capture);
}