ane-editor 0.2.0

A New Editor / Agent Native Editor — a modern vim-inspired terminal editor built for humans and code agents
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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
use std::collections::HashMap;
use std::io::{IsTerminal, Read};
use std::path::Path;

use anyhow::{Context, Result, bail};

use crate::data::buffer::Buffer;
use crate::data::chord_types::{Action, Scope};
use crate::data::lsp::registry;
use crate::data::lsp::types::ServerState;

use super::chord_engine::ChordEngine;
use super::chord_engine::types::{ChordArgs, ChordQuery};
use super::lsp_engine::LspEngine;

pub trait FrontendCapabilities {
    fn is_interactive(&self) -> bool;
}

#[cfg(test)]
struct HeadlessContext;
#[cfg(test)]
impl FrontendCapabilities for HeadlessContext {
    fn is_interactive(&self) -> bool {
        false
    }
}

pub fn parse_chord(input: &str) -> Result<ChordQuery> {
    ChordEngine::parse(input)
}

fn args_are_empty(args: &ChordArgs) -> bool {
    args.target_name.is_none()
        && args.parent_name.is_none()
        && args.target_line.is_none()
        && args.cursor_pos.is_none()
        && args.value.is_none()
        && args.find.is_none()
        && args.replace.is_none()
}

use super::chord_engine::types::ListItem;

#[derive(Debug)]
pub struct ChordResult {
    pub original: String,
    pub modified: String,
    pub warnings: Vec<String>,
    pub yanked: Option<String>,
    pub listed_items: Vec<ListItem>,
}

pub fn execute_chord(
    frontend: &dyn FrontendCapabilities,
    path: &Path,
    chord: &ChordQuery,
    lsp: &mut LspEngine,
) -> Result<ChordResult> {
    if chord.action.requires_interactive() && !frontend.is_interactive() {
        bail!("Jump action requires an interactive frontend; use ane in TUI mode");
    }

    if !path.exists() {
        bail!("file not found: {}", path.display());
    }

    let abs_path = std::fs::canonicalize(path)
        .with_context(|| format!("failed to canonicalize path: {}", path.display()))?;

    let buffer = Buffer::from_file(&abs_path)?;
    let original = buffer.content();
    let path_str = abs_path.to_string_lossy().to_string();
    let mut buffers = HashMap::new();
    buffers.insert(path_str.clone(), buffer);

    let mut chord = chord.clone();
    resolve_stdin_sentinels(&mut chord)?;

    if args_are_empty(&chord.args) && chord.action != Action::List && chord.scope != Scope::Buffer {
        bail!(
            "exec mode requires explicit parameters, e.g. {}(fn_name, \"body\")",
            chord.short_form()
        );
    }

    if !frontend.is_interactive() {
        match chord.action {
            Action::Change if chord.args.value.is_none() => {
                bail!(
                    "{} requires an explicit value: pass value:\"...\" (or value:- for stdin); use value:\"\" to clear intentionally",
                    chord.short_form()
                );
            }
            Action::Replace
                if chord.args.value.is_none()
                    && !(chord.args.find.is_some() && chord.args.replace.is_some()) =>
            {
                bail!(
                    "{} requires either value:\"...\" or both find:\"...\" and replace:\"...\"",
                    chord.short_form()
                );
            }
            _ => {}
        }
    }

    if chord.requires_lsp {
        let lsp_timeout = lsp.startup_timeout();
        let lang = registry::detect_language_from_path(&abs_path).ok_or_else(|| {
            anyhow::anyhow!("no language server available for {}", path.display())
        })?;

        let root_path = abs_path
            .parent()
            .ok_or_else(|| anyhow::anyhow!("cannot determine parent directory"))?;

        lsp.start_for_context(root_path, &[&abs_path])?;

        let state = lsp.await_ready(lang, lsp_timeout)?;

        match state {
            ServerState::Running => {}
            ServerState::Failed => {
                bail!("LSP server for {} failed to start", lang.name());
            }
            _ => {
                bail!(
                    "LSP server for {} did not become ready within 30s",
                    lang.name()
                );
            }
        }
    }

    let resolved = ChordEngine::resolve(&chord, &buffers, lsp)?;
    let actions = ChordEngine::patch(&resolved, &buffers)?;

    if let Some(action) = actions.get(&path_str) {
        let modified = if let Some(ref diff) = action.diff {
            diff.modified.clone()
        } else {
            original.clone()
        };

        if modified != original {
            std::fs::write(&abs_path, &modified)?;
        }

        Ok(ChordResult {
            original,
            modified,
            warnings: action.warnings.clone(),
            yanked: action.yanked_content.clone(),
            listed_items: action.listed_items.clone(),
        })
    } else {
        Ok(ChordResult {
            original: original.clone(),
            modified: original,
            warnings: Vec::new(),
            yanked: None,
            listed_items: vec![],
        })
    }
}

fn strip_trailing_newline(s: &mut String) {
    if s.ends_with('\n') {
        s.pop();
        if s.ends_with('\r') {
            s.pop();
        }
    }
}

fn resolve_stdin_sentinels(chord: &mut ChordQuery) -> Result<()> {
    let has_sentinel = [
        chord.args.value.as_deref(),
        chord.args.target_name.as_deref(),
        chord.args.find.as_deref(),
        chord.args.replace.as_deref(),
    ]
    .contains(&Some("-"));

    if !has_sentinel {
        return Ok(());
    }

    if std::io::stdin().is_terminal() {
        bail!("chord parameter '-' requires piped input on stdin");
    }

    let mut stdin_content = String::new();
    std::io::stdin()
        .read_to_string(&mut stdin_content)
        .map_err(|e| anyhow::anyhow!("failed to read stdin: {e}"))?;

    // Strip exactly one trailing newline so `echo "foo" | ane exec ...` matches a literal "foo"
    strip_trailing_newline(&mut stdin_content);

    // All `-` sentinels share the same stdin read
    if chord.args.value.as_deref() == Some("-") {
        chord.args.value = Some(stdin_content.clone());
    }
    if chord.args.target_name.as_deref() == Some("-") {
        if let Ok(n) = stdin_content.parse::<usize>() {
            chord.args.target_name = None;
            chord.args.target_line = Some(n);
        } else {
            chord.args.target_name = Some(stdin_content.clone());
        }
    }
    if chord.args.find.as_deref() == Some("-") {
        chord.args.find = Some(stdin_content.clone());
    }
    if chord.args.replace.as_deref() == Some("-") {
        chord.args.replace = Some(stdin_content);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::lsp_engine::LspEngineConfig;
    use crate::data::chord_types::{Action, Component, Positional, Scope};
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn temp_file(content: &str) -> NamedTempFile {
        let mut f = NamedTempFile::new().unwrap();
        f.write_all(content.as_bytes()).unwrap();
        f
    }

    fn default_engine() -> LspEngine {
        LspEngine::new(LspEngineConfig::default())
    }

    // --- strip_trailing_newline ---

    #[test]
    fn trailing_newline_strip_removes_lf() {
        let mut s = "foo\n".to_string();
        strip_trailing_newline(&mut s);
        assert_eq!(s, "foo");
    }

    #[test]
    fn trailing_newline_strip_no_change_without_newline() {
        let mut s = "foo".to_string();
        strip_trailing_newline(&mut s);
        assert_eq!(s, "foo");
    }

    #[test]
    fn trailing_newline_strip_removes_crlf() {
        let mut s = "foo\r\n".to_string();
        strip_trailing_newline(&mut s);
        assert_eq!(s, "foo");
    }

    // --- stdin-is-TTY rejection ---

    #[test]
    fn stdin_sentinel_tty_rejection_exact_message() {
        use std::io::IsTerminal;
        if !std::io::stdin().is_terminal() {
            // Can only assert TTY rejection when stdin is an interactive terminal.
            // When piped (e.g. in CI), this path is exercised by binary tests instead.
            return;
        }
        let mut chord = parse_chord("cels").unwrap();
        chord.args.target_line = Some(0);
        chord.args.value = Some("-".to_string());

        let result = resolve_stdin_sentinels(&mut chord);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "chord parameter '-' requires piped input on stdin"
        );
    }

    #[test]
    fn resolve_stdin_sentinels_noop_when_no_sentinel() {
        let mut chord = parse_chord("cels").unwrap();
        chord.args.target_line = Some(0);
        chord.args.value = Some("literal text".to_string());

        resolve_stdin_sentinels(&mut chord).unwrap();
        assert_eq!(chord.args.value.as_deref(), Some("literal text"));
    }

    #[test]
    fn resolve_stdin_sentinels_noop_when_no_args() {
        let mut chord = parse_chord("cels").unwrap();
        resolve_stdin_sentinels(&mut chord).unwrap();
        assert!(chord.args.value.is_none());
    }

    // --- error message formats ---

    #[test]
    fn error_message_file_not_found_exact_format() {
        let chord = parse_chord("cels").unwrap();
        let result = execute_chord(
            &HeadlessContext,
            Path::new("/nonexistent/path/does-not-exist.rs"),
            &chord,
            &mut default_engine(),
        );
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("file not found: /nonexistent/path/does-not-exist.rs"),
            "got: {msg}"
        );
    }

    #[test]
    fn error_message_exec_requires_explicit_params() {
        // LSP-scoped chord with no target_name or cursor_pos triggers this error before
        // any LSP server is started.
        let mut f_rs = tempfile::Builder::new().suffix(".rs").tempfile().unwrap();
        f_rs.write_all(b"fn main() {}").unwrap();
        f_rs.flush().unwrap();

        let chord = parse_chord("cifc").unwrap(); // ChangeInsideFunctionContents, requires_lsp=true
        // No target_name or cursor_pos set → error before LSP starts
        let result = execute_chord(&HeadlessContext, f_rs.path(), &chord, &mut default_engine());
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("exec mode requires explicit parameters"),
            "got: {msg}"
        );

        // Fix 1: Buffer-scope chords are exempt — no target is meaningful for a whole-file operation.
        let chord_yebs = parse_chord("yebs").unwrap();
        let result = execute_chord(
            &HeadlessContext,
            f_rs.path(),
            &chord_yebs,
            &mut default_engine(),
        );
        assert!(
            result.is_ok(),
            "yebs with no args must not raise the explicit-params error; got: {:?}",
            result.err()
        );
    }

    #[test]
    fn error_message_bare_chord_rejected_for_line_scope() {
        // Non-LSP scope without args must also be rejected.
        let f = temp_file("hello\n");
        let chord = parse_chord("cels").unwrap();
        let result = execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine());
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("exec mode requires explicit parameters"),
            "got: {msg}"
        );
    }

    #[test]
    fn error_message_no_language_server_for_non_rust_file() {
        // Non-.rs file with an LSP-scoped chord that has a target_name set.
        // Fails with "no language server available" after the param check.
        let mut f = tempfile::Builder::new().suffix(".txt").tempfile().unwrap();
        f.write_all(b"some content").unwrap();
        f.flush().unwrap();

        let mut chord = parse_chord("cifc").unwrap();
        chord.args.target_name = Some("some_fn".to_string());
        chord.args.value = Some("body".to_string());

        let result = execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine());
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("no language server available"), "got: {msg}");
    }

    #[test]
    fn parse_short_form_cifc() {
        let parsed = parse_chord("cifc").unwrap();
        assert_eq!(parsed.action, Action::Change);
        assert_eq!(parsed.positional, Positional::Inside);
        assert_eq!(parsed.scope, Scope::Function);
        assert_eq!(parsed.component, Component::Contents);
        assert!(parsed.requires_lsp);
    }

    #[test]
    fn parse_long_form() {
        let parsed = parse_chord("ChangeInsideFunctionContents").unwrap();
        assert_eq!(parsed.action, Action::Change);
        assert_eq!(parsed.positional, Positional::Inside);
        assert_eq!(parsed.scope, Scope::Function);
        assert_eq!(parsed.component, Component::Contents);
        assert!(parsed.requires_lsp);
    }

    #[test]
    fn parse_change_entire_line_self() {
        let parsed = parse_chord("cels").unwrap();
        assert_eq!(parsed.action, Action::Change);
        assert_eq!(parsed.positional, Positional::Entire);
        assert_eq!(parsed.scope, Scope::Line);
        assert_eq!(parsed.component, Component::Self_);
        assert!(!parsed.requires_lsp);
    }

    #[test]
    fn parse_delete_entire_line_self() {
        let parsed = parse_chord("dels").unwrap();
        assert_eq!(parsed.action, Action::Delete);
        assert_eq!(parsed.positional, Positional::Entire);
        assert_eq!(parsed.scope, Scope::Line);
        assert_eq!(parsed.component, Component::Self_);
    }

    #[test]
    fn parse_delete_entire_line_self_long() {
        let parsed = parse_chord("DeleteEntireLineSelf").unwrap();
        assert_eq!(parsed.action, Action::Delete);
        assert_eq!(parsed.positional, Positional::Entire);
        assert_eq!(parsed.scope, Scope::Line);
        assert_eq!(parsed.component, Component::Self_);
    }

    #[test]
    fn parse_yank_entire_function_contents() {
        let parsed = parse_chord("yefc").unwrap();
        assert_eq!(parsed.action, Action::Yank);
        assert_eq!(parsed.positional, Positional::Entire);
        assert_eq!(parsed.scope, Scope::Function);
        assert_eq!(parsed.component, Component::Contents);
        assert!(parsed.requires_lsp);
    }

    #[test]
    fn parse_append_after_line_end() {
        let parsed = parse_chord("aale").unwrap();
        assert_eq!(parsed.action, Action::Append);
        assert_eq!(parsed.positional, Positional::After);
        assert_eq!(parsed.scope, Scope::Line);
        assert_eq!(parsed.component, Component::End);
        assert!(!parsed.requires_lsp);
    }

    #[test]
    fn parse_with_args() {
        let parsed = parse_chord("cifp(target:getData, value:\"(x: i32)\")").unwrap();
        assert_eq!(parsed.action, Action::Change);
        assert_eq!(parsed.scope, Scope::Function);
        assert_eq!(parsed.component, Component::Parameters);
        assert_eq!(parsed.args.target_name.as_deref(), Some("getData"));
        assert_eq!(parsed.args.value.as_deref(), Some("(x: i32)"));
    }

    #[test]
    fn parse_invalid_combination() {
        let result = parse_chord("cilp");
        assert!(result.is_err());
    }

    #[test]
    fn execute_change_line() {
        let f = temp_file("aaa\nbbb\nccc");
        let mut chord = parse_chord("cels").unwrap();
        chord.args.target_line = Some(1);
        chord.args.value = Some("xxx".to_string());
        let result =
            execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine()).unwrap();
        assert!(result.modified.contains("xxx"));
        assert!(!result.modified.contains("bbb"));
    }

    #[test]
    fn execute_delete_line() {
        let f = temp_file("aaa\nbbb\nccc");
        let mut chord = parse_chord("dels").unwrap();
        chord.args.target_line = Some(1);
        let result =
            execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine()).unwrap();
        assert!(!result.modified.contains("bbb"));
        assert!(result.modified.contains("aaa"));
        assert!(result.modified.contains("ccc"));
    }

    #[test]
    fn parse_unknown_chord() {
        let result = parse_chord("zzzz");
        assert!(result.is_err());
    }

    #[test]
    fn parse_delete_entire_buffer_self() {
        let parsed = parse_chord("debs").unwrap();
        assert_eq!(parsed.action, Action::Delete);
        assert_eq!(parsed.positional, Positional::Entire);
        assert_eq!(parsed.scope, Scope::Buffer);
        assert_eq!(parsed.component, Component::Self_);
        assert!(!parsed.requires_lsp);
    }

    // --- work item 0005: Jump / To / Delimiter ---

    #[test]
    fn execute_chord_jump_rejects_before_file_io_check() {
        // Jump on a non-interactive frontend must fail with the interactive error
        // BEFORE the file-not-found check fires — even for a nonexistent path.
        let chord = parse_chord("jefc").unwrap();
        let result = execute_chord(
            &HeadlessContext,
            Path::new("/nonexistent/path/does-not-exist.rs"),
            &chord,
            &mut default_engine(),
        );
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("interactive") || msg.contains("Jump"),
            "expected interactive error before file-not-found, got: {msg}"
        );
        assert!(
            !msg.contains("file not found"),
            "file-not-found must not fire before interactive check, got: {msg}"
        );
    }

    // Fix 1 — Buffer-scope chords succeed with no args

    #[test]
    fn execute_yank_entire_buffer_no_args_succeeds() {
        let f = temp_file("line one\nline two\n");
        let chord = parse_chord("yebs").unwrap(); // no target, no value
        let result =
            execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine()).unwrap();
        assert_eq!(
            result.yanked.as_deref(),
            Some("line one\nline two"),
            "yebs must return full file content"
        );
    }

    // Fix 2 — aals inserts content immediately after the specified line

    #[test]
    fn execute_append_after_line_inserts_at_correct_position() {
        // 5-line file; target_line = 2 (0-based) = "ccc".
        // Inserted line must appear right after "ccc", not at EOF.
        let f = temp_file("aaa\nbbb\nccc\nddd\neee\n");
        let mut chord = parse_chord("aals").unwrap();
        chord.args.target_line = Some(2); // 0-based index 2 = "ccc"
        chord.args.value = Some("xxx".to_string());
        let result =
            execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine()).unwrap();
        let lines: Vec<&str> = result.modified.lines().collect();
        let ccc_idx = lines.iter().position(|l| *l == "ccc").expect("ccc present");
        let xxx_idx = lines.iter().position(|l| *l == "xxx").expect("xxx present");
        let ddd_idx = lines.iter().position(|l| *l == "ddd").expect("ddd present");
        assert_eq!(
            xxx_idx,
            ccc_idx + 1,
            "inserted line must immediately follow the target line"
        );
        assert!(
            ddd_idx > xxx_idx,
            "original line after target must come after the inserted line"
        );
        assert!(
            xxx_idx < lines.len() - 1,
            "inserted line must not be at EOF"
        );
    }

    // Fix 5 — aebs appends on a genuinely new line

    #[test]
    fn execute_aebs_appends_on_new_line() {
        let f = temp_file("existing\n");
        let mut chord = parse_chord("aebs").unwrap();
        chord.args.value = Some("appended".to_string());
        let result =
            execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine()).unwrap();
        let lines: Vec<&str> = result.modified.lines().collect();
        assert!(
            lines.contains(&"existing"),
            "original content must be preserved"
        );
        assert_eq!(
            lines.last().copied(),
            Some("appended"),
            "appended value must appear as a separate last line"
        );
        assert!(
            !result.modified.contains("existingappended"),
            "value must not be concatenated inline"
        );
    }

    // CLI value-required guard: Change/Replace must carry an explicit value
    // when the frontend is non-interactive, so chords like `cebs` and `rebs`
    // cannot silently clear a file.

    #[test]
    fn cebs_without_value_errors_and_preserves_file() {
        let f = temp_file("keep me\n");
        let chord = parse_chord("cebs").unwrap();
        let result = execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine());
        assert!(result.is_err(), "cebs with no value must fail on CLI");
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("requires an explicit value"),
            "expected explicit-value error, got: {msg}"
        );
        let on_disk = std::fs::read_to_string(f.path()).unwrap();
        assert_eq!(on_disk, "keep me\n", "file must be untouched after error");
    }

    #[test]
    fn rebs_without_value_or_find_replace_errors() {
        let f = temp_file("keep me\n");
        let chord = parse_chord("rebs").unwrap();
        let result = execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine());
        assert!(result.is_err(), "rebs with no args must fail on CLI");
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("requires either value") && msg.contains("find"),
            "expected value-or-find-replace error, got: {msg}"
        );
        let on_disk = std::fs::read_to_string(f.path()).unwrap();
        assert_eq!(on_disk, "keep me\n", "file must be untouched after error");
    }

    #[test]
    fn cebs_with_explicit_empty_value_succeeds() {
        let f = temp_file("clear me\n");
        let mut chord = parse_chord("cebs").unwrap();
        chord.args.value = Some(String::new());
        let result = execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine());
        assert!(
            result.is_ok(),
            "explicit value:\"\" is the documented escape hatch for intentional clear; got: {:?}",
            result.err()
        );
        let on_disk = std::fs::read_to_string(f.path()).unwrap();
        assert!(
            on_disk.trim().is_empty(),
            "file content must be cleared when value:\"\" is explicit; got: {on_disk:?}"
        );
        assert!(
            !on_disk.contains("clear me"),
            "original content must be gone; got: {on_disk:?}"
        );
    }

    #[test]
    fn rebs_with_find_and_replace_succeeds() {
        let f = temp_file("hello world\n");
        let mut chord = parse_chord("rebs").unwrap();
        chord.args.find = Some("hello".to_string());
        chord.args.replace = Some("goodbye".to_string());
        let result = execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine());
        assert!(
            result.is_ok(),
            "rebs with find+replace pair must succeed without value; got: {:?}",
            result.err()
        );
        let on_disk = std::fs::read_to_string(f.path()).unwrap();
        assert!(
            on_disk.contains("goodbye"),
            "find/replace must apply; got: {on_disk}"
        );
    }

    #[test]
    fn change_line_without_value_errors() {
        let f = temp_file("first\nsecond\n");
        let mut chord = parse_chord("cels").unwrap();
        chord.args.target_line = Some(0);
        let result = execute_chord(&HeadlessContext, f.path(), &chord, &mut default_engine());
        assert!(
            result.is_err(),
            "cels with target but no value must fail on CLI"
        );
        let on_disk = std::fs::read_to_string(f.path()).unwrap();
        assert_eq!(
            on_disk, "first\nsecond\n",
            "file must be untouched after error"
        );
    }

    #[test]
    fn change_value_required_guard_skipped_when_frontend_is_interactive() {
        struct InteractiveContext;
        impl FrontendCapabilities for InteractiveContext {
            fn is_interactive(&self) -> bool {
                true
            }
        }
        let f = temp_file("first\nsecond\n");
        let mut chord = parse_chord("cels").unwrap();
        chord.args.target_line = Some(0);
        // In the interactive (TUI) path, missing value is valid — the frontend
        // would enter interactive edit mode. The guard must not fire.
        let result = execute_chord(&InteractiveContext, f.path(), &chord, &mut default_engine());
        assert!(
            result.is_ok(),
            "interactive frontend must bypass the value-required guard; got: {:?}",
            result.err()
        );
    }
}