par-term 0.30.0

Cross-platform GPU-accelerated terminal emulator with inline graphics support (Sixel, iTerm2, Kitty)
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
//! Tests for the settings window module

use par_term::config::Config;
use par_term::settings_ui::SettingsUI;
use par_term::settings_ui::section::CollapsibleSection;
use par_term::settings_ui::sidebar::{SettingsTab, tab_matches_search};
use par_term::settings_window::SettingsWindowAction;
use std::collections::HashSet;

#[test]
fn test_settings_window_action_none() {
    let action = SettingsWindowAction::None;
    assert!(matches!(action, SettingsWindowAction::None));
}

#[test]
fn test_settings_window_action_close() {
    let action = SettingsWindowAction::Close;
    assert!(matches!(action, SettingsWindowAction::Close));
}

#[test]
fn test_settings_window_action_apply_config() {
    let config = Config::default();
    let action = SettingsWindowAction::ApplyConfig(config.clone());

    if let SettingsWindowAction::ApplyConfig(applied_config) = action {
        assert_eq!(applied_config.window_title, config.window_title);
        assert_eq!(applied_config.font_size, config.font_size);
    } else {
        panic!("Expected ApplyConfig variant");
    }
}

#[test]
fn test_settings_window_action_save_config() {
    let config = Config::default();
    let action = SettingsWindowAction::SaveConfig(config.clone());

    if let SettingsWindowAction::SaveConfig(saved_config) = action {
        assert_eq!(saved_config.window_title, config.window_title);
        assert_eq!(saved_config.font_size, config.font_size);
    } else {
        panic!("Expected SaveConfig variant");
    }
}

#[test]
fn test_settings_window_action_debug_format() {
    // Test that all variants implement Debug
    let none = SettingsWindowAction::None;
    let close = SettingsWindowAction::Close;
    let apply = SettingsWindowAction::ApplyConfig(Config::default());
    let save = SettingsWindowAction::SaveConfig(Config::default());

    // These should not panic
    let _ = format!("{:?}", none);
    let _ = format!("{:?}", close);
    let _ = format!("{:?}", apply);
    let _ = format!("{:?}", save);
}

#[test]
fn test_settings_window_action_clone() {
    // Test that all variants implement Clone
    let none = SettingsWindowAction::None;
    let close = SettingsWindowAction::Close;

    let none_clone = none.clone();
    let close_clone = close.clone();

    assert!(matches!(none_clone, SettingsWindowAction::None));
    assert!(matches!(close_clone, SettingsWindowAction::Close));
}

// ============================================================================
// section_matches logic tests (L-14)
// Tests the CollapsibleSection::matches_search() logic and tab_matches_search()
// ============================================================================

#[test]
fn test_section_matches_empty_query_always_matches() {
    // An empty search query must match every section regardless of title/keywords.
    let mut collapsed: HashSet<String> = HashSet::new();
    let section = CollapsibleSection::new("Font Settings", "font", &mut collapsed, "")
        .keywords(&["typeface", "size", "bold"]);
    assert!(section.matches_search(), "Empty query should always match");
}

#[test]
fn test_section_matches_title_exact() {
    let mut collapsed: HashSet<String> = HashSet::new();
    let section = CollapsibleSection::new("Font Settings", "font", &mut collapsed, "Font Settings")
        .keywords(&[]);
    assert!(section.matches_search(), "Exact title match should succeed");
}

#[test]
fn test_section_matches_title_case_insensitive() {
    let mut collapsed: HashSet<String> = HashSet::new();
    let section = CollapsibleSection::new("Font Settings", "font", &mut collapsed, "font settings")
        .keywords(&[]);
    assert!(
        section.matches_search(),
        "Title match should be case-insensitive"
    );
}

#[test]
fn test_section_matches_title_partial() {
    let mut collapsed: HashSet<String> = HashSet::new();
    let section =
        CollapsibleSection::new("Font Settings", "font", &mut collapsed, "font").keywords(&[]);
    assert!(
        section.matches_search(),
        "Partial title match should succeed"
    );
}

#[test]
fn test_section_matches_keyword_case_insensitive() {
    let mut collapsed: HashSet<String> = HashSet::new();
    let section = CollapsibleSection::new("Appearance", "appearance", &mut collapsed, "LIGATURES")
        .keywords(&["ligatures", "kerning"]);
    assert!(
        section.matches_search(),
        "Keyword match should be case-insensitive"
    );
}

#[test]
fn test_section_no_match_returns_false() {
    let mut collapsed: HashSet<String> = HashSet::new();
    let section = CollapsibleSection::new("Font Settings", "font", &mut collapsed, "network")
        .keywords(&["typeface", "size", "bold"]);
    assert!(
        !section.matches_search(),
        "Query with no matching title or keyword should return false"
    );
}

#[test]
fn test_section_matches_keyword_partial() {
    let mut collapsed: HashSet<String> = HashSet::new();
    let section = CollapsibleSection::new("Terminal", "terminal", &mut collapsed, "scroll")
        .keywords(&["scrollback", "shell"]);
    assert!(
        section.matches_search(),
        "Partial keyword match should succeed"
    );
}

// ============================================================================
// tab_matches_search tests
// ============================================================================

#[test]
fn test_tab_matches_search_empty_query() {
    // Every tab should match an empty query.
    for tab in SettingsTab::all() {
        assert!(
            tab_matches_search(*tab, ""),
            "Tab {:?} should match empty query",
            tab
        );
    }
}

#[test]
fn test_tab_matches_search_by_display_name() {
    // Each tab should be found by its own display name.
    for tab in SettingsTab::all() {
        let name = tab.display_name().to_lowercase();
        assert!(
            tab_matches_search(*tab, &name),
            "Tab {:?} should match its own display name '{}'",
            tab,
            name
        );
    }
}

#[test]
fn test_tab_matches_search_appearance_keywords() {
    assert!(
        tab_matches_search(SettingsTab::Appearance, "font"),
        "Appearance tab should match 'font'"
    );
    assert!(
        tab_matches_search(SettingsTab::Appearance, "cursor"),
        "Appearance tab should match 'cursor'"
    );
    assert!(
        tab_matches_search(SettingsTab::Appearance, "THEME"),
        "Appearance tab should match 'THEME' (case-insensitive)"
    );
}

#[test]
fn test_tab_matches_search_window_keywords() {
    assert!(
        tab_matches_search(SettingsTab::Window, "opacity"),
        "Window tab should match 'opacity'"
    );
    assert!(
        tab_matches_search(SettingsTab::Window, "tab bar"),
        "Window tab should match 'tab bar'"
    );
}

#[test]
fn test_tab_matches_search_no_match() {
    // A query that exists in no tab's name or keywords should return false.
    // "xyzzy_nonexistent_query" is unlikely to appear in any keyword list.
    assert!(
        !tab_matches_search(SettingsTab::Appearance, "xyzzy_nonexistent_query"),
        "Appearance tab should not match nonsense query"
    );
}

#[test]
fn test_tab_matches_search_cross_tab_isolation() {
    // "tmux" keyword belongs to Advanced, not Appearance.
    assert!(
        !tab_matches_search(SettingsTab::Appearance, "tmux"),
        "Appearance tab should not match 'tmux'"
    );
    assert!(
        tab_matches_search(SettingsTab::Advanced, "tmux"),
        "Advanced tab should match 'tmux'"
    );
}

// ============================================================================
// Validation range tests (L-14)
// Tests that Config default values fall within expected ranges and that
// values can be set within documented bounds.
// ============================================================================

#[test]
fn test_font_size_default_in_valid_range() {
    // The appearance tab slider range is 6.0..=48.0
    let config = Config::default();
    assert!(
        config.font_size >= 6.0,
        "Default font_size should be >= 6.0 (slider minimum)"
    );
    assert!(
        config.font_size <= 48.0,
        "Default font_size should be <= 48.0 (slider maximum)"
    );
}

#[test]
fn test_window_opacity_default_in_valid_range() {
    let config = Config::default();
    assert!(
        config.window.window_opacity >= 0.0,
        "Default window_opacity should be >= 0.0"
    );
    assert!(
        config.window.window_opacity <= 1.0,
        "Default window_opacity should be <= 1.0"
    );
}

#[test]
fn test_background_image_opacity_default_in_valid_range() {
    let config = Config::default();
    assert!(config.background_image_opacity >= 0.0);
    assert!(config.background_image_opacity <= 1.0);
}

#[test]
fn test_inactive_tab_opacity_default_in_valid_range() {
    let config = Config::default();
    assert!(config.inactive_tab_opacity >= 0.0);
    assert!(config.inactive_tab_opacity <= 1.0);
}

#[test]
fn test_scrollback_lines_default_positive() {
    let config = Config::default();
    assert!(
        config.scrollback.scrollback_lines > 0,
        "Default scrollback_lines should be > 0"
    );
}

#[test]
fn test_tab_bar_height_default_positive() {
    let config = Config::default();
    assert!(
        config.tab_bar_height > 0.0,
        "Default tab_bar_height should be > 0"
    );
}

#[test]
fn test_tab_min_width_default_positive() {
    let config = Config::default();
    assert!(
        config.tab_min_width > 0.0,
        "Default tab_min_width should be > 0"
    );
}

#[test]
fn test_max_fps_default_reasonable() {
    let config = Config::default();
    assert!(config.max_fps > 0, "Default max_fps should be > 0");
    assert!(
        config.max_fps <= 240,
        "Default max_fps should be <= 240 (reasonable upper bound)"
    );
}

// ============================================================================
// has_changes state machine tests (L-14)
// ============================================================================

#[test]
fn test_has_changes_initially_false() {
    let config = Config::default();
    let settings = SettingsUI::new(config);
    assert!(
        !settings.has_changes,
        "has_changes should be false on initial creation"
    );
}

#[test]
fn test_has_changes_set_to_true() {
    let config = Config::default();
    let mut settings = SettingsUI::new(config);
    assert!(!settings.has_changes);

    // Simulate a setting change (as the UI code does)
    settings.has_changes = true;
    assert!(
        settings.has_changes,
        "has_changes should be true after marking a change"
    );
}

#[test]
fn test_has_changes_reset_to_false() {
    let config = Config::default();
    let mut settings = SettingsUI::new(config);

    // Mark as changed
    settings.has_changes = true;
    assert!(settings.has_changes);

    // Simulate save (reset)
    settings.has_changes = false;
    assert!(
        !settings.has_changes,
        "has_changes should return to false after save"
    );
}

#[test]
fn test_has_changes_after_config_field_modification() {
    let config = Config::default();
    let mut settings = SettingsUI::new(config);

    assert!(!settings.has_changes, "Should start clean");

    // Modify a config field and mark has_changes (as the UI tab code does)
    settings.config.font_size = 24.0;
    settings.has_changes = true;

    assert!(
        settings.has_changes,
        "has_changes should be true after modifying config.font_size"
    );
    assert_eq!(
        settings.config.font_size, 24.0,
        "Config change should be reflected"
    );
}

#[test]
fn test_has_changes_multiple_modifications() {
    let config = Config::default();
    let mut settings = SettingsUI::new(config);

    // Apply multiple changes
    settings.config.font_size = 16.0;
    settings.has_changes = true;

    settings.config.window.window_opacity = 0.9;
    // has_changes stays true (no intermediate reset)

    assert!(
        settings.has_changes,
        "has_changes should remain true across multiple changes"
    );
    assert_eq!(settings.config.font_size, 16.0);
    assert!((settings.config.window.window_opacity - 0.9).abs() < f32::EPSILON);
}

#[test]
fn test_settings_ui_config_is_cloned_on_creation() {
    let config = Config {
        font_size: 20.0,
        ..Config::default()
    };

    let settings = SettingsUI::new(config.clone());
    assert_eq!(
        settings.config.font_size, 20.0,
        "SettingsUI should use the provided config"
    );
}