par-term 0.30.4

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
//! Tests for URL and file path detection utilities.

use super::*;

#[test]
fn test_detect_http_url() {
    let text = "Visit https://example.com for more info";
    let urls = detect_urls_in_line(text, 0);
    assert_eq!(urls.len(), 1);
    assert_eq!(urls[0].url, "https://example.com");
    assert_eq!(urls[0].start_col, 6);
    assert_eq!(urls[0].end_col, 25); // Exclusive end position
}

#[test]
fn test_detect_www_url() {
    let text = "Check out www.example.com";
    let urls = detect_urls_in_line(text, 0);
    assert_eq!(urls.len(), 1);
    assert_eq!(urls[0].url, "www.example.com");
}

#[test]
fn test_detect_multiple_urls() {
    let text = "See https://example.com and http://test.org";
    let urls = detect_urls_in_line(text, 0);
    assert_eq!(urls.len(), 2);
    assert_eq!(urls[0].url, "https://example.com");
    assert_eq!(urls[1].url, "http://test.org");
}

#[test]
fn test_find_url_at_position() {
    let text = "Visit https://example.com for more";
    let urls = detect_urls_in_line(text, 5);

    // Position within URL
    assert!(find_url_at_position(&urls, 10, 5).is_some());

    // Position outside URL
    assert!(find_url_at_position(&urls, 0, 5).is_none());
    assert!(find_url_at_position(&urls, 30, 5).is_none());

    // Wrong row
    assert!(find_url_at_position(&urls, 10, 6).is_none());
}

#[test]
fn test_no_urls() {
    let text = "This line has no URLs at all";
    let urls = detect_urls_in_line(text, 0);
    assert_eq!(urls.len(), 0);
}

#[test]
fn test_url_schemes() {
    let text = "ftp://files.com ssh://git.com file:///path git://repo.com";
    let urls = detect_urls_in_line(text, 0);
    assert_eq!(urls.len(), 4);
}

#[test]
fn test_detect_relative_file_path() {
    let text = "./src/lambda_check_sf_status/.gitignore";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1, "Should detect exactly one path");
    assert_eq!(paths[0].url, "./src/lambda_check_sf_status/.gitignore");
    assert_eq!(paths[0].start_col, 0);
    assert_eq!(paths[0].end_col, text.len());
}

#[test]
fn test_detect_nested_path_no_double_match() {
    // This test ensures we don't match /src/handler.py inside ./foo/src/handler.py
    let text = "./src/lambda_sap_po_to_zen/src/handler.py";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(
        paths.len(),
        1,
        "Should detect exactly one path, not multiple overlapping ones"
    );
    assert_eq!(paths[0].url, text);
    assert_eq!(paths[0].start_col, 0);
}

#[test]
fn test_detect_home_path() {
    let text = "~/Documents/file.txt";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "~/Documents/file.txt");
}

#[test]
fn test_detect_path_with_line_number() {
    let text = "./src/main.rs:42";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "./src/main.rs");
    if let DetectedItemType::FilePath { line, column } = &paths[0].item_type {
        assert_eq!(*line, Some(42));
        assert_eq!(*column, None);
    } else {
        panic!("Expected FilePath type");
    }
}

#[test]
fn test_detect_path_with_line_and_col() {
    let text = "./src/main.rs:42:10";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "./src/main.rs");
    if let DetectedItemType::FilePath { line, column } = &paths[0].item_type {
        assert_eq!(*line, Some(42));
        assert_eq!(*column, Some(10));
    } else {
        panic!("Expected FilePath type");
    }
}

#[test]
fn test_absolute_path_with_multiple_components() {
    let text = "/Users/probello/.claude";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(
        paths.len(),
        1,
        "Should match absolute path at start of string"
    );
    assert_eq!(paths[0].url, "/Users/probello/.claude");
    assert_eq!(paths[0].start_col, 0);
}

#[test]
fn test_absolute_path_after_whitespace() {
    let text = "ls /Users/probello/.claude";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(
        paths.len(),
        1,
        "Should match absolute path after whitespace"
    );
    assert_eq!(paths[0].url, "/Users/probello/.claude");
    assert_eq!(paths[0].start_col, 3);
}

#[test]
fn test_no_match_single_component_absolute_path() {
    // Single-component paths like /etc are too likely to be false positives
    let text = "/etc";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(
        paths.len(),
        0,
        "Should not match single-component absolute paths"
    );
}

#[test]
fn test_no_false_absolute_match_inside_relative() {
    // Absolute path branch should NOT match /bar/baz inside ./foo/bar/baz
    let text = "./foo/bar/baz";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(
        paths.len(),
        1,
        "Should only match the relative path, not internal absolute"
    );
    assert_eq!(paths[0].url, "./foo/bar/baz");
}

/// Verify that regex byte offsets can be correctly mapped to column indices
/// when multi-byte UTF-8 characters precede the matched text.
/// This is the mapping that url_hover.rs applies after detection.
#[test]
fn test_byte_offset_to_column_mapping_with_multibyte() {
    // Simulate a terminal line: "★ ~/docs" where ★ is a 3-byte UTF-8 char
    // Cell layout: [★][ ][~][/][d][o][c][s]
    // Columns:      0   1  2  3  4  5  6  7
    let graphemes = ["★", " ", "~", "/", "d", "o", "c", "s"];
    let cols = graphemes.len();

    // Build line and byte-to-col mapping (same logic as url_hover.rs)
    let mut line = String::new();
    let mut byte_to_col: Vec<usize> = Vec::new();
    for (col_idx, g) in graphemes.iter().enumerate() {
        for _ in 0..g.len() {
            byte_to_col.push(col_idx);
        }
        line.push_str(g);
    }
    byte_to_col.push(cols); // sentinel

    let map = |b: usize| -> usize { byte_to_col.get(b).copied().unwrap_or(cols) };

    // Detect file path in the concatenated string
    let paths = detect_file_paths_in_line(&line, 0);
    assert_eq!(paths.len(), 1, "Should detect ~/docs");

    // The regex returns byte offsets: "★" is 3 bytes, " " is 1 byte
    // so ~/docs starts at byte 4 (not column 2)
    assert_eq!(paths[0].start_col, 4, "Byte offset should be 4");

    // After mapping, column index should be 2
    let start_col = map(paths[0].start_col);
    let end_col = map(paths[0].end_col);
    assert_eq!(start_col, 2, "Column should be 2 (after ★ and space)");
    assert_eq!(end_col, cols, "End column should be 8 (end of line)");
}

/// Verify that file path detection stops at tmux pane separator characters (box-drawing).
/// The │ (U+2502) character is used by tmux to draw vertical pane dividers. Without
/// this guard, a file path in the left pane would be detected as extending through the
/// separator into the right pane, causing the link highlight color to bleed across panes.
#[test]
fn test_file_path_stops_at_tmux_box_drawing_separator() {
    // Simulate a terminal row spanning two tmux panes:
    // left pane: "• Bash(cat /Users/probello/.claude/projects/file"
    // separator:  "│"
    // right pane: "to) Compiling..."
    let text = "cat /Users/probello/.claude/projects/file│to) Compiling";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1, "Should detect exactly one path");
    assert_eq!(
        paths[0].url, "/Users/probello/.claude/projects/file",
        "Path should not include the box-drawing separator or right-pane content"
    );
    // end_col must be at the │ character, not past it
    let end_byte = paths[0].end_col;
    assert!(
        !text[..end_byte].contains('│'),
        "Detected path must end before the tmux separator"
    );
}

/// Verify home-relative paths also stop at box-drawing separators.
#[test]
fn test_home_path_stops_at_tmux_box_drawing_separator() {
    let text = "~/Documents/file.txt│right_pane_content";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "~/Documents/file.txt");
}

// --- trailing sentence punctuation stripping ---

#[test]
fn test_file_path_strips_trailing_period() {
    // Common case: file path at end of sentence
    let text = "the file is at ~/thefile.txt.";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "~/thefile.txt");
    assert_eq!(paths[0].end_col, text.len() - 1); // excludes the trailing '.'
}

#[test]
fn test_file_path_strips_trailing_period_absolute() {
    let text = "see /Users/probello/readme.md.";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "/Users/probello/readme.md");
}

#[test]
fn test_file_path_strips_trailing_ellipsis() {
    let text = "loading ~/Documents/file.txt...";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "~/Documents/file.txt");
}

#[test]
fn test_file_path_strips_trailing_exclamation() {
    let text = "check ~/important/file.rs!";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "~/important/file.rs");
}

#[test]
fn test_file_path_strips_trailing_question() {
    let text = "did you mean ~/config/settings.yaml?";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "~/config/settings.yaml");
}

#[test]
fn test_file_path_preserves_internal_dots() {
    // Internal dots should NOT be stripped
    let text = "~/src/file.tar.gz is the archive";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "~/src/file.tar.gz");
}

#[test]
fn test_file_path_preserves_dotfiles() {
    let text = "edit ~/.gitignore";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "~/.gitignore");
}

#[test]
fn test_file_path_with_line_number_and_trailing_period() {
    let text = "error at ./src/main.rs:42.";
    let paths = detect_file_paths_in_line(text, 0);
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].url, "./src/main.rs");
    if let DetectedItemType::FilePath { line, column } = &paths[0].item_type {
        assert_eq!(*line, Some(42));
        assert_eq!(*column, None);
    } else {
        panic!("Expected FilePath type");
    }
}

#[test]
fn test_url_strips_trailing_period() {
    let text = "Visit https://example.com.";
    let urls = detect_urls_in_line(text, 0);
    assert_eq!(urls.len(), 1);
    assert_eq!(urls[0].url, "https://example.com");
}

#[test]
fn test_url_preserves_internal_dots() {
    let text = "Visit https://www.example.com/page.html for info";
    let urls = detect_urls_in_line(text, 0);
    assert_eq!(urls.len(), 1);
    assert_eq!(urls[0].url, "https://www.example.com/page.html");
}

// --- ensure_url_scheme tests ---

#[test]
fn test_ensure_url_scheme_adds_https_when_no_scheme() {
    assert_eq!(
        ensure_url_scheme("www.example.com"),
        "https://www.example.com"
    );
    assert_eq!(
        ensure_url_scheme("example.com/path"),
        "https://example.com/path"
    );
}

#[test]
fn test_ensure_url_scheme_preserves_existing_scheme() {
    assert_eq!(
        ensure_url_scheme("https://example.com"),
        "https://example.com"
    );
    assert_eq!(
        ensure_url_scheme("http://example.com"),
        "http://example.com"
    );
    assert_eq!(
        ensure_url_scheme("ftp://files.example.com"),
        "ftp://files.example.com"
    );
    assert_eq!(
        ensure_url_scheme("file:///tmp/test.html"),
        "file:///tmp/test.html"
    );
}

// --- expand_link_handler tests ---

#[test]
fn test_expand_link_handler_replaces_url_placeholder() {
    let parts =
        expand_link_handler("firefox {url}", "https://example.com").expect("should succeed");
    assert_eq!(parts, vec!["firefox", "https://example.com"]);
}

#[test]
fn test_expand_link_handler_multi_word_command() {
    let parts = expand_link_handler("open -a Firefox {url}", "https://example.com")
        .expect("should succeed");
    assert_eq!(parts, vec!["open", "-a", "Firefox", "https://example.com"]);
}

#[test]
fn test_expand_link_handler_no_placeholder() {
    // If command has no {url}, it still works - the URL just doesn't appear
    let parts = expand_link_handler("my-browser", "https://example.com").expect("should succeed");
    assert_eq!(parts, vec!["my-browser"]);
}

#[test]
fn test_expand_link_handler_errors_on_empty_expansion() {
    // A command that is only whitespace after expansion should error
    let result = expand_link_handler("   ", "https://example.com");
    assert!(result.is_err());
    assert_eq!(
        result.unwrap_err(),
        "Link handler command is empty after expansion"
    );
}

#[test]
fn test_expand_link_handler_empty_command() {
    let result = expand_link_handler("", "https://example.com");
    assert!(result.is_err());
}

// --- H1 security: URL argument injection prevention ---

#[test]
fn test_expand_link_handler_url_with_spaces_stays_single_arg() {
    // A crafted URL with spaces must NOT inject additional arguments
    let parts = expand_link_handler(
        "firefox {url}",
        "https://evil.com --new-window javascript:alert(1)",
    )
    .expect("should succeed");
    // The URL (including its spaces) must remain a single argument
    assert_eq!(parts.len(), 2);
    assert_eq!(parts[0], "firefox");
    assert_eq!(
        parts[1],
        "https://evil.com --new-window javascript:alert(1)"
    );
}

#[test]
fn test_expand_link_handler_url_with_shell_metacharacters() {
    // Shell metacharacters in URLs must not cause issues
    let parts = expand_link_handler("open {url}", "https://example.com/search?q=foo&bar=baz|cat")
        .expect("should succeed");
    assert_eq!(parts.len(), 2);
    assert_eq!(parts[1], "https://example.com/search?q=foo&bar=baz|cat");
}

#[test]
fn test_expand_link_handler_quoted_template_preserved() {
    // A template that uses shell quoting should be parsed correctly
    let parts = expand_link_handler("open -a 'Google Chrome' {url}", "https://example.com")
        .expect("should succeed");
    assert_eq!(
        parts,
        vec!["open", "-a", "Google Chrome", "https://example.com"]
    );
}

// --- H2 security: shell_escape tests ---

#[test]
fn test_shell_escape_basic_path() {
    assert_eq!(shell_escape("/tmp/file.txt"), "'/tmp/file.txt'");
}

#[test]
fn test_shell_escape_path_with_single_quotes() {
    assert_eq!(
        shell_escape("/tmp/it's a file.txt"),
        "'/tmp/it'\\''s a file.txt'"
    );
}

#[test]
fn test_shell_escape_path_with_backticks() {
    // Backticks inside single quotes are safe (not interpreted)
    assert_eq!(
        shell_escape("/tmp/`rm -rf /`/file.txt"),
        "'/tmp/`rm -rf /`/file.txt'"
    );
}

#[test]
fn test_shell_escape_path_with_dollar_expansion() {
    // $(cmd) inside single quotes is safe (not interpreted)
    assert_eq!(
        shell_escape("/tmp/$(whoami)/file.txt"),
        "'/tmp/$(whoami)/file.txt'"
    );
}