apr-cli 0.4.13

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

    #[test]
    fn test_tab_titles() {
        let titles = Tab::titles();
        assert_eq!(titles.len(), 4);
        assert!(titles[0].contains("Overview"));
        assert!(titles[1].contains("Tensors"));
        assert!(titles[2].contains("Stats"));
        assert!(titles[3].contains("Help"));
    }

    #[test]
    fn test_tab_index() {
        assert_eq!(Tab::Overview.index(), 0);
        assert_eq!(Tab::Tensors.index(), 1);
        assert_eq!(Tab::Stats.index(), 2);
        assert_eq!(Tab::Help.index(), 3);
    }

    #[test]
    fn test_app_new_no_file() {
        let app = App::new(None);
        assert!(app.file_path.is_none());
        assert!(app.reader.is_none());
        assert!(app.tensors.is_empty());
        assert!(!app.should_quit);
    }

    #[test]
    fn test_app_next_tab() {
        let mut app = App::new(None);
        assert_eq!(app.current_tab, Tab::Overview);
        app.next_tab();
        assert_eq!(app.current_tab, Tab::Tensors);
        app.next_tab();
        assert_eq!(app.current_tab, Tab::Stats);
        app.next_tab();
        assert_eq!(app.current_tab, Tab::Help);
        app.next_tab();
        assert_eq!(app.current_tab, Tab::Overview);
    }

    #[test]
    fn test_app_prev_tab() {
        let mut app = App::new(None);
        assert_eq!(app.current_tab, Tab::Overview);
        app.prev_tab();
        assert_eq!(app.current_tab, Tab::Help);
        app.prev_tab();
        assert_eq!(app.current_tab, Tab::Stats);
        app.prev_tab();
        assert_eq!(app.current_tab, Tab::Tensors);
        app.prev_tab();
        assert_eq!(app.current_tab, Tab::Overview);
    }

    #[test]
    fn test_truncate_name_short() {
        assert_eq!(truncate_name("short", 10), "short");
    }

    #[test]
    fn test_truncate_name_long() {
        assert_eq!(
            truncate_name("this_is_a_very_long_tensor_name", 20),
            "this_is_a_very_lo..."
        );
    }

    #[test]
    fn test_tensor_selection_empty() {
        let mut app = App::new(None);
        app.select_next_tensor();
        assert!(app.tensor_list_state.selected().is_none());
        app.select_prev_tensor();
        assert!(app.tensor_list_state.selected().is_none());
    }

    // TUI frame capture tests using ratatui's TestBackend
    // Converts to probar TuiFrame for assertions
    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 app to a test backend and capture frame
        fn render_frame(app: &mut App, width: u16, height: u16) -> TuiFrame {
            let backend = TestBackend::new(width, height);
            let mut terminal = Terminal::new(backend).unwrap();
            terminal.draw(|f| ui(f, app)).unwrap();
            // Convert ratatui buffer to probar TuiFrame
            TuiFrame::from_buffer(terminal.backend().buffer(), 0)
        }

        #[test]
        fn test_tui_overview_no_file() {
            let mut app = App::new(None);
            let frame = render_frame(&mut app, 80, 24);

            // Should show "No model loaded" message
            assert!(
                frame.contains("No model loaded"),
                "Frame should show no model message:\n{}",
                frame.as_text()
            );
        }

        #[test]
        fn test_tui_tabs_displayed() {
            let mut app = App::new(None);
            let frame = render_frame(&mut app, 80, 24);

            // Should show all tab titles
            assert!(frame.contains("Overview"), "Should show Overview tab");
            assert!(frame.contains("Tensors"), "Should show Tensors tab");
            assert!(frame.contains("Stats"), "Should show Stats tab");
            assert!(frame.contains("Help"), "Should show Help tab");
        }

        #[test]
        fn test_tui_title_bar() {
            let mut app = App::new(None);
            let frame = render_frame(&mut app, 80, 24);

            // Should show title
            assert!(
                frame.contains("APR Model Inspector"),
                "Should show title bar"
            );
        }

        #[test]
        fn test_tui_help_view() {
            let mut app = App::new(None);
            app.current_tab = Tab::Help;
            let frame = render_frame(&mut app, 80, 24);

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

        #[test]
        fn test_tui_stats_empty() {
            let mut app = App::new(None);
            app.current_tab = Tab::Stats;
            let frame = render_frame(&mut app, 80, 24);

            // Should show no data message
            assert!(
                frame.contains("No tensor data"),
                "Stats should show no data message"
            );
        }

        #[test]
        fn test_tui_tensors_empty() {
            let mut app = App::new(None);
            app.current_tab = Tab::Tensors;
            let frame = render_frame(&mut app, 80, 24);

            // Should show tensor browser (even if empty)
            assert!(
                frame.contains("Tensor") || frame.contains("navigate"),
                "Tensors view should be shown"
            );
        }

        #[test]
        fn test_tui_frame_dimensions() {
            let mut app = App::new(None);
            let frame = render_frame(&mut app, 100, 30);

            assert_eq!(frame.width(), 100, "Frame width should match");
            assert_eq!(frame.height(), 30, "Frame height should match");
        }

        // =========================================================================
        // ADVANCED PROBAR TESTS: Playwright-style assertions
        // =========================================================================

        #[test]
        fn test_probar_chained_assertions() {
            let mut app = App::new(None);
            let frame = render_frame(&mut app, 80, 24);

            // Playwright-style chained assertions
            let mut assertion = expect_frame(&frame);
            let r1 = assertion.to_contain_text("APR Model Inspector");
            assert!(r1.is_ok(), "Should contain title");

            let r2 = assertion.to_contain_text("Overview");
            assert!(r2.is_ok(), "Should contain Overview");

            let r3 = assertion.to_contain_text("Navigation");
            assert!(r3.is_ok(), "Should contain Navigation");

            let r4 = assertion.not_to_contain_text("ERROR");
            assert!(r4.is_ok(), "Should not contain ERROR");
        }

        #[test]
        fn test_probar_soft_assertions() {
            let mut app = App::new(None);
            let frame = render_frame(&mut app, 80, 24);

            // Soft assertions collect all failures instead of failing fast
            let mut assertion = expect_frame(&frame).soft();
            let _ = assertion.to_contain_text("APR Model Inspector");
            let _ = assertion.to_contain_text("Overview");
            let _ = assertion.to_contain_text("Help");
            let _ = assertion.to_have_size(80, 24);

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

        #[test]
        fn test_probar_regex_matching() {
            let mut app = App::new(None);
            app.current_tab = Tab::Help;
            let frame = render_frame(&mut app, 80, 24);

            // Regex pattern matching for dynamic content
            let mut assertion = expect_frame(&frame);
            let result = assertion.to_match(r"Version: \d+\.\d+\.\d+");

            assert!(result.is_ok(), "Should match version pattern");
        }

        // =========================================================================
        // SNAPSHOT TESTING: Golden file comparisons
        // =========================================================================

        #[test]
        fn test_snapshot_creation_and_matching() {
            let mut app = App::new(None);
            let frame = render_frame(&mut app, 80, 24);

            // Create snapshot from frame
            let snapshot = TuiSnapshot::from_frame("overview_no_file", &frame);

            assert_eq!(snapshot.name, "overview_no_file");
            assert_eq!(snapshot.width, 80);
            assert_eq!(snapshot.height, 24);
            assert!(!snapshot.hash.is_empty(), "Hash should be computed");

            // Snapshots with same content should match
            let frame2 = render_frame(&mut app, 80, 24);
            let snapshot2 = TuiSnapshot::from_frame("overview_no_file_2", &frame2);

            assert!(
                snapshot.matches(&snapshot2),
                "Identical frames should have matching snapshots"
            );
        }

        #[test]
        fn test_snapshot_with_metadata() {
            let mut app = App::new(None);
            let frame = render_frame(&mut app, 80, 24);

            let snapshot = TuiSnapshot::from_frame("test", &frame)
                .with_metadata("test_name", "overview_no_file")
                .with_metadata("tab", "overview")
                .with_metadata("model_loaded", "false");

            assert_eq!(
                snapshot.metadata.get("test_name"),
                Some(&"overview_no_file".to_string())
            );
            assert_eq!(snapshot.metadata.get("tab"), Some(&"overview".to_string()));
        }

        #[test]
        fn test_snapshot_different_tabs_differ() {
            let mut app = App::new(None);

            // Overview tab
            let frame_overview = render_frame(&mut app, 80, 24);
            let snap_overview = TuiSnapshot::from_frame("overview", &frame_overview);

            // Help tab
            app.current_tab = Tab::Help;
            let frame_help = render_frame(&mut app, 80, 24);
            let snap_help = TuiSnapshot::from_frame("help", &frame_help);

            // Different tabs should NOT match
            assert!(
                !snap_overview.matches(&snap_help),
                "Different tabs should have different snapshots"
            );
        }

        // =========================================================================
        // FRAME SEQUENCE: Animation / state transition testing
        // =========================================================================

        #[test]
        fn test_frame_sequence_tab_navigation() {
            let mut app = App::new(None);
            let mut sequence = FrameSequence::new("tab_navigation");

            // Capture frame for each tab (simulating user navigation)
            app.current_tab = Tab::Overview;
            sequence.add_frame(&render_frame(&mut app, 80, 24));

            app.current_tab = Tab::Tensors;
            sequence.add_frame(&render_frame(&mut app, 80, 24));

            app.current_tab = Tab::Stats;
            sequence.add_frame(&render_frame(&mut app, 80, 24));

            app.current_tab = Tab::Help;
            sequence.add_frame(&render_frame(&mut app, 80, 24));

            assert_eq!(sequence.len(), 4, "Should have 4 frames");
            assert!(!sequence.is_empty());

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

        #[test]
        fn test_frame_sequence_diff_detection() {
            let mut app = App::new(None);

            // Create two sequences with mostly same content
            let mut seq1 = FrameSequence::new("seq1");
            let mut seq2 = FrameSequence::new("seq2");

            // Same: Overview
            app.current_tab = Tab::Overview;
            seq1.add_frame(&render_frame(&mut app, 80, 24));
            seq2.add_frame(&render_frame(&mut app, 80, 24));

            // Different: Tensors vs Stats
            app.current_tab = Tab::Tensors;
            seq1.add_frame(&render_frame(&mut app, 80, 24));
            app.current_tab = Tab::Stats;
            seq2.add_frame(&render_frame(&mut app, 80, 24));

            // Same: Help
            app.current_tab = Tab::Help;
            seq1.add_frame(&render_frame(&mut app, 80, 24));
            seq2.add_frame(&render_frame(&mut app, 80, 24));

            // Find differing frames
            let diffs = seq1.diff_frames(&seq2);
            assert_eq!(diffs, vec![1], "Only frame index 1 should differ");
        }

        // =========================================================================
        // SNAPSHOT MANAGER: Persistent golden file testing
        // =========================================================================

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

            let temp_dir = TempDir::new().unwrap();
            let manager = SnapshotManager::new(temp_dir.path());

            let mut app = App::new(None);
            let frame = render_frame(&mut app, 80, 24);

            // First run: creates snapshot
            let result = manager.assert_snapshot("tui_overview", &frame);
            assert!(result.is_ok(), "First snapshot should be created");
            assert!(manager.exists("tui_overview"), "Snapshot file should exist");

            // Second run: matches existing
            let result2 = manager.assert_snapshot("tui_overview", &frame);
            assert!(result2.is_ok(), "Same frame should match snapshot");

            // List snapshots
            let list = manager.list().unwrap();
            assert!(list.contains(&"tui_overview".to_string()));
        }

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

            let temp_dir = TempDir::new().unwrap();
            let manager = SnapshotManager::new(temp_dir.path());

            let mut app = App::new(None);

            // Create snapshot with Overview tab
            app.current_tab = Tab::Overview;
            let frame1 = render_frame(&mut app, 80, 24);
            manager.assert_snapshot("test_snap", &frame1).unwrap();

            // Try to assert with Help tab (should fail)
            app.current_tab = Tab::Help;
            let frame2 = render_frame(&mut app, 80, 24);
            let result = manager.assert_snapshot("test_snap", &frame2);

            assert!(result.is_err(), "Changed frame should fail snapshot");
        }

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

            let temp_dir = TempDir::new().unwrap();
            let manager = SnapshotManager::new(temp_dir.path()).with_update_mode(true);

            let mut app = App::new(None);

            // Create initial snapshot
            app.current_tab = Tab::Overview;
            let frame1 = render_frame(&mut app, 80, 24);
            manager.assert_snapshot("updatable", &frame1).unwrap();

            // Update with different content (update mode allows this)
            app.current_tab = Tab::Help;
            let frame2 = render_frame(&mut app, 80, 24);
            let result = manager.assert_snapshot("updatable", &frame2);

            assert!(result.is_ok(), "Update mode should allow changes");

            // Verify snapshot was updated
            let loaded = manager.load("updatable").unwrap();
            assert!(
                loaded.content.iter().any(|l| l.contains("Keyboard")),
                "Snapshot should now contain Help content"
            );
        }

        // =========================================================================
        // PIXEL-LEVEL LINE ASSERTIONS
        // =========================================================================

        #[test]
        fn test_line_level_assertions() {
            let mut app = App::new(None);
            app.current_tab = Tab::Help;
            let frame = render_frame(&mut app, 80, 24);

            let mut assertion = expect_frame(&frame);
            let result = assertion.to_contain_text("Keyboard Shortcuts");
            assert!(result.is_ok());
        }

        #[test]
        fn test_frame_identical_comparison() {
            let mut app = App::new(None);

            let frame1 = render_frame(&mut app, 80, 24);
            let frame2 = render_frame(&mut app, 80, 24);

            let mut assertion = expect_frame(&frame1);
            let result = assertion.to_be_identical_to(&frame2);
            assert!(result.is_ok(), "Same state should produce identical frames");
        }

        #[test]
        fn test_frame_non_identical_detection() {
            let mut app = App::new(None);

            app.current_tab = Tab::Overview;
            let frame1 = render_frame(&mut app, 80, 24);

            app.current_tab = Tab::Help;
            let frame2 = render_frame(&mut app, 80, 24);

            let mut assertion = expect_frame(&frame1);
            let result = assertion.to_be_identical_to(&frame2);
            assert!(result.is_err(), "Different tabs should not be identical");
        }
    }