dpc-tau-cli-term 0.2.1

A minimal Unix-first coding agent.
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
use std::{fs, sync as path_std_sync};

use crate::completion::{
    CommandCompletion, CompletionData, CompletionRule, CompletionRules, build_candidates,
    build_candidates_with_home, build_candidates_with_home_and_cwd, build_candidates_with_rules,
};

/// Ensures the built-in `./` rule reads the injected working directory and
/// keeps its local prefix in both preview and accepted file candidates.
#[test]
fn dotslash_token_triggers_filesystem_candidates() {
    let cwd = tempfile::tempdir().expect("tempdir");
    fs::write(cwd.path().join("tau-cli-term-dotslash.txt"), "").expect("write test file");
    let buffer = "./tau-cli-term-dotslash";
    let candidates = build_candidates_with_home_and_cwd(
        &[CommandCompletion::new(":whatever", "")],
        &CompletionData::new(),
        buffer,
        buffer.len(),
        None,
        cwd.path(),
    );

    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].label, "./tau-cli-term-dotslash.txt");
    assert_eq!(candidates[0].description, "file");
    assert_eq!(candidates[0].replacement, "./tau-cli-term-dotslash.txt");
}

/// Ensures a home completion previews the typed tilde form, then expands only
/// its accepted token while preserving cursor position and quoted prompt text.
#[test]
fn home_relative_token_previews_tilde_and_accepts_absolute_home_path() {
    let home = tempfile::tempdir().expect("tempdir");
    fs::write(home.path().join("alpha.txt"), "").expect("write alpha");
    fs::write(home.path().join("beta.txt"), "").expect("write beta");
    let buffer = r#"open ~/a and "~/literal""#;
    let cursor = "open ~/a".len();

    let cands = build_candidates_with_home(
        &[CommandCompletion::new(":whatever", "")],
        &CompletionData::new(),
        buffer,
        cursor,
        Some(home.path()),
    );

    assert_eq!(cands.len(), 1);
    assert_eq!(cands[0].label, "~/alpha.txt");
    assert_eq!(cands[0].replacement, r#"open ~/alpha.txt and "~/literal""#);
    assert_eq!(cands[0].cursor, "open ~/alpha.txt".len());
    let acceptance = cands[0].acceptance.as_ref().expect("home acceptance");
    assert_eq!(
        acceptance.replacement,
        format!(
            r#"open {} and "~/literal""#,
            home.path().join("alpha.txt").display()
        )
    );
    assert_eq!(
        acceptance.cursor,
        "open ".len() + home.path().join("alpha.txt").to_string_lossy().len()
    );
}

/// Ensures a missing HOME retains the existing behavior: no `~/` candidate
/// exists, so prompt text cannot be expanded by completion.
#[test]
fn home_relative_token_without_home_has_no_completion() {
    let buffer = "open ~/a";
    assert!(
        build_candidates_with_home(
            &[CommandCompletion::new(":whatever", "")],
            &CompletionData::new(),
            buffer,
            buffer.len(),
            None,
        )
        .is_empty()
    );
}

/// Ensures accepted candidates use the same lossy path conversion as existing
/// filesystem completion when the configured HOME path is not UTF-8.
#[cfg(unix)]
#[test]
fn home_relative_token_with_non_utf8_home_uses_lossy_absolute_acceptance() {
    use std::ffi::OsString;
    use std::os::unix::ffi::OsStringExt;

    let parent = tempfile::tempdir().expect("tempdir");
    let home = parent.path().join(OsString::from_vec(vec![b'h', 0x80]));
    fs::create_dir(&home).expect("create non-UTF-8 home");
    fs::write(home.join("alpha.txt"), "").expect("write alpha");
    let buffer = "open ~/a";
    let candidates = build_candidates_with_home(
        &[CommandCompletion::new(":whatever", "")],
        &CompletionData::new(),
        buffer,
        buffer.len(),
        Some(&home),
    );

    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].replacement, "open ~/alpha.txt");
    assert_eq!(
        candidates[0]
            .acceptance
            .as_ref()
            .expect("home acceptance")
            .replacement,
        format!("open {}", home.join("alpha.txt").to_string_lossy())
    );
}

#[test]
fn filesystem_directory_candidates_include_trailing_slash() {
    // Directory completions should be visibly distinct from files and accepting
    // one should leave the prompt ready to complete or type a child path.
    let home = tempfile::tempdir().expect("tempdir");
    fs::create_dir(home.path().join("alpha-dir")).expect("mkdir alpha-dir");
    fs::write(home.path().join("alpha.txt"), "").expect("write alpha file");
    let buffer = "open ~/alpha";
    let cursor = buffer.len();

    let cands = build_candidates_with_home(
        &[CommandCompletion::new(":whatever", "")],
        &CompletionData::new(),
        buffer,
        cursor,
        Some(home.path()),
    );

    let dir = cands
        .iter()
        .find(|cand| cand.description == "directory")
        .expect("directory candidate");
    assert_eq!(dir.label, "~/alpha-dir/");
    assert_eq!(dir.replacement, "open ~/alpha-dir/");

    let file = cands
        .iter()
        .find(|cand| cand.description == "file")
        .expect("file candidate");
    assert_eq!(file.label, "~/alpha.txt");
    assert_eq!(file.replacement, "open ~/alpha.txt");
}

/// Keeps an intrinsic command buffer out of generic filesystem completion.
#[test]
fn command_buffer_does_not_route_to_filesystem() {
    let cands = build_candidates(
        &[CommandCompletion::new(":model", "Switch model")],
        &CompletionData::new(),
        ":mod",
        ":mod".len(),
    );
    assert_eq!(cands.len(), 1);
    assert_eq!(cands[0].replacement, ":model");
}

/// Ensures a leading absolute path uses the shipped slash path rule rather than
/// entering command completion.
#[test]
fn leading_absolute_path_completes_from_the_filesystem() {
    let directory = tempfile::tempdir().expect("tempdir");
    fs::write(directory.path().join("alpha.txt"), "").expect("write alpha");
    let buffer = format!("{}/a", directory.path().display());

    let candidates = build_candidates(
        &[CommandCompletion::new(":model", "Switch model")],
        &CompletionData::new(),
        &buffer,
        buffer.len(),
    );

    assert_eq!(candidates.len(), 1);
    assert_eq!(
        candidates[0].replacement,
        format!("{}/alpha.txt", directory.path().display())
    );
    assert!(!candidates[0].label.starts_with(':'));
}

/// Ensures configured colon triggers cannot shadow intrinsic command mode or
/// leak prompt-text completers into command-name and argument completion.
#[test]
fn configured_colon_rules_cannot_shadow_command_mode() {
    let rules = CompletionRules::new(vec![
        CompletionRule::parse(":", "complete_path").expect("path rule"),
        CompletionRule::parse(":set", "complete_with_command printf hostile")
            .expect("command rule"),
    ]);
    let data = CompletionData::new();

    let root = build_candidates_with_rules(
        &[CommandCompletion::new(":model", "Switch model")],
        &data,
        &rules,
        ":mod",
        ":mod".len(),
    );
    assert_eq!(root.len(), 1);
    assert_eq!(root[0].replacement, ":model");

    assert!(
        build_candidates_with_rules(
            &[CommandCompletion::new(":set", "Set UI state")],
            &data,
            &rules,
            ":set /tmp",
            ":set /tmp".len(),
        )
        .is_empty()
    );
    assert!(
        rules
            .command_for_exact_token(":set", ":set".len())
            .is_none()
    );
}

/// Ensures doubled-colon literal prompt text does not open command completion.
#[test]
fn literal_colon_escape_does_not_enter_command_completion() {
    assert!(
        build_candidates(
            &[CommandCompletion::new(":model", "Switch model")],
            &CompletionData::new(),
            "::model",
            "::model".len(),
        )
        .is_empty()
    );
}

#[test]
fn at_token_completes_agent_mentions_in_prompt_text() {
    // Agent mentions are prompt-text completions, not commands. Accepting
    // one must replace only the current `@...` token and preserve surrounding
    // prompt text.
    let data = CompletionData::new();
    data.set_agent_mention_completer(path_std_sync::Arc::new(|args| {
        assert_eq!(args, ["wo"]);
        vec![crate::completion::CompletionItem::new("worker", "agent")]
    }));
    let buffer = "ask @wo for help";
    let cursor = "ask @wo".len();

    let cands = build_candidates(
        &[CommandCompletion::new(":model", "Switch model")],
        &data,
        buffer,
        cursor,
    );

    assert_eq!(cands.len(), 1);
    assert_eq!(cands[0].label, "worker");
    assert_eq!(cands[0].description, "agent");
    assert_eq!(cands[0].replacement, "ask @worker for help");
}

/// Session completion replaces only a word-leading ampersand token and keeps
/// ordinary surrounding prompt text unchanged.
#[test]
fn ampersand_token_completes_running_session_ids() {
    let data = CompletionData::new();
    data.set_session_completer(path_std_sync::Arc::new(|args| {
        assert_eq!(args, ["roj"]);
        vec![crate::completion::CompletionItem::new(
            "project-main",
            "/work/project",
        )]
    }));
    let buffer = "ask &roj about this";
    let cursor = "ask &roj".len();

    let candidates = build_candidates(&[], &data, buffer, cursor);

    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].label, "project-main");
    assert_eq!(candidates[0].description, "/work/project");
    assert_eq!(candidates[0].replacement, "ask &project-main about this");
    assert_eq!(candidates[0].cursor, "ask &project-main".len());
}

/// The named session rule parses and participates in the built-in rule set.
#[test]
fn complete_sessions_rule_parses_and_is_built_in() {
    assert!(matches!(
        CompletionRule::parse("&", "complete_sessions").map(|rule| rule.kind),
        Some(crate::completion::CompletionRuleKind::Sessions)
    ));
    let data = CompletionData::new();
    data.set_session_completer(path_std_sync::Arc::new(|_| {
        vec![crate::completion::CompletionItem::plain("session-one")]
    }));
    assert_eq!(build_candidates(&[], &data, "&", 1).len(), 1);
}

/// Session completion never treats an embedded ampersand as a token trigger and
/// never erases a remote-agent suffix from a session address.
#[test]
fn session_completion_rejects_embedded_tokens_and_remote_agent_suffixes() {
    let data = CompletionData::new();
    data.set_session_completer(path_std_sync::Arc::new(|_| {
        vec![crate::completion::CompletionItem::plain("project-main")]
    }));

    assert!(build_candidates(&[], &data, "x&proj", "x&proj".len()).is_empty());
    assert!(build_candidates(&[], &data, "&proj/@agent", "&proj".len()).is_empty());
    assert!(build_candidates(&[], &data, "&proj/@agent", "&proj/@agent".len()).is_empty());
}

/// Intrinsic command mode retains precedence over session completion in command
/// arguments.
#[test]
fn command_mode_does_not_offer_session_completion() {
    let data = CompletionData::new();
    data.set_session_completer(path_std_sync::Arc::new(|_| {
        vec![crate::completion::CompletionItem::plain("project-main")]
    }));
    assert!(build_candidates(&[], &data, ":anything &proj", 15).is_empty());
}

/// Preserves indentation while completing a leading command token.
#[test]
fn leading_whitespace_command_preserves_prefix() {
    let cands = build_candidates(
        &[CommandCompletion::new(":model", "Switch model")],
        &CompletionData::new(),
        "  :mod",
        "  :mod".len(),
    );
    assert_eq!(cands.len(), 1);
    assert_eq!(cands[0].replacement, "  :model");
}

#[test]
fn configured_actions_preserve_surrounding_prompt_text() {
    let rules = CompletionRules::new(vec![
        CompletionRule::parse("#", "complete_actions").expect("rule parses"),
    ]);
    let cands = build_candidates_with_rules(
        &[CommandCompletion::new(":model", "Switch model")],
        &CompletionData::new(),
        &rules,
        "ask #mod now",
        "ask #mod".len(),
    );
    assert_eq!(cands.len(), 1);
    assert_eq!(cands[0].replacement, "ask #model now");
}

/// Gives intrinsic leading commands precedence over configurable token rules.
#[test]
fn command_completion_requires_whole_token_and_loses_to_leading_command_tokens() {
    let rules = CompletionRules::new(vec![
        CompletionRule::parse("#/", "complete_with_command fzf").expect("rule parses"),
        CompletionRule::parse(":", "complete_with_command fzf").expect("rule parses"),
    ]);
    assert!(rules.command_for_exact_token("#/foo", "#/".len()).is_none());
    assert!(rules.command_for_exact_token(":", ":".len()).is_none());
    assert!(
        rules
            .command_for_exact_token("ask #/", "ask #/".len())
            .is_some()
    );
}

#[test]
fn non_slash_non_path_buffer_returns_nothing() {
    let cands = build_candidates(
        &[CommandCompletion::new(":model", "Switch model")],
        &CompletionData::new(),
        "hello",
        "hello".len(),
    );
    assert!(cands.is_empty());
}

/// Ensures the built-in `../` rule reads the injected parent directory and
/// preserves the traversal prefix in an accepted file candidate.
#[test]
fn parent_traversal_token_is_recognised() {
    let parent = tempfile::tempdir().expect("tempdir");
    let cwd = parent.path().join("child");
    fs::create_dir(&cwd).expect("create child directory");
    fs::write(parent.path().join("tau-cli-term-parent.txt"), "").expect("write test file");
    let buffer = "../tau-cli-term-parent";
    let candidates = build_candidates_with_home_and_cwd(
        &[CommandCompletion::new(":whatever", "")],
        &CompletionData::new(),
        buffer,
        buffer.len(),
        None,
        &cwd,
    );

    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].label, "../tau-cli-term-parent.txt");
    assert_eq!(candidates[0].description, "file");
    assert_eq!(candidates[0].replacement, "../tau-cli-term-parent.txt");
}