fresh-editor 0.3.2

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! End-to-end coverage for the user `init.ts` auto-loader (design M0).
//!
//! We drive the loader directly on the test harness. The harness doesn't
//! replicate `main()`'s boot sequence, so it doesn't call `load_init_script`
//! on its own — we do. That's enough to exercise: file discovery, the
//! `--no-init` escape hatch, and successful evaluation.

use crate::common::harness::EditorTestHarness;
use fresh::config_io::DirectoryContext;
use std::fs;
use std::path::PathBuf;

/// Build a harness whose `DirectoryContext::config_dir` is
/// `<tempdir>/config` (the layout `DirectoryContext::for_testing` uses).
/// Returns the harness, the temp guard, and the resolved config_dir.
fn harness_with_scratch_config_dir() -> (EditorTestHarness, tempfile::TempDir, PathBuf) {
    let temp = tempfile::TempDir::new().expect("tempdir");
    let dir_context = DirectoryContext::for_testing(temp.path());
    let config_dir = dir_context.config_dir.clone();
    fs::create_dir_all(&config_dir).unwrap();

    let working_dir = temp.path().join("work");
    fs::create_dir_all(&working_dir).unwrap();

    let harness = EditorTestHarness::with_shared_dir_context(
        80,
        24,
        Default::default(),
        working_dir,
        dir_context,
    )
    .expect("harness");
    (harness, temp, config_dir)
}

fn write_init_ts(config_dir: &std::path::Path, body: &str) {
    fs::write(config_dir.join("init.ts"), body).unwrap();
}

#[test]
fn missing_init_ts_is_silent() {
    let (mut harness, _tmp, _config_dir) = harness_with_scratch_config_dir();

    // Capture any pre-existing status (other plugins may set one) so we only
    // assert init.ts doesn't introduce a new one.
    let before = harness.editor().get_status_message().cloned();

    harness.editor_mut().load_init_script(true);

    let after = harness.editor().get_status_message().cloned();
    assert_eq!(
        before, after,
        "loading a missing init.ts must not change the status"
    );
    // And specifically: nothing should mention init.ts.
    assert!(
        after
            .as_deref()
            .map(|s| !s.contains("init.ts"))
            .unwrap_or(true),
        "status should not mention init.ts when the file is absent: {after:?}"
    );
}

#[test]
fn disabled_flag_skips_init_ts_even_when_present() {
    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();
    write_init_ts(&config_dir, "throw new Error('should not run');");

    // `enabled = false` models `--no-init` / `--safe`.
    harness.editor_mut().load_init_script(false);

    // The eval intentionally would have thrown; if we skipped, no failure
    // banner should surface.
    let status = harness.editor().get_status_message().cloned();
    let offending = status
        .as_deref()
        .map(|s| s.contains("init.ts:"))
        .unwrap_or(false);
    assert!(
        !offending,
        "disabled init.ts must not surface a failure: status = {status:?}"
    );
}

#[test]
fn set_setting_updates_effective_config() {
    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();

    let before: serde_json::Value =
        serde_json::to_value(&harness.editor().config_for_tests().editor).unwrap();
    let original_tab_size = before["tab_size"].as_u64().unwrap_or(4);
    let target_tab_size = if original_tab_size == 7 { 3 } else { 7 };

    write_init_ts(
        &config_dir,
        &format!(
            r#"
            const editor = getEditor();
            editor.setSetting("editor.tab_size", {target_tab_size});
            "#
        ),
    );

    harness.editor_mut().load_init_script(true);
    harness.editor_mut().process_async_messages();

    let after_tab = harness.editor().config_for_tests().editor.tab_size;
    assert_eq!(
        after_tab as u64, target_tab_size,
        "init.ts setSetting should update the effective tab_size"
    );
}

#[test]
fn editor_on_accepts_a_closure_and_plugins_loaded_fires() {
    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();

    write_init_ts(
        &config_dir,
        r#"
        const editor = getEditor();
        editor.on("plugins_loaded", () => {
            editor.setStatus("plugins_loaded fired");
        });
        "#,
    );

    harness.editor_mut().load_init_script(true);
    harness.editor_mut().fire_plugins_loaded_hook();

    // Hook dispatch is async (plugin thread) — poll until the SetStatus
    // command arrives rather than hoping a single process_async_messages
    // is enough.
    harness
        .wait_until(|h| {
            h.editor()
                .get_status_message()
                .map(|s| s.contains("plugins_loaded fired"))
                .unwrap_or(false)
        })
        .unwrap();
}

#[test]
fn export_plugin_api_and_get_plugin_api_round_trip() {
    // A plugin exports a typed surface; init.ts-style code reaches it via
    // getPluginApi and calls through to the plugin's own configure method.
    // All in one plugin because the harness loads exactly one source at a
    // time — but the same mechanism works across plugins at runtime.
    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();

    write_init_ts(
        &config_dir,
        r#"
        const editor = getEditor();

        // Pretend this is a separate plugin publishing its config API.
        let stored = null;
        editor.exportPluginApi("fake-dashboard", {
            configure(opts) { stored = opts; },
            getStored() { return stored; },
        });

        // And this is init.ts reaching it.
        const api = editor.getPluginApi("fake-dashboard");
        if (api === null) {
            editor.setStatus("ERR: api not found");
        } else {
            api.configure({ title: "Hello" });
            const back = api.getStored();
            editor.setStatus(`got back: ${back.title}`);
        }
        "#,
    );

    harness.editor_mut().load_init_script(true);
    harness.editor_mut().process_async_messages();

    let status = harness
        .editor()
        .get_status_message()
        .cloned()
        .unwrap_or_default();
    assert!(
        status.contains("got back: Hello"),
        "expected configure/read round-trip through getPluginApi: status = {status:?}"
    );
}

#[test]
fn get_plugin_api_returns_null_when_name_unknown() {
    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();

    write_init_ts(
        &config_dir,
        r#"
        const editor = getEditor();
        const api = editor.getPluginApi("does-not-exist");
        editor.setStatus(api === null ? "null" : "not-null");
        "#,
    );

    harness.editor_mut().load_init_script(true);
    harness.editor_mut().process_async_messages();

    let status = harness
        .editor()
        .get_status_message()
        .cloned()
        .unwrap_or_default();
    assert!(
        status.contains("null"),
        "getPluginApi for unknown name should return null: status = {status:?}"
    );
}

#[test]
fn ready_hook_fires_and_can_be_observed_with_legacy_on_form() {
    // The legacy string-handler form must keep working alongside the
    // closure overload.
    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();

    write_init_ts(
        &config_dir,
        r#"
        const editor = getEditor();
        function on_ready_handler() {
            editor.setStatus("ready fired");
        }
        registerHandler("on_ready_handler", on_ready_handler);
        editor.on("ready", "on_ready_handler");
        "#,
    );

    harness.editor_mut().load_init_script(true);
    harness.editor_mut().fire_ready_hook();

    harness
        .wait_until(|h| {
            h.editor()
                .get_status_message()
                .map(|s| s.contains("ready fired"))
                .unwrap_or(false)
        })
        .unwrap();
}

#[test]
fn set_setting_is_fire_and_forget_across_reload() {
    // setSetting writes persist across reload — fire-and-forget, same model
    // as Neovim/VS Code/Emacs/Sublime. A reload that no longer calls
    // setSetting does NOT revert the prior value.
    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();

    let original_tab_size = harness.editor().config_for_tests().editor.tab_size as u64;
    let overridden = if original_tab_size == 7 { 3 } else { 7 };

    // First run: write an override.
    write_init_ts(
        &config_dir,
        &format!(
            r#"
            const editor = getEditor();
            editor.setSetting("editor.tab_size", {overridden});
            "#
        ),
    );
    harness.editor_mut().load_init_script(true);
    harness.editor_mut().process_async_messages();
    assert_eq!(
        harness.editor().config_for_tests().editor.tab_size as u64,
        overridden
    );

    // Second run: no setSetting at all — the old write persists.
    write_init_ts(
        &config_dir,
        r#"
        const editor = getEditor();
        editor.setStatus("init.ts reloaded with no writes");
        "#,
    );
    harness.editor_mut().load_init_script(true);
    harness.editor_mut().process_async_messages();

    assert_eq!(
        harness.editor().config_for_tests().editor.tab_size as u64,
        overridden,
        "fire-and-forget: the prior setSetting write survives reload"
    );
}

#[test]
fn init_reload_action_picks_up_file_edits() {
    use fresh::input::keybindings::Action;

    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();

    // Initial content: writes one sentinel.
    write_init_ts(
        &config_dir,
        r#"
        const editor = getEditor();
        editor.setStatus("first load");
        "#,
    );
    harness.editor_mut().load_init_script(true);
    harness.editor_mut().process_async_messages();

    // Edit the file.
    write_init_ts(
        &config_dir,
        r#"
        const editor = getEditor();
        editor.setStatus("second load");
        "#,
    );

    // Dispatch the palette action.
    harness
        .editor_mut()
        .dispatch_action_for_tests(Action::InitReload);
    harness.editor_mut().process_async_messages();

    let status = harness
        .editor()
        .get_status_message()
        .cloned()
        .unwrap_or_default();
    assert!(
        status.contains("second load"),
        "init: Reload should re-read the file and run the new body: status = {status:?}"
    );
}

#[test]
fn init_edit_creates_starter_template_when_missing() {
    use fresh::input::keybindings::Action;

    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();
    assert!(!config_dir.join("init.ts").exists(), "precondition");

    harness
        .editor_mut()
        .dispatch_action_for_tests(Action::InitEdit);
    harness.editor_mut().process_async_messages();

    let created = config_dir.join("init.ts");
    assert!(
        created.exists(),
        "init: Edit should create the starter file"
    );

    let body = std::fs::read_to_string(&created).unwrap();
    assert!(
        body.contains("const editor = getEditor();"),
        "starter template should set up the plugin API: body starts {:?}",
        &body.get(..60)
    );
    // Every example should be commented out — empty init is valid.
    assert!(body.contains("// Example:"));
}

#[test]
fn init_edit_refreshes_plugins_d_ts() {
    // The user's tsconfig.json lists `types/plugins.d.ts` in `files`, so
    // it must exist by the time the LSP picks up init.ts or the typed
    // `getPluginApi("foo")` overload stays untyped. Startup writes the
    // file once; the InitEdit action re-runs that write so plugins
    // loaded/unloaded since boot are reflected before the user opens
    // init.ts, and so a user who deleted the file gets it back.
    use fresh::input::keybindings::Action;

    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();
    let plugins_dts = config_dir.join("types").join("plugins.d.ts");

    // Delete whatever the harness may have written at boot so we can
    // assert that InitEdit itself re-creates the file.
    if plugins_dts.exists() {
        fs::remove_file(&plugins_dts).unwrap();
    }

    harness
        .editor_mut()
        .dispatch_action_for_tests(Action::InitEdit);
    harness.editor_mut().process_async_messages();

    assert!(
        plugins_dts.exists(),
        "InitEdit must write types/plugins.d.ts so the user's tsconfig.json resolves"
    );
    let body = std::fs::read_to_string(&plugins_dts).unwrap();
    assert!(
        body.contains("AUTO-GENERATED"),
        "plugins.d.ts should carry the fresh autogen header: {body:?}"
    );
}

#[test]
fn init_check_action_reports_ok_on_a_clean_file() {
    use fresh::input::keybindings::Action;

    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();
    write_init_ts(&config_dir, "const editor = getEditor();\n");

    harness
        .editor_mut()
        .dispatch_action_for_tests(Action::InitCheck);
    harness.editor_mut().process_async_messages();

    let status = harness
        .editor()
        .get_status_message()
        .cloned()
        .unwrap_or_default();
    assert!(
        status.contains("init.ts: ok"),
        "expected 'init.ts: ok', got {status:?}"
    );
}

#[test]
fn init_check_action_reports_an_error_on_a_broken_file() {
    use fresh::input::keybindings::Action;

    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();
    write_init_ts(&config_dir, "function broken(\n");

    harness
        .editor_mut()
        .dispatch_action_for_tests(Action::InitCheck);
    harness.editor_mut().process_async_messages();

    let status = harness
        .editor()
        .get_status_message()
        .cloned()
        .unwrap_or_default();
    assert!(
        status.contains("init.ts:") && status.contains("error"),
        "expected an init.ts error report, got {status:?}"
    );
}

#[test]
fn init_ts_is_loaded_as_a_plugin_named_init_ts() {
    let (mut harness, _tmp, config_dir) = harness_with_scratch_config_dir();

    // A minimal init.ts that just registers a status message lets us verify
    // it actually ran inside the plugin runtime.
    write_init_ts(
        &config_dir,
        r#"
        const editor = getEditor();
        editor.setStatus("init.ts ran");
        "#,
    );

    harness.editor_mut().load_init_script(true);

    // Drain any plugin-command traffic queued by the load.
    harness.editor_mut().process_async_messages();

    let status = harness
        .editor()
        .get_status_message()
        .cloned()
        .unwrap_or_default();
    assert!(
        status.contains("init.ts ran"),
        "expected init.ts to set a status; got {status:?}"
    );
}