debtmap 0.17.0

Code complexity and technical debt analyzer
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
//! Pure action determination for list view keyboard handling.
//!
//! This module separates the pure logic of "which action does this key trigger?"
//! from the effectful "execute this action" code. Following Stillwater philosophy:
//! - Pure core: `determine_list_action` maps key + context → action
//! - Imperative shell: `execute_list_action` performs the actual mutations
//!
//! This separation enables:
//! - Unit testing the action determination without mocking app state
//! - Property testing key-action mappings
//! - Clear documentation of what each key does

use crossterm::event::{KeyCode, KeyEvent};

/// Actions that can be triggered from list view.
///
/// This enum represents all possible user intents in the list view,
/// independent of how they're executed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListAction {
    /// Quit the application.
    Quit,

    /// Move selection up by one item.
    MoveUp,

    /// Move selection down by one item.
    MoveDown,

    /// Jump to first item.
    JumpToTop,

    /// Jump to last item.
    JumpToBottom,

    /// Move selection up by a page.
    PageUp,

    /// Move selection down by a page.
    PageDown,

    /// Toggle location grouping.
    ToggleGrouping,

    /// Enter detail view for selected item.
    EnterDetail,

    /// Enter search mode.
    EnterSearch,

    /// Open sort menu.
    OpenSortMenu,

    /// Open filter menu.
    OpenFilterMenu,

    /// Show help overlay.
    ShowHelp,

    /// Copy file path to clipboard.
    CopyPath,

    /// Copy complete item as LLM-optimized markdown to clipboard.
    CopyItemAsLlm,

    /// Open selected file in editor.
    OpenInEditor,
}

/// Context needed to determine if an action is valid.
///
/// This captures the minimal state needed to evaluate guards,
/// allowing the determination function to remain pure.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ListActionContext {
    /// Whether there are any items in the list.
    pub has_items: bool,

    /// Whether an item is currently selected.
    pub has_selection: bool,
}

impl ListActionContext {
    /// Create context for an empty list.
    #[cfg(test)]
    pub fn empty() -> Self {
        Self {
            has_items: false,
            has_selection: false,
        }
    }

    /// Create context for a list with selection.
    #[cfg(test)]
    pub fn with_selection() -> Self {
        Self {
            has_items: true,
            has_selection: true,
        }
    }
}

/// Pure function: Determine which action a key triggers in list view.
///
/// This function is the pure core of key handling. It takes immutable
/// inputs (key event and context) and returns an optional action.
/// No side effects, no mutations, fully testable.
///
/// # Arguments
/// * `key` - The key event to process
/// * `ctx` - Context about current state (for guard evaluation)
///
/// # Returns
/// * `Some(action)` - The action to execute
/// * `None` - Key has no action or guard prevented it
pub fn determine_list_action(key: KeyEvent, ctx: ListActionContext) -> Option<ListAction> {
    match key.code {
        // Quit - always available
        KeyCode::Char('q') => Some(ListAction::Quit),

        // Navigation - always available
        KeyCode::Up | KeyCode::Char('k') => Some(ListAction::MoveUp),
        KeyCode::Down | KeyCode::Char('j') => Some(ListAction::MoveDown),
        KeyCode::Char('g') | KeyCode::Home => Some(ListAction::JumpToTop),
        KeyCode::Char('G') | KeyCode::End => Some(ListAction::JumpToBottom),
        KeyCode::PageUp => Some(ListAction::PageUp),
        KeyCode::PageDown => Some(ListAction::PageDown),

        // Grouping toggle - switches between individual score-ranked items
        // and location-grouped rows.
        KeyCode::Char('u') => Some(ListAction::ToggleGrouping),

        // Detail view - guarded: requires items and selection
        // Enter, Right arrow, and 'l' all open detail view (vim-style navigation)
        KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') => {
            if ctx.has_items && ctx.has_selection {
                Some(ListAction::EnterDetail)
            } else {
                None
            }
        }

        // Search - always available from list
        KeyCode::Char('/') => Some(ListAction::EnterSearch),

        // Sort menu - always available from list
        KeyCode::Char('s') => Some(ListAction::OpenSortMenu),

        // Filter menu - always available from list
        KeyCode::Char('f') => Some(ListAction::OpenFilterMenu),

        // Help - always available
        KeyCode::Char('?') => Some(ListAction::ShowHelp),

        // Clipboard - requires selection
        KeyCode::Char('c') => {
            if ctx.has_selection {
                Some(ListAction::CopyPath)
            } else {
                None
            }
        }

        // Copy item as LLM-optimized markdown - requires selection
        KeyCode::Char('C') => {
            if ctx.has_selection {
                Some(ListAction::CopyItemAsLlm)
            } else {
                None
            }
        }

        // Editor - requires selection
        KeyCode::Char('e') | KeyCode::Char('o') => {
            if ctx.has_selection {
                Some(ListAction::OpenInEditor)
            } else {
                None
            }
        }

        // No action for this key
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::KeyModifiers;

    /// Create a KeyEvent from a KeyCode.
    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    // ============================================================================
    // Quit Action Tests
    // ============================================================================

    #[test]
    fn quit_with_q() {
        let ctx = ListActionContext::empty();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('q')), ctx),
            Some(ListAction::Quit)
        );
    }

    #[test]
    fn quit_works_with_any_context() {
        // Quit should work regardless of state
        for ctx in [
            ListActionContext::empty(),
            ListActionContext::with_selection(),
        ] {
            assert_eq!(
                determine_list_action(key(KeyCode::Char('q')), ctx),
                Some(ListAction::Quit)
            );
        }
    }

    // ============================================================================
    // Navigation Action Tests
    // ============================================================================

    #[test]
    fn move_up_with_up_arrow() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Up), ctx),
            Some(ListAction::MoveUp)
        );
    }

    #[test]
    fn move_up_with_k() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('k')), ctx),
            Some(ListAction::MoveUp)
        );
    }

    #[test]
    fn move_down_with_down_arrow() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Down), ctx),
            Some(ListAction::MoveDown)
        );
    }

    #[test]
    fn move_down_with_j() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('j')), ctx),
            Some(ListAction::MoveDown)
        );
    }

    #[test]
    fn jump_to_top_with_g() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('g')), ctx),
            Some(ListAction::JumpToTop)
        );
    }

    #[test]
    fn toggle_grouping_with_u() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('u')), ctx),
            Some(ListAction::ToggleGrouping)
        );
    }

    #[test]
    fn jump_to_top_with_home() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Home), ctx),
            Some(ListAction::JumpToTop)
        );
    }

    #[test]
    fn jump_to_bottom_with_end() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::End), ctx),
            Some(ListAction::JumpToBottom)
        );
    }

    #[test]
    fn jump_to_bottom_with_uppercase_g() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('G')), ctx),
            Some(ListAction::JumpToBottom)
        );
    }

    #[test]
    fn page_up_key() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::PageUp), ctx),
            Some(ListAction::PageUp)
        );
    }

    #[test]
    fn page_down_key() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::PageDown), ctx),
            Some(ListAction::PageDown)
        );
    }

    // ============================================================================
    // View Transition Tests
    // ============================================================================

    #[test]
    fn enter_detail_requires_selection() {
        // Without selection - no action
        let empty = ListActionContext::empty();
        assert_eq!(determine_list_action(key(KeyCode::Enter), empty), None);

        // With items but no selection - no action
        let items_only = ListActionContext {
            has_items: true,
            has_selection: false,
        };
        assert_eq!(determine_list_action(key(KeyCode::Enter), items_only), None);

        // With selection - action
        let with_sel = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Enter), with_sel),
            Some(ListAction::EnterDetail)
        );
    }

    #[test]
    fn enter_detail_with_right_arrow() {
        // Right arrow should also enter detail view (vim-style navigation)
        let with_sel = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Right), with_sel),
            Some(ListAction::EnterDetail)
        );

        // Without selection - no action
        let empty = ListActionContext::empty();
        assert_eq!(determine_list_action(key(KeyCode::Right), empty), None);
    }

    #[test]
    fn enter_detail_with_l_key() {
        // 'l' should also enter detail view (vim-style navigation)
        let with_sel = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('l')), with_sel),
            Some(ListAction::EnterDetail)
        );

        // Without selection - no action
        let empty = ListActionContext::empty();
        assert_eq!(determine_list_action(key(KeyCode::Char('l')), empty), None);
    }

    #[test]
    fn enter_search_with_slash() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('/')), ctx),
            Some(ListAction::EnterSearch)
        );
    }

    #[test]
    fn open_sort_menu_with_s() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('s')), ctx),
            Some(ListAction::OpenSortMenu)
        );
    }

    #[test]
    fn open_filter_menu_with_f() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('f')), ctx),
            Some(ListAction::OpenFilterMenu)
        );
    }

    #[test]
    fn show_help_with_question_mark() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('?')), ctx),
            Some(ListAction::ShowHelp)
        );
    }

    // ============================================================================
    // Action Requiring Selection Tests
    // ============================================================================

    #[test]
    fn copy_path_requires_selection() {
        // Without selection
        let empty = ListActionContext::empty();
        assert_eq!(determine_list_action(key(KeyCode::Char('c')), empty), None);

        // With selection
        let with_sel = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('c')), with_sel),
            Some(ListAction::CopyPath)
        );
    }

    #[test]
    fn copy_item_as_llm_requires_selection() {
        // Without selection
        let empty = ListActionContext::empty();
        assert_eq!(determine_list_action(key(KeyCode::Char('C')), empty), None);

        // With selection
        let with_sel = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('C')), with_sel),
            Some(ListAction::CopyItemAsLlm)
        );
    }

    #[test]
    fn open_in_editor_with_e_requires_selection() {
        // Without selection
        let empty = ListActionContext::empty();
        assert_eq!(determine_list_action(key(KeyCode::Char('e')), empty), None);

        // With selection
        let with_sel = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('e')), with_sel),
            Some(ListAction::OpenInEditor)
        );
    }

    #[test]
    fn open_in_editor_with_o_requires_selection() {
        // Without selection
        let empty = ListActionContext::empty();
        assert_eq!(determine_list_action(key(KeyCode::Char('o')), empty), None);

        // With selection
        let with_sel = ListActionContext::with_selection();
        assert_eq!(
            determine_list_action(key(KeyCode::Char('o')), with_sel),
            Some(ListAction::OpenInEditor)
        );
    }

    // ============================================================================
    // Unknown Key Tests
    // ============================================================================

    #[test]
    fn unknown_key_returns_none() {
        let ctx = ListActionContext::with_selection();
        assert_eq!(determine_list_action(key(KeyCode::Char('x')), ctx), None);
        assert_eq!(determine_list_action(key(KeyCode::Char('z')), ctx), None);
        assert_eq!(determine_list_action(key(KeyCode::F(1)), ctx), None);
    }

    // ============================================================================
    // Pure Function Property Tests
    // ============================================================================

    #[test]
    fn determine_action_is_deterministic() {
        let ctx = ListActionContext::with_selection();
        let k = key(KeyCode::Enter);

        // Same input always produces same output
        let r1 = determine_list_action(k, ctx);
        let r2 = determine_list_action(k, ctx);
        assert_eq!(r1, r2);
    }

    #[test]
    fn context_affects_guarded_actions() {
        let k = key(KeyCode::Enter);

        // Different contexts produce different results for guarded actions
        let empty = ListActionContext::empty();
        let with_sel = ListActionContext::with_selection();

        assert_ne!(
            determine_list_action(k, empty),
            determine_list_action(k, with_sel)
        );
    }

    #[test]
    fn navigation_keys_work_on_empty_list() {
        // Navigation keys should still return actions on empty list
        // (the execution layer handles the empty case)
        let empty = ListActionContext::empty();

        assert_eq!(
            determine_list_action(key(KeyCode::Up), empty),
            Some(ListAction::MoveUp)
        );
        assert_eq!(
            determine_list_action(key(KeyCode::Down), empty),
            Some(ListAction::MoveDown)
        );
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use crossterm::event::KeyModifiers;
    use proptest::prelude::*;

    fn key_code_strategy() -> impl Strategy<Value = KeyCode> {
        prop_oneof![
            Just(KeyCode::Char('q')),
            Just(KeyCode::Char('k')),
            Just(KeyCode::Char('j')),
            Just(KeyCode::Char('g')),
            Just(KeyCode::Char('G')),
            Just(KeyCode::Char('u')),
            Just(KeyCode::Char('l')),
            Just(KeyCode::Char('/')),
            Just(KeyCode::Char('s')),
            Just(KeyCode::Char('f')),
            Just(KeyCode::Char('?')),
            Just(KeyCode::Char('c')),
            Just(KeyCode::Char('C')),
            Just(KeyCode::Char('e')),
            Just(KeyCode::Char('o')),
            Just(KeyCode::Up),
            Just(KeyCode::Down),
            Just(KeyCode::Left),
            Just(KeyCode::Right),
            Just(KeyCode::Home),
            Just(KeyCode::End),
            Just(KeyCode::PageUp),
            Just(KeyCode::PageDown),
            Just(KeyCode::Enter),
            Just(KeyCode::Esc),
            Just(KeyCode::Tab),
        ]
    }

    fn context_strategy() -> impl Strategy<Value = ListActionContext> {
        (any::<bool>(), any::<bool>()).prop_map(|(has_items, has_selection)| ListActionContext {
            has_items,
            has_selection: has_items && has_selection, // Can't have selection without items
        })
    }

    proptest! {
        /// Property: Quit action is always available regardless of context.
        #[test]
        fn quit_always_available(ctx in context_strategy()) {
            let key = KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE);
            prop_assert_eq!(determine_list_action(key, ctx), Some(ListAction::Quit));
        }

        /// Property: Navigation actions are always available.
        #[test]
        fn navigation_always_available(ctx in context_strategy()) {
            let up_key = KeyEvent::new(KeyCode::Up, KeyModifiers::NONE);
            let down_key = KeyEvent::new(KeyCode::Down, KeyModifiers::NONE);

            prop_assert_eq!(determine_list_action(up_key, ctx), Some(ListAction::MoveUp));
            prop_assert_eq!(determine_list_action(down_key, ctx), Some(ListAction::MoveDown));
        }

        /// Property: Detail view keys (Enter, Right, 'l') require both items and selection.
        #[test]
        fn detail_keys_require_items_and_selection(ctx in context_strategy()) {
            // All three keys should behave identically
            for code in [KeyCode::Enter, KeyCode::Right, KeyCode::Char('l')] {
                let key = KeyEvent::new(code, KeyModifiers::NONE);
                let result = determine_list_action(key, ctx);

                if ctx.has_items && ctx.has_selection {
                    prop_assert_eq!(result, Some(ListAction::EnterDetail));
                } else {
                    prop_assert_eq!(result, None);
                }
            }
        }

        /// Property: Pure function - same input always produces same output.
        #[test]
        fn deterministic(
            code in key_code_strategy(),
            ctx in context_strategy()
        ) {
            let key = KeyEvent::new(code, KeyModifiers::NONE);
            let r1 = determine_list_action(key, ctx);
            let r2 = determine_list_action(key, ctx);
            prop_assert_eq!(r1, r2);
        }
    }
}