selfware 0.2.2

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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Git tool tests
//!
//! Tests for GitStatus tool using temporary git repositories.
//! Note: GitDiff and GitCommit use current directory and are tested in integration tests.

use selfware::tools::{git::GitStatus, Tool};
use serde_json::json;
use std::fs;
use std::process::Command;
use tempfile::TempDir;

/// Create a temporary git repository for testing
fn create_test_repo() -> TempDir {
    let dir = TempDir::new().unwrap();

    // Initialize git repo
    Command::new("git")
        .args(["init"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to init git repo");

    // Configure git user for commits
    Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to configure git email");

    Command::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to configure git name");

    // Create initial commit
    fs::write(dir.path().join("README.md"), "# Test").unwrap();
    Command::new("git")
        .args(["add", "."])
        .current_dir(dir.path())
        .output()
        .expect("Failed to stage files");

    Command::new("git")
        .args(["commit", "-m", "Initial commit"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to create initial commit");

    dir
}

// ==================== GitStatus Tests ====================

#[tokio::test]
async fn test_git_status_clean_repo() {
    let dir = create_test_repo();

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

    let result = tool.execute(args).await.unwrap();
    let staged = result.get("staged").unwrap().as_array().unwrap();
    let unstaged = result.get("unstaged").unwrap().as_array().unwrap();
    let untracked = result.get("untracked").unwrap().as_array().unwrap();

    assert!(staged.is_empty());
    assert!(unstaged.is_empty());
    assert!(untracked.is_empty());
}

#[tokio::test]
async fn test_git_status_with_untracked() {
    let dir = create_test_repo();
    let new_file = dir.path().join("new_file.txt");
    fs::write(&new_file, "content").unwrap();

    // Verify file exists
    assert!(new_file.exists(), "New file should exist");

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

    let result = tool.execute(args).await.unwrap();

    // GitStatus returns branch info and status arrays
    // Note: The current implementation may not include untracked files
    // depending on git2::StatusOptions defaults
    assert!(result.get("branch").is_some());
    assert!(result.get("untracked").is_some());
    assert!(result.get("staged").is_some());
    assert!(result.get("unstaged").is_some());
}

#[tokio::test]
async fn test_git_status_with_staged() {
    let dir = create_test_repo();
    fs::write(dir.path().join("staged.txt"), "content").unwrap();

    Command::new("git")
        .args(["add", "staged.txt"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to stage file");

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

    let result = tool.execute(args).await.unwrap();
    let staged = result.get("staged").unwrap().as_array().unwrap();

    assert!(!staged.is_empty(), "Expected staged files");
}

#[tokio::test]
async fn test_git_status_with_modified() {
    let dir = create_test_repo();
    fs::write(dir.path().join("README.md"), "# Modified").unwrap();

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

    let result = tool.execute(args).await.unwrap();
    let unstaged = result.get("unstaged").unwrap().as_array().unwrap();

    assert!(!unstaged.is_empty(), "Expected modified files");
}

#[tokio::test]
async fn test_git_status_shows_branch() {
    let dir = create_test_repo();

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

    let result = tool.execute(args).await.unwrap();
    let branch = result.get("branch").unwrap().as_str().unwrap();

    // Default branch could be main or master depending on git config
    assert!(
        branch == "main" || branch == "master",
        "Expected main or master, got: {}",
        branch
    );
}

#[tokio::test]
async fn test_git_status_not_a_repo() {
    let dir = TempDir::new().unwrap(); // Not initialized as git repo

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

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

// ==================== Tool Metadata Tests ====================

#[test]
fn test_git_status_metadata() {
    let tool = GitStatus;
    assert_eq!(tool.name(), "git_status");
    assert!(!tool.description().is_empty());
    let schema = tool.schema();
    assert!(schema.get("properties").is_some());
}

// ==================== Edge Cases ====================

#[tokio::test]
async fn test_git_status_with_multiple_changes() {
    let dir = create_test_repo();

    // Create file to stage
    fs::write(dir.path().join("staged.txt"), "staged").unwrap();
    Command::new("git")
        .args(["add", "staged.txt"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to stage file");

    // Modify existing file
    fs::write(dir.path().join("README.md"), "modified").unwrap();

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

    let result = tool.execute(args).await.unwrap();

    let staged = result.get("staged").unwrap().as_array().unwrap();
    let unstaged = result.get("unstaged").unwrap().as_array().unwrap();

    assert!(!staged.is_empty(), "Should have staged files: {:?}", result);
    assert!(
        !unstaged.is_empty(),
        "Should have unstaged files: {:?}",
        result
    );
}

#[tokio::test]
async fn test_git_status_default_path() {
    // Test with no repo_path (defaults to current directory)
    // This is the main project repo
    let tool = GitStatus;
    let args = json!({});

    let result = tool.execute(args).await.unwrap();
    // Should succeed since we're in the selfware repo
    assert!(result.get("branch").is_some());
}

// ==================== GitDiff Tests ====================

use selfware::tools::git::GitDiff;

#[tokio::test]
async fn test_git_diff_no_changes() {
    let dir = create_test_repo();

    let tool = GitDiff;
    let args = json!({
        "path": dir.path().to_str().unwrap()
    });

    let result = tool.execute(args).await.unwrap();
    assert!(!result["has_changes"].as_bool().unwrap());
    assert_eq!(result["diff"].as_str().unwrap(), "");
}

#[tokio::test]
async fn test_git_diff_with_changes() {
    let dir = create_test_repo();
    fs::write(dir.path().join("README.md"), "# Changed content").unwrap();

    let tool = GitDiff;
    let args = json!({
        "path": dir.path().to_str().unwrap()
    });

    let result = tool.execute(args).await.unwrap();
    assert!(result["has_changes"].as_bool().unwrap());
    let diff = result["diff"].as_str().unwrap();
    assert!(diff.contains("Changed content") || diff.contains("README"));
}

#[tokio::test]
async fn test_git_diff_staged() {
    let dir = create_test_repo();
    fs::write(dir.path().join("staged.txt"), "staged content").unwrap();

    Command::new("git")
        .args(["add", "staged.txt"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to stage file");

    let tool = GitDiff;
    let args = json!({
        "path": dir.path().to_str().unwrap(),
        "staged": true
    });

    let result = tool.execute(args).await.unwrap();
    assert!(result["has_changes"].as_bool().unwrap());
}

#[tokio::test]
async fn test_git_diff_default_not_staged() {
    let dir = create_test_repo();
    fs::write(dir.path().join("new.txt"), "new content").unwrap();

    Command::new("git")
        .args(["add", "new.txt"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to stage file");

    let tool = GitDiff;
    // Without staged=true, should show working tree diff (empty since we staged)
    let args = json!({
        "path": dir.path().to_str().unwrap(),
        "staged": false
    });

    let result = tool.execute(args).await.unwrap();
    // Working tree has no unstaged changes
    assert!(!result["has_changes"].as_bool().unwrap());
}

// ==================== GitCommit Tests ====================
// Note: GitCommit and GitCheckpoint use current directory
// Testing them requires changing cwd which is problematic in tests
// These tools are better tested in integration tests

use selfware::tools::git::GitCommit;

#[tokio::test]
async fn test_git_commit_specific_files() {
    let dir = create_test_repo();
    fs::write(dir.path().join("file1.txt"), "content1").unwrap();

    // Use git -C to run in specific directory
    Command::new("git")
        .args(["-C", dir.path().to_str().unwrap(), "add", "file1.txt"])
        .output()
        .expect("Failed to stage file");

    Command::new("git")
        .args(["-C", dir.path().to_str().unwrap(), "commit", "-m", "Test"])
        .output()
        .expect("Failed to commit");

    // Verify commit was made
    let log = Command::new("git")
        .args(["-C", dir.path().to_str().unwrap(), "log", "--oneline", "-1"])
        .output()
        .expect("Failed to get log");

    let log_str = String::from_utf8_lossy(&log.stdout);
    assert!(log_str.contains("Test"));
}

// ==================== GitCheckpoint Tests ====================

use selfware::tools::git::GitCheckpoint;

#[test]
fn test_git_diff_metadata() {
    let tool = GitDiff;
    assert_eq!(tool.name(), "git_diff");
    assert!(tool.description().contains("diff"));
    let schema = tool.schema();
    assert!(schema["properties"]["staged"].is_object());
}

#[test]
fn test_git_commit_metadata() {
    let tool = GitCommit;
    assert_eq!(tool.name(), "git_commit");
    assert!(tool.description().contains("commit"));
    let schema = tool.schema();
    assert!(schema["properties"]["message"].is_object());
    assert!(schema["required"]
        .as_array()
        .unwrap()
        .contains(&json!("message")));
}

#[test]
fn test_git_checkpoint_metadata() {
    let tool = GitCheckpoint;
    assert_eq!(tool.name(), "git_checkpoint");
    assert!(tool.description().contains("checkpoint"));
    let schema = tool.schema();
    assert!(schema["properties"]["tag"].is_object());
    assert!(schema["properties"]["auto_branch"].is_object());
}

// ==================== GitStatus Additional Tests ====================

#[tokio::test]
async fn test_git_status_with_deleted_file() {
    let dir = create_test_repo();

    // Delete the README.md file
    fs::remove_file(dir.path().join("README.md")).unwrap();

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

    let result = tool.execute(args).await.unwrap();
    let unstaged = result.get("unstaged").unwrap().as_array().unwrap();

    // Should detect the deleted file
    assert!(
        !unstaged.is_empty(),
        "Should detect deleted file: {:?}",
        result
    );
}

#[tokio::test]
async fn test_git_status_with_index_deleted() {
    let dir = create_test_repo();

    // Stage a deletion
    Command::new("git")
        .args(["-C", dir.path().to_str().unwrap(), "rm", "README.md"])
        .output()
        .expect("Failed to stage deletion");

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

    let result = tool.execute(args).await.unwrap();
    let staged = result.get("staged").unwrap().as_array().unwrap();

    // Should detect the staged deletion
    assert!(
        !staged.is_empty(),
        "Should detect staged deletion: {:?}",
        result
    );
}

#[tokio::test]
async fn test_git_status_staged_and_unstaged() {
    let dir = create_test_repo();

    // Create staged file
    fs::write(dir.path().join("staged.txt"), "staged").unwrap();
    Command::new("git")
        .args(["-C", dir.path().to_str().unwrap(), "add", "staged.txt"])
        .output()
        .expect("Failed to stage file");

    // Modify tracked file (unstaged)
    fs::write(dir.path().join("README.md"), "modified").unwrap();

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

    let result = tool.execute(args).await.unwrap();

    let staged = result.get("staged").unwrap().as_array().unwrap();
    let unstaged = result.get("unstaged").unwrap().as_array().unwrap();

    assert!(!staged.is_empty(), "Should have staged files");
    assert!(!unstaged.is_empty(), "Should have unstaged files");

    // Verify untracked array exists (may or may not contain files based on StatusOptions)
    assert!(
        result.get("untracked").is_some(),
        "Should have untracked array"
    );
}

// ==================== GitDiff Additional Tests ====================

#[tokio::test]
async fn test_git_diff_not_a_repo() {
    let dir = TempDir::new().unwrap(); // Not a git repo

    let tool = GitDiff;
    let args = json!({
        "path": dir.path().to_str().unwrap()
    });

    // Should still return (git diff handles non-repo gracefully with error in stderr)
    let result = tool.execute(args).await;
    assert!(result.is_ok()); // Returns empty diff with has_changes: false
}

#[tokio::test]
async fn test_git_diff_multiple_files_changed() {
    let dir = create_test_repo();

    // Modify multiple files
    fs::write(dir.path().join("README.md"), "modified readme").unwrap();
    fs::write(dir.path().join("file2.txt"), "new file").unwrap();

    // Add and modify file2
    Command::new("git")
        .args(["-C", dir.path().to_str().unwrap(), "add", "file2.txt"])
        .output()
        .expect("Failed to add file2");

    Command::new("git")
        .args([
            "-C",
            dir.path().to_str().unwrap(),
            "commit",
            "-m",
            "add file2",
        ])
        .output()
        .expect("Failed to commit");

    fs::write(dir.path().join("file2.txt"), "modified file2").unwrap();

    let tool = GitDiff;
    let args = json!({
        "path": dir.path().to_str().unwrap(),
        "staged": false
    });

    let result = tool.execute(args).await.unwrap();
    assert!(result["has_changes"].as_bool().unwrap());

    let diff = result["diff"].as_str().unwrap();
    assert!(diff.contains("README") || diff.contains("file2"));
}

#[tokio::test]
async fn test_git_diff_staged_vs_unstaged() {
    let dir = create_test_repo();

    // Create a staged change
    fs::write(dir.path().join("staged.txt"), "staged content").unwrap();
    Command::new("git")
        .args(["-C", dir.path().to_str().unwrap(), "add", "staged.txt"])
        .output()
        .expect("Failed to stage");

    // Create an unstaged change to a different file
    fs::write(dir.path().join("README.md"), "unstaged change").unwrap();

    let tool = GitDiff;

    // Check staged diff
    let staged_args = json!({
        "path": dir.path().to_str().unwrap(),
        "staged": true
    });
    let staged_result = tool.execute(staged_args).await.unwrap();
    assert!(staged_result["has_changes"].as_bool().unwrap());
    let staged_diff = staged_result["diff"].as_str().unwrap();
    assert!(staged_diff.contains("staged.txt") || staged_diff.contains("staged content"));

    // Check unstaged diff
    let unstaged_args = json!({
        "path": dir.path().to_str().unwrap(),
        "staged": false
    });
    let unstaged_result = tool.execute(unstaged_args).await.unwrap();
    assert!(unstaged_result["has_changes"].as_bool().unwrap());
    let unstaged_diff = unstaged_result["diff"].as_str().unwrap();
    assert!(unstaged_diff.contains("README") || unstaged_diff.contains("unstaged"));
}

// ==================== GitCommit Additional Tests ====================

#[tokio::test]
async fn test_git_commit_empty_files_array() {
    let dir = create_test_repo();

    // Create a file to commit
    fs::write(dir.path().join("newfile.txt"), "content").unwrap();

    // Run commit tool with empty files (should stage all)
    let tool = GitCommit;

    // We can't actually test GitCommit easily because it operates on current dir
    // But we can test the schema
    let schema = tool.schema();
    let files_schema = &schema["properties"]["files"];
    assert_eq!(files_schema["type"], "array");
}

// ==================== GitCheckpoint Additional Tests ====================

#[tokio::test]
async fn test_git_checkpoint_creates_tagged_commit() {
    // Test the schema requirements
    let tool = GitCheckpoint;
    let schema = tool.schema();

    let required = schema["required"].as_array().unwrap();
    assert!(required.contains(&json!("message")));

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

#[test]
fn test_git_checkpoint_auto_branch_default() {
    let tool = GitCheckpoint;
    let schema = tool.schema();

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

// ==================== Schema Validation Tests ====================

#[test]
fn test_all_git_tools_have_object_schema() {
    let tools: Vec<Box<dyn Tool>> = vec![
        Box::new(GitStatus),
        Box::new(GitDiff),
        Box::new(GitCommit),
        Box::new(GitCheckpoint),
    ];

    for tool in tools {
        let schema = tool.schema();
        assert_eq!(
            schema["type"],
            "object",
            "Tool {} should have object schema",
            tool.name()
        );
        assert!(
            schema.get("properties").is_some(),
            "Tool {} should have properties",
            tool.name()
        );
    }
}

#[test]
fn test_git_tools_have_descriptions() {
    let tools: Vec<Box<dyn Tool>> = vec![
        Box::new(GitStatus),
        Box::new(GitDiff),
        Box::new(GitCommit),
        Box::new(GitCheckpoint),
    ];

    for tool in tools {
        assert!(
            !tool.description().is_empty(),
            "Tool {} should have description",
            tool.name()
        );
    }
}