apr-cli 0.4.12

CLI tool for APR model inspection, debugging, and operations
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

    #[test]
    fn test_federation_app_should_quit() {
        let catalog = Arc::new(ModelCatalog::new());
        let health = Arc::new(HealthChecker::default());
        let circuit_breaker = Arc::new(CircuitBreaker::default());
        let mut app = FederationApp::new(catalog, health, circuit_breaker);

        assert!(!app.should_quit);
        app.should_quit = true;
        assert!(app.should_quit);
    }

    #[test]
    fn test_federation_app_policies_count() {
        let catalog = Arc::new(ModelCatalog::new());
        let health = Arc::new(HealthChecker::default());
        let circuit_breaker = Arc::new(CircuitBreaker::default());
        let app = FederationApp::new(catalog, health, circuit_breaker);

        // Default policies: health, latency, privacy, locality, cost
        assert_eq!(app.policies.len(), 5);
        assert_eq!(app.policies[0].name, "health");
        assert_eq!(app.policies[1].name, "latency");
        assert_eq!(app.policies[2].name, "privacy");
        assert_eq!(app.policies[3].name, "locality");
        assert_eq!(app.policies[4].name, "cost");
    }

    #[test]
    fn test_federation_app_max_history_default() {
        let catalog = Arc::new(ModelCatalog::new());
        let health = Arc::new(HealthChecker::default());
        let circuit_breaker = Arc::new(CircuitBreaker::default());
        let app = FederationApp::new(catalog, health, circuit_breaker);

        assert_eq!(app.max_history, 100);
    }

    // =========================================================================
    // Probar TUI Frame Tests
    // =========================================================================

    mod tui_frame_tests {
        use super::*;
        use jugar_probar::tui::{
            expect_frame, FrameSequence, SnapshotManager, TuiFrame, TuiSnapshot,
        };
        use ratatui::backend::TestBackend;
        use ratatui::Terminal;

        /// Helper to render federation app to a test backend
        fn render_frame(app: &FederationApp, width: u16, height: u16) -> TuiFrame {
            let backend = TestBackend::new(width, height);
            let mut terminal = Terminal::new(backend).expect("terminal creation");
            terminal
                .draw(|f| render_federation_dashboard(f, app))
                .expect("draw");
            TuiFrame::from_buffer(terminal.backend().buffer(), 0)
        }

        fn create_test_app() -> FederationApp {
            let catalog = Arc::new(ModelCatalog::new());
            let health = Arc::new(HealthChecker::default());
            let circuit_breaker = Arc::new(CircuitBreaker::default());
            FederationApp::new(catalog, health, circuit_breaker)
        }

        #[test]
        fn test_federation_tui_title_displayed() {
            let app = create_test_app();
            let frame = render_frame(&app, 100, 30);

            assert!(
                frame.contains("Federation Gateway"),
                "Should show title: {}",
                frame.as_text()
            );
        }

        #[test]
        fn test_federation_tui_all_tabs_displayed() {
            let app = create_test_app();
            let frame = render_frame(&app, 100, 30);

            assert!(frame.contains("Catalog"), "Should show Catalog tab");
            assert!(frame.contains("Health"), "Should show Health tab");
            assert!(frame.contains("Routing"), "Should show Routing tab");
            assert!(frame.contains("Circuits"), "Should show Circuits tab");
            assert!(frame.contains("Stats"), "Should show Stats tab");
            assert!(frame.contains("Policies"), "Should show Policies tab");
            assert!(frame.contains("Help"), "Should show Help tab");
        }

        #[test]
        fn test_federation_tui_catalog_empty() {
            let app = create_test_app();
            let frame = render_frame(&app, 100, 30);

            // Default tab is catalog, should show empty message
            assert!(
                frame.contains("No models registered") || frame.contains("MODEL CATALOG"),
                "Should show catalog panel"
            );
        }

        #[test]
        fn test_federation_tui_health_tab() {
            let mut app = create_test_app();
            app.current_tab = FederationTab::Health;
            let frame = render_frame(&app, 100, 30);

            assert!(
                frame.contains("NODE HEALTH"),
                "Should show health panel title"
            );
        }

        #[test]
        fn test_federation_tui_routing_tab() {
            let mut app = create_test_app();
            app.current_tab = FederationTab::Routing;
            let frame = render_frame(&app, 100, 30);

            assert!(
                frame.contains("ROUTING DECISIONS"),
                "Should show routing panel title"
            );
        }

        #[test]
        fn test_federation_tui_circuits_tab() {
            let mut app = create_test_app();
            app.current_tab = FederationTab::Circuits;
            let frame = render_frame(&app, 100, 30);

            assert!(
                frame.contains("CIRCUIT BREAKERS"),
                "Should show circuits panel title"
            );
        }

        #[test]
        fn test_federation_tui_stats_tab() {
            let mut app = create_test_app();
            app.current_tab = FederationTab::Stats;
            let frame = render_frame(&app, 100, 30);

            assert!(
                frame.contains("GATEWAY STATS"),
                "Should show stats panel title"
            );
            assert!(
                frame.contains("Total Requests"),
                "Should show request count"
            );
        }

        #[test]
        fn test_federation_tui_policies_tab() {
            let mut app = create_test_app();
            app.current_tab = FederationTab::Policies;
            let frame = render_frame(&app, 100, 30);

            assert!(
                frame.contains("ACTIVE POLICIES"),
                "Should show policies panel title"
            );
            assert!(frame.contains("health"), "Should show health policy");
            assert!(frame.contains("latency"), "Should show latency policy");
        }

        #[test]
        fn test_federation_tui_help_tab() {
            let mut app = create_test_app();
            app.current_tab = FederationTab::Help;
            let frame = render_frame(&app, 100, 30);

            assert!(
                frame.contains("Keyboard Shortcuts"),
                "Should show keyboard shortcuts"
            );
            assert!(frame.contains("q / Esc"), "Should show quit shortcut");
        }

        #[test]
        fn test_federation_tui_frame_dimensions() {
            let app = create_test_app();
            let frame = render_frame(&app, 120, 40);

            assert_eq!(frame.width(), 120, "Frame width");
            assert_eq!(frame.height(), 40, "Frame height");
        }

        // =====================================================================
        // Playwright-style Assertions
        // =====================================================================

        #[test]
        fn test_probar_chained_assertions() {
            let app = create_test_app();
            let frame = render_frame(&app, 100, 30);

            let mut assertion = expect_frame(&frame);
            assert!(assertion.to_contain_text("Federation Gateway").is_ok());
            assert!(assertion.to_contain_text("Navigation").is_ok());
            assert!(assertion.not_to_contain_text("ERROR").is_ok());
        }

        #[test]
        fn test_probar_soft_assertions() {
            let app = create_test_app();
            let frame = render_frame(&app, 100, 30);

            let mut assertion = expect_frame(&frame).soft();
            let _ = assertion.to_contain_text("Federation Gateway");
            let _ = assertion.to_contain_text("Catalog");
            let _ = assertion.to_have_size(100, 30);

            assert!(assertion.errors().is_empty(), "No soft assertion errors");
            assert!(assertion.finalize().is_ok());
        }

        // =====================================================================
        // Snapshot Testing
        // =====================================================================

        #[test]
        fn test_snapshot_creation() {
            let app = create_test_app();
            let frame = render_frame(&app, 100, 30);

            let snapshot = TuiSnapshot::from_frame("federation_catalog", &frame);

            assert_eq!(snapshot.name, "federation_catalog");
            assert_eq!(snapshot.width, 100);
            assert_eq!(snapshot.height, 30);
            assert!(!snapshot.hash.is_empty());
        }

        #[test]
        fn test_snapshot_different_tabs_differ() {
            let mut app = create_test_app();

            app.current_tab = FederationTab::Catalog;
            let frame_catalog = render_frame(&app, 100, 30);
            let snap_catalog = TuiSnapshot::from_frame("catalog", &frame_catalog);

            app.current_tab = FederationTab::Help;
            let frame_help = render_frame(&app, 100, 30);
            let snap_help = TuiSnapshot::from_frame("help", &frame_help);

            assert!(
                !snap_catalog.matches(&snap_help),
                "Different tabs should have different snapshots"
            );
        }

        // =====================================================================
        // Frame Sequence Testing
        // =====================================================================

        #[test]
        fn test_frame_sequence_tab_navigation() {
            let mut app = create_test_app();
            let mut sequence = FrameSequence::new("federation_tabs");

            for tab in [
                FederationTab::Catalog,
                FederationTab::Health,
                FederationTab::Routing,
                FederationTab::Circuits,
                FederationTab::Stats,
                FederationTab::Policies,
                FederationTab::Help,
            ] {
                app.current_tab = tab;
                sequence.add_frame(&render_frame(&app, 100, 30));
            }

            assert_eq!(sequence.len(), 7, "Should have 7 frames");

            let first = sequence.first().expect("first");
            let last = sequence.last().expect("last");
            assert!(!first.matches(last), "First and last should differ");
        }

        // =====================================================================
        // Snapshot Manager Tests
        // =====================================================================

        #[test]
        fn test_snapshot_manager_workflow() {
            use tempfile::TempDir;

            let temp_dir = TempDir::new().expect("temp dir");
            let manager = SnapshotManager::new(temp_dir.path());

            let app = create_test_app();
            let frame = render_frame(&app, 100, 30);

            let result = manager.assert_snapshot("federation_dashboard", &frame);
            assert!(result.is_ok(), "First snapshot should be created");
            assert!(
                manager.exists("federation_dashboard"),
                "Snapshot should exist"
            );

            let result2 = manager.assert_snapshot("federation_dashboard", &frame);
            assert!(result2.is_ok(), "Same frame should match");
        }

        // =====================================================================
        // UX Coverage Tests
        // =====================================================================

        #[test]
        fn test_ux_coverage_federation_elements() {
            use jugar_probar::ux_coverage::{InteractionType, StateId, UxCoverageBuilder};

            let mut tracker = UxCoverageBuilder::new()
                .clickable("tab", "catalog")
                .clickable("tab", "health")
                .clickable("tab", "routing")
                .clickable("tab", "circuits")
                .clickable("tab", "stats")
                .clickable("tab", "policies")
                .clickable("tab", "help")
                .clickable("nav", "next_tab")
                .clickable("nav", "prev_tab")
                .clickable("nav", "next_row")
                .clickable("nav", "prev_row")
                .clickable("nav", "quit")
                .screen("catalog")
                .screen("health")
                .screen("routing")
                .screen("circuits")
                .screen("stats")
                .screen("policies")
                .screen("help")
                .build();

            let mut app = create_test_app();

            // Cover all tabs
            for (tab, name) in [
                (FederationTab::Catalog, "catalog"),
                (FederationTab::Health, "health"),
                (FederationTab::Routing, "routing"),
                (FederationTab::Circuits, "circuits"),
                (FederationTab::Stats, "stats"),
                (FederationTab::Policies, "policies"),
                (FederationTab::Help, "help"),
            ] {
                app.current_tab = tab;
                tracker.record_interaction(
                    &jugar_probar::ux_coverage::ElementId::new("tab", name),
                    InteractionType::Click,
                );
                tracker.record_state(StateId::new("screen", name));
            }

            // Cover navigation
            app.next_tab();
            tracker.record_interaction(
                &jugar_probar::ux_coverage::ElementId::new("nav", "next_tab"),
                InteractionType::Click,
            );

            app.prev_tab();
            tracker.record_interaction(
                &jugar_probar::ux_coverage::ElementId::new("nav", "prev_tab"),
                InteractionType::Click,
            );

            app.select_next();
            tracker.record_interaction(
                &jugar_probar::ux_coverage::ElementId::new("nav", "next_row"),
                InteractionType::Click,
            );

            app.select_prev();
            tracker.record_interaction(
                &jugar_probar::ux_coverage::ElementId::new("nav", "prev_row"),
                InteractionType::Click,
            );

            app.should_quit = true;
            tracker.record_interaction(
                &jugar_probar::ux_coverage::ElementId::new("nav", "quit"),
                InteractionType::Click,
            );

            let report = tracker.generate_report();
            println!("{}", report.summary());

            assert!(report.is_complete, "UX coverage must be COMPLETE");
            assert!(tracker.meets(100.0), "Coverage: {}", tracker.summary());
        }

        #[test]
        fn test_ux_coverage_with_gui_macro() {
            use jugar_probar::gui_coverage;

            let mut gui = gui_coverage! {
                buttons: [
                    "tab_catalog", "tab_health", "tab_routing",
                    "tab_circuits", "tab_stats", "tab_policies", "tab_help",
                    "nav_next", "nav_prev", "quit"
                ],
                screens: [
                    "catalog", "health", "routing",
                    "circuits", "stats", "policies", "help"
                ]
            };

            // Cover all buttons
            gui.click("tab_catalog");
            gui.click("tab_health");
            gui.click("tab_routing");
            gui.click("tab_circuits");
            gui.click("tab_stats");
            gui.click("tab_policies");
            gui.click("tab_help");
            gui.click("nav_next");
            gui.click("nav_prev");
            gui.click("quit");

            // Cover all screens
            gui.visit("catalog");
            gui.visit("health");
            gui.visit("routing");
            gui.visit("circuits");
            gui.visit("stats");
            gui.visit("policies");
            gui.visit("help");

            assert!(gui.is_complete(), "Should have 100% coverage");
            assert!(gui.meets(100.0), "Coverage: {}", gui.summary());

            let report = gui.generate_report();
            println!(
                "Federation UX Coverage:\n  Elements: {}/{}\n  States: {}/{}\n  Overall: {:.1}%",
                report.covered_elements,
                report.total_elements,
                report.covered_states,
                report.total_states,
                report.overall_coverage * 100.0
            );
        }
    }