par-term 0.30.10

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
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
//! Integration tests for environment variable substitution, allowlists,
//! tab bar position, auto dark mode, and auto tab style config settings.

#![allow(clippy::field_reassign_with_default)]

use par_term::config::{
    Config, TabBarPosition, TabStyle, is_env_var_allowed, substitute_variables,
    substitute_variables_with_allowlist,
};

// ============================================================================
// Variable Substitution Tests
// ============================================================================

/// Helper to set an environment variable in tests.
///
/// # Safety
///
/// `std::env::set_var` is `unsafe` in Rust 2024 because modifying the process
/// environment is not thread-safe. This is acceptable here because:
/// - Cargo's default test harness runs tests on multiple threads, but each test
///   that calls this helper uses a unique, test-specific env var name (prefixed
///   with `PAR_TERM_TEST_`) that is not read by other concurrently-running tests.
/// - The env var is set and removed within the same test body, minimising the
///   window during which it is visible to other threads.
/// - These are unit tests only; they never run in production.
unsafe fn set_test_var(key: &str, val: &str) {
    // SAFETY: See function-level safety comment.
    unsafe { std::env::set_var(key, val) };
}

/// Helper to remove an environment variable in tests.
///
/// # Safety
///
/// Same reasoning as `set_test_var`: the variable being removed is a
/// test-specific key that is not shared with concurrently-running tests.
unsafe fn remove_test_var(key: &str) {
    // SAFETY: See set_test_var safety comment.
    unsafe { std::env::remove_var(key) };
}

#[test]
fn test_substitute_variables_basic_env_var() {
    unsafe { set_test_var("PAR_TERM_TEST_VAR", "hello_world") };
    let result = substitute_variables("value: ${PAR_TERM_TEST_VAR}");
    assert_eq!(result, "value: hello_world");
    unsafe { remove_test_var("PAR_TERM_TEST_VAR") };
}

#[test]
fn test_substitute_variables_home_and_user() {
    // HOME should be set on all Unix-like systems
    let home = std::env::var("HOME").unwrap_or_default();
    let result = substitute_variables("path: ${HOME}/Pictures/bg.png");
    assert_eq!(result, format!("path: {home}/Pictures/bg.png"));
}

#[test]
fn test_substitute_variables_multiple_vars() {
    unsafe { set_test_var("PAR_TERM_TEST_A", "alpha") };
    unsafe { set_test_var("PAR_TERM_TEST_B", "beta") };
    let result = substitute_variables("${PAR_TERM_TEST_A} and ${PAR_TERM_TEST_B}");
    assert_eq!(result, "alpha and beta");
    unsafe { remove_test_var("PAR_TERM_TEST_A") };
    unsafe { remove_test_var("PAR_TERM_TEST_B") };
}

#[test]
fn test_substitute_variables_missing_var_unchanged() {
    // Unset vars should remain as-is (PAR_TERM_ prefix so it passes allowlist)
    unsafe { remove_test_var("PAR_TERM_NONEXISTENT_12345") };
    let result = substitute_variables("value: ${PAR_TERM_NONEXISTENT_12345}");
    assert_eq!(result, "value: ${PAR_TERM_NONEXISTENT_12345}");
}

#[test]
fn test_substitute_variables_default_value() {
    unsafe { remove_test_var("PAR_TERM_MISSING_WITH_DEFAULT") };
    let result = substitute_variables("shell: ${PAR_TERM_MISSING_WITH_DEFAULT:-/bin/bash}");
    assert_eq!(result, "shell: /bin/bash");
}

#[test]
fn test_substitute_variables_default_value_not_used_when_set() {
    unsafe { set_test_var("PAR_TERM_SET_WITH_DEFAULT", "/bin/zsh") };
    let result = substitute_variables("shell: ${PAR_TERM_SET_WITH_DEFAULT:-/bin/bash}");
    assert_eq!(result, "shell: /bin/zsh");
    unsafe { remove_test_var("PAR_TERM_SET_WITH_DEFAULT") };
}

#[test]
fn test_substitute_variables_escaped_dollar() {
    // $${VAR} should produce the literal ${VAR}
    unsafe { set_test_var("PAR_TERM_TEST_ESC", "should_not_appear") };
    let result = substitute_variables("literal: $${PAR_TERM_TEST_ESC}");
    assert_eq!(result, "literal: ${PAR_TERM_TEST_ESC}");
    unsafe { remove_test_var("PAR_TERM_TEST_ESC") };
}

#[test]
fn test_substitute_variables_no_vars() {
    let input = "cols: 80\nrows: 24\nfont_size: 12.0";
    let result = substitute_variables(input);
    assert_eq!(result, input);
}

#[test]
fn test_substitute_variables_adjacent_vars() {
    unsafe { set_test_var("PAR_TERM_TEST_X", "foo") };
    unsafe { set_test_var("PAR_TERM_TEST_Y", "bar") };
    let result = substitute_variables("${PAR_TERM_TEST_X}${PAR_TERM_TEST_Y}");
    assert_eq!(result, "foobar");
    unsafe { remove_test_var("PAR_TERM_TEST_X") };
    unsafe { remove_test_var("PAR_TERM_TEST_Y") };
}

#[test]
fn test_substitute_variables_in_yaml_config() {
    unsafe { set_test_var("PAR_TERM_TEST_FONT", "Fira Code") };
    unsafe { set_test_var("PAR_TERM_TEST_TITLE", "My Terminal") };
    let yaml = r#"
font_family: "${PAR_TERM_TEST_FONT}"
window_title: "${PAR_TERM_TEST_TITLE}"
cols: 120
"#;
    let substituted = substitute_variables(yaml);
    let config: Config = serde_yaml_ng::from_str(&substituted).unwrap();
    assert_eq!(config.font_family, "Fira Code");
    assert_eq!(config.window_title, "My Terminal");
    assert_eq!(config.cols, 120);
    unsafe { remove_test_var("PAR_TERM_TEST_FONT") };
    unsafe { remove_test_var("PAR_TERM_TEST_TITLE") };
}

#[test]
fn test_substitute_variables_partial_string() {
    unsafe { set_test_var("PAR_TERM_TEST_USER", "testuser") };
    let result = substitute_variables("badge: ${PAR_TERM_TEST_USER}@myhost");
    assert_eq!(result, "badge: testuser@myhost");
    unsafe { remove_test_var("PAR_TERM_TEST_USER") };
}

#[test]
fn test_substitute_variables_empty_default() {
    unsafe { remove_test_var("PAR_TERM_EMPTY_DEFAULT") };
    let result = substitute_variables("val: ${PAR_TERM_EMPTY_DEFAULT:-}");
    assert_eq!(result, "val: ");
}

// ============================================================================
// Environment Variable Allowlist Tests
// ============================================================================

#[test]
fn test_allowlist_permits_common_safe_vars() {
    // These should all be on the allowlist
    assert!(is_env_var_allowed("HOME"));
    assert!(is_env_var_allowed("USER"));
    assert!(is_env_var_allowed("USERNAME"));
    assert!(is_env_var_allowed("LOGNAME"));
    assert!(is_env_var_allowed("SHELL"));
    assert!(is_env_var_allowed("TERM"));
    assert!(is_env_var_allowed("LANG"));
    assert!(is_env_var_allowed("PATH"));
    assert!(is_env_var_allowed("EDITOR"));
    assert!(is_env_var_allowed("VISUAL"));
    assert!(is_env_var_allowed("PAGER"));
    assert!(is_env_var_allowed("TMPDIR"));
    assert!(is_env_var_allowed("DISPLAY"));
    assert!(is_env_var_allowed("WAYLAND_DISPLAY"));
    assert!(is_env_var_allowed("HOSTNAME"));
    assert!(is_env_var_allowed("HOST"));
    assert!(is_env_var_allowed("COLORTERM"));
    assert!(is_env_var_allowed("TERM_PROGRAM"));
}

#[test]
fn test_allowlist_permits_xdg_vars() {
    assert!(is_env_var_allowed("XDG_CONFIG_HOME"));
    assert!(is_env_var_allowed("XDG_DATA_HOME"));
    assert!(is_env_var_allowed("XDG_STATE_HOME"));
    assert!(is_env_var_allowed("XDG_CACHE_HOME"));
    assert!(is_env_var_allowed("XDG_RUNTIME_DIR"));
}

#[test]
fn test_allowlist_permits_windows_vars() {
    assert!(is_env_var_allowed("APPDATA"));
    assert!(is_env_var_allowed("LOCALAPPDATA"));
    assert!(is_env_var_allowed("USERPROFILE"));
}

#[test]
fn test_allowlist_permits_par_term_prefix() {
    assert!(is_env_var_allowed("PAR_TERM_CUSTOM"));
    assert!(is_env_var_allowed("PAR_TERM_SOMETHING_ELSE"));
    assert!(is_env_var_allowed("PAR_TERM_"));
}

#[test]
fn test_allowlist_permits_lc_prefix() {
    assert!(is_env_var_allowed("LC_ALL"));
    assert!(is_env_var_allowed("LC_CTYPE"));
    assert!(is_env_var_allowed("LC_MESSAGES"));
    assert!(is_env_var_allowed("LC_COLLATE"));
}

#[test]
fn test_allowlist_blocks_sensitive_vars() {
    assert!(!is_env_var_allowed("AWS_SECRET_ACCESS_KEY"));
    assert!(!is_env_var_allowed("API_KEY"));
    assert!(!is_env_var_allowed("GITHUB_TOKEN"));
    assert!(!is_env_var_allowed("DATABASE_URL"));
    assert!(!is_env_var_allowed("SECRET_KEY"));
    assert!(!is_env_var_allowed("OPENAI_API_KEY"));
    assert!(!is_env_var_allowed("SSH_PRIVATE_KEY"));
    assert!(!is_env_var_allowed("RANDOM_VAR"));
}

#[test]
fn test_substitute_allowlisted_var_resolves() {
    // HOME is on the allowlist and should be set on all Unix-like systems
    let home = std::env::var("HOME").unwrap_or_default();
    let result = substitute_variables("path: ${HOME}/config");
    assert_eq!(result, format!("path: {home}/config"));
}

#[test]
fn test_substitute_non_allowlisted_var_blocked() {
    // Set a non-allowlisted variable and verify it's NOT substituted
    unsafe { set_test_var("SECRET_API_KEY_TEST_M3", "super_secret") };
    let result = substitute_variables("key: ${SECRET_API_KEY_TEST_M3}");
    // Should remain as literal text, not resolved
    assert_eq!(result, "key: ${SECRET_API_KEY_TEST_M3}");
    unsafe { remove_test_var("SECRET_API_KEY_TEST_M3") };
}

#[test]
fn test_substitute_par_term_prefix_resolves() {
    unsafe { set_test_var("PAR_TERM_MY_SETTING", "custom_value") };
    let result = substitute_variables("setting: ${PAR_TERM_MY_SETTING}");
    assert_eq!(result, "setting: custom_value");
    unsafe { remove_test_var("PAR_TERM_MY_SETTING") };
}

#[test]
fn test_substitute_lc_prefix_resolves() {
    unsafe { set_test_var("LC_TEST_LOCALE", "en_US.UTF-8") };
    let result = substitute_variables("locale: ${LC_TEST_LOCALE}");
    assert_eq!(result, "locale: en_US.UTF-8");
    unsafe { remove_test_var("LC_TEST_LOCALE") };
}

#[test]
fn test_substitute_allow_all_overrides_allowlist() {
    // With allow_all=true, even non-allowlisted vars should resolve
    unsafe { set_test_var("SECRET_OVERRIDE_TEST_M3", "resolved_secret") };
    let result = substitute_variables_with_allowlist("key: ${SECRET_OVERRIDE_TEST_M3}", true);
    assert_eq!(result, "key: resolved_secret");
    unsafe { remove_test_var("SECRET_OVERRIDE_TEST_M3") };
}

#[test]
fn test_substitute_allow_all_false_blocks_non_allowlisted() {
    unsafe { set_test_var("BLOCKED_VAR_TEST_M3", "should_not_appear") };
    let result = substitute_variables_with_allowlist("val: ${BLOCKED_VAR_TEST_M3}", false);
    assert_eq!(result, "val: ${BLOCKED_VAR_TEST_M3}");
    unsafe { remove_test_var("BLOCKED_VAR_TEST_M3") };
}

#[test]
fn test_substitute_mixed_allowed_and_blocked() {
    unsafe { set_test_var("PAR_TERM_GOOD", "allowed") };
    unsafe { set_test_var("NAUGHTY_SECRET_TEST_M3", "blocked") };
    let result = substitute_variables("good: ${PAR_TERM_GOOD}, bad: ${NAUGHTY_SECRET_TEST_M3}");
    assert_eq!(result, "good: allowed, bad: ${NAUGHTY_SECRET_TEST_M3}");
    unsafe { remove_test_var("PAR_TERM_GOOD") };
    unsafe { remove_test_var("NAUGHTY_SECRET_TEST_M3") };
}

#[test]
fn test_substitute_non_allowlisted_with_default_uses_literal() {
    // Non-allowlisted var with a default — the entire ${VAR:-default} is left as-is
    unsafe { remove_test_var("BLOCKED_DEFAULT_TEST_M3") };
    let result = substitute_variables("val: ${BLOCKED_DEFAULT_TEST_M3:-fallback}");
    // The variable is blocked, so the placeholder stays as literal text
    assert_eq!(result, "val: ${BLOCKED_DEFAULT_TEST_M3:-fallback}");
}

#[test]
fn test_substitute_escaped_dollar_still_works_with_allowlist() {
    // Escaped dollars should still produce literal ${VAR} regardless of allowlist
    let result = substitute_variables("literal: $${HOME}");
    assert_eq!(result, "literal: ${HOME}");
}

#[test]
fn test_config_default_allow_all_env_vars_is_false() {
    let config = Config::default();
    assert!(!config.allow_all_env_vars);
}

// ============================================================================
// Tab Bar Position Configuration Tests
// ============================================================================

#[test]
fn test_tab_bar_position_default() {
    let config = Config::default();
    assert_eq!(config.tab_bar_position, TabBarPosition::Top);
    assert_eq!(config.tab_bar_width, 160.0);
}

#[test]
fn test_tab_bar_position_serialization() {
    // Round-trip serialization for all variants
    for &position in TabBarPosition::all() {
        let mut config = Config::default();
        config.tab_bar_position = position;

        let yaml = serde_yaml_ng::to_string(&config).unwrap();
        let deserialized: Config = serde_yaml_ng::from_str(&yaml).unwrap();
        assert_eq!(
            deserialized.tab_bar_position, position,
            "Round-trip failed for {:?}",
            position
        );
    }
}

#[test]
fn test_tab_bar_position_yaml_variants() {
    let yaml = r#"tab_bar_position: top"#;
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(config.tab_bar_position, TabBarPosition::Top);

    let yaml = r#"tab_bar_position: bottom"#;
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(config.tab_bar_position, TabBarPosition::Bottom);

    let yaml = r#"tab_bar_position: left"#;
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(config.tab_bar_position, TabBarPosition::Left);
}

#[test]
fn test_tab_bar_position_partial_yaml() {
    // Missing tab_bar_position should default to Top
    let yaml = r#"
cols: 100
"#;
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(config.tab_bar_position, TabBarPosition::Top);
    assert_eq!(config.tab_bar_width, 160.0);
}

#[test]
fn test_tab_bar_width_yaml_deserialization() {
    let yaml = r#"
tab_bar_position: left
tab_bar_width: 250.0
"#;
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(config.tab_bar_position, TabBarPosition::Left);
    assert!((config.tab_bar_width - 250.0).abs() < f32::EPSILON);
}

#[test]
fn test_tab_bar_width_yaml_serialization() {
    let mut config = Config::default();
    config.tab_bar_position = TabBarPosition::Left;
    config.tab_bar_width = 200.0;

    let yaml = serde_yaml_ng::to_string(&config).unwrap();
    assert!(yaml.contains("tab_bar_position: left"));
    assert!(yaml.contains("tab_bar_width: 200.0"));
}

// ============================================================================
// Auto Dark Mode Tests
// ============================================================================

#[test]
fn test_auto_dark_mode_defaults() {
    let config = Config::default();
    assert!(!config.auto_dark_mode);
    assert_eq!(config.light_theme, "light-background");
    assert_eq!(config.dark_theme, "dark-background");
}

#[test]
fn test_apply_system_theme_disabled() {
    let mut config = Config::default();
    config.auto_dark_mode = false;
    // Should not change theme when auto_dark_mode is off
    assert!(!config.apply_system_theme(true));
    assert!(!config.apply_system_theme(false));
    assert_eq!(config.theme, "dark-background");
}

#[test]
fn test_apply_system_theme_dark() {
    let mut config = Config::default();
    config.auto_dark_mode = true;
    config.theme = "light-background".to_string();
    config.dark_theme = "dracula".to_string();

    assert!(config.apply_system_theme(true));
    assert_eq!(config.theme, "dracula");
}

#[test]
fn test_apply_system_theme_light() {
    let mut config = Config::default();
    config.auto_dark_mode = true;
    config.theme = "dark-background".to_string();
    config.light_theme = "solarized-light".to_string();

    assert!(config.apply_system_theme(false));
    assert_eq!(config.theme, "solarized-light");
}

#[test]
fn test_apply_system_theme_no_change() {
    let mut config = Config::default();
    config.auto_dark_mode = true;
    config.theme = "dark-background".to_string();
    config.dark_theme = "dark-background".to_string();

    // Already using the dark theme, should return false (no change)
    assert!(!config.apply_system_theme(true));
    assert_eq!(config.theme, "dark-background");
}

#[test]
fn test_auto_dark_mode_yaml_deserialization() {
    let yaml = r#"
auto_dark_mode: true
light_theme: solarized-light
dark_theme: dracula
"#;
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert!(config.auto_dark_mode);
    assert_eq!(config.light_theme, "solarized-light");
    assert_eq!(config.dark_theme, "dracula");
}

#[test]
fn test_auto_dark_mode_yaml_defaults_when_absent() {
    let yaml = "cols: 120\n";
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert!(!config.auto_dark_mode);
    assert_eq!(config.light_theme, "light-background");
    assert_eq!(config.dark_theme, "dark-background");
}

// =============================================================================
// Auto Tab Style Tests
// =============================================================================

#[test]
fn test_auto_tab_style_defaults() {
    let config = Config::default();
    assert_eq!(config.tab_style, TabStyle::Dark);
    assert_eq!(config.light_tab_style, TabStyle::Light);
    assert_eq!(config.dark_tab_style, TabStyle::Dark);
}

#[test]
fn test_apply_system_tab_style_disabled_when_not_automatic() {
    let mut config = Config::default();
    config.tab_style = TabStyle::Dark;
    assert!(!config.apply_system_tab_style(true));
    assert!(!config.apply_system_tab_style(false));
}

#[test]
fn test_apply_system_tab_style_dark() {
    let mut config = Config::default();
    config.tab_style = TabStyle::Automatic;
    config.dark_tab_style = TabStyle::HighContrast;

    assert!(config.apply_system_tab_style(true));
    // Should have applied HighContrast colors but kept Automatic as the tab_style
    assert_eq!(config.tab_style, TabStyle::Automatic);
    // HighContrast sets tab_bar_background to [0, 0, 0]
    assert_eq!(config.tab_bar_background, [0, 0, 0]);
}

#[test]
fn test_apply_system_tab_style_light() {
    let mut config = Config::default();
    config.tab_style = TabStyle::Automatic;
    config.light_tab_style = TabStyle::Light;

    assert!(config.apply_system_tab_style(false));
    assert_eq!(config.tab_style, TabStyle::Automatic);
    // Light sets tab_bar_background to [235, 235, 235]
    assert_eq!(config.tab_bar_background, [235, 235, 235]);
}

#[test]
fn test_apply_system_tab_style_preserves_automatic() {
    let mut config = Config::default();
    config.tab_style = TabStyle::Automatic;
    config.dark_tab_style = TabStyle::Compact;

    config.apply_system_tab_style(true);
    // tab_style must remain Automatic after applying
    assert_eq!(config.tab_style, TabStyle::Automatic);
}

#[test]
fn test_tab_style_all_concrete_excludes_automatic() {
    let concrete = TabStyle::all_concrete();
    assert!(!concrete.contains(&TabStyle::Automatic));
    assert_eq!(concrete.len(), 5);
}

#[test]
fn test_tab_style_all_includes_automatic() {
    let all = TabStyle::all();
    assert!(all.contains(&TabStyle::Automatic));
    assert_eq!(all.len(), 6);
}

#[test]
fn test_auto_tab_style_yaml_deserialization() {
    let yaml = r#"
tab_style: automatic
light_tab_style: compact
dark_tab_style: high_contrast
"#;
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(config.tab_style, TabStyle::Automatic);
    assert_eq!(config.light_tab_style, TabStyle::Compact);
    assert_eq!(config.dark_tab_style, TabStyle::HighContrast);
}

#[test]
fn test_auto_tab_style_yaml_defaults_when_absent() {
    let yaml = "cols: 120\n";
    let config: Config = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(config.light_tab_style, TabStyle::Light);
    assert_eq!(config.dark_tab_style, TabStyle::Dark);
}