selfware 0.6.3

Your personal AI workshop — software you own, software that lasts
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
use super::*;

#[test]
fn test_git_status_name() {
    let tool = GitStatus::new();
    assert_eq!(tool.name(), "git_status");
}

#[test]
fn test_git_status_description() {
    let tool = GitStatus::new();
    assert!(tool.description().contains("status"));
}

#[test]
fn test_git_status_schema() {
    let tool = GitStatus::new();
    let schema = tool.schema();
    assert_eq!(schema["type"], "object");
}

#[test]
fn test_git_diff_name() {
    let tool = GitDiff::new();
    assert_eq!(tool.name(), "git_diff");
}

#[test]
fn test_git_diff_description() {
    let tool = GitDiff::new();
    assert!(tool.description().contains("diff"));
}

#[test]
fn test_git_diff_schema() {
    let tool = GitDiff::new();
    let schema = tool.schema();
    assert_eq!(schema["type"], "object");
    assert!(schema["properties"]["staged"].is_object());
}

#[test]
fn test_git_commit_name() {
    let tool = GitCommit::new();
    assert_eq!(tool.name(), "git_commit");
}

#[test]
fn test_git_commit_description() {
    let tool = GitCommit::new();
    assert!(tool.description().contains("commit"));
}

#[test]
fn test_git_commit_schema() {
    let tool = GitCommit::new();
    let schema = tool.schema();
    assert_eq!(schema["type"], "object");
    assert!(schema["properties"]["message"].is_object());
    assert!(schema["properties"]["files"].is_object());
}

#[test]
fn test_git_checkpoint_name() {
    let tool = GitCheckpoint::new();
    assert_eq!(tool.name(), "git_checkpoint");
}

#[test]
fn test_git_checkpoint_description() {
    let tool = GitCheckpoint::new();
    assert!(tool.description().contains("checkpoint"));
    assert!(tool.description().contains("rollback"));
}

#[test]
fn test_git_checkpoint_schema() {
    let tool = GitCheckpoint::new();
    let schema = tool.schema();
    assert_eq!(schema["type"], "object");
    assert!(schema["properties"]["message"].is_object());
    assert!(schema["properties"]["tag"].is_object());
    assert!(schema["properties"]["auto_branch"].is_object());
}

#[test]
fn test_git_checkpoint_schema_required() {
    let tool = GitCheckpoint::new();
    let schema = tool.schema();
    let required = schema["required"].as_array().unwrap();
    assert!(required.contains(&serde_json::json!("message")));
}

#[test]
fn test_git_commit_schema_required() {
    let tool = GitCommit::new();
    let schema = tool.schema();
    let required = schema["required"].as_array().unwrap();
    assert!(required.contains(&serde_json::json!("message")));
}

#[test]
fn test_git_commit_schema_commit_types() {
    let tool = GitCommit::new();
    let schema = tool.schema();
    let commit_type = &schema["properties"]["commit_type"];
    let enum_values = commit_type["enum"].as_array().unwrap();

    assert!(enum_values.contains(&serde_json::json!("feat")));
    assert!(enum_values.contains(&serde_json::json!("fix")));
    assert!(enum_values.contains(&serde_json::json!("refactor")));
}

#[tokio::test]
async fn test_git_status_execute() {
    let _g = crate::test_support::CwdGuard::hold();
    let tool = GitStatus::new();
    let args = serde_json::json!({});

    // This will work in a git repo (like this project)
    let result = tool.execute(args).await;
    assert!(result.is_ok());

    let output = result.unwrap();
    assert!(output.get("branch").is_some() || output.get("error").is_some());
}

#[tokio::test]
async fn test_git_diff_execute_unstaged() {
    let _g = crate::test_support::CwdGuard::hold();
    let tool = GitDiff::new();
    let args = serde_json::json!({"staged": false});

    let result = tool.execute(args).await;
    assert!(result.is_ok());

    let output = result.unwrap();
    assert!(output.get("diff").is_some() || output.get("error").is_some());
}

#[tokio::test]
async fn test_git_diff_execute_staged() {
    let _g = crate::test_support::CwdGuard::hold();
    let tool = GitDiff::new();
    let args = serde_json::json!({"staged": true});

    let result = tool.execute(args).await;
    assert!(result.is_ok());
}

fn isolated_git_repo() -> (crate::test_support::CwdGuard, tempfile::TempDir) {
    let dir = tempfile::TempDir::new().unwrap();
    let guard = crate::test_support::CwdGuard::enter(dir.path());
    fn git(args: &[&str]) {
        std::process::Command::new("git")
            .args(args)
            .status()
            .unwrap();
    }
    git(&["init", "-q"]);
    git(&["config", "user.email", "t@t"]);
    git(&["config", "user.name", "t"]);
    std::fs::write(dir.path().join("f.txt"), "x").unwrap();
    git(&["add", "-A"]);
    git(&["commit", "-qm", "base"]);
    (guard, dir)
}

#[tokio::test]
async fn test_git_commit_with_message() {
    let _iso = isolated_git_repo();
    let tool = GitCommit::new();
    // This test creates a real commit - only check that it handles the case
    // when there's nothing to commit gracefully
    let args = serde_json::json!({
        "message": "Test commit",
        "files": []
    });

    // This may fail if nothing to commit, but shouldn't panic
    let result = tool.execute(args).await;
    // We accept both Ok (committed) and Err (nothing to commit)
    assert!(result.is_ok() || result.is_err());
}

#[tokio::test]
async fn empty_files_commit_excludes_untracked() {
    let (_guard, dir) = isolated_git_repo();
    // Modify a tracked file and drop a new untracked one (a stray secret).
    std::fs::write(dir.path().join("f.txt"), "modified content").unwrap();
    std::fs::write(dir.path().join("untracked_secret.txt"), "ghp_secret").unwrap();

    let tool = GitCommit::new();
    let args = serde_json::json!({ "message": "commit tracked only", "files": [] });
    tool.execute(args).await.expect("commit of tracked change");

    let status = std::process::Command::new("git")
        .args(["status", "--porcelain"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let status_str = String::from_utf8_lossy(&status.stdout);

    // The untracked file must NOT have been swept into the commit.
    assert!(
        status_str.contains("untracked_secret.txt"),
        "empty-files commit must leave untracked files untracked; status: {status_str}"
    );
    // The tracked modification WAS committed (no longer pending).
    assert!(
        !status_str.contains("f.txt"),
        "tracked modification should have been committed; status: {status_str}"
    );
}

#[tokio::test]
async fn test_git_checkpoint_execute() {
    let _iso = isolated_git_repo();
    let tool = GitCheckpoint::new();
    let args = serde_json::json!({
        "message": "Test checkpoint"
    });

    // This might fail if there's nothing to commit, but shouldn't panic
    let result = tool.execute(args).await;
    // We just verify it returns Ok or expected Err
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_git_diff_schema_properties() {
    let tool = GitDiff::new();
    let schema = tool.schema();

    assert!(schema["properties"]["staged"].is_object());
    assert!(schema["properties"]["path"].is_object());
    assert!(schema["properties"]["base"].is_object());
}

#[test]
fn test_git_checkpoint_schema_defaults() {
    let tool = GitCheckpoint::new();
    let schema = tool.schema();

    let auto_branch = &schema["properties"]["auto_branch"];
    assert_eq!(auto_branch["default"], true);
}

#[test]
fn test_git_status_schema_properties() {
    let tool = GitStatus::new();
    let schema = tool.schema();

    assert!(schema["properties"]["repo_path"].is_object());
}

#[test]
fn test_git_commit_schema_files_array() {
    let tool = GitCommit::new();
    let schema = tool.schema();

    let files = &schema["properties"]["files"];
    assert_eq!(files["type"], "array");
}

// Additional tests for error paths and edge cases

#[tokio::test]
async fn test_git_status_not_a_repo() {
    use tempfile::TempDir;
    let temp_dir = TempDir::new().unwrap();

    let tool = GitStatus::new();
    let args = serde_json::json!({
        "repo_path": temp_dir.path().to_str().unwrap()
    });

    let result = tool.execute(args).await;
    // Should fail since it's not a git repo
    assert!(result.is_err());
}

#[tokio::test]
async fn test_git_status_with_explicit_current_dir() {
    let _g = crate::test_support::CwdGuard::hold();
    let tool = GitStatus::new();
    let args = serde_json::json!({
        "repo_path": "."  // Explicit current dir
    });

    // Should work since we're in a git repo
    let result = tool.execute(args).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_git_diff_with_specific_path() {
    let _g = crate::test_support::CwdGuard::hold();
    let tool = GitDiff::new();
    let args = serde_json::json!({
        "path": ".",
        "staged": false
    });

    let result = tool.execute(args).await;
    assert!(result.is_ok());

    let output = result.unwrap();
    // Should have diff field (may be empty)
    assert!(output.get("diff").is_some());
    assert!(output.get("has_changes").is_some());
}

#[tokio::test]
async fn test_git_commit_with_specific_files() {
    let _iso = isolated_git_repo();
    let tool = GitCommit::new();
    let args = serde_json::json!({
        "message": "Test specific files",
        "files": ["nonexistent_file_12345.txt"]  // File doesn't exist
    });

    // `git add` exits non-zero for an unmatched pathspec; the tool must
    // surface that error instead of silently committing a partial stage.
    let result = tool.execute(args).await;
    let err = result.expect_err("add of a nonexistent path must fail the commit");
    assert!(
        err.to_string().contains("git add failed"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn test_git_commit_surfaces_add_u_failure() {
    // `git add -u` outside a work tree fails; the error must be surfaced
    // rather than proceeding to a bogus commit.
    let dir = tempfile::TempDir::new().unwrap();
    let _guard = crate::test_support::CwdGuard::enter(dir.path());
    let tool = GitCommit::new();
    let result = tool
        .execute(serde_json::json!({ "message": "x", "files": [] }))
        .await;
    let err = result.expect_err("add -u outside a repo must fail");
    assert!(
        err.to_string().contains("git add"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn test_git_diff_honors_base_param() {
    let (_guard, dir) = isolated_git_repo();
    // Modify the tracked file; diff against HEAD must show it.
    std::fs::write(dir.path().join("f.txt"), "changed content").unwrap();

    let tool = GitDiff::new();
    let result = tool
        .execute(serde_json::json!({ "base": "HEAD" }))
        .await
        .expect("diff against HEAD");
    assert_eq!(result["has_changes"], true);
    assert!(
        result["diff"].as_str().unwrap().contains("changed content"),
        "diff against base must include the modification: {result}"
    );
}

#[tokio::test]
async fn test_git_diff_bad_base_reports_error_not_empty_diff() {
    let _iso = isolated_git_repo();
    let tool = GitDiff::new();
    // A bogus revision used to be reported as has_changes:false with the
    // error silently dropped.
    let result = tool
        .execute(serde_json::json!({ "base": "nonexistent-revision-xyz" }))
        .await;
    let err = result.expect_err("bad base must error, not fake an empty diff");
    assert!(
        err.to_string().contains("git diff failed"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn test_git_diff_rejects_flag_like_base() {
    let _iso = isolated_git_repo();
    let tool = GitDiff::new();
    let result = tool
        .execute(serde_json::json!({ "base": "--output=/tmp/injected" }))
        .await;
    let err = result.expect_err("flag-like base must be rejected");
    assert!(
        err.to_string().contains("Invalid base"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn test_git_checkpoint_with_tag() {
    let _iso = isolated_git_repo();
    let tool = GitCheckpoint::new();
    let args = serde_json::json!({
        "message": "Test checkpoint with tag",
        "tag": "test-checkpoint-tag"
    });

    let result = tool.execute(args).await;
    // May succeed or fail depending on repo state
    assert!(result.is_ok() || result.is_err());
}

#[tokio::test]
async fn test_git_checkpoint_disable_auto_branch() {
    let _iso = isolated_git_repo();
    let tool = GitCheckpoint::new();
    let args = serde_json::json!({
        "message": "Test no auto branch",
        "auto_branch": false
    });

    let result = tool.execute(args).await;
    // Should handle gracefully
    assert!(result.is_ok() || result.is_err());
}

#[test]
fn test_git_status_schema_has_repo_path() {
    let tool = GitStatus::new();
    let schema = tool.schema();

    let repo_path = &schema["properties"]["repo_path"];
    assert_eq!(repo_path["type"], "string");
}

#[test]
fn test_git_diff_schema_has_base() {
    let tool = GitDiff::new();
    let schema = tool.schema();

    let base = &schema["properties"]["base"];
    assert_eq!(base["type"], "string");
}

#[test]
fn test_git_checkpoint_message_required() {
    let tool = GitCheckpoint::new();
    let schema = tool.schema();

    let required = schema["required"].as_array().unwrap();
    assert_eq!(required.len(), 1);
    assert!(required.contains(&serde_json::json!("message")));
}

// GitPush tests

#[test]
fn test_git_push_name() {
    let tool = GitPush::new();
    assert_eq!(tool.name(), "git_push");
}

#[test]
fn test_git_push_description() {
    let tool = GitPush::new();
    assert!(tool.description().contains("Push"));
}

#[test]
fn test_git_push_schema() {
    let tool = GitPush::new();
    let schema = tool.schema();
    assert_eq!(schema["type"], "object");
    assert!(schema["properties"]["remote"].is_object());
    assert!(schema["properties"]["branch"].is_object());
    assert!(schema["properties"]["force"].is_object());
}

#[test]
fn test_git_push_schema_defaults() {
    let tool = GitPush::new();
    let schema = tool.schema();
    assert_eq!(schema["properties"]["remote"]["default"], "origin");
    assert_eq!(schema["properties"]["force"]["default"], false);
}

#[tokio::test]
async fn test_git_push_execute() {
    let tool = GitPush::new();
    // Push to nonexistent remote will fail, but shouldn't panic
    let args = serde_json::json!({
        "remote": "nonexistent_remote_test",
        "branch": "test-branch"
    });
    let result = tool.execute(args).await;
    // Should return Ok with success: false (remote doesn't exist)
    assert!(result.is_ok());
    let output = result.unwrap();
    assert_eq!(output["success"], false);
}

#[tokio::test]
async fn test_git_push_execute_blocks_protected_branch() {
    let safety_config = crate::config::SafetyConfig {
        protected_branches: vec!["main".to_string()],
        ..Default::default()
    };
    let tool = GitPush::with_safety_config(safety_config);
    let args = serde_json::json!({
        "remote": "nonexistent_remote_test",
        "branch": "main"
    });
    let result = tool.execute(args).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("protected branch"));
}

#[tokio::test]
async fn test_git_push_execute_allows_non_protected_branch() {
    let safety_config = crate::config::SafetyConfig {
        protected_branches: vec!["main".to_string()],
        ..Default::default()
    };
    let tool = GitPush::with_safety_config(safety_config);
    let args = serde_json::json!({
        "remote": "nonexistent_remote_test",
        "branch": "some-other-branch"
    });
    let result = tool.execute(args).await;
    // Not blocked by protected_branches; fails later since the remote
    // doesn't exist, same as the un-configured case above.
    assert!(result.is_ok());
    assert_eq!(result.unwrap()["success"], false);
}

// Tests for validate_tag_name function

#[test]
fn test_validate_tag_name_valid() {
    assert!(validate_tag_name("v1.0.0").is_ok());
    assert!(validate_tag_name("release-2024").is_ok());
    assert!(validate_tag_name("feature/new-thing").is_ok());
    assert!(validate_tag_name("hotfix_123").is_ok());
    assert!(validate_tag_name("a").is_ok());
}

#[test]
fn test_validate_tag_name_empty() {
    assert!(validate_tag_name("").is_err());
}

#[test]
fn test_validate_tag_name_too_long() {
    let long_name = "a".repeat(257);
    assert!(validate_tag_name(&long_name).is_err());
}

#[test]
fn test_validate_tag_name_starts_with_dash() {
    assert!(validate_tag_name("-v1.0.0").is_err());
}

#[test]
fn test_validate_tag_name_invalid_chars() {
    assert!(validate_tag_name("v1.0 0").is_err()); // space
    assert!(validate_tag_name("v1.0@0").is_err()); // @
    assert!(validate_tag_name("v1.0#0").is_err()); // #
    assert!(validate_tag_name("v1.0$0").is_err()); // $
    assert!(validate_tag_name("v1.0!0").is_err()); // !
    assert!(validate_tag_name("v1.0*0").is_err()); // *
}

#[test]
fn test_validate_tag_name_exactly_256() {
    let name = "a".repeat(256);
    assert!(validate_tag_name(&name).is_ok());
}

// Tests for write_commit_message_file

#[test]
fn test_write_commit_message_file_creates_file() {
    let message = "Test commit message";
    let path = write_commit_message_file(message);
    assert!(path.is_some());

    let path = path.unwrap();
    let content = std::fs::read_to_string(&path).unwrap();
    assert_eq!(content, message);

    // Clean up
    let _ = std::fs::remove_file(&path);
}

#[test]
fn test_write_commit_message_file_multiline() {
    let message = "Line 1\nLine 2\nLine 3";
    let path = write_commit_message_file(message);
    assert!(path.is_some());

    let path = path.unwrap();
    let content = std::fs::read_to_string(&path).unwrap();
    assert_eq!(content, message);

    // Clean up
    let _ = std::fs::remove_file(&path);
}

#[test]
fn test_write_commit_message_file_unique_names() {
    let path1 = write_commit_message_file("msg1");
    let path2 = write_commit_message_file("msg2");

    assert!(path1.is_some());
    assert!(path2.is_some());
    assert_ne!(path1, path2);

    // Clean up
    let _ = std::fs::remove_file(path1.unwrap());
    let _ = std::fs::remove_file(path2.unwrap());
}