tftio-prompter 2.3.0

A CLI tool for composing reusable prompt snippets from a library using TOML profiles
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
//! Integration tests for the prompter CLI.
//!
//! These tests verify the complete CLI functionality by running the actual
//! binary with various arguments and checking the results.

use std::env;
use std::fs;
use std::io::Read;
use std::path::PathBuf;
use std::process::Command;

fn tmp_home(prefix: &str) -> PathBuf {
    let mut p = env::temp_dir();
    let unique = format!(
        "{}_{}_{}",
        prefix,
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    );
    p.push(unique);
    p
}

fn read_all(path: &std::path::Path) -> Vec<u8> {
    let mut f = fs::File::open(path).unwrap();
    let mut buf = Vec::new();
    f.read_to_end(&mut buf).unwrap();
    buf
}

const fn bin_path() -> &'static str {
    env!("CARGO_BIN_EXE_prompter")
}

#[test]
fn test_init_list_validate_run() {
    let home = tmp_home("prompter_it_home");
    fs::create_dir_all(&home).unwrap();

    // init
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .arg("init")
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "init failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // list
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .arg("list")
        .output()
        .unwrap();
    assert!(out.status.success());
    let list = String::from_utf8_lossy(&out.stdout);
    assert!(list.contains("python.api"));
    assert!(list.contains("general.testing"));

    // validate
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .arg("validate")
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "validate failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // run profile
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .arg("run")
        .arg("python.api")
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Check output contains expected content (pre-prompt + prefix + file contents + post-prompt)
    let output_str = String::from_utf8_lossy(&out.stdout);
    // Should start with default pre-prompt
    assert!(output_str.starts_with("You are an LLM coding agent."));
    // Should contain system prefix
    assert!(output_str.contains("Today is "));
    assert!(output_str.contains(", and you are running on a "));
    assert!(output_str.contains(" system.\n\n"));
    // Should contain the library file contents
    let lib = home.join(".local/prompter/library");
    let c_bytes = read_all(&lib.join("a/b/c.md"));
    let h_bytes = read_all(&lib.join("f/g/h.md"));
    let c_content = String::from_utf8_lossy(&c_bytes);
    let h_content = String::from_utf8_lossy(&h_bytes);
    assert!(output_str.contains(&*c_content));
    assert!(output_str.contains(&*h_content));
    // Should end with default post-prompt
    assert!(output_str.ends_with(
        "Now, read the @AGENTS.md and @CLAUDE.md files in this directory, if they exist."
    ));
}

#[test]
fn test_missing_file_and_unknown_profile_fail() {
    let home = tmp_home("prompter_it_missing");
    let cfg_path = home.join(".config/prompter");
    let lib_path = home.join(".local/prompter/library");
    fs::create_dir_all(&cfg_path).unwrap();
    fs::create_dir_all(&lib_path).unwrap();

    let cfg = r#"
[root]
depends_on = ["does.not.exist.md", "unknown_profile"]
"#;
    fs::write(cfg_path.join("config.toml"), cfg).unwrap();

    // validate should fail
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .arg("validate")
        .output()
        .unwrap();
    assert!(!out.status.success());
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("Missing file:"));
    assert!(err.contains("Unknown profile:"));

    // running profile should also fail
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .arg("run")
        .arg("root")
        .output()
        .unwrap();
    assert!(!out.status.success());
}

#[test]
fn test_recursive_resolution_and_separator() {
    let home = tmp_home("prompter_it_recursive");
    let cfg_path = home.join(".config/prompter");
    let lib_path = home.join(".local/prompter/library");
    fs::create_dir_all(&cfg_path).unwrap();
    fs::create_dir_all(lib_path.join("a")).unwrap();
    fs::create_dir_all(lib_path.join("f")).unwrap();

    // files
    fs::write(home.join(".local/prompter/library/a/x.md"), b"AX\n").unwrap();
    fs::write(home.join(".local/prompter/library/f/y.md"), b"FY\n").unwrap();

    // config with recursive profile dep
    let cfg = r#"
[child]
depends_on = ["a/x.md"]

[root]
depends_on = ["child", "f/y.md", "a/x.md"]
"#;
    fs::write(cfg_path.join("config.toml"), cfg).unwrap();

    // run with separator that will be unescaped
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .args(["run", "--separator", "\\n--\\n", "root"]) // CLI will unescape to "\n--\n"
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Dedup behavior: only first occurrence of a file is included
    let output_str = String::from_utf8_lossy(&out.stdout);
    // Should start with default pre-prompt
    assert!(output_str.starts_with("You are an LLM coding agent."));
    // Should contain system prefix
    assert!(output_str.contains("Today is "));
    assert!(output_str.contains(", and you are running on a "));
    assert!(output_str.contains(" system.\n\n"));
    // Should contain file content with separator, in expected order
    assert!(output_str.contains("AX\n"));
    assert!(output_str.contains("\n--\n"));
    assert!(output_str.contains("FY\n"));
    // Should end with the default post-prompt
    assert!(output_str.ends_with(
        "Now, read the @AGENTS.md and @CLAUDE.md files in this directory, if they exist."
    ));
}

#[test]
fn test_cycle_detection_in_validate() {
    let home = tmp_home("prompter_it_cycle");
    let cfg_path = home.join(".config/prompter");
    let lib_path = home.join(".local/prompter/library");
    fs::create_dir_all(&cfg_path).unwrap();
    fs::create_dir_all(&lib_path).unwrap();

    let cfg = r#"
[A]
depends_on = ["B"]
[B]
depends_on = ["A"]
"#;
    fs::write(cfg_path.join("config.toml"), cfg).unwrap();

    let out = Command::new(bin_path())
        .env("HOME", &home)
        .arg("validate")
        .output()
        .unwrap();
    assert!(!out.status.success());
    let err = String::from_utf8_lossy(&out.stderr);
    assert!(err.contains("Cycle detected"), "stderr: {err}");
}

#[test]
fn test_version_flag() {
    let out = Command::new(bin_path()).arg("version").output().unwrap();
    assert!(out.status.success());
    let got = String::from_utf8_lossy(&out.stdout).trim().to_string();
    let expected = format!("prompter {}", env!("CARGO_PKG_VERSION"));
    assert_eq!(got, expected);
}

#[test]
fn test_version_json_flag() {
    let out = Command::new(bin_path())
        .args(["--json", "version"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("\"version\""));
}

#[test]
fn test_completions_bash() {
    let out = Command::new(bin_path())
        .args(["completions", "bash"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("prompter"));
    assert!(stdout.contains("_prompter"));
}

#[test]
fn test_completions_zsh() {
    let out = Command::new(bin_path())
        .args(["completions", "zsh"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("prompter"));
}

#[test]
fn test_completions_fish() {
    let out = Command::new(bin_path())
        .args(["completions", "fish"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("prompter"));
}

#[test]
fn test_doctor_command() {
    let out = Command::new(bin_path()).arg("doctor").output().unwrap();
    // Doctor may succeed or fail depending on local setup
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("health check"));
}

#[test]
fn test_doctor_command_json() {
    let out = Command::new(bin_path())
        .args(["--json", "doctor"])
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("\"config_file_exists\""));
}

#[test]
fn test_help_flag() {
    let out = Command::new(bin_path()).arg("--help").output().unwrap();
    // Help output may go to stdout or stderr depending on exit code
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(combined.contains("prompter") || combined.contains("Usage:"));
}

#[test]
fn test_run_with_separator() {
    let home = tmp_home("prompter_it_sep");
    fs::create_dir_all(&home).unwrap();

    // Init first
    Command::new(bin_path())
        .env("HOME", &home)
        .arg("init")
        .output()
        .unwrap();

    // Run with custom separator
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .args(["run", "--separator", "\\n---\\n", "python.api"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("\n---\n"));
}

#[test]
fn test_run_with_custom_pre_prompt() {
    let home = tmp_home("prompter_it_pre");
    fs::create_dir_all(&home).unwrap();

    Command::new(bin_path())
        .env("HOME", &home)
        .arg("init")
        .output()
        .unwrap();

    let out = Command::new(bin_path())
        .env("HOME", &home)
        .args(["run", "--pre-prompt", "Custom prefix", "python.api"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.starts_with("Custom prefix"));
}

#[test]
fn test_run_with_custom_post_prompt() {
    let home = tmp_home("prompter_it_post");
    fs::create_dir_all(&home).unwrap();

    Command::new(bin_path())
        .env("HOME", &home)
        .arg("init")
        .output()
        .unwrap();

    let out = Command::new(bin_path())
        .env("HOME", &home)
        .args(["run", "--post-prompt", "Custom suffix", "python.api"])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.ends_with("Custom suffix"));
}

#[test]
fn test_multiple_profiles_shorthand() {
    let home = tmp_home("prompter_it_multi");
    let cfg_path = home.join(".config/prompter");
    let lib_path = home.join(".local/prompter/library");
    fs::create_dir_all(&cfg_path).unwrap();
    fs::create_dir_all(lib_path.join("a")).unwrap();
    fs::create_dir_all(lib_path.join("b")).unwrap();
    fs::create_dir_all(lib_path.join("shared")).unwrap();

    // Create files
    fs::write(lib_path.join("shared/common.md"), b"COMMON\n").unwrap();
    fs::write(lib_path.join("a/only.md"), b"A_ONLY\n").unwrap();
    fs::write(lib_path.join("b/only.md"), b"B_ONLY\n").unwrap();

    // Config where both profiles depend on shared file
    let cfg = r#"
[profile.a]
depends_on = ["shared/common.md", "a/only.md"]

[profile.b]
depends_on = ["shared/common.md", "b/only.md"]
"#;
    fs::write(cfg_path.join("config.toml"), cfg).unwrap();

    // Run with multiple profiles: `prompter run profile.a profile.b`
    let out = Command::new(bin_path())
        .env("HOME", &home)
        .args(["run", "profile.a", "profile.b"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let stdout = String::from_utf8_lossy(&out.stdout);

    // Common file should appear exactly once (deduplication)
    let common_count = stdout.matches("COMMON").count();
    assert_eq!(
        common_count, 1,
        "shared/common.md should appear exactly once, found {common_count}"
    );

    // Both profile-specific files should appear
    assert!(stdout.contains("A_ONLY"));
    assert!(stdout.contains("B_ONLY"));

    // Order should be: common (from profile.a), then a/only, then b/only
    let common_pos = stdout.find("COMMON").unwrap();
    let a_pos = stdout.find("A_ONLY").unwrap();
    let b_pos = stdout.find("B_ONLY").unwrap();
    assert!(
        common_pos < a_pos && a_pos < b_pos,
        "Files should appear in order: COMMON < A_ONLY < B_ONLY"
    );
}