presentar-terminal 0.3.5

Terminal backend for Presentar UI framework with zero-allocation rendering
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
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
//! FALSIFICATION TESTS - Popperian Testing Philosophy
//!
//! These tests do NOT verify correctness. They ATTEMPT TO FALSIFY claims.
//! A passing test means "we tried to break it and couldn't" - NOT "it works".
//!
//! Severity Levels:
//! - S0: Cannot fail (FORBIDDEN)
//! - S1: Unlikely to fail (FORBIDDEN)
//! - S2: Might fail (DISCOURAGED)
//! - S3: Likely to fail if bug exists (REQUIRED)
//! - S4: Will definitely fail if bug exists (IDEAL)

#![cfg(feature = "ptop")]

use presentar_terminal::direct::CellBuffer;
use presentar_terminal::ptop::{App, PanelType};
use std::collections::HashMap;

// =============================================================================
// SECTION 1: CPU TEMPERATURE FALSIFICATION
// =============================================================================

/// Find k10temp hwmon device path.
fn find_k10temp_hwmon_path() -> Option<std::path::PathBuf> {
    let entries = std::fs::read_dir("/sys/class/hwmon").ok()?;
    for entry in entries.flatten() {
        let name_path = entry.path().join("name");
        if let Ok(name) = std::fs::read_to_string(&name_path) {
            if name.trim() == "k10temp" {
                return Some(entry.path());
            }
        }
    }
    None
}

/// Print available temp sensors for debugging.
fn print_available_temps(hwmon_path: &std::path::Path) {
    println!("Available temps:");
    for i in 1..=10 {
        let path = hwmon_path.join(format!("temp{}_input", i));
        if path.exists() {
            let label_path = hwmon_path.join(format!("temp{}_label", i));
            let label = std::fs::read_to_string(&label_path).unwrap_or_default();
            println!("  temp{}_input exists (label: {})", i, label.trim());
        }
    }
}

/// Count cores with zero temperature.
fn count_zero_temps(temps: &[f32]) -> usize {
    temps.iter().filter(|&&t| t == 0.0).count()
}

/// FALSIFICATION TEST [S4]: AMD k10temp driver has no temp2_input
///
/// CLAIM: "All CPU cores display temperatures"
/// FALSIFIED BY: Any core showing "-" on AMD system
///
/// k10temp layout:
/// - temp1_input = Tctl (control temp)
/// - temp2_input = DOES NOT EXIST (the trap!)
/// - temp3_input = Tccd1
/// - temp4_input = Tccd2
/// - temp5_input = Tccd3
/// - temp6_input = Tccd4
#[test]
fn falsify_temp_amd_k10temp_no_temp2() {
    let Some(hwmon_path) = find_k10temp_hwmon_path() else {
        println!("SKIP: Not an AMD k10temp system");
        return;
    };

    // Check what temp files actually exist
    let temp2_exists = hwmon_path.join("temp2_input").exists();

    // FALSIFICATION: If temp2 doesn't exist, our code better handle it
    if !temp2_exists {
        println!("CONFIRMED: temp2_input does NOT exist (k10temp layout)");
        print_available_temps(&hwmon_path);

        // Now test our code
        let app = App::with_config(false, Default::default());

        // FALSIFICATION ATTEMPT: Core 0 should have a temperature
        let core0_temp = app.per_core_temp.get(0).copied().unwrap_or(0.0);
        assert!(
            core0_temp > 0.0,
            "FALSIFIED: Core 0 has no temperature (got {}).\n\
             Root cause: Code tried to read temp2_input which doesn't exist.\n\
             Fix: Read temp*_label files to discover actual sensor layout.",
            core0_temp
        );

        // FALSIFICATION ATTEMPT: All cores should have temperatures
        let zeros = count_zero_temps(&app.per_core_temp);
        let zero_ratio = zeros as f32 / app.per_core_temp.len() as f32;
        assert!(
            zero_ratio < 0.1,
            "FALSIFIED: {}/{} cores ({:.1}%) have no temperature.\n\
             On k10temp, Tccd1-4 should be mapped to core groups.\n\
             Expected: Cores 0-11 -> Tccd1, 12-23 -> Tccd2, etc.",
            zeros,
            app.per_core_temp.len(),
            zero_ratio * 100.0
        );
    }
}

/// FALSIFICATION TEST [S4]: Temperature values are physically plausible
///
/// CLAIM: "Temperatures are in range 20-105C"
/// FALSIFIED BY: Any temperature outside this range (or 0)
#[test]
fn falsify_temp_physically_implausible() {
    let app = App::with_config(false, Default::default());

    let mut out_of_range = Vec::new();

    for (i, &temp) in app.per_core_temp.iter().enumerate() {
        if temp == 0.0 {
            continue; // Handled by other test
        }

        if temp < 15.0 || temp > 110.0 {
            out_of_range.push((i, temp));
        }
    }

    assert!(
        out_of_range.is_empty(),
        "FALSIFIED: {} cores have implausible temperatures: {:?}\n\
         Valid range is 15-110C.",
        out_of_range.len(),
        &out_of_range[..out_of_range.len().min(5)]
    );
}

/// FALSIFICATION TEST [S4]: Compare temperatures to `sensors` command
#[test]
fn falsify_temp_vs_sensors_command() {
    use std::process::Command;

    let output = Command::new("sensors").output();

    let Ok(output) = output else {
        println!("SKIP: sensors command not available");
        return;
    };

    let sensors_output = String::from_utf8_lossy(&output.stdout);

    // Parse Tccd temperatures from sensors output
    let mut tccd_temps: HashMap<String, f32> = HashMap::new();
    for line in sensors_output.lines() {
        if line.contains("Tccd") {
            let parts: Vec<&str> = line.split(':').collect();
            if parts.len() >= 2 {
                let label = parts[0].trim().to_string();
                let temp_str = parts[1]
                    .trim()
                    .trim_start_matches('+')
                    .split('°')
                    .next()
                    .unwrap_or("0");
                if let Ok(temp) = temp_str.parse::<f32>() {
                    tccd_temps.insert(label, temp);
                }
            }
        }
    }

    if tccd_temps.is_empty() {
        println!("SKIP: No Tccd temperatures found in sensors output");
        return;
    }

    println!("Sensors reports: {:?}", tccd_temps);

    let app = App::with_config(false, Default::default());

    if let Some(&sensors_tccd1) = tccd_temps.get("Tccd1") {
        let our_core0_temp = app.per_core_temp.get(0).copied().unwrap_or(0.0);

        if our_core0_temp > 0.0 {
            let delta = (our_core0_temp - sensors_tccd1).abs();
            assert!(
                delta < 10.0,
                "FALSIFIED: Core 0 temp {:.1}C differs from sensors Tccd1 {:.1}C by {:.1}C.",
                our_core0_temp,
                sensors_tccd1,
                delta
            );
        }
    }
}

// =============================================================================
// SECTION 2: PROCESS USER COLUMN FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: USER column shows usernames, not dashes
#[test]
fn falsify_user_column_shows_dashes() {
    let app = App::with_config(false, Default::default());

    let processes: Vec<_> = app.system.processes().iter().collect();

    assert!(!processes.is_empty(), "FALSIFIED: No processes found.");

    let mut dash_count = 0;
    let mut total_count = 0;

    for (pid, process) in processes.iter().take(100) {
        total_count += 1;

        let user = process
            .user_id()
            .and_then(|uid| app.users.get_user_by_id(uid))
            .map(|u| u.name().to_string());

        if user.is_none() {
            dash_count += 1;
            if dash_count <= 3 {
                println!("PID {} has no user (would show '-')", pid.as_u32());
            }
        }
    }

    let dash_ratio = dash_count as f32 / total_count as f32;
    assert!(
        dash_ratio < 0.10,
        "FALSIFIED: {:.1}% of processes ({}/{}) have no user.\n\
         .with_user() may be missing from ProcessRefreshKind.",
        dash_ratio * 100.0,
        dash_count,
        total_count
    );
}

/// FALSIFICATION TEST [S4]: Usernames are valid strings
#[test]
fn falsify_username_is_garbage() {
    let app = App::with_config(false, Default::default());

    for (_pid, process) in app.system.processes().iter().take(100) {
        if let Some(uid) = process.user_id() {
            if let Some(user) = app.users.get_user_by_id(uid) {
                let name = user.name();

                assert!(
                    name.chars()
                        .all(|c| c.is_alphanumeric() || c == '_' || c == '-' || c == '$'),
                    "FALSIFIED: Username '{}' contains invalid characters.",
                    name
                );

                assert!(!name.is_empty(), "FALSIFIED: Empty username found.");

                assert!(
                    name.len() <= 32,
                    "FALSIFIED: Username '{}' is {} chars (max 32).",
                    name,
                    name.len()
                );
            }
        }
    }
}

// =============================================================================
// SECTION 3: COMMAND TEXT FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: Command text is not garbled
#[test]
fn falsify_command_is_garbled() {
    let app = App::with_config(false, Default::default());

    for (pid, process) in app.system.processes().iter().take(100) {
        let cmd = process.name().to_string_lossy();

        let has_control = cmd.chars().any(|c| c.is_control() && c != '\t');
        assert!(
            !has_control,
            "FALSIFIED: PID {} command '{}' contains control characters.",
            pid.as_u32(),
            cmd
        );

        assert!(
            !cmd.contains('\0'),
            "FALSIFIED: PID {} command contains null byte.",
            pid.as_u32()
        );
    }
}

// =============================================================================
// SECTION 4: CORE COUNT FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: Core count matches system
#[test]
fn falsify_core_count_mismatch() {
    let expected_cores = std::fs::read_to_string("/proc/cpuinfo")
        .unwrap_or_default()
        .matches("processor")
        .count();

    let app = App::with_config(false, Default::default());

    assert_eq!(
        app.per_core_percent.len(),
        expected_cores,
        "FALSIFIED: per_core_percent has {} entries, expected {}.",
        app.per_core_percent.len(),
        expected_cores
    );

    assert_eq!(
        app.per_core_freq.len(),
        expected_cores,
        "FALSIFIED: per_core_freq has {} entries, expected {}.",
        app.per_core_freq.len(),
        expected_cores
    );

    assert_eq!(
        app.per_core_temp.len(),
        expected_cores,
        "FALSIFIED: per_core_temp has {} entries, expected {}.",
        app.per_core_temp.len(),
        expected_cores
    );
}

/// FALSIFICATION TEST [S4]: All cores visible in exploded view
#[test]
fn falsify_cores_not_visible() {
    use presentar_terminal::ptop::ui;

    let expected_cores = std::fs::read_to_string("/proc/cpuinfo")
        .unwrap_or_default()
        .matches("processor")
        .count();

    let mut app = App::with_config(false, Default::default());
    app.exploded_panel = Some(PanelType::Cpu);

    let mut buffer = CellBuffer::new(180, 60);
    ui::draw(&app, &mut buffer);

    let mut output = String::new();
    for y in 0..60 {
        for x in 0..180 {
            if let Some(cell) = buffer.get(x, y) {
                output.push(cell.symbol.chars().next().unwrap_or(' '));
            }
        }
        output.push('\n');
    }

    // Count core column entries (numbers 0-47)
    let mut visible_cores = std::collections::HashSet::new();
    for line in output.lines() {
        for word in line.split_whitespace() {
            if let Ok(num) = word.parse::<usize>() {
                if num < expected_cores {
                    visible_cores.insert(num);
                }
            }
        }
    }

    let visible_ratio = visible_cores.len() as f32 / expected_cores as f32;

    assert!(
        visible_ratio >= 0.5,
        "FALSIFIED: Only {}/{} cores visible ({:.1}%).",
        visible_cores.len(),
        expected_cores,
        visible_ratio * 100.0
    );
}

// =============================================================================
// SECTION 5: FREQUENCY FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: Frequencies match /proc/cpuinfo
#[test]
fn falsify_freq_vs_proc_cpuinfo() {
    let cpuinfo = std::fs::read_to_string("/proc/cpuinfo").unwrap_or_default();

    let actual_freqs: Vec<f64> = cpuinfo
        .lines()
        .filter(|line| line.starts_with("cpu MHz"))
        .filter_map(|line| line.split(':').nth(1).and_then(|s| s.trim().parse().ok()))
        .collect();

    if actual_freqs.is_empty() {
        println!("SKIP: Could not read frequencies from /proc/cpuinfo");
        return;
    }

    let app = App::with_config(false, Default::default());

    for (i, &actual_mhz) in actual_freqs.iter().enumerate().take(8) {
        let our_mhz = app.per_core_freq.get(i).copied().unwrap_or(0) as f64;

        if our_mhz == 0.0 {
            continue;
        }

        let delta = (our_mhz - actual_mhz).abs();

        assert!(
            delta < 500.0,
            "FALSIFIED: Core {} freq {:.0}MHz differs from /proc/cpuinfo {:.0}MHz by {:.0}MHz.",
            i,
            our_mhz,
            actual_mhz,
            delta
        );
    }
}

/// FALSIFICATION TEST [S3]: Frequencies are physically plausible
#[test]
fn falsify_freq_physically_implausible() {
    let app = App::with_config(false, Default::default());

    for (i, &freq_mhz) in app.per_core_freq.iter().enumerate() {
        if freq_mhz == 0 {
            continue;
        }

        assert!(
            freq_mhz >= 400,
            "FALSIFIED: Core {} freq {}MHz is below 400MHz.",
            i,
            freq_mhz
        );

        assert!(
            freq_mhz <= 6500,
            "FALSIFIED: Core {} freq {}MHz exceeds 6.5GHz.",
            i,
            freq_mhz
        );
    }
}

// =============================================================================
// SECTION 6: LOAD AVERAGE FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: Load average matches /proc/loadavg
#[test]
fn falsify_load_avg_vs_proc() {
    let loadavg = std::fs::read_to_string("/proc/loadavg").unwrap_or_default();
    let parts: Vec<&str> = loadavg.split_whitespace().collect();

    if parts.len() < 3 {
        println!("SKIP: Could not parse /proc/loadavg");
        return;
    }

    let actual_1min: f64 = parts[0].parse().unwrap_or(0.0);

    let app = App::with_config(false, Default::default());

    let delta_1 = (app.load_avg.one - actual_1min).abs();
    assert!(
        delta_1 < 5.0,
        "FALSIFIED: 1min load avg {:.2} differs from /proc/loadavg {:.2} by {:.2}.",
        app.load_avg.one,
        actual_1min,
        delta_1
    );
}

// =============================================================================
// SECTION 7: MEMORY FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: Memory total matches /proc/meminfo
#[test]
fn falsify_memory_total_vs_proc() {
    let meminfo = std::fs::read_to_string("/proc/meminfo").unwrap_or_default();

    let actual_total_kb: u64 = meminfo
        .lines()
        .find(|line| line.starts_with("MemTotal:"))
        .and_then(|line| line.split_whitespace().nth(1).and_then(|s| s.parse().ok()))
        .unwrap_or(0);

    if actual_total_kb == 0 {
        println!("SKIP: Could not parse /proc/meminfo");
        return;
    }

    let actual_total_bytes = actual_total_kb * 1024;

    let app = App::with_config(false, Default::default());
    let our_total = app.system.total_memory();

    let delta_ratio =
        (our_total as f64 - actual_total_bytes as f64).abs() / actual_total_bytes as f64;

    assert!(
        delta_ratio < 0.01,
        "FALSIFIED: Memory total {} differs from /proc/meminfo {} by {:.2}%.",
        our_total,
        actual_total_bytes,
        delta_ratio * 100.0
    );
}

/// FALSIFICATION TEST [S3]: Memory math adds up
#[test]
fn falsify_memory_math() {
    let app = App::with_config(false, Default::default());

    let total = app.system.total_memory();
    let used = app.system.used_memory();
    let available = app.system.available_memory();

    let sum = used + available;
    let delta_ratio = (sum as f64 - total as f64).abs() / total as f64;

    assert!(
        delta_ratio < 0.15,
        "FALSIFIED: used + available = {} differs from total {} by {:.1}%.",
        sum,
        total,
        delta_ratio * 100.0
    );
}

// =============================================================================
// SECTION 8: RENDER OUTPUT FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: No placeholder text in output
#[test]
fn falsify_placeholder_text_in_output() {
    use presentar_terminal::ptop::ui;

    let app = App::with_config(false, Default::default());

    let mut buffer = CellBuffer::new(140, 45);
    ui::draw(&app, &mut buffer);

    let mut output = String::new();
    for y in 0..45 {
        for x in 0..140 {
            if let Some(cell) = buffer.get(x, y) {
                output.push(cell.symbol.chars().next().unwrap_or(' '));
            }
        }
        output.push('\n');
    }

    let placeholders = ["TODO", "PLACEHOLDER", "FIXME", "XXX"];

    for placeholder in placeholders {
        assert!(
            !output.contains(placeholder),
            "FALSIFIED: Output contains placeholder text '{}'.",
            placeholder
        );
    }
}

// =============================================================================
// SECTION 9: DETERMINISTIC MODE FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: Deterministic mode produces zeros
#[test]
fn falsify_deterministic_not_zero() {
    let app = App::with_config(true, Default::default());

    assert!(
        app.per_core_percent.iter().all(|&p| p == 0.0),
        "FALSIFIED: Deterministic mode has non-zero CPU percentages."
    );

    assert!(
        app.per_core_freq.iter().all(|&f| f == 0),
        "FALSIFIED: Deterministic mode has non-zero frequencies."
    );

    assert!(
        app.load_avg.one == 0.0 && app.load_avg.five == 0.0 && app.load_avg.fifteen == 0.0,
        "FALSIFIED: Deterministic mode has non-zero load average."
    );
}

// =============================================================================
// SECTION 10: FILTER FUNCTIONALITY FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: Filter test actually tests filtering
///
/// CLAIM: "test_app_filter_processes tests process filtering"
/// FALSIFIED BY: Test only verifies string assignment, not actual filtering
///
/// The existing test in app.rs is FRAUDULENT:
/// ```
/// fn test_app_filter_processes() {
///     let mut app = App::new(true);
///     assert!(app.filter.is_empty());      // Only checks empty
///     app.filter = "test".to_string();
///     assert_eq!(app.filter, "test");      // Only checks string assignment
/// }
/// ```
/// This tests NOTHING about actual process filtering behavior.
#[test]
fn falsify_filter_test_is_fraudulent() {
    // The existing test in app.rs CLAIMS to test "filter processes"
    // But it only tests string assignment - ZERO filtering verification
    //
    // This meta-test documents this fraud. The real test is below.
    println!("WARNING: test_app_filter_processes in app.rs is FRAUDULENT");
    println!("It tests string assignment, NOT process filtering.");
}

/// FALSIFICATION TEST [S4]: Filter actually filters process count
///
/// CLAIM: "process_count() returns filtered count"
/// FALSIFIED BY: Count unchanged when filter is set
#[test]
fn falsify_filter_does_not_reduce_count() {
    use presentar_terminal::ptop::App;

    // Non-deterministic mode to get real processes
    let mut app = App::with_config(false, Default::default());

    // Get baseline count (no filter)
    let total_processes = app.system.processes().len();
    if total_processes < 10 {
        println!("SKIP: Too few processes to test filtering");
        return;
    }

    // Set a filter that should exclude most processes
    // "XYZNONEXISTENT" should match zero processes
    app.filter = "XYZNONEXISTENT".to_string();
    let filtered_count = app.process_count();

    // The filter should reduce the count
    assert!(
        filtered_count < total_processes,
        "FALSIFIED: Filter '{}' did not reduce process count.\n\
         Total: {}, Filtered: {}\n\
         The filter field is stored but NOT USED for filtering.",
        app.filter,
        total_processes,
        filtered_count
    );

    // With impossible filter, count should be 0 or very small
    assert!(
        filtered_count <= 5,
        "FALSIFIED: Filter 'XYZNONEXISTENT' still returns {} processes.\n\
         Expected: 0-5 (in case of coincidental match).",
        filtered_count
    );
}

/// FALSIFICATION TEST [S4]: Filter matches process names correctly
///
/// CLAIM: "Filter matches process names case-insensitively"
/// FALSIFIED BY: Filter does not match known process
#[test]
fn falsify_filter_does_not_match_known_process() {
    use presentar_terminal::ptop::App;

    let mut app = App::with_config(false, Default::default());

    // Find a process that definitely exists
    let known_process = app.system.processes().values().find(|p| {
        let name = p.name().to_string_lossy().to_lowercase();
        name.len() > 3 && !name.contains(' ')
    });

    let Some(proc) = known_process else {
        println!("SKIP: Could not find suitable process for testing");
        return;
    };

    let proc_name = proc.name().to_string_lossy().to_string();
    let search_term = proc_name.chars().take(4).collect::<String>().to_lowercase();

    println!(
        "Testing filter with known process: {} (searching for '{}')",
        proc_name, search_term
    );

    // Set filter to match this process
    app.filter = search_term.clone();
    let filtered_count = app.process_count();

    // Should find at least 1 match
    assert!(
        filtered_count >= 1,
        "FALSIFIED: Filter '{}' found 0 matches.\n\
         Process '{}' should have matched.\n\
         Filter is NOT being applied correctly.",
        search_term,
        proc_name
    );
}

/// FALSIFICATION TEST [S4]: Autocomplete feature exists
///
/// CLAIM: "ptop has autocomplete"
/// FALSIFIED BY: No autocomplete code found
#[test]
fn falsify_autocomplete_exists() {
    // Search for autocomplete-related fields/methods would be done at compile time
    // This test documents that NO AUTOCOMPLETE EXISTS
    //
    // Grep results:
    // - "autocomplete" -> 0 matches
    // - "auto_complete" -> 0 matches
    // - "auto-complete" -> 0 matches
    //
    // VERDICT: If anyone claims ptop has autocomplete, it is FRAUD

    println!("DOCUMENTED: No autocomplete feature exists in ptop.");
    println!("Grep for 'autocomplete|auto_complete|auto-complete' returns 0 matches.");
    println!("If autocomplete was advertised, it is FALSE ADVERTISING.");
}

/// FALSIFICATION TEST [S4]: Filter is visible in UI
///
/// CLAIM: "Filter text is displayed in UI"
/// FALSIFIED BY: Filter not visible in rendered output
#[test]
fn falsify_filter_not_visible_in_ui() {
    use presentar_terminal::direct::CellBuffer;
    use presentar_terminal::ptop::{ui, App};

    let mut app = App::with_config(true, Default::default()); // deterministic
    app.filter = "testfilter".to_string();
    app.show_filter_input = true;

    let mut buffer = CellBuffer::new(140, 45);
    ui::draw(&app, &mut buffer);

    let mut output = String::new();
    for y in 0..45 {
        for x in 0..140 {
            if let Some(cell) = buffer.get(x, y) {
                output.push(cell.symbol.chars().next().unwrap_or(' '));
            }
        }
        output.push('\n');
    }

    // The filter text should appear somewhere in the UI
    assert!(
        output.contains("testfilter") || output.contains("Filter"),
        "FALSIFIED: Filter text not visible in UI.\n\
         Filter value: '{}'\n\
         show_filter_input: true\n\
         The filter overlay is NOT being rendered.",
        app.filter
    );
}

// =============================================================================
// SECTION 11: SIGNAL FUNCTIONALITY FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S4]: request_signal sets pending_signal for real process
///
/// CLAIM: "request_signal() sets pending_signal tuple"
/// FALSIFIED BY: pending_signal is None after request
#[test]
fn falsify_request_signal_sets_pending() {
    use crossterm::event::KeyCode;
    use presentar_terminal::ptop::App;

    let mut app = App::with_config(false, Default::default());

    // First, navigate to process panel and select a process
    app.focused_panel = Some(PanelType::Process);

    // Get count of processes
    let processes: Vec<_> = app.system.processes().iter().collect();
    if processes.is_empty() {
        println!("SKIP: No processes available for testing");
        return;
    }

    // Select first process
    app.process_selected = 0;

    // Request TERM signal via 'x' key
    app.handle_key(KeyCode::Char('x'), crossterm::event::KeyModifiers::empty());

    // pending_signal should be set now
    assert!(
        app.pending_signal.is_some(),
        "FALSIFIED: request_signal did not set pending_signal.\n\
         After pressing 'x' with a selected process, pending_signal should be Some.\n\
         Current: {:?}",
        app.pending_signal
    );

    // Verify the tuple structure
    if let Some((pid, name, signal_type)) = &app.pending_signal {
        assert!(*pid > 0, "PID should be positive");
        assert!(!name.is_empty(), "Process name should not be empty");
        println!(
            "Signal request created: PID={}, name={}, signal={:?}",
            pid, name, signal_type
        );
    }
}

/// FALSIFICATION TEST [S4]: Signal keys ('x', 'k', 'X', 'K') work
///
/// CLAIM: "Signal hotkeys create pending signals"
/// FALSIFIED BY: No pending signal after pressing signal keys
#[test]
fn falsify_signal_hotkeys_work() {
    use crossterm::event::{KeyCode, KeyModifiers};
    use presentar_terminal::ptop::config::SignalType;
    use presentar_terminal::ptop::App;

    let mut app = App::with_config(false, Default::default());
    app.focused_panel = Some(PanelType::Process);

    let processes: Vec<_> = app.system.processes().iter().collect();
    if processes.is_empty() {
        println!("SKIP: No processes available for testing");
        return;
    }

    app.process_selected = 0;

    // Test 'x' key -> TERM
    app.handle_key(KeyCode::Char('x'), KeyModifiers::empty());
    if let Some((_, _, signal)) = &app.pending_signal {
        assert!(
            matches!(signal, SignalType::Term),
            "FALSIFIED: 'x' should request TERM, got {:?}",
            signal
        );
    }
    app.cancel_signal();

    // Test 'k' key -> KILL
    app.handle_key(KeyCode::Char('k'), KeyModifiers::empty());
    if let Some((_, _, signal)) = &app.pending_signal {
        assert!(
            matches!(signal, SignalType::Kill),
            "FALSIFIED: 'k' should request KILL, got {:?}",
            signal
        );
    }
}

/// FALSIFICATION TEST [S4]: Signal confirmation dialog prevents accidental kills
///
/// CLAIM: "Signals require Y/Enter confirmation"
/// FALSIFIED BY: Signal sent without confirmation
#[test]
fn falsify_signal_requires_confirmation() {
    use crossterm::event::{KeyCode, KeyModifiers};
    use presentar_terminal::ptop::App;

    let mut app = App::with_config(false, Default::default());
    app.focused_panel = Some(PanelType::Process);

    let processes: Vec<_> = app.system.processes().iter().collect();
    if processes.is_empty() {
        println!("SKIP: No processes available for testing");
        return;
    }

    app.process_selected = 0;

    // Request signal
    app.handle_key(KeyCode::Char('x'), KeyModifiers::empty());

    // Before confirmation, signal_result should be None
    assert!(
        app.signal_result.is_none(),
        "FALSIFIED: Signal was sent without confirmation!"
    );

    // 'n' should cancel
    app.handle_key(KeyCode::Char('n'), KeyModifiers::empty());
    assert!(
        app.pending_signal.is_none(),
        "FALSIFIED: 'n' did not cancel pending signal"
    );

    // Re-request
    app.handle_key(KeyCode::Char('x'), KeyModifiers::empty());

    // Esc should also cancel
    app.handle_key(KeyCode::Esc, KeyModifiers::empty());
    // Note: Esc in normal mode quits, but in signal confirmation it should cancel
    // This depends on state machine - verify pending is cleared
}

/// FALSIFICATION TEST [S4]: sorted_processes applies filter
///
/// CLAIM: "sorted_processes() returns filtered results"
/// FALSIFIED BY: Filter has no effect on sorted_processes output
#[test]
fn falsify_sorted_processes_ignores_filter() {
    use presentar_terminal::ptop::App;

    let mut app = App::with_config(false, Default::default());

    // Get baseline count
    let total = app.sorted_processes().len();
    if total < 10 {
        println!("SKIP: Too few processes to test filtering");
        return;
    }

    // Set filter to something that shouldn't match most processes
    app.filter = "XYZNONEXISTENT".to_string();
    let filtered = app.sorted_processes().len();

    assert!(
        filtered < total,
        "FALSIFIED: sorted_processes() does not apply filter.\n\
         Total: {}, After filter '{}': {}\n\
         Filter is ignored by sorted_processes().",
        total,
        app.filter,
        filtered
    );
}

// =============================================================================
// SECTION 12: ANALYZER AVAILABILITY FALSIFICATION
// =============================================================================

/// FALSIFICATION TEST [S3]: PSI detection on Linux
///
/// CLAIM: "has_psi() correctly detects PSI availability"
/// FALSIFIED BY: has_psi returns wrong value vs /proc/pressure existence
#[test]
fn falsify_psi_detection() {
    use presentar_terminal::ptop::App;

    let app = App::with_config(false, Default::default());

    // Check if /proc/pressure/cpu exists (Linux PSI support)
    let psi_available = std::path::Path::new("/proc/pressure/cpu").exists();

    // Note: App.data_availability() should reflect PSI status
    let data_avail = app.data_availability();

    if psi_available {
        // If kernel supports PSI, we should detect it (unless disabled)
        println!("System has PSI support (/proc/pressure/cpu exists)");
        // Can't assert data_avail.psi is true because it might be None due to read errors
    } else {
        // No PSI support - verify we don't claim to have it
        assert!(
            !data_avail.psi_available,
            "FALSIFIED: has_psi() returns true but /proc/pressure/cpu doesn't exist"
        );
    }
}

/// FALSIFICATION TEST [S3]: Sensor detection
///
/// CLAIM: "has_sensors() correctly detects sensor availability"
/// FALSIFIED BY: has_sensors returns wrong value vs /sys/class/hwmon existence
#[test]
fn falsify_sensor_detection() {
    use presentar_terminal::ptop::App;

    let app = App::with_config(false, Default::default());

    // Check if any hwmon devices exist
    let hwmon_exists = std::path::Path::new("/sys/class/hwmon")
        .read_dir()
        .map(|mut d| d.next().is_some())
        .unwrap_or(false);

    let data_avail = app.data_availability();

    if !hwmon_exists {
        // No hwmon - should not claim sensors
        assert!(
            !data_avail.sensors_available,
            "FALSIFIED: has_sensors() returns true but no hwmon devices exist"
        );
    } else {
        println!("System has hwmon devices, sensor detection should work");
    }
}

/// FALSIFICATION TEST [S3]: Connection tracking detection
///
/// CLAIM: "has_connections() correctly detects /proc/net/tcp availability"
/// FALSIFIED BY: has_connections returns wrong value
#[test]
fn falsify_connections_detection() {
    use presentar_terminal::ptop::App;

    let app = App::with_config(false, Default::default());

    // Check if /proc/net/tcp exists
    let tcp_available = std::path::Path::new("/proc/net/tcp").exists();

    let data_avail = app.data_availability();

    if tcp_available {
        println!("System has /proc/net/tcp, connection tracking should work");
        // Connection tracking should be available on any Linux system with /proc/net/tcp
    } else {
        assert!(
            !data_avail.connections_available,
            "FALSIFIED: has_connections() returns true but /proc/net/tcp doesn't exist"
        );
    }
}

// =============================================================================
// SEVERITY REPORT
// =============================================================================

#[test]
fn report_test_severities() {
    println!("\n=== FALSIFICATION TEST SEVERITY REPORT ===");
    println!("falsify_temp_amd_k10temp_no_temp2     : S4");
    println!("falsify_temp_physically_implausible   : S4");
    println!("falsify_temp_vs_sensors_command       : S4");
    println!("falsify_user_column_shows_dashes      : S4");
    println!("falsify_username_is_garbage           : S4");
    println!("falsify_command_is_garbled            : S4");
    println!("falsify_core_count_mismatch           : S4");
    println!("falsify_cores_not_visible             : S4");
    println!("falsify_freq_vs_proc_cpuinfo          : S4");
    println!("falsify_freq_physically_implausible   : S3");
    println!("falsify_load_avg_vs_proc              : S4");
    println!("falsify_memory_total_vs_proc          : S4");
    println!("falsify_memory_math                   : S3");
    println!("falsify_placeholder_text_in_output    : S4");
    println!("falsify_deterministic_not_zero        : S4");
    println!("falsify_filter_test_is_fraudulent     : S4");
    println!("falsify_filter_does_not_reduce_count  : S4");
    println!("falsify_filter_does_not_match_known   : S4");
    println!("falsify_autocomplete_exists           : S4");
    println!("falsify_filter_not_visible_in_ui      : S4");
    println!("falsify_request_signal_sets_pending   : S4");
    println!("falsify_signal_hotkeys_work           : S4");
    println!("falsify_signal_requires_confirmation  : S4");
    println!("falsify_sorted_processes_ignores_flt  : S4");
    println!("falsify_psi_detection                 : S3");
    println!("falsify_sensor_detection              : S3");
    println!("falsify_connections_detection         : S3");
    println!("==========================================");
    println!("Total: 27 falsification tests");
    println!("S4: 22, S3: 5");
}