strop-engine 0.32.3

strop editor engine: documents, grammar, services and sessions — no frontend UI dependencies
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
//! Completion reducer tests. The worker is suppressed with a fixture
//! tape (hermetic: no real `~/.ssh` read, no client, no network) and
//! answered through the production handler — exactly how the TUI's
//! forwarded deliveries arrive.

use super::*;
use strop_core::Buffer;

fn editor() -> Editor {
    Editor::new(Buffer::from_text("x\n"))
}

fn suppress(e: &mut Editor) {
    e.tape = std::rc::Rc::new(strop_trace::replay::Tape::fixture(|_, _| {
        Err(std::io::Error::other(
            "unexpected remote-completion observation",
        ))
    }));
}

fn cand(uri: &str, directory: bool) -> RemoteCandidate {
    RemoteCandidate {
        uri: uri.to_owned(),
        directory,
    }
}

fn hosts_result(uris: &[&str]) -> RemoteCompletionResult {
    RemoteCompletionResult::Candidates {
        items: uris.iter().map(|uri| cand(uri, false)).collect(),
        source: CandidateSource::Config,
        notes: Vec::new(),
        listed_directory: None,
    }
}

fn deliver(e: &mut Editor, ticket: Ticket<RemoteCompletionKey>, result: RemoteCompletionResult) {
    e.handle_remote_completion(Completion {
        ticket,
        outcome: Outcome::Success(result),
    });
}

fn ticket_for(e: &Editor, text: &str, query: RemoteCompletionQuery) -> Ticket<RemoteCompletionKey> {
    let pending = e.remote_completion.ticket().expect("request in flight");
    assert_eq!(pending.key.text, text);
    assert_eq!(pending.key.query, query);
    pending
}

fn complete_worker(editor: &mut Editor) {
    let event = editor
        .remote_completion
        .rx
        .as_ref()
        .unwrap()
        .recv_timeout(std::time::Duration::from_secs(5))
        .unwrap();
    editor.handle_remote_completion(event);
}

#[test]
fn local_completion_preserves_native_names_and_opens_the_selected_file() {
    #[cfg(unix)]
    let name = {
        use std::os::unix::ffi::OsStringExt;
        std::ffi::OsString::from_vec(b"odd name\n\xff.txt".to_vec())
    };
    #[cfg(not(unix))]
    let name = std::ffi::OsString::from("odd name.txt");
    let root = tempfile::tempdir().unwrap();
    let path = root.path().join(name);
    std::fs::write(&path, "native source\n").unwrap();
    let canonical = std::fs::canonicalize(&path).unwrap();
    let mut editor = Editor::new_in(Buffer::from_text(""), root.path().to_owned());
    editor.feed_text(":e odd <tab>");
    complete_worker(&mut editor);
    let uri = strop_workspace::ResourceLocation::local(canonical.clone())
        .uri()
        .unwrap();
    assert_eq!(editor.pending.text(), format!(":e {uri}"));
    editor.feed_text("<cr>");
    editor.wait_io().unwrap();
    assert_eq!(editor.buf().text(), "native source\n");
    assert_eq!(editor.buf().file_identity(), Some(canonical.as_path()));
}

#[test]
fn local_directory_completion_descends_and_rejects_stale_field_results() {
    let root = tempfile::tempdir().unwrap();
    std::fs::create_dir(root.path().join("folder")).unwrap();
    std::fs::write(root.path().join("folder/child.txt"), "child\n").unwrap();
    let mut editor = Editor::new_in(Buffer::from_text(""), root.path().to_owned());
    editor.feed_text(":e fol<tab>");
    complete_worker(&mut editor);
    assert!(editor.pending.text().ends_with("/folder/"));
    editor.feed_text("<tab>");
    complete_worker(&mut editor);
    assert!(editor.pending.text().ends_with("/folder/child.txt"));
    editor.feed_text("<esc><esc>:e fol<tab>x");
    let edited = editor.pending.text().to_owned();
    complete_worker(&mut editor);
    assert_eq!(editor.pending.text(), edited);
}

#[test]
fn host_candidates_apply_then_cycle_without_new_requests() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://al");
    e.feed_text("<tab>");
    let ticket = ticket_for(
        &e,
        ":e ssh://al",
        RemoteCompletionQuery::Hosts {
            partial: "al".into(),
        },
    );
    deliver(
        &mut e,
        ticket,
        hosts_result(&["ssh://alpha.example.com", "ssh://alpine.example.com"]),
    );
    assert_eq!(e.pending.text(), ":e ssh://alpha.example.com");
    assert!(e.message.contains("alpine.example.com"));
    // The second Tab cycles the landed list; no new request exists.
    e.feed_text("<tab>");
    assert!(e.remote_completion.ticket().is_none());
    assert_eq!(e.pending.text(), ":e ssh://alpine.example.com");
    // …and wraps around.
    e.feed_text("<tab>");
    assert_eq!(e.pending.text(), ":e ssh://alpha.example.com");
}

#[test]
fn tail_range_browse_and_follow_complete_their_uri_operand() {
    let mut e = editor();
    suppress(&mut e);
    // :tail BYTES URI keeps the byte count through the apply.
    e.feed_text(":tail 64k ssh://bui");
    e.feed_text("<tab>");
    let ticket = ticket_for(
        &e,
        ":tail 64k ssh://bui",
        RemoteCompletionQuery::Hosts {
            partial: "bui".into(),
        },
    );
    deliver(&mut e, ticket, hosts_result(&["ssh://build.example.com"]));
    assert_eq!(e.pending.text(), ":tail 64k ssh://build.example.com");
    // :tail without a byte count completes too.
    e.feed_text("<esc><esc>");
    e.feed_text(":tail ssh://bui");
    e.feed_text("<tab>");
    let ticket = ticket_for(
        &e,
        ":tail ssh://bui",
        RemoteCompletionQuery::Hosts {
            partial: "bui".into(),
        },
    );
    deliver(&mut e, ticket, hosts_result(&["ssh://build.example.com"]));
    assert_eq!(e.pending.text(), ":tail ssh://build.example.com");
    // :range with a missing BYTES refuses with the shape hint.
    e.feed_text("<esc><esc>");
    e.feed_text(":range 100 ssh://bui");
    e.feed_text("<tab>");
    assert_eq!(e.message, ":range needs START BYTES before the URI");
    assert!(e.remote_completion.ticket().is_none());
    // :range START BYTES URI completes and keeps both numbers.
    e.feed_text("<esc><esc>");
    e.feed_text(":range 100 4k ssh://bui");
    e.feed_text("<tab>");
    let ticket = ticket_for(
        &e,
        ":range 100 4k ssh://bui",
        RemoteCompletionQuery::Hosts {
            partial: "bui".into(),
        },
    );
    deliver(&mut e, ticket, hosts_result(&["ssh://build.example.com"]));
    assert_eq!(e.pending.text(), ":range 100 4k ssh://build.example.com");
    // :browse completes paths like :e does.
    e.feed_text("<esc><esc>");
    e.feed_text(":browse ssh://build/var/l");
    e.feed_text("<tab>");
    let ticket = ticket_for(
        &e,
        ":browse ssh://build/var/l",
        RemoteCompletionQuery::Path {
            authority: "build".into(),
            directory: "/var".into(),
            segment: "l".into(),
        },
    );
    deliver(
        &mut e,
        ticket,
        RemoteCompletionResult::ConnectRequired {
            endpoint: "ssh://build".into(),
        },
    );
    assert!(e.message.contains("never connects"));
    // :follow with no operand is not a completion at all.
    e.feed_text("<esc><esc>");
    e.feed_text(":follow");
    e.feed_text("<tab>");
    assert!(e.remote_completion.ticket().is_none());
}

#[test]
fn late_results_cannot_replace_edited_input() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://al");
    e.feed_text("<tab>");
    let ticket = e.remote_completion.ticket().expect("request in flight");
    e.feed_text("x"); // the user kept typing after the request
    deliver(
        &mut e,
        ticket,
        hosts_result(&["ssh://alpha.example.com", "ssh://alpine.example.com"]),
    );
    assert_eq!(
        e.pending.text(),
        ":e ssh://alx",
        "input must stay untouched"
    );
    assert!(e.remote_completion.ready.is_none());
    assert!(
        !e.message.contains("alpine"),
        "candidate list must not overwrite the line's state"
    );
}

#[test]
fn late_results_after_cancel_are_dropped_not_applied() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://al");
    e.feed_text("<tab>");
    let ticket = e.remote_completion.ticket().expect("request in flight");
    e.feed_text("<esc><esc>"); // Esc-Esc closes the prompt
    assert!(!e.pending.is_active());
    deliver(&mut e, ticket, hosts_result(&["ssh://alpha.example.com"]));
    assert!(!e.pending.is_active(), "no prompt may be reopened");
    assert_eq!(e.buf().text().to_string(), "x\n");
}

#[test]
fn focus_change_rejects_delivery() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://al");
    e.feed_text("<tab>");
    let ticket = e.remote_completion.ticket().expect("request in flight");
    e.focus_epoch += 1; // a pane switch happened after the request
    deliver(&mut e, ticket, hosts_result(&["ssh://alpha.example.com"]));
    assert_eq!(e.pending.text(), ":e ssh://al");
    assert!(e.remote_completion.ready.is_none());
}

#[test]
fn superseded_tickets_are_rejected() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://al");
    e.feed_text("<tab>");
    let stale = e.remote_completion.ticket().expect("request in flight");
    e.feed_text("p");
    e.feed_text("<tab>"); // replaces the request
    let current = e.remote_completion.ticket().expect("request in flight");
    assert_ne!(stale.request, current.request);
    deliver(&mut e, stale, hosts_result(&["ssh://stale.example.com"]));
    assert_eq!(e.pending.text(), ":e ssh://alp");
    deliver(&mut e, current, hosts_result(&["ssh://alpha.example.com"]));
    assert_eq!(e.pending.text(), ":e ssh://alpha.example.com");
}

#[test]
fn path_completion_never_connects_and_says_so() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://build/var/l");
    e.feed_text("<tab>");
    let ticket = ticket_for(
        &e,
        ":e ssh://build/var/l",
        RemoteCompletionQuery::Path {
            authority: "build".into(),
            directory: "/var".into(),
            segment: "l".into(),
        },
    );
    deliver(
        &mut e,
        ticket,
        RemoteCompletionResult::ConnectRequired {
            endpoint: "ssh://build".into(),
        },
    );
    assert_eq!(e.pending.text(), ":e ssh://build/var/l");
    assert!(e.message.contains("no live connection"));
    assert!(e.message.contains("never connects"));
    assert!(e.remote_completion.ready.is_none());
}

#[test]
fn directories_route_as_prefixes_files_as_full_uris() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://build/var/l");
    e.feed_text("<tab>");
    let ticket = e.remote_completion.ticket().expect("request in flight");
    // Worker-sorted order: directories first, each group by URI.
    deliver(
        &mut e,
        ticket,
        RemoteCompletionResult::Candidates {
            items: vec![
                cand("ssh://build/var/log/", true),
                cand("ssh://build/var/local", false),
            ],
            source: CandidateSource::Connection,
            notes: Vec::new(),
            listed_directory: Some("ssh://build/var".into()),
        },
    );
    // Directories apply first and keep a trailing slash…
    assert_eq!(e.pending.text(), ":e ssh://build/var/log/");
    assert!(e.message.contains("log/"), "directory shown with its slash");
    e.feed_text("<tab>");
    assert_eq!(e.pending.text(), ":e ssh://build/var/local");
    e.feed_text("<tab>");
    assert_eq!(e.pending.text(), ":e ssh://build/var/log/");
    // …and the live listing feeds the fallback cache.
    assert!(e
        .remote_completion
        .cached("ssh://build/var")
        .is_some_and(|items| items.len() == 2));
    // Typing into the selected directory starts a deeper query rather than
    // cycling the original alternatives.
    e.feed_text("s<tab>");
    let ticket = ticket_for(
        &e,
        ":e ssh://build/var/log/s",
        RemoteCompletionQuery::Path {
            authority: "build".into(),
            directory: "/var/log".into(),
            segment: "s".into(),
        },
    );
    deliver(
        &mut e,
        ticket,
        RemoteCompletionResult::Candidates {
            items: vec![cand("ssh://build/var/log/syslog", false)],
            source: CandidateSource::Cache,
            notes: Vec::new(),
            listed_directory: None,
        },
    );
    assert_eq!(e.pending.text(), ":e ssh://build/var/log/syslog");
    assert!(
        e.message.contains("(cached)"),
        "cache is labeled, never live"
    );
}

#[test]
fn percent_prefixes_classify_into_the_path_segment() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://build/%2");
    e.feed_text("<tab>");
    ticket_for(
        &e,
        ":e ssh://build/%2",
        RemoteCompletionQuery::Path {
            authority: "build".into(),
            directory: "/".into(),
            segment: "%2".into(),
        },
    );
    // A bare endpoint prefix stays host completion.
    e.feed_text("<esc><esc>");
    e.feed_text(":e ssh://bu");
    e.feed_text("<tab>");
    ticket_for(
        &e,
        ":e ssh://bu",
        RemoteCompletionQuery::Hosts {
            partial: "bu".into(),
        },
    );
}

#[cfg(unix)]
#[test]
fn applied_uris_keep_native_path_identity() {
    let mut e = editor();
    suppress(&mut e);
    e.feed_text(":e ssh://build/d");
    e.feed_text("<tab>");
    let ticket = e.remote_completion.ticket().expect("request in flight");
    deliver(
        &mut e,
        ticket,
        RemoteCompletionResult::Candidates {
            items: vec![cand("ssh://build/caf%FF%20menu.txt", false)],
            source: CandidateSource::Connection,
            notes: Vec::new(),
            listed_directory: None,
        },
    );
    assert_eq!(e.pending.text(), ":e ssh://build/caf%FF%20menu.txt");
    let file = RemoteFile::parse("ssh://build/caf%FF%20menu.txt").unwrap();
    assert_eq!(
        file.path().as_os_str().as_encoded_bytes(),
        b"/caf\xFF menu.txt".as_slice()
    );
}

#[test]
fn home_paths_are_refused_not_guessed() {
    let mut e = editor();
    e.feed_text(":e ssh://build/~");
    e.feed_text("<tab>");
    assert!(e.message.contains("home"));
    assert!(e.remote_completion.ticket().is_none());
    e.feed_text("<esc><esc>");
    e.feed_text(":e ssh://~/notes");
    e.feed_text("<tab>");
    assert!(e.message.contains("home"));
    assert!(e.remote_completion.ticket().is_none());
}

#[test]
fn non_file_commands_fall_back_to_command_name_cycling() {
    let mut e = editor();
    e.feed_text(":help ssh://x/y");
    e.feed_text("<tab>");
    assert!(e.remote_completion.ticket().is_none());
    // Command-name cycling still works on the same key.
    e.feed_text("<esc><esc>");
    e.feed_text(":he");
    e.feed_text("<tab>");
    assert_eq!(e.pending.text(), ":help");
}

#[test]
fn raw_spaces_and_mid_line_cursors_refuse_with_a_hint() {
    let mut e = editor();
    e.feed_text(":e ssh://build/a b");
    e.feed_text("<tab>");
    assert!(e.message.contains("%20"), "raw space gets an escape hint");
    e.feed_text("<esc><esc>");
    e.feed_text(":e ssh://build/lo");
    e.feed_text("<esc>h"); // line's normal mode, caret moved left
    e.feed_pending(crate::editor::Key::Tab);
    assert!(e.message.contains("end of the line"));
}