rch 2.1.4

Remote Compilation Helper - PreToolUse hook CLI
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
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
//! Mock data generators for TUI testing.
//!
//! Provides factory functions to create test instances of TUI state
//! structures with sensible defaults. These are useful for unit tests
//! that need realistic but deterministic test data.
//!
//! # Example
//!
//! ```ignore
//! use crate::tui::test_data::*;
//!
//! let state = mock_state_with_workers(3);
//! assert_eq!(state.workers.len(), 3);
//!
//! let worker = mock_worker("test-1");
//! assert_eq!(worker.id, "test-1");
//! ```

use chrono::{Duration as ChronoDuration, Utc};
use rch_common::fleet_diff::WorkerObservation;
use rch_common::remediation_view::{
    DiskLevel, JobsInput, ProofQueueInput, RemediationView, RemediationWorkerRow, assemble,
    build_inputs,
};
use std::collections::VecDeque;
use std::path::PathBuf;
use std::time::Duration;
use tracing::debug;

use super::state::{
    ActiveBuild, BuildProgress, BuildStatus, CircuitState, ColorBlindMode, DaemonState,
    FilterState, HistoricalBuild, LogViewState, Panel, Status, TuiState, WorkerState, WorkerStatus,
};

// ============================================================================
// Worker Mock Generators
// ============================================================================

/// Create a mock worker with sensible defaults.
///
/// # Arguments
///
/// * `id` - The worker identifier
///
/// # Returns
///
/// A `WorkerState` with healthy status and default values.
pub fn mock_worker(id: &str) -> WorkerState {
    debug!("TEST DATA: creating mock worker '{}'", id);
    WorkerState {
        id: id.to_string(),
        host: format!("{}.local", id),
        status: WorkerStatus::Healthy,
        circuit: CircuitState::Closed,
        total_slots: 8,
        used_slots: 1,
        latency_ms: 10,
        last_seen: Utc::now(),
        builds_completed: 0,
    }
}

/// Create a mock worker with specific status.
///
/// # Arguments
///
/// * `id` - The worker identifier
/// * `status` - The worker status
/// * `circuit` - The circuit breaker state
pub fn mock_worker_with_status(
    id: &str,
    status: WorkerStatus,
    circuit: CircuitState,
) -> WorkerState {
    debug!(
        "TEST DATA: creating mock worker '{}' status={:?} circuit={:?}",
        id, status, circuit
    );
    WorkerState {
        id: id.to_string(),
        host: format!("{}.local", id),
        status,
        circuit,
        total_slots: 8,
        used_slots: 2,
        latency_ms: 15,
        last_seen: Utc::now(),
        builds_completed: 5,
    }
}

/// Create a mock worker with specific slot configuration.
///
/// # Arguments
///
/// * `id` - The worker identifier
/// * `used_slots` - Number of slots currently in use
/// * `total_slots` - Total available slots
pub fn mock_worker_with_slots(id: &str, used_slots: u32, total_slots: u32) -> WorkerState {
    debug!(
        "TEST DATA: creating mock worker '{}' slots={}/{}",
        id, used_slots, total_slots
    );
    WorkerState {
        id: id.to_string(),
        host: format!("{}.local", id),
        status: WorkerStatus::Healthy,
        circuit: CircuitState::Closed,
        total_slots,
        used_slots,
        latency_ms: 10,
        last_seen: Utc::now(),
        builds_completed: 0,
    }
}

/// Create a healthy worker with a specific host.
pub fn mock_worker_with_host(id: &str, host: &str) -> WorkerState {
    WorkerState {
        id: id.to_string(),
        host: host.to_string(),
        status: WorkerStatus::Healthy,
        circuit: CircuitState::Closed,
        total_slots: 8,
        used_slots: 0,
        latency_ms: 10,
        last_seen: Utc::now(),
        builds_completed: 0,
    }
}

// ============================================================================
// Build Mock Generators
// ============================================================================

/// Create a mock active build.
///
/// # Arguments
///
/// * `id` - Build identifier
/// * `command` - The command being executed
pub fn mock_active_build(id: &str, command: &str) -> ActiveBuild {
    debug!(
        "TEST DATA: creating mock active build '{}' cmd='{}'",
        id, command
    );
    ActiveBuild {
        id: id.to_string(),
        command: command.to_string(),
        worker: Some("worker-1".to_string()),
        started_at: Utc::now(),
        progress: Some(BuildProgress {
            phase: "compiling".to_string(),
            percent: Some(50),
            current_file: None,
        }),
        status: BuildStatus::Compiling,
    }
}

/// Create a mock active build with specific status.
///
/// # Arguments
///
/// * `id` - Build identifier
/// * `command` - The command being executed
/// * `status` - The build status
/// * `progress_percent` - Optional progress percentage
pub fn mock_active_build_with_status(
    id: &str,
    command: &str,
    status: BuildStatus,
    progress_percent: Option<u8>,
) -> ActiveBuild {
    debug!(
        "TEST DATA: creating mock active build '{}' status={:?} progress={:?}",
        id, status, progress_percent
    );
    ActiveBuild {
        id: id.to_string(),
        command: command.to_string(),
        worker: Some("worker-1".to_string()),
        started_at: Utc::now(),
        progress: progress_percent.map(|p| BuildProgress {
            phase: format!("{:?}", status).to_lowercase(),
            percent: Some(p),
            current_file: None,
        }),
        status,
    }
}

/// Create a mock active build with specific worker.
pub fn mock_active_build_with_worker(id: &str, command: &str, worker_id: &str) -> ActiveBuild {
    ActiveBuild {
        id: id.to_string(),
        command: command.to_string(),
        worker: Some(worker_id.to_string()),
        started_at: Utc::now(),
        progress: Some(BuildProgress {
            phase: "compiling".to_string(),
            percent: Some(50),
            current_file: None,
        }),
        status: BuildStatus::Compiling,
    }
}

/// Create a mock active build with no worker (pending).
pub fn mock_pending_build(id: &str, command: &str) -> ActiveBuild {
    ActiveBuild {
        id: id.to_string(),
        command: command.to_string(),
        worker: None,
        started_at: Utc::now(),
        progress: None,
        status: BuildStatus::Pending,
    }
}

// ============================================================================
// Historical Build Mock Generators
// ============================================================================

/// Create a mock historical build record.
///
/// # Arguments
///
/// * `id` - Build identifier
/// * `command` - The command that was executed
/// * `worker` - Optional worker that executed the build
/// * `success` - Whether the build succeeded
pub fn mock_historical_build(
    id: &str,
    command: &str,
    worker: Option<&str>,
    success: bool,
) -> HistoricalBuild {
    debug!(
        "TEST DATA: creating mock history '{}' worker={:?} success={}",
        id, worker, success
    );
    let started_at = Utc::now() - ChronoDuration::seconds(5);
    let completed_at = Utc::now();
    HistoricalBuild {
        id: id.to_string(),
        command: command.to_string(),
        worker: worker.map(|w| w.to_string()),
        started_at,
        completed_at,
        duration_ms: 5000,
        success,
        exit_code: Some(if success { 0 } else { 1 }),
    }
}

/// Create a mock historical build with specific duration.
///
/// # Arguments
///
/// * `id` - Build identifier
/// * `command` - The command that was executed
/// * `duration_ms` - Build duration in milliseconds
/// * `success` - Whether the build succeeded
pub fn mock_historical_build_with_duration(
    id: &str,
    command: &str,
    duration_ms: u64,
    success: bool,
) -> HistoricalBuild {
    debug!(
        "TEST DATA: creating mock history '{}' duration={}ms success={}",
        id, duration_ms, success
    );
    let started_at = Utc::now() - ChronoDuration::milliseconds(duration_ms as i64);
    let completed_at = Utc::now();
    HistoricalBuild {
        id: id.to_string(),
        command: command.to_string(),
        worker: Some("worker-1".to_string()),
        started_at,
        completed_at,
        duration_ms,
        success,
        exit_code: Some(if success { 0 } else { 1 }),
    }
}

/// Create a mock historical build with specific exit code.
pub fn mock_historical_build_with_exit_code(
    id: &str,
    command: &str,
    exit_code: i32,
) -> HistoricalBuild {
    let success = exit_code == 0;
    let started_at = Utc::now() - ChronoDuration::seconds(3);
    HistoricalBuild {
        id: id.to_string(),
        command: command.to_string(),
        worker: Some("worker-1".to_string()),
        started_at,
        completed_at: Utc::now(),
        duration_ms: 3000,
        success,
        exit_code: Some(exit_code),
    }
}

/// Create a mock historical build executed locally (no worker).
pub fn mock_local_build(id: &str, command: &str, success: bool) -> HistoricalBuild {
    mock_historical_build(id, command, None, success)
}

// ============================================================================
// Daemon State Mock Generators
// ============================================================================

/// Create a mock daemon state with running status.
pub fn mock_daemon_running() -> DaemonState {
    debug!("TEST DATA: creating mock daemon (running)");
    DaemonState {
        status: Status::Running,
        uptime: Duration::from_secs(3600),
        version: "0.1.0-test".to_string(),
        config_path: PathBuf::from("/etc/rch/config.toml"),
        socket_path: PathBuf::from("/tmp/rch.sock"),
        builds_today: 42,
        bytes_transferred: 1_000_000,
    }
}

/// Create a mock daemon state with stopped status.
pub fn mock_daemon_stopped() -> DaemonState {
    debug!("TEST DATA: creating mock daemon (stopped)");
    DaemonState {
        status: Status::Stopped,
        uptime: Duration::ZERO,
        version: String::new(),
        config_path: PathBuf::new(),
        socket_path: PathBuf::from("/tmp/rch.sock"),
        builds_today: 0,
        bytes_transferred: 0,
    }
}

/// Create a mock daemon state with error status.
pub fn mock_daemon_error() -> DaemonState {
    DaemonState {
        status: Status::Error,
        uptime: Duration::from_secs(100),
        version: "0.1.0-test".to_string(),
        config_path: PathBuf::new(),
        socket_path: PathBuf::from("/tmp/rch.sock"),
        builds_today: 5,
        bytes_transferred: 50_000,
    }
}

// ============================================================================
// Log View State Mock Generators
// ============================================================================

/// Create a mock log view state.
///
/// # Arguments
///
/// * `build_id` - The build ID being viewed
/// * `line_count` - Number of log lines to generate
pub fn mock_log_view(build_id: &str, line_count: usize) -> LogViewState {
    debug!(
        "TEST DATA: creating mock log view '{}' with {} lines",
        build_id, line_count
    );
    let mut lines = VecDeque::with_capacity(line_count);
    for i in 0..line_count {
        let line = if i % 10 == 0 {
            format!("   Compiling crate-{} v1.0.0", i)
        } else if i % 15 == 0 {
            format!("warning: unused variable `x` in line {}", i)
        } else if i % 20 == 0 {
            format!("error[E0425]: cannot find value `y` in line {}", i)
        } else {
            format!("Building [{}/{}] target/debug/lib{}.rlib", i, line_count, i)
        };
        lines.push_back(line);
    }
    LogViewState {
        build_id: build_id.to_string(),
        lines,
        scroll_offset: 0,
        auto_scroll: true,
    }
}

/// Create an empty log view state.
pub fn mock_log_view_empty(build_id: &str) -> LogViewState {
    LogViewState {
        build_id: build_id.to_string(),
        lines: VecDeque::new(),
        scroll_offset: 0,
        auto_scroll: true,
    }
}

/// Create a log view with scroll position.
pub fn mock_log_view_scrolled(
    build_id: &str,
    line_count: usize,
    scroll_offset: usize,
) -> LogViewState {
    let mut view = mock_log_view(build_id, line_count);
    view.scroll_offset = scroll_offset;
    view.auto_scroll = false;
    view
}

// ============================================================================
// Filter State Mock Generators
// ============================================================================

/// Create a filter state with query.
pub fn mock_filter_with_query(query: &str) -> FilterState {
    FilterState {
        query: query.to_string(),
        worker_filter: None,
        success_only: false,
        failed_only: false,
    }
}

/// Create a filter state with worker filter.
pub fn mock_filter_with_worker(worker_id: &str) -> FilterState {
    FilterState {
        query: String::new(),
        worker_filter: Some(worker_id.to_string()),
        success_only: false,
        failed_only: false,
    }
}

/// Create a filter state for success-only builds.
pub fn mock_filter_success_only() -> FilterState {
    FilterState {
        query: String::new(),
        worker_filter: None,
        success_only: true,
        failed_only: false,
    }
}

/// Create a filter state for failed-only builds.
pub fn mock_filter_failed_only() -> FilterState {
    FilterState {
        query: String::new(),
        worker_filter: None,
        success_only: false,
        failed_only: true,
    }
}

// ============================================================================
// Full TuiState Mock Generators
// ============================================================================

/// Create a default TUI state (empty).
pub fn mock_state() -> TuiState {
    debug!("TEST DATA: creating default mock state");
    TuiState::new()
}

/// Create a TUI state with specified number of workers.
///
/// # Arguments
///
/// * `count` - Number of workers to create
pub fn mock_state_with_workers(count: usize) -> TuiState {
    debug!("TEST DATA: creating mock state with {} workers", count);
    let mut state = TuiState::new();
    state.daemon = mock_daemon_running();
    state.workers = (0..count)
        .map(|i| mock_worker(&format!("worker-{}", i + 1)))
        .collect();
    state
}

/// Create a TUI state with workers and active builds.
///
/// # Arguments
///
/// * `worker_count` - Number of workers to create
/// * `build_count` - Number of active builds to create
pub fn mock_state_with_builds(worker_count: usize, build_count: usize) -> TuiState {
    debug!(
        "TEST DATA: creating mock state with {} workers and {} builds",
        worker_count, build_count
    );
    let mut state = mock_state_with_workers(worker_count);
    state.active_builds = (0..build_count)
        .map(|i| mock_active_build(&format!("build-{}", i + 1), "cargo build"))
        .collect();
    state
}

/// Create a TUI state with build history.
///
/// # Arguments
///
/// * `history_count` - Number of historical builds to create
pub fn mock_state_with_history(history_count: usize) -> TuiState {
    debug!(
        "TEST DATA: creating mock state with {} history items",
        history_count
    );
    let mut state = mock_state_with_workers(2);
    state.build_history = (0..history_count)
        .map(|i| {
            mock_historical_build(
                &format!("h{}", i + 1),
                &format!("cargo build --release ({})", i),
                Some("worker-1"),
                i % 3 != 0, // Every 3rd build fails
            )
        })
        .collect();
    state
}

/// Create a complete TUI state with all features populated.
///
/// This creates a realistic state similar to what the `--mock-data` flag produces.
pub fn mock_state_complete() -> TuiState {
    debug!("TEST DATA: creating complete mock state");
    let mut state = TuiState::new();

    state.daemon = DaemonState {
        status: Status::Running,
        uptime: Duration::from_secs(7200),
        version: "0.1.0-mock".to_string(),
        config_path: PathBuf::from("/etc/rch/config.toml"),
        socket_path: PathBuf::from("/tmp/rch.sock"),
        builds_today: 25,
        bytes_transferred: 500_000_000,
    };

    state.workers = vec![
        mock_worker_with_status("worker-1", WorkerStatus::Healthy, CircuitState::Closed),
        mock_worker_with_status("worker-2", WorkerStatus::Degraded, CircuitState::HalfOpen),
        mock_worker_with_status("worker-3", WorkerStatus::Draining, CircuitState::Open),
        mock_worker_with_slots("worker-4", 7, 8),
    ];

    state.active_builds = vec![
        mock_active_build_with_status(
            "b1",
            "cargo build --release",
            BuildStatus::Compiling,
            Some(65),
        ),
        mock_active_build_with_status("b2", "cargo test", BuildStatus::Syncing, Some(20)),
    ];

    state.build_history = (0..10)
        .map(|i| {
            mock_historical_build_with_duration(
                &format!("h{}", i + 1),
                "cargo test --workspace",
                1000 + (i as u64) * 500,
                i % 5 != 0,
            )
        })
        .collect();

    state
}

/// Create a TUI state with error condition.
pub fn mock_state_with_error(error_msg: &str) -> TuiState {
    let mut state = TuiState::new();
    state.daemon = mock_daemon_stopped();
    state.error = Some(error_msg.to_string());
    state
}

/// Create a TUI state with help overlay visible.
pub fn mock_state_with_help() -> TuiState {
    let mut state = mock_state_complete();
    state.show_help = true;
    state
}

/// Create a TUI state in filter mode.
pub fn mock_state_filter_mode(query: &str) -> TuiState {
    let mut state = mock_state_with_history(10);
    state.filter_mode = true;
    state.filter = mock_filter_with_query(query);
    state
}

/// Create a TUI state with log view open.
pub fn mock_state_with_logs(line_count: usize) -> TuiState {
    let mut state = mock_state_complete();
    state.log_view = Some(mock_log_view("build-log-test", line_count));
    state.selected_panel = Panel::Logs;
    state
}

/// Create a TUI state with specific panel selected.
pub fn mock_state_with_panel(panel: Panel) -> TuiState {
    let mut state = mock_state_complete();
    state.selected_panel = panel;
    state.selected_index = 0;
    state
}

/// Create a TUI state with accessibility options.
pub fn mock_state_accessible(high_contrast: bool, color_blind: ColorBlindMode) -> TuiState {
    let mut state = mock_state_complete();
    state.high_contrast = high_contrast;
    state.color_blind = color_blind;
    state
}

// ============================================================================
// Remediation view fixtures (bd-session-history-remediation-ocv9i.14.4)
//
// Built through the real rch_common assembler so the fixtures exercise the same
// classification path the daemon uses, and the seven mandated dashboard states
// stay in lockstep with the foundation.
// ============================================================================

/// A healthy, command-admissible worker row for remediation fixtures.
fn rem_ready_row(id: &str) -> RemediationWorkerRow {
    RemediationWorkerRow {
        observation: WorkerObservation {
            worker_id: id.to_string(),
            configured: true,
            in_daemon_pool: true,
            reachable: true,
            admin_disabled: false,
            temporarily_bypassed: false,
            facts_known: true,
            command_admissible: true,
        },
        disk_level: DiskLevel::Ok,
        reclaiming: false,
        free_ratio: Some(0.7),
        slots_used: 2,
        slots_total: 8,
        telemetry_known: true,
        telemetry_fresh: true,
        telemetry_age_secs: Some(5),
        recovered_pending_canary: false,
        absent_secs: None,
    }
}

fn rem_view(
    rows: &[RemediationWorkerRow],
    jobs: JobsInput,
    proof: ProofQueueInput,
) -> RemediationView {
    let inputs = build_inputs(rows, jobs, proof, Vec::new(), 300);
    assemble(&inputs, 1_700_000_000_000)
}

/// Healthy fleet: every band nominal.
pub fn mock_remediation_view_healthy() -> RemediationView {
    rem_view(
        &[
            rem_ready_row("css"),
            rem_ready_row("ovh-a"),
            rem_ready_row("ovh-b"),
        ],
        JobsInput {
            active: 1,
            queued: 0,
            stuck: 0,
        },
        ProofQueueInput::default(),
    )
}

/// Degraded: one worker temporarily bypassed (self-healing in progress).
pub fn mock_remediation_view_degraded() -> RemediationView {
    let mut bypassed = rem_ready_row("ovh-a");
    bypassed.observation.temporarily_bypassed = true;
    rem_view(
        &[rem_ready_row("css"), bypassed, rem_ready_row("ovh-b")],
        JobsInput::default(),
        ProofQueueInput::default(),
    )
}

/// No admissible workers: all live but none has trustworthy capability facts.
pub fn mock_remediation_view_no_admissible() -> RemediationView {
    let mut a = rem_ready_row("css");
    a.observation.facts_known = false;
    let mut b = rem_ready_row("ovh-a");
    b.observation.facts_known = false;
    rem_view(&[a, b], JobsInput::default(), ProofQueueInput::default())
}

/// Proof queued: deferred-proof conveyor working through queued/replaying.
pub fn mock_remediation_view_proof_queued() -> RemediationView {
    rem_view(
        &[rem_ready_row("css"), rem_ready_row("ovh-a")],
        JobsInput::default(),
        ProofQueueInput {
            queued: 3,
            replaying: 1,
            ..ProofQueueInput::default()
        },
    )
}

/// Disk pressure: a worker at critical pressure with no reclaim (operator).
pub fn mock_remediation_view_disk_pressure() -> RemediationView {
    let mut crit = rem_ready_row("css");
    crit.disk_level = DiskLevel::Critical;
    crit.free_ratio = Some(0.02);
    rem_view(
        &[crit, rem_ready_row("ovh-a")],
        JobsInput::default(),
        ProofQueueInput::default(),
    )
}

/// Stale telemetry: a worker's telemetry exceeds the freshness window.
pub fn mock_remediation_view_stale_telemetry() -> RemediationView {
    let mut stale = rem_ready_row("css");
    stale.telemetry_fresh = false;
    stale.telemetry_age_secs = Some(1200);
    rem_view(
        &[stale, rem_ready_row("ovh-a")],
        JobsInput::default(),
        ProofQueueInput::default(),
    )
}

/// Auto-rejoin pending: a recovered worker probing back in (self-healing).
pub fn mock_remediation_view_auto_rejoin_pending() -> RemediationView {
    let mut canary = rem_ready_row("css");
    canary.observation.temporarily_bypassed = true;
    canary.recovered_pending_canary = true;
    rem_view(
        &[canary, rem_ready_row("ovh-a")],
        JobsInput::default(),
        ProofQueueInput::default(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use tracing::info;

    fn init_test_logging() {
        let _ = tracing_subscriber::fmt()
            .with_test_writer()
            .with_max_level(tracing::Level::DEBUG)
            .try_init();
    }

    // ==================== Worker Tests ====================

    #[test]
    fn test_mock_worker_basic() {
        init_test_logging();
        info!("TEST START: test_mock_worker_basic");
        let worker = mock_worker("test-1");
        assert_eq!(worker.id, "test-1");
        assert_eq!(worker.host, "test-1.local");
        assert_eq!(worker.status, WorkerStatus::Healthy);
        assert_eq!(worker.circuit, CircuitState::Closed);
        assert_eq!(worker.total_slots, 8);
        assert_eq!(worker.used_slots, 1);
        info!("TEST PASS: test_mock_worker_basic");
    }

    #[test]
    fn test_mock_worker_with_status() {
        init_test_logging();
        info!("TEST START: test_mock_worker_with_status");
        let worker = mock_worker_with_status("w1", WorkerStatus::Degraded, CircuitState::HalfOpen);
        assert_eq!(worker.status, WorkerStatus::Degraded);
        assert_eq!(worker.circuit, CircuitState::HalfOpen);
        info!("TEST PASS: test_mock_worker_with_status");
    }

    #[test]
    fn test_mock_worker_with_slots() {
        init_test_logging();
        info!("TEST START: test_mock_worker_with_slots");
        let worker = mock_worker_with_slots("w1", 5, 16);
        assert_eq!(worker.used_slots, 5);
        assert_eq!(worker.total_slots, 16);
        info!("TEST PASS: test_mock_worker_with_slots");
    }

    #[test]
    fn test_mock_worker_with_host() {
        init_test_logging();
        info!("TEST START: test_mock_worker_with_host");
        let worker = mock_worker_with_host("w1", "192.168.1.100");
        assert_eq!(worker.host, "192.168.1.100");
        info!("TEST PASS: test_mock_worker_with_host");
    }

    // ==================== Active Build Tests ====================

    #[test]
    fn test_mock_active_build_basic() {
        init_test_logging();
        info!("TEST START: test_mock_active_build_basic");
        let build = mock_active_build("b1", "cargo build");
        assert_eq!(build.id, "b1");
        assert_eq!(build.command, "cargo build");
        assert_eq!(build.status, BuildStatus::Compiling);
        assert!(build.worker.is_some());
        assert!(build.progress.is_some());
        info!("TEST PASS: test_mock_active_build_basic");
    }

    #[test]
    fn test_mock_active_build_with_status() {
        init_test_logging();
        info!("TEST START: test_mock_active_build_with_status");
        let build = mock_active_build_with_status("b1", "cmd", BuildStatus::Syncing, Some(30));
        assert_eq!(build.status, BuildStatus::Syncing);
        assert_eq!(build.progress.as_ref().unwrap().percent, Some(30));
        info!("TEST PASS: test_mock_active_build_with_status");
    }

    #[test]
    fn test_mock_pending_build() {
        init_test_logging();
        info!("TEST START: test_mock_pending_build");
        let build = mock_pending_build("b1", "cargo test");
        assert_eq!(build.status, BuildStatus::Pending);
        assert!(build.worker.is_none());
        assert!(build.progress.is_none());
        info!("TEST PASS: test_mock_pending_build");
    }

    // ==================== Historical Build Tests ====================

    #[test]
    fn test_mock_historical_build_success() {
        init_test_logging();
        info!("TEST START: test_mock_historical_build_success");
        let build = mock_historical_build("h1", "cargo build", Some("w1"), true);
        assert_eq!(build.id, "h1");
        assert!(build.success);
        assert_eq!(build.exit_code, Some(0));
        assert_eq!(build.worker, Some("w1".to_string()));
        info!("TEST PASS: test_mock_historical_build_success");
    }

    #[test]
    fn test_mock_historical_build_failure() {
        init_test_logging();
        info!("TEST START: test_mock_historical_build_failure");
        let build = mock_historical_build("h1", "cargo test", Some("w1"), false);
        assert!(!build.success);
        assert_eq!(build.exit_code, Some(1));
        info!("TEST PASS: test_mock_historical_build_failure");
    }

    #[test]
    fn test_mock_local_build() {
        init_test_logging();
        info!("TEST START: test_mock_local_build");
        let build = mock_local_build("h1", "cargo check", true);
        assert!(build.worker.is_none());
        info!("TEST PASS: test_mock_local_build");
    }

    #[test]
    fn test_mock_historical_build_with_duration() {
        init_test_logging();
        info!("TEST START: test_mock_historical_build_with_duration");
        let build = mock_historical_build_with_duration("h1", "cmd", 5000, true);
        assert_eq!(build.duration_ms, 5000);
        info!("TEST PASS: test_mock_historical_build_with_duration");
    }

    // ==================== Daemon State Tests ====================

    #[test]
    fn test_mock_daemon_running() {
        init_test_logging();
        info!("TEST START: test_mock_daemon_running");
        let daemon = mock_daemon_running();
        assert_eq!(daemon.status, Status::Running);
        assert!(!daemon.version.is_empty());
        assert!(daemon.uptime > Duration::ZERO);
        info!("TEST PASS: test_mock_daemon_running");
    }

    #[test]
    fn test_mock_daemon_stopped() {
        init_test_logging();
        info!("TEST START: test_mock_daemon_stopped");
        let daemon = mock_daemon_stopped();
        assert_eq!(daemon.status, Status::Stopped);
        assert_eq!(daemon.uptime, Duration::ZERO);
        info!("TEST PASS: test_mock_daemon_stopped");
    }

    #[test]
    fn test_mock_daemon_error() {
        init_test_logging();
        info!("TEST START: test_mock_daemon_error");
        let daemon = mock_daemon_error();
        assert_eq!(daemon.status, Status::Error);
        info!("TEST PASS: test_mock_daemon_error");
    }

    // ==================== Log View Tests ====================

    #[test]
    fn test_mock_log_view() {
        init_test_logging();
        info!("TEST START: test_mock_log_view");
        let log_view = mock_log_view("b1", 50);
        assert_eq!(log_view.build_id, "b1");
        assert_eq!(log_view.lines.len(), 50);
        assert!(log_view.auto_scroll);
        info!("TEST PASS: test_mock_log_view");
    }

    #[test]
    fn test_mock_log_view_empty() {
        init_test_logging();
        info!("TEST START: test_mock_log_view_empty");
        let log_view = mock_log_view_empty("b1");
        assert!(log_view.lines.is_empty());
        info!("TEST PASS: test_mock_log_view_empty");
    }

    #[test]
    fn test_mock_log_view_scrolled() {
        init_test_logging();
        info!("TEST START: test_mock_log_view_scrolled");
        let log_view = mock_log_view_scrolled("b1", 100, 25);
        assert_eq!(log_view.scroll_offset, 25);
        assert!(!log_view.auto_scroll);
        info!("TEST PASS: test_mock_log_view_scrolled");
    }

    // ==================== Filter State Tests ====================

    #[test]
    fn test_mock_filter_with_query() {
        init_test_logging();
        info!("TEST START: test_mock_filter_with_query");
        let filter = mock_filter_with_query("cargo");
        assert_eq!(filter.query, "cargo");
        assert!(filter.worker_filter.is_none());
        info!("TEST PASS: test_mock_filter_with_query");
    }

    #[test]
    fn test_mock_filter_with_worker() {
        init_test_logging();
        info!("TEST START: test_mock_filter_with_worker");
        let filter = mock_filter_with_worker("worker-1");
        assert_eq!(filter.worker_filter, Some("worker-1".to_string()));
        info!("TEST PASS: test_mock_filter_with_worker");
    }

    #[test]
    fn test_mock_filter_success_only() {
        init_test_logging();
        info!("TEST START: test_mock_filter_success_only");
        let filter = mock_filter_success_only();
        assert!(filter.success_only);
        assert!(!filter.failed_only);
        info!("TEST PASS: test_mock_filter_success_only");
    }

    #[test]
    fn test_mock_filter_failed_only() {
        init_test_logging();
        info!("TEST START: test_mock_filter_failed_only");
        let filter = mock_filter_failed_only();
        assert!(!filter.success_only);
        assert!(filter.failed_only);
        info!("TEST PASS: test_mock_filter_failed_only");
    }

    // ==================== Full State Tests ====================

    #[test]
    fn test_mock_state_empty() {
        init_test_logging();
        info!("TEST START: test_mock_state_empty");
        let state = mock_state();
        assert!(state.workers.is_empty());
        assert!(state.active_builds.is_empty());
        assert!(state.build_history.is_empty());
        info!("TEST PASS: test_mock_state_empty");
    }

    #[test]
    fn test_mock_state_with_workers() {
        init_test_logging();
        info!("TEST START: test_mock_state_with_workers");
        let state = mock_state_with_workers(5);
        assert_eq!(state.workers.len(), 5);
        assert_eq!(state.daemon.status, Status::Running);
        for (i, w) in state.workers.iter().enumerate() {
            assert_eq!(w.id, format!("worker-{}", i + 1));
        }
        info!("TEST PASS: test_mock_state_with_workers");
    }

    #[test]
    fn test_mock_state_with_builds() {
        init_test_logging();
        info!("TEST START: test_mock_state_with_builds");
        let state = mock_state_with_builds(2, 3);
        assert_eq!(state.workers.len(), 2);
        assert_eq!(state.active_builds.len(), 3);
        info!("TEST PASS: test_mock_state_with_builds");
    }

    #[test]
    fn test_mock_state_with_history() {
        init_test_logging();
        info!("TEST START: test_mock_state_with_history");
        let state = mock_state_with_history(10);
        assert_eq!(state.build_history.len(), 10);
        info!("TEST PASS: test_mock_state_with_history");
    }

    #[test]
    fn test_mock_state_complete() {
        init_test_logging();
        info!("TEST START: test_mock_state_complete");
        let state = mock_state_complete();
        assert!(!state.workers.is_empty());
        assert!(!state.active_builds.is_empty());
        assert!(!state.build_history.is_empty());
        assert_eq!(state.daemon.status, Status::Running);
        info!("TEST PASS: test_mock_state_complete");
    }

    #[test]
    fn test_mock_state_with_error() {
        init_test_logging();
        info!("TEST START: test_mock_state_with_error");
        let state = mock_state_with_error("Connection refused");
        assert_eq!(state.error, Some("Connection refused".to_string()));
        assert_eq!(state.daemon.status, Status::Stopped);
        info!("TEST PASS: test_mock_state_with_error");
    }

    #[test]
    fn test_mock_state_with_help() {
        init_test_logging();
        info!("TEST START: test_mock_state_with_help");
        let state = mock_state_with_help();
        assert!(state.show_help);
        info!("TEST PASS: test_mock_state_with_help");
    }

    #[test]
    fn test_mock_state_filter_mode() {
        init_test_logging();
        info!("TEST START: test_mock_state_filter_mode");
        let state = mock_state_filter_mode("test");
        assert!(state.filter_mode);
        assert_eq!(state.filter.query, "test");
        info!("TEST PASS: test_mock_state_filter_mode");
    }

    #[test]
    fn test_mock_state_with_logs() {
        init_test_logging();
        info!("TEST START: test_mock_state_with_logs");
        let state = mock_state_with_logs(50);
        assert!(state.log_view.is_some());
        assert_eq!(state.log_view.as_ref().unwrap().lines.len(), 50);
        assert_eq!(state.selected_panel, Panel::Logs);
        info!("TEST PASS: test_mock_state_with_logs");
    }

    #[test]
    fn test_mock_state_with_panel() {
        init_test_logging();
        info!("TEST START: test_mock_state_with_panel");
        let state = mock_state_with_panel(Panel::BuildHistory);
        assert_eq!(state.selected_panel, Panel::BuildHistory);
        assert_eq!(state.selected_index, 0);
        info!("TEST PASS: test_mock_state_with_panel");
    }

    #[test]
    fn test_mock_state_accessible() {
        init_test_logging();
        info!("TEST START: test_mock_state_accessible");
        let state = mock_state_accessible(true, ColorBlindMode::Deuteranopia);
        assert!(state.high_contrast);
        assert_eq!(state.color_blind, ColorBlindMode::Deuteranopia);
        info!("TEST PASS: test_mock_state_accessible");
    }
}