aipack 0.8.24

Command Agent runner to accelerate production coding with genai.
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
use super::*;
type Result<T = ()> = core::result::Result<T, Box<dyn std::error::Error>>; // For tests.

// Helper to construct `changes` string for tests, ensuring markers are on their own lines.
fn format_change_block(search: &str, replace: &str) -> String {
	format!("{LINE_MARKER_SEARCH_START}\n{search}\n{LINE_MARKER_SEP}\n{replace}\n{LINE_MARKER_REPLACE_END}")
}

fn format_multiple_change_blocks(blocks: Vec<(&str, &str)>, separator_str: &str) -> String {
	blocks
		.into_iter()
		.map(|(s, r)| format_change_block(s, r))
		.collect::<Vec<String>>()
		.join(separator_str)
}

#[test]
fn test_support_text_apply_change_simple_replace_no_markers() -> Result {
	// -- Setup & Fixtures
	let original = "Hello world";
	let changes = "Hallo Welt".to_string();

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	assert_eq!(result, "Hallo Welt");
	Ok(())
}

#[test]
fn test_support_text_apply_change_simple_replace_contains_end_marker_only_as_text() -> Result {
	// -- Setup & Fixtures
	let original = "Hello world";
	// LINE_MARKER_REPLACE_END is not at the start of a line, so simple mode.
	let changes = format!("Some text {LINE_MARKER_REPLACE_END} with an end marker style part");

	// -- Exec
	let result = apply_changes(original, changes.clone())?.0;

	// -- Check
	assert_eq!(result, changes);
	Ok(())
}

#[test]
fn test_support_text_apply_change_single_valid_block_original_markers_format() -> Result {
	// -- Setup & Fixtures
	let original = "Hello old_text world, and old_text again.";
	// The old MARKER_... constants included newlines.
	// The format_change_block helper ensures clean lines now.
	let changes = format_change_block("old_text", "new_text");

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	// replacen replaces only the first "old_text".
	assert_eq!(result, "Hello new_text world, and old_text again.");
	Ok(())
}

#[test]
fn test_support_text_apply_change_single_valid_block_explicit_lines() -> Result {
	// -- Setup & Fixtures
	let original = "Hello old_text1 world.";
	let changes = format_change_block("old_text1", "new_text1");

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	assert_eq!(result, "Hello new_text1 world.");
	Ok(())
}

#[test]
fn test_support_text_apply_change_multiline_patterns_in_block_new_format() -> Result {
	// -- Setup & Fixtures
	let original = "First line\nSecond old line\nThird line";
	let search_pattern_text = "Second old line";
	let replace_pattern_text = "Second new line\nAnd a new third line";

	let changes = format_change_block(search_pattern_text, replace_pattern_text);

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	let expected = "First line\nSecond new line\nAnd a new third line\nThird line";
	assert_eq!(result, expected);
	Ok(())
}

#[test]
fn test_support_text_apply_change_multiple_valid_blocks() -> Result {
	// -- Setup & Fixtures
	let original = "one two three two one";
	let blocks_data = vec![("one", "1"), ("two", "2"), ("three", "3")];
	let changes = format_multiple_change_blocks(blocks_data, "\n");

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	// 1. "one two three two one".replacen("one", "1", 1) -> "1 two three two one"
	// 2. "1 two three two one".replacen("two", "2", 1) -> "1 2 three two one"
	// 3. "1 2 three two one".replacen("three", "3", 1) -> "1 2 3 two one"
	assert_eq!(result, "1 2 3 two one");
	Ok(())
}

#[test]
fn test_support_text_apply_change_multiple_blocks_with_newlines_between() -> Result {
	// -- Setup & Fixtures
	let original = "one two three two one";
	let blocks_data = vec![("one", "1"), ("two", "2"), ("three", "3")];
	let changes = format_multiple_change_blocks(blocks_data, "\n\n\n"); // Whitespace lines between blocks

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	assert_eq!(result, "1 2 3 two one"); // Same logic as above
	Ok(())
}

#[test]
fn test_support_text_apply_change_multiple_blocks_with_mixed_whitespace_and_trailing_clean_markers() -> Result {
	// -- Setup & Fixtures
	let original = "apple banana cherry";
	let block1 = format_change_block("apple", "A");
	let block2 = format_change_block("banana", "B");
	let block3 = format_change_block("cherry", "C");
	// TODO: Should allow spaced after the markers on same line
	let changes = format!("{block1}\n{block2}\n{block3}");

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	// 1. "apple banana cherry".replacen("apple", "A", 1) -> "A banana cherry"
	// 2. "A banana cherry".replacen("banana", "B", 1) -> "A B cherry"
	// 3. "A B cherry".replacen("cherry", "C", 1) -> "A B C"
	assert_eq!(result, "A B C");
	Ok(())
}

#[test]
fn test_support_text_apply_change_error_marker_line_with_trailing_whitespace() -> Result {
	// -- Setup & Fixtures
	let original = "apple";
	let changes = format!(
		"{}\napple\n{}\n{}\n{}",
		LINE_MARKER_SEARCH_START,
		LINE_MARKER_SEP,
		"A",
		LINE_MARKER_REPLACE_END // This is correct
	)
	// Now make one of the marker lines incorrect by adding trailing space
	.replace(LINE_MARKER_SEP, &format!("{LINE_MARKER_SEP} ")); // "======= "

	// -- Exec
	let result = apply_changes(original, changes);

	// -- Check
	assert!(result.is_err(), "Should fail due to malformed marker line");
	if let Err(e) = result {
		// The line "======= " is not a valid separator, so it's part of search pattern.
		// Parsing ends in InSearchPattern state.
		assert!(
			e.to_string().contains(&format!("Missing separator marker '{LINE_MARKER_SEP}'")),
			"Error message mismatch: {e}"
		);
	}
	Ok(())
}

#[test]
fn test_support_text_apply_change_error_with_preamble() -> Result {
	// -- Setup & Fixtures
	let original = "Replace target.";
	let changes = format!(
		"Some preamble text, should cause error.\n{LINE_MARKER_SEARCH_START}\ntarget\n{LINE_MARKER_SEP}\nreplacement\n{LINE_MARKER_REPLACE_END}"
	);

	// -- Exec
	let result = apply_changes(original, changes);

	// -- Check
	assert!(result.is_err(), "Should fail due to preamble text");
	if let Err(e) = result {
		assert!(
			e.to_string().contains("Expected '<<<<<<< SEARCH' or a whitespace line"),
			"Error message mismatch: {e}"
		);
		assert!(
			e.to_string().contains("Line: 'Some preamble text"),
			"Error message mismatch: {e}"
		);
	}
	Ok(())
}

#[test]
fn test_support_text_apply_change_error_with_interstitial_text() -> Result {
	// -- Setup & Fixtures
	let original = "one two";
	let block1 = format_change_block("one", "1");
	let block2 = format_change_block("two", "2");
	let changes = format!("{block1}\n  Some interstitial text, should cause error.\n{block2}");

	// -- Exec
	let result = apply_changes(original, changes);

	// -- Check
	assert!(result.is_err(), "Should fail due to interstitial text");
	if let Err(e) = result {
		assert!(
			e.to_string().contains("Expected '<<<<<<< SEARCH' or a whitespace line"),
			"Error message mismatch: {e}"
		);
		assert!(
			e.to_string().contains("Line: '  Some interstitial text"),
			"Error message mismatch: {e}"
		);
	}
	Ok(())
}

#[test]
fn test_support_text_apply_change_error_with_trailing_text() -> Result {
	// -- Setup & Fixtures
	let original = "Replace target.";
	let block = format_change_block("target", "replacement");
	let changes = format!("{block}\n  Some trailing text, should cause error.");

	// -- Exec
	let result = apply_changes(original, changes);

	// -- Check
	assert!(result.is_err(), "Should fail due to trailing text");
	if let Err(e) = result {
		assert!(
			e.to_string().contains("Expected '<<<<<<< SEARCH' or a whitespace line"),
			"Error message mismatch: {e}"
		);
		assert!(
			e.to_string().contains("Line: '  Some trailing text"),
			"Error message mismatch: {e}"
		);
	}
	Ok(())
}

#[test]
fn test_support_text_apply_change_search_not_found_in_original() -> Result {
	// -- Setup & Fixtures
	let original = "Hello world";
	let changes = format_change_block("not_found_text", "replacement");

	// -- Exec
	let (result, info) = apply_changes(original, changes)?;

	// -- Check
	assert_eq!(
		result, original,
		"Original should be unchanged as search pattern not found"
	);
	assert_eq!(info.changed_count, 0);
	assert_eq!(info.failed_changes.len(), 1);
	assert_eq!(info.failed_changes[0].search, "not_found_text");
	assert_eq!(info.failed_changes[0].reason, "Search block not found in content");

	Ok(())
}

#[test]
fn test_support_text_apply_change_mixed_success_and_failure() -> Result {
	// -- Setup & Fixtures
	let original = "one two three";
	let changes = format!(
		"{}\n{}",
		format_change_block("two", "2"),  // Should succeed
		format_change_block("four", "4")  // Should fail
	);

	// -- Exec
	let (result, info) = apply_changes(original, changes)?;

	// -- Check
	assert_eq!(result, "one 2 three");
	assert_eq!(info.changed_count, 1);
	assert_eq!(info.failed_changes.len(), 1);
	assert_eq!(info.failed_changes[0].search, "four");
	assert_eq!(info.failed_changes[0].reason, "Search block not found in content");

	Ok(())
}

#[test]
fn test_support_text_apply_change_empty_search_pattern() -> Result {
	// -- Setup & Fixtures
	let original = "abc";
	let changes = format_change_block("", "X"); // Empty search pattern

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	// "abc".replacen("", "X", 1) results in "Xabc"
	assert_eq!(result, "Xabc");
	Ok(())
}

#[test]
fn test_support_text_apply_change_empty_replace_pattern() -> Result {
	// -- Setup & Fixtures
	let original = "delete this text now";
	let changes = format_change_block("this text", ""); // Empty replace pattern

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	assert_eq!(result, "delete  now");
	Ok(())
}

#[test]
fn test_support_text_apply_change_empty_replace_pattern_removes_line_and_newline() -> Result {
	// -- Setup & Fixtures
	let original = "line one\nline two\nline three\n";
	let changes = format_change_block("line two", ""); // Empty replace pattern

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	assert_eq!(result, "line one\nline three\n");
	Ok(())
}

#[test]
fn test_support_text_apply_change_malformed_missing_separator() -> Result {
	// -- Setup & Fixtures
	let original = "Hello world";
	let changes = format!(
		"{LINE_MARKER_SEARCH_START}\nsearch_text_no_sep_then_end\n{LINE_MARKER_REPLACE_END}" // Missing LINE_MARKER_SEP
	);

	// -- Exec
	let result = apply_changes(original, changes);

	// -- Check
	assert!(result.is_err(), "Should fail due to missing separator");
	if let Err(e) = result {
		assert!(
			e.to_string().contains(&format!("Missing separator marker '{LINE_MARKER_SEP}'")),
			"Error message mismatch: {e}"
		);
	}
	Ok(())
}

#[test]
fn test_support_text_apply_change_malformed_missing_end() -> Result {
	// -- Setup & Fixtures
	let original = "Hello world";
	let changes = format!(
		"{LINE_MARKER_SEARCH_START}\nsearch_text\n{LINE_MARKER_SEP}\nreplace_text_no_end" // Missing LINE_MARKER_REPLACE_END
	);

	// -- Exec
	let result = apply_changes(original, changes);

	// -- Check
	assert!(result.is_err(), "Should fail due to missing end marker");
	if let Err(e) = result {
		assert!(
			e.to_string()
				.contains(&format!("Missing end marker '{LINE_MARKER_REPLACE_END}'")),
			"Error message mismatch: {e}"
		);
	}
	Ok(())
}

#[test]
fn test_support_text_apply_change_markers_as_text_in_patterns() -> Result {
	// -- Setup & Fixtures
	let original = format!(
		"Content with {LINE_MARKER_SEARCH_START} inside, and also {LINE_MARKER_SEP} and {LINE_MARKER_REPLACE_END}."
	);
	// Search for the literal text of a marker, not as a structural marker.
	let search_pattern = LINE_MARKER_SEARCH_START; // e.g. "<<<<<<< SEARCH"
	let replace_pattern = "FOUND_IT";
	let changes = format_change_block(search_pattern, replace_pattern);

	// -- Exec
	let result = apply_changes(original.as_str(), changes)?.0;

	// -- Check
	assert_eq!(
		result,
		format!(
			"Content with FOUND_IT inside, and also {LINE_MARKER_SEP} and {LINE_MARKER_REPLACE_END}.", // Only first "<<<<<<< SEARCH" is replaced
		)
	);
	Ok(())
}

#[test]
fn test_support_text_apply_change_block_at_eof_no_trailing_newline_in_changes_input() -> Result {
	// -- Setup & Fixtures
	let original = "fix this";
	// format_change_block ensures markers are on their own lines.
	// The `changes_str` itself might not have a final newline after the last marker line.
	// This is handled fine by `lines()` iterator.
	let changes_str_no_final_newline = format_change_block("this", "that");

	// -- Exec
	let result = apply_changes(original, changes_str_no_final_newline)?.0;

	// -- Check
	assert_eq!(result, "fix that");
	Ok(())
}

#[test]
fn test_support_text_apply_change_empty_changes_string() -> Result {
	// -- Setup & Fixtures
	let original = "original content";
	let changes = "".to_string();

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	// Empty changes string -> simple replace mode -> replaces original with ""
	assert_eq!(result, "");
	Ok(())
}

#[test]
fn test_support_text_apply_change_changes_is_just_search_start_marker_line() -> Result {
	// -- Setup & Fixtures
	let original = "original";
	let changes = LINE_MARKER_SEARCH_START.to_string(); // Just one line: "<<<<<<< SEARCH"

	// -- Exec
	let result = apply_changes(original, changes);

	// -- Check
	assert!(result.is_err());
	if let Err(e) = result {
		// Ends in search pattern, missing separator
		assert!(e.to_string().contains(&format!("Missing separator marker '{LINE_MARKER_SEP}'")));
	}
	Ok(())
}

#[test]
fn test_support_text_apply_change_changes_is_incomplete_block_missing_replace_text_and_end_marker() -> Result {
	// -- Setup & Fixtures
	let original = "original";
	let changes = format!("{LINE_MARKER_SEARCH_START}\nsearch_text\n{LINE_MARKER_SEP}");
	// Lines: "<<<<<<< SEARCH", "search_text", "======="

	// -- Exec
	let result = apply_changes(original, changes);

	// -- Check
	assert!(result.is_err());
	if let Err(e) = result {
		// Ends in replace pattern (empty), missing end marker
		assert!(
			e.to_string()
				.contains(&format!("Missing end marker '{LINE_MARKER_REPLACE_END}'"))
		);
	}
	Ok(())
}

#[test]
fn test_support_text_apply_change_empty_search_pattern_in_block() -> Result {
	// -- Setup & Fixtures
	let original = "abc def";
	let changes = format_change_block("", "X-"); // search="", replace="X-"

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	assert_eq!(result, "X-abc def");
	Ok(())
}

#[test]
fn test_support_text_apply_change_empty_replace_pattern_in_block() -> Result {
	// -- Setup & Fixtures
	let original = "abc remove_this def";
	let changes = format_change_block("remove_this", ""); // search="remove_this", replace=""

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	assert_eq!(result, "abc  def");
	Ok(())
}

#[test]
fn test_support_text_apply_change_multiline_search_and_replace() -> Result {
	// -- Setup & Fixtures
	let original = "line one\nline two\nline three\nline four";
	let search = "line two\nline three";
	let replace = "new line A\nnew line B";
	let changes = format_change_block(search, replace);

	// -- Exec
	let result = apply_changes(original, changes)?.0;

	// -- Check
	assert_eq!(result, "line one\nnew line A\nnew line B\nline four");
	Ok(())
}

#[test]
fn test_support_text_apply_change_crlf_windows() -> Result {
	// -- Setup & Fixtures
	let original = "line one\nline two\nline three\nline four";
	let original = original.replace("\n", "\r\n");
	let search = "line two\nline three";
	let replace = "new line A\nnew line B";
	let changes = format_change_block(search, replace);

	// -- Exec
	let result = apply_changes(&original, changes)?.0;

	// -- Check
	assert_eq!(result, "line one\nnew line A\nnew line B\nline four");

	Ok(())
}