-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathintegration.rs
More file actions
626 lines (551 loc) · 18.9 KB
/
integration.rs
File metadata and controls
626 lines (551 loc) · 18.9 KB
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
use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;
fn oo() -> Command {
Command::cargo_bin("oo").unwrap()
}
#[test]
fn test_echo_passthrough() {
oo().args(["echo", "hello"])
.assert()
.success()
.stdout("hello\n");
}
#[test]
fn test_multiword_echo() {
oo().args(["echo", "hello", "world"])
.assert()
.success()
.stdout("hello world\n");
}
#[test]
fn test_false_failure() {
oo().args(["false"])
.assert()
.failure()
.stdout(predicate::str::starts_with("\u{2717}")); // ✗
}
#[test]
fn test_exit_code_preserved() {
oo().args(["sh", "-c", "exit 42"]).assert().code(42);
}
#[test]
fn test_version() {
oo().arg("version")
.assert()
.success()
.stdout(predicate::str::contains(env!("CARGO_PKG_VERSION")));
}
#[test]
fn test_no_args_shows_help() {
oo().assert()
.success()
.stdout(predicate::str::contains("Usage"));
}
#[test]
fn test_large_output_gets_indicator() {
// `git log` is categorized as Data, so large output gets indexed (●).
// Create a temp git repo to generate logs that exceed 4KB threshold.
let dir = TempDir::new().unwrap();
Command::new("git")
.args(["init"])
.current_dir(dir.path())
.output()
.unwrap();
// Create multiple commits with content to exceed 4KB
for i in 0..100 {
std::fs::write(dir.path().join("file.txt"), format!("content {}\n", i)).unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(dir.path())
.output()
.unwrap();
Command::new("git")
.args(["commit", "-m", &format!("commit {}", i)])
.env("GIT_AUTHOR_NAME", "Test")
.env("GIT_AUTHOR_EMAIL", "test@example.com")
.env("GIT_COMMITTER_NAME", "Test")
.env("GIT_COMMITTER_EMAIL", "test@example.com")
.current_dir(dir.path())
.output()
.unwrap();
}
// Run `oo git log` in the repo — should get ● (Large/indexed) indicator
oo().args(["git", "log"])
.current_dir(dir.path())
.assert()
.success()
.stdout(predicate::str::starts_with("\u{25CF}")); // ●
}
#[test]
fn test_stderr_included_in_failure() {
oo().args(["sh", "-c", "echo failure_msg >&2; exit 1"])
.assert()
.failure()
.stdout(predicate::str::contains("failure_msg"));
}
#[test]
fn test_forget_runs() {
oo().arg("forget")
.assert()
.success()
.stdout(predicate::str::contains("Cleared session data"));
}
#[test]
fn test_help_no_args_shows_usage() {
oo().arg("help")
.assert()
.success()
.stdout(predicate::str::contains("Usage:"));
}
#[test]
fn test_help_includes_help_cmd_in_usage() {
// Verify the help command itself appears in the no-args usage output
oo().assert()
.success()
.stdout(predicate::str::contains("oo help <cmd>"));
}
#[test]
fn test_help_empty_arg() {
Command::cargo_bin("oo")
.unwrap()
.args(&["help", ""])
.assert()
.failure();
}
// ---------------------------------------------------------------------------
// oo init
// ---------------------------------------------------------------------------
#[test]
fn test_init_creates_hooks_json() {
let dir = TempDir::new().unwrap();
oo().arg("init").current_dir(dir.path()).assert().success();
let hooks_path = dir.path().join(".claude").join("hooks.json");
assert!(
hooks_path.exists(),
".claude/hooks.json must exist after oo init"
);
}
#[test]
fn test_init_hooks_json_is_valid_json() {
let dir = TempDir::new().unwrap();
oo().arg("init").current_dir(dir.path()).assert().success();
let content = std::fs::read_to_string(dir.path().join(".claude").join("hooks.json")).unwrap();
let parsed: serde_json::Value =
serde_json::from_str(&content).expect("hooks.json must be valid JSON");
assert!(parsed.get("hooks").is_some());
}
#[test]
fn test_init_prints_agents_snippet() {
let dir = TempDir::new().unwrap();
oo().arg("init")
.current_dir(dir.path())
.assert()
.success()
.stdout(predicate::str::contains(
"Prefix all shell commands with `oo`",
));
}
#[test]
fn test_init_second_run_does_not_overwrite() {
let dir = TempDir::new().unwrap();
// First run creates the file.
oo().arg("init").current_dir(dir.path()).assert().success();
// Overwrite with sentinel content.
let hooks_path = dir.path().join(".claude").join("hooks.json");
std::fs::write(&hooks_path, r#"{"hooks":[],"sentinel":true}"#).unwrap();
// Second run must not clobber the file.
oo().arg("init").current_dir(dir.path()).assert().success();
let after = std::fs::read_to_string(&hooks_path).unwrap();
assert!(
after.contains("\"sentinel\":true"),
"pre-existing hooks.json must not be overwritten on second oo init"
);
}
#[test]
fn test_init_second_run_succeeds_without_error() {
let dir = TempDir::new().unwrap();
oo().arg("init").current_dir(dir.path()).assert().success();
// Second invocation must exit 0 — idempotent.
oo().arg("init").current_dir(dir.path()).assert().success();
}
// ---------------------------------------------------------------------------
// oo init --format generic
// ---------------------------------------------------------------------------
#[test]
fn test_init_format_generic_exits_success() {
let dir = TempDir::new().unwrap();
oo().args(["init", "--format", "generic"])
.current_dir(dir.path())
.assert()
.success();
}
#[test]
fn test_init_format_generic_prints_agents_snippet() {
let dir = TempDir::new().unwrap();
oo().args(["init", "--format", "generic"])
.current_dir(dir.path())
.assert()
.success()
.stdout(predicate::str::contains(
"Prefix all shell commands with `oo`",
));
}
#[test]
fn test_init_format_generic_prints_setup_section() {
let dir = TempDir::new().unwrap();
oo().args(["init", "--format", "generic"])
.current_dir(dir.path())
.assert()
.success()
.stdout(predicate::str::contains("## Setup"));
}
#[test]
fn test_init_format_generic_prints_shell_commands_instructions() {
let dir = TempDir::new().unwrap();
oo().args(["init", "--format", "generic"])
.current_dir(dir.path())
.assert()
.success()
.stdout(predicate::str::contains("oo recall"))
.stdout(predicate::str::contains("oo help"))
.stdout(predicate::str::contains("oo learn"));
}
#[test]
fn test_init_format_generic_prints_alias_section() {
let dir = TempDir::new().unwrap();
oo().args(["init", "--format", "generic"])
.current_dir(dir.path())
.assert()
.success()
.stdout(predicate::str::contains("alias o='oo'"));
}
#[test]
fn test_init_format_generic_does_not_create_hooks_json() {
let dir = TempDir::new().unwrap();
oo().args(["init", "--format", "generic"])
.current_dir(dir.path())
.assert()
.success();
// Generic format must NOT create any files.
let hooks_path = dir.path().join(".claude").join("hooks.json");
assert!(
!hooks_path.exists(),
"oo init --format generic must not create .claude/hooks.json"
);
}
// ---------------------------------------------------------------------------
// oo init --format claude
// ---------------------------------------------------------------------------
#[test]
fn test_init_format_claude_creates_hooks_json() {
let dir = TempDir::new().unwrap();
oo().args(["init", "--format", "claude"])
.current_dir(dir.path())
.assert()
.success();
let hooks_path = dir.path().join(".claude").join("hooks.json");
assert!(
hooks_path.exists(),
".claude/hooks.json must exist after oo init --format claude"
);
}
#[test]
fn test_init_format_claude_prints_agents_snippet() {
let dir = TempDir::new().unwrap();
oo().args(["init", "--format", "claude"])
.current_dir(dir.path())
.assert()
.success()
.stdout(predicate::str::contains(
"Prefix all shell commands with `oo`",
));
}
// ---------------------------------------------------------------------------
// recall command
// ---------------------------------------------------------------------------
#[test]
fn test_recall_no_args() {
// `oo recall` with no query should fail with a helpful error
oo().arg("recall")
.assert()
.failure()
.stderr(predicate::str::contains("recall requires a query"));
}
#[test]
fn test_recall_no_results() {
// A query that matches nothing should exit 0 and mention no results
oo().args(["recall", "xyzzy_nonexistent_query_12345"])
.assert()
.success()
.stdout(predicate::str::contains("No results found"));
}
// ---------------------------------------------------------------------------
// learn command
// ---------------------------------------------------------------------------
#[test]
fn test_learn_no_args() {
// `oo learn` with no command should fail with a helpful error
oo().arg("learn")
.assert()
.failure()
.stderr(predicate::str::contains("learn requires a command"));
}
#[test]
fn test_learn_no_api_key() {
// `oo learn echo hello` spawns a background child that checks the API key.
// The foreground process always exits 0 (it just runs the command and detaches).
// What we can verify: stderr mentions the learning intent for the command.
oo().args(["learn", "echo", "hello"])
.env_remove("ANTHROPIC_API_KEY")
.env_remove("OPENAI_API_KEY")
.assert()
.success()
.stderr(predicate::str::contains("[learning pattern for"));
}
// ---------------------------------------------------------------------------
// run command edge cases
// ---------------------------------------------------------------------------
#[test]
fn test_run_command_not_found() {
// A command that doesn't exist should result in a failure indicator or error
oo().args(["nonexistent_binary_xyz_abc_123"])
.assert()
.failure();
}
#[test]
fn test_run_multiword_command() {
// Multiple args are passed through correctly
oo().args(["echo", "hello", "world"])
.assert()
.success()
.stdout(predicate::str::contains("hello world"));
}
#[test]
fn test_passthrough_git_version() {
// `git --version` produces small output (< 4096 bytes) and exits 0.
// oo must pass it through verbatim — no ✓ or ● prefix.
// This tests the passthrough classification tier end-to-end.
oo().args(["git", "--version"])
.assert()
.success()
.stdout(predicate::str::starts_with("git version"));
}
#[test]
fn test_failure_stderr_output() {
// `ls /nonexistent_path_xyz` should exit non-zero and show ✗ with stderr content
oo().args(["ls", "/nonexistent_path_xyz_abc_12345"])
.assert()
.failure()
.stdout(predicate::str::starts_with("\u{2717}")); // ✗
}
// ---------------------------------------------------------------------------
// parse_action dispatch (CLI integration)
// ---------------------------------------------------------------------------
#[test]
fn test_dispatch_version() {
// `oo version` must exit 0
oo().arg("version").assert().success();
}
#[test]
fn test_dispatch_forget() {
// `oo forget` must exit 0 and clear session data
oo().arg("forget")
.assert()
.success()
.stdout(predicate::str::contains("Cleared session data"));
}
#[test]
fn test_dispatch_run() {
// `oo echo hi` dispatches to run — exits 0 with command output
oo().args(["echo", "hi"])
.assert()
.success()
.stdout(predicate::str::contains("hi"));
}
#[test]
fn test_dispatch_recall_query_joined() {
// `oo recall hello world` — multi-word query joined, exits 0
oo().args(["recall", "hello", "world"]).assert().success();
}
#[test]
fn test_dispatch_help() {
// `oo help` — shows usage
oo().arg("help")
.assert()
.success()
.stdout(predicate::str::contains("Usage"));
}
#[test]
fn test_dispatch_init() {
// `oo init` — runs the init command (tested more fully in init block above)
let dir = TempDir::new().unwrap();
oo().arg("init").current_dir(dir.path()).assert().success();
}
// ---------------------------------------------------------------------------
// oo patterns subcommand
// ---------------------------------------------------------------------------
#[test]
fn test_patterns_no_learned_patterns() {
// When no patterns exist (or patterns dir is absent), exit 0 and print "no patterns yet"
let dir = TempDir::new().unwrap();
oo().arg("patterns")
.env("HOME", dir.path())
.env("XDG_CONFIG_HOME", dir.path().join(".config"))
.assert()
.success()
.stdout(predicate::str::contains("no patterns yet"));
}
#[test]
fn test_patterns_with_learned_pattern() {
// When a valid pattern file exists, list it.
// Use double_o::learn::patterns_dir() to resolve the platform-correct path
// (macOS: ~/Library/Application Support/oo/patterns, Linux: ~/.config/oo/patterns).
let dir = TempDir::new().unwrap();
// Build the platform-appropriate path under our temp HOME by temporarily
// setting HOME and querying dirs::config_dir equivalent logic.
// We know patterns_dir() = dirs::config_dir()/oo/patterns, so replicate that.
#[cfg(target_os = "macos")]
let patterns_dir = dir
.path()
.join("Library")
.join("Application Support")
.join("oo")
.join("patterns");
#[cfg(not(target_os = "macos"))]
let patterns_dir = dir.path().join(".config").join("oo").join("patterns");
std::fs::create_dir_all(&patterns_dir).unwrap();
std::fs::write(
patterns_dir.join("pytest.toml"),
"command_match = \"^pytest\"\n[success]\npattern = '(?P<n>\\d+) passed'\nsummary = \"{n} passed\"\n",
)
.unwrap();
oo().arg("patterns")
.env("HOME", dir.path())
.env("XDG_CONFIG_HOME", dir.path().join(".config"))
.assert()
.success()
.stdout(predicate::str::contains("^pytest"));
}
#[test]
fn test_learn_provider_logged_to_stderr() {
// Part 1: provider name must appear in stderr before background spawn
oo().args(["learn", "echo", "hello"])
.env_remove("ANTHROPIC_API_KEY")
.env_remove("OPENAI_API_KEY")
.env_remove("CEREBRAS_API_KEY")
.assert()
.success()
.stderr(predicate::str::contains("anthropic"));
}
// ---------------------------------------------------------------------------
// version / format tests
// ---------------------------------------------------------------------------
#[test]
fn test_version_shows_oo_prefix() {
// The plain "oo" logo must appear in the version output
oo().arg("version")
.assert()
.success()
.stdout(predicate::str::starts_with("oo "));
}
#[test]
fn test_version_shows_version_number() {
// Must contain the version from Cargo.toml
let version = env!("CARGO_PKG_VERSION");
oo().arg("version")
.assert()
.success()
.stdout(predicate::str::contains(version));
}
// ---------------------------------------------------------------------------
// help command integration
// ---------------------------------------------------------------------------
/// Network-dependent: looks up cheat.sh for `ls`.
/// Marked ignore — run manually when network is available.
#[test]
#[ignore = "requires network access to cheat.sh"]
fn test_help_with_valid_command() {
oo().args(["help", "ls"])
.assert()
.success()
.stdout(predicate::str::is_empty().not());
}
// ---------------------------------------------------------------------------
// cargo-dist configuration verification
// ---------------------------------------------------------------------------
#[test]
fn test_cargo_toml_has_shell_installer_enabled() {
// Verify that Cargo.toml configures shell installer for cargo-dist
let cargo_toml = include_str!("../Cargo.toml");
assert!(
cargo_toml.contains(r#"installers = ["shell"]"#),
"Cargo.toml must have shell installer enabled: installers = [\"shell\"]"
);
}
#[test]
fn test_cargo_toml_has_install_path_cargo_home() {
// Verify that Cargo.toml configures install-path as CARGO_HOME
let cargo_toml = include_str!("../Cargo.toml");
assert!(
cargo_toml.contains(r#"install-path = "CARGO_HOME""#),
"Cargo.toml must have install-path = \"CARGO_HOME\""
);
}
#[test]
fn test_cargo_toml_has_cargo_dist_version() {
// Verify that Cargo.toml specifies a cargo-dist version
let cargo_toml = include_str!("../Cargo.toml");
assert!(
cargo_toml.contains("cargo-dist-version ="),
"Cargo.toml must specify cargo-dist-version for dist config"
);
}
// ---------------------------------------------------------------------------
// project patterns (.oo/patterns)
// ---------------------------------------------------------------------------
#[test]
fn test_project_patterns_listed_when_present() {
let dir = TempDir::new().unwrap();
std::fs::create_dir_all(dir.path().join(".git")).unwrap();
let pat_dir = dir.path().join(".oo").join("patterns");
std::fs::create_dir_all(&pat_dir).unwrap();
std::fs::write(
pat_dir.join("mytest.toml"),
"command_match = \"^mytest\"\n[success]\npattern = '(?P<n>\\d+) ok'\nsummary = \"{n} ok\"\n",
)
.unwrap();
oo().arg("patterns")
.current_dir(dir.path())
.env("HOME", dir.path())
.env("XDG_CONFIG_HOME", dir.path().join(".config"))
.assert()
.success()
.stdout(predicate::str::contains("Project"))
.stdout(predicate::str::contains("^mytest"));
}
#[test]
fn test_project_patterns_override_builtins() {
// Create a project pattern for "echo" with a success pattern that matches
// the output, proving project patterns are loaded and take effect.
let dir = TempDir::new().unwrap();
std::fs::create_dir_all(dir.path().join(".git")).unwrap();
let pat_dir = dir.path().join(".oo").join("patterns");
std::fs::create_dir_all(&pat_dir).unwrap();
// A pattern that matches "echo" and summarises any output as "proj-match"
std::fs::write(
pat_dir.join("echo.toml"),
"command_match = \"^echo\\\\b\"\n[success]\npattern = '(?s)(?P<all>.+)'\nsummary = \"proj-match\"\n",
)
.unwrap();
// Generate enough output to exceed SMALL_THRESHOLD so the pattern is consulted.
// SMALL_THRESHOLD is 4096 bytes; we need > 4096 bytes of output.
let big_arg = "x".repeat(5000);
oo().args(["echo", &big_arg])
.current_dir(dir.path())
.env("HOME", dir.path())
.env("XDG_CONFIG_HOME", dir.path().join(".config"))
.assert()
.success()
.stdout(predicate::str::contains("proj-match"));
}