-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_git_tools_e2e.cpp
More file actions
858 lines (783 loc) · 33.7 KB
/
Copy pathtest_git_tools_e2e.cpp
File metadata and controls
858 lines (783 loc) · 33.7 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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
// test_git_tools_e2e.cpp — end-to-end validation of Notepatra v0.1.58's
// agentic git tools. Spins up a temporary git repo, makes 3 known commits,
// then drives every git tool via AiTools::execute() and asserts on the
// JSON wire shape.
//
// Tools exercised:
// git_status — clean & dirty
// git_diff — working-tree, staged, path-scoped
// git_log — default count, max_count clamping, path scoping
// git_branch_list — main + extra branch
// git_show — by full SHA, by HEAD, by HEAD~1
//
// Error paths exercised:
// no_workspace — empty workspace string
// not_a_repo — workspace is a non-git directory
// io_error — git_show with empty / flag-prefixed / metachar SHA
// git_error — git_show with nonexistent SHA
// outside_workspace — path argument with ".."
//
// Plus a real-world integration sweep against the actual Notepatra repo
// (path passed via the NOTEPATRA_REPO_ROOT env var; skipped if unset) —
// read-only by design.
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QProcess>
#include <QString>
#include <QTemporaryDir>
#include <QTextStream>
#include <cstdio>
#include "ai_tools.h"
#include "git_tools.h"
static int g_pass = 0;
static int g_fail = 0;
struct FailDetail { QString label; };
static QList<FailDetail> g_failDetails;
static void check(const char *label, bool ok) {
if (ok) { ++g_pass; fprintf(stdout, " ok %s\n", label); }
else {
++g_fail;
fprintf(stderr, " FAIL %s\n", label);
g_failDetails.append({QString::fromUtf8(label)});
}
}
static void writeFile(const QString &path, const QByteArray &content) {
QFile f(path);
f.open(QFile::WriteOnly);
f.write(content);
f.close();
}
// Run `git <args>` in workspace, return exit code; abort if not 0 (test
// setup failure, distinct from a tool returning git_error).
static int runGitOrDie(const QString &workspace, const QStringList &args) {
QProcess p;
p.setWorkingDirectory(workspace);
p.start("git", args);
if (!p.waitForStarted(2000)) {
fprintf(stderr, "FATAL: git did not start\n");
return -1;
}
if (!p.waitForFinished(5000)) {
fprintf(stderr, "FATAL: git timed out\n");
p.kill();
return -1;
}
int rc = p.exitCode();
if (rc != 0) {
fprintf(stderr, "git %s failed (exit %d): %s\n",
qPrintable(args.join(' ')), rc,
qPrintable(QString::fromUtf8(p.readAllStandardError())));
}
return rc;
}
// Run `git rev-parse <ref>` and return the resulting SHA (or empty on fail).
static QString gitRevParse(const QString &workspace, const QString &ref) {
QProcess p;
p.setWorkingDirectory(workspace);
p.start("git", {"rev-parse", ref});
p.waitForFinished(5000);
if (p.exitCode() != 0) return QString();
return QString::fromUtf8(p.readAllStandardOutput()).trimmed();
}
// Helper: invoke AiTools::execute, return JSON-parsed result body for
// easy field assertions. Caller still owns the raw ToolResult.
static QJsonObject parseToolBody(const AiTools::ToolResult &r) {
return QJsonDocument::fromJson(r.content.toUtf8()).object();
}
// ── Test suites ──────────────────────────────────────────────────────────
static void testNoWorkspaceErrors() {
fprintf(stdout, "[suite] no_workspace error path\n");
const QStringList tools = {
"git_status", "git_diff", "git_log", "git_branch_list", "git_show"
};
for (const QString &name : tools) {
AiTools::ToolCall call;
call.id = "nw_" + name;
call.name = name;
if (name == "git_show") {
call.args = QJsonObject{{"commit", "HEAD"}};
} else {
call.args = QJsonObject{};
}
AiTools::ToolResult r = AiTools::execute(call, QString());
check(qPrintable(name + ": empty ws → isError"), r.isError);
check(qPrintable(name + ": empty ws → kind=no_workspace"),
r.errorKind == "no_workspace");
QJsonObject body = parseToolBody(r);
check(qPrintable(name + ": empty ws → ok=false in JSON"),
body.value("ok").toBool() == false);
check(qPrintable(name + ": empty ws → error_kind in JSON body"),
body.value("error_kind").toString() == "no_workspace");
}
}
static void testNotARepo(const QString &nonRepoDir) {
fprintf(stdout, "[suite] not_a_repo error path\n");
AiTools::ToolCall call;
call.id = "nr1";
call.name = "git_status";
call.args = QJsonObject{};
AiTools::ToolResult r = AiTools::execute(call, nonRepoDir);
check("git_status on non-repo: isError", r.isError);
check("git_status on non-repo: kind=not_a_repo",
r.errorKind == "not_a_repo");
AiTools::ToolCall call2;
call2.id = "nr2";
call2.name = "git_log";
call2.args = QJsonObject{};
AiTools::ToolResult r2 = AiTools::execute(call2, nonRepoDir);
check("git_log on non-repo: isError", r2.isError);
check("git_log on non-repo: kind=not_a_repo",
r2.errorKind == "not_a_repo");
}
// ── 1) Build the test repo ───────────────────────────────────────────────
//
// Three commits, on branch `main`:
// commit 1: README.md ("# Test repo\n")
// commit 2: src/foo.cpp + src/bar.cpp
// commit 3: modify README.md
// Plus a second branch `feature/x` with one extra commit.
struct RepoFixture {
QString ws;
QString sha1; // README.md initial
QString sha2; // src/{foo,bar}.cpp added
QString sha3; // README modified
QString featureSha; // commit on feature/x
};
static bool buildFixture(QTemporaryDir &tmp, RepoFixture &out) {
out.ws = tmp.path();
if (runGitOrDie(out.ws, {"init", "-q", "-b", "main"}) != 0) return false;
runGitOrDie(out.ws, {"config", "user.email", "test@notepatra.local"});
runGitOrDie(out.ws, {"config", "user.name", "Notepatra Test"});
runGitOrDie(out.ws, {"config", "commit.gpgsign", "false"});
// Commit 1: README.md
writeFile(out.ws + "/README.md", "# Test repo\n\nFor git tools.\n");
runGitOrDie(out.ws, {"add", "README.md"});
runGitOrDie(out.ws, {"commit", "-q", "-m", "initial: add README"});
out.sha1 = gitRevParse(out.ws, "HEAD");
// Commit 2: add src/{foo,bar}.cpp
QDir(out.ws).mkpath("src");
writeFile(out.ws + "/src/foo.cpp", "int foo() { return 1; }\n");
writeFile(out.ws + "/src/bar.cpp", "int bar() { return 2; }\n");
runGitOrDie(out.ws, {"add", "src/foo.cpp", "src/bar.cpp"});
runGitOrDie(out.ws, {"commit", "-q", "-m", "feat: add src files"});
out.sha2 = gitRevParse(out.ws, "HEAD");
// Commit 3: modify README.md
writeFile(out.ws + "/README.md",
"# Test repo\n\nFor git tools.\n\nUpdated.\n");
runGitOrDie(out.ws, {"add", "README.md"});
runGitOrDie(out.ws, {"commit", "-q", "-m", "docs: update README"});
out.sha3 = gitRevParse(out.ws, "HEAD");
// Feature branch with one commit
runGitOrDie(out.ws, {"checkout", "-q", "-b", "feature/x"});
writeFile(out.ws + "/feature.txt", "feature work\n");
runGitOrDie(out.ws, {"add", "feature.txt"});
runGitOrDie(out.ws, {"commit", "-q", "-m", "feat: feature branch work"});
out.featureSha = gitRevParse(out.ws, "HEAD");
// Back to main for the rest of the tests
runGitOrDie(out.ws, {"checkout", "-q", "main"});
return !out.sha1.isEmpty() && !out.sha2.isEmpty() &&
!out.sha3.isEmpty() && !out.featureSha.isEmpty();
}
// ── 2) git_status ────────────────────────────────────────────────────────
static void testGitStatus(const RepoFixture &fx) {
fprintf(stdout, "[suite] git_status\n");
// Clean repo
{
AiTools::ToolCall call;
call.id = "gs_clean";
call.name = "git_status";
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_status clean: not error", !r.isError);
QJsonObject body = parseToolBody(r);
check("git_status clean: ok=true", body.value("ok").toBool());
QJsonObject result = body.value("result").toObject();
check("git_status clean: branch == main",
result.value("branch").toString() == "main");
check("git_status clean: clean=true",
result.value("clean").toBool());
check("git_status clean: staged is empty array",
result.value("staged").toArray().isEmpty());
check("git_status clean: modified is empty array",
result.value("modified").toArray().isEmpty());
check("git_status clean: untracked is empty array",
result.value("untracked").toArray().isEmpty());
check("git_status clean: ahead == 0",
result.value("ahead").toInt() == 0);
check("git_status clean: behind == 0",
result.value("behind").toInt() == 0);
}
// Dirty: modify foo.cpp + create new file
writeFile(fx.ws + "/src/foo.cpp", "int foo() { return 42; } // modified\n");
writeFile(fx.ws + "/scratch.txt", "untracked\n");
{
AiTools::ToolCall call;
call.id = "gs_dirty";
call.name = "git_status";
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_status dirty: not clean",
result.value("clean").toBool() == false);
QJsonArray modified = result.value("modified").toArray();
bool sawFoo = false;
for (const auto &v : modified) if (v.toString() == "src/foo.cpp") sawFoo = true;
check("git_status dirty: src/foo.cpp in modified[]", sawFoo);
QJsonArray untracked = result.value("untracked").toArray();
bool sawScratch = false;
for (const auto &v : untracked) if (v.toString() == "scratch.txt") sawScratch = true;
check("git_status dirty: scratch.txt in untracked[]", sawScratch);
}
// Stage scratch.txt → moves to staged[]
runGitOrDie(fx.ws, {"add", "scratch.txt"});
{
AiTools::ToolCall call;
call.id = "gs_staged";
call.name = "git_status";
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QJsonArray staged = result.value("staged").toArray();
bool sawScratch = false;
for (const auto &v : staged) if (v.toString() == "scratch.txt") sawScratch = true;
check("git_status staged: scratch.txt in staged[]", sawScratch);
}
// Reset for downstream tests
runGitOrDie(fx.ws, {"checkout", "-q", "--", "src/foo.cpp"});
runGitOrDie(fx.ws, {"reset", "-q", "HEAD", "scratch.txt"});
QFile::remove(fx.ws + "/scratch.txt");
}
// ── 3) git_diff ──────────────────────────────────────────────────────────
static void testGitDiff(const RepoFixture &fx) {
fprintf(stdout, "[suite] git_diff\n");
// Working-tree diff with no changes → empty
{
AiTools::ToolCall call;
call.id = "gd_clean";
call.name = "git_diff";
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_diff clean: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_diff clean: text is empty",
result.value("text").toString().isEmpty());
check("git_diff clean: truncated=false",
result.value("truncated").toBool() == false);
check("git_diff clean: staged=false",
result.value("staged").toBool() == false);
}
// Modify foo.cpp, expect non-empty diff with file path
writeFile(fx.ws + "/src/foo.cpp",
"int foo() { return 42; } // changed\n");
{
AiTools::ToolCall call;
call.id = "gd_dirty";
call.name = "git_diff";
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QString text = result.value("text").toString();
check("git_diff: contains src/foo.cpp",
text.contains("src/foo.cpp"));
check("git_diff: contains diff marker '+'",
text.contains("+int foo() { return 42; } // changed"));
check("git_diff: text non-empty",
!text.isEmpty());
}
// Path-scoped diff with bar.cpp (which is unchanged) → empty even
// though foo.cpp is dirty
{
AiTools::ToolCall call;
call.id = "gd_path";
call.name = "git_diff";
call.args = QJsonObject{{"path", "src/bar.cpp"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_diff path=bar.cpp: text empty (bar unchanged)",
result.value("text").toString().isEmpty());
check("git_diff path=bar.cpp: path echoed in result",
result.value("path").toString() == "src/bar.cpp");
}
// Path traversal attempt
{
AiTools::ToolCall call;
call.id = "gd_trav";
call.name = "git_diff";
call.args = QJsonObject{{"path", "../../../etc/passwd"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_diff path=../../etc/passwd: isError", r.isError);
check("git_diff path=../../etc/passwd: kind=outside_workspace",
r.errorKind == "outside_workspace");
}
// Stage foo.cpp, then `staged: true` should show it
runGitOrDie(fx.ws, {"add", "src/foo.cpp"});
{
AiTools::ToolCall call;
call.id = "gd_staged";
call.name = "git_diff";
call.args = QJsonObject{{"staged", true}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_diff staged: staged=true echoed",
result.value("staged").toBool());
check("git_diff staged: text references foo.cpp",
result.value("text").toString().contains("foo.cpp"));
}
// Reset for downstream tests
runGitOrDie(fx.ws, {"reset", "-q", "--hard", "HEAD"});
}
// ── 4) git_log ───────────────────────────────────────────────────────────
static void testGitLog(const RepoFixture &fx) {
fprintf(stdout, "[suite] git_log\n");
// Default count → at least 3 commits
{
AiTools::ToolCall call;
call.id = "gl_default";
call.name = "git_log";
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_log default: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QJsonArray commits = result.value("commits").toArray();
check("git_log default: count >= 3", commits.size() >= 3);
check("git_log default: max_count == 20",
result.value("max_count").toInt() == 20);
// First commit (newest) should be sha3 / docs: update README
if (!commits.isEmpty()) {
QJsonObject head = commits[0].toObject();
check("git_log default: HEAD hash == sha3",
head.value("hash").toString() == fx.sha3);
check("git_log default: HEAD subject == 'docs: update README'",
head.value("subject").toString() == "docs: update README");
check("git_log default: HEAD has author",
!head.value("author").toString().isEmpty());
check("git_log default: HEAD has date (ISO)",
head.value("date").toString().contains("T"));
}
// Find sha1 in the commit list
bool sawSha1 = false;
for (const auto &v : commits) {
if (v.toObject().value("hash").toString() == fx.sha1) {
sawSha1 = true;
break;
}
}
check("git_log default: contains sha1", sawSha1);
}
// max_count=2 → only 2 commits, both newest
{
AiTools::ToolCall call;
call.id = "gl_n2";
call.name = "git_log";
call.args = QJsonObject{{"max_count", 2}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QJsonArray commits = result.value("commits").toArray();
check("git_log n=2: count == 2", commits.size() == 2);
check("git_log n=2: max_count == 2",
result.value("max_count").toInt() == 2);
}
// max_count clamping: > 100 should clamp to 100 (we don't have 100
// commits, so just verify max_count echoed correctly)
{
AiTools::ToolCall call;
call.id = "gl_huge";
call.name = "git_log";
call.args = QJsonObject{{"max_count", 5000}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_log n=5000: clamped to 100",
result.value("max_count").toInt() == 100);
}
// Path-scoped: only commits touching README.md → 2 (sha1 + sha3)
{
AiTools::ToolCall call;
call.id = "gl_path";
call.name = "git_log";
call.args = QJsonObject{{"path", "README.md"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QJsonArray commits = result.value("commits").toArray();
check("git_log path=README.md: count == 2",
commits.size() == 2);
check("git_log path=README.md: path echoed",
result.value("path").toString() == "README.md");
}
// Path-scoped: src/foo.cpp → only 1 commit (sha2)
{
AiTools::ToolCall call;
call.id = "gl_path_foo";
call.name = "git_log";
call.args = QJsonObject{{"path", "src/foo.cpp"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QJsonArray commits = result.value("commits").toArray();
check("git_log path=src/foo.cpp: count == 1",
commits.size() == 1);
if (!commits.isEmpty()) {
check("git_log path=src/foo.cpp: hash == sha2",
commits[0].toObject().value("hash").toString() == fx.sha2);
}
}
// Path traversal in log
{
AiTools::ToolCall call;
call.id = "gl_trav";
call.name = "git_log";
call.args = QJsonObject{{"path", "../escape"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_log path='../escape': isError", r.isError);
check("git_log path='../escape': kind=outside_workspace",
r.errorKind == "outside_workspace");
}
}
// ── 5) git_branch_list ──────────────────────────────────────────────────
static void testGitBranchList(const RepoFixture &fx) {
fprintf(stdout, "[suite] git_branch_list\n");
AiTools::ToolCall call;
call.id = "gb1";
call.name = "git_branch_list";
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_branch_list: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_branch_list: current == main",
result.value("current").toString() == "main");
check("git_branch_list: count == 2",
result.value("count").toInt() == 2);
QJsonArray branches = result.value("branches").toArray();
bool sawMain = false, sawFeature = false;
for (const auto &v : branches) {
QJsonObject b = v.toObject();
if (b.value("name").toString() == "main") {
sawMain = true;
check("git_branch_list: main is current",
b.value("current").toBool() == true);
} else if (b.value("name").toString() == "feature/x") {
sawFeature = true;
check("git_branch_list: feature/x not current",
b.value("current").toBool() == false);
}
}
check("git_branch_list: main present", sawMain);
check("git_branch_list: feature/x present", sawFeature);
}
// ── 6) git_show ──────────────────────────────────────────────────────────
static void testGitShow(const RepoFixture &fx) {
fprintf(stdout, "[suite] git_show\n");
// By full SHA
{
AiTools::ToolCall call;
call.id = "gsh_sha";
call.name = "git_show";
call.args = QJsonObject{{"commit", fx.sha2}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show full-sha: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QString text = result.value("text").toString();
check("git_show full-sha: text contains sha prefix",
text.contains(fx.sha2.left(7)));
check("git_show full-sha: text contains commit message",
text.contains("feat: add src files"));
check("git_show full-sha: text references foo.cpp",
text.contains("src/foo.cpp"));
check("git_show full-sha: text references bar.cpp",
text.contains("src/bar.cpp"));
check("git_show full-sha: commit echoed",
result.value("commit").toString() == fx.sha2);
check("git_show full-sha: truncated=false (small)",
result.value("truncated").toBool() == false);
}
// By short SHA (7 chars)
{
AiTools::ToolCall call;
call.id = "gsh_short";
call.name = "git_show";
call.args = QJsonObject{{"commit", fx.sha1.left(7)}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show short-sha: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_show short-sha: text references README.md",
result.value("text").toString().contains("README.md"));
}
// By relative ref HEAD
{
AiTools::ToolCall call;
call.id = "gsh_head";
call.name = "git_show";
call.args = QJsonObject{{"commit", "HEAD"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show HEAD: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_show HEAD: text contains 'docs: update README'",
result.value("text").toString().contains("docs: update README"));
}
// By HEAD~1
{
AiTools::ToolCall call;
call.id = "gsh_h1";
call.name = "git_show";
call.args = QJsonObject{{"commit", "HEAD~1"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show HEAD~1: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_show HEAD~1: text contains 'feat: add src files'",
result.value("text").toString().contains("feat: add src files"));
}
// By branch name
{
AiTools::ToolCall call;
call.id = "gsh_branch";
call.name = "git_show";
call.args = QJsonObject{{"commit", "feature/x"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show feature/x: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("git_show feature/x: contains feature.txt",
result.value("text").toString().contains("feature.txt"));
}
// ── Error paths ──
// Empty commit
{
AiTools::ToolCall call;
call.id = "gsh_empty";
call.name = "git_show";
call.args = QJsonObject{{"commit", ""}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show empty: isError", r.isError);
check("git_show empty: kind=io_error",
r.errorKind == "io_error");
}
// Missing commit field
{
AiTools::ToolCall call;
call.id = "gsh_missing";
call.name = "git_show";
call.args = QJsonObject{};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show missing arg: isError", r.isError);
}
// Flag-prefixed (e.g. --upload-pack injection attempt)
{
AiTools::ToolCall call;
call.id = "gsh_flag";
call.name = "git_show";
call.args = QJsonObject{{"commit", "--upload-pack=evil"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show '--flag': isError", r.isError);
check("git_show '--flag': kind=io_error",
r.errorKind == "io_error");
}
// Shell metacharacter injection attempts
const QList<QString> meta = {
"abc;rm -rf /", "abc&evil", "abc|cat", "abc`whoami`", "abc$X", "abc def"
};
for (const QString &m : meta) {
AiTools::ToolCall call;
call.id = "gsh_meta";
call.name = "git_show";
call.args = QJsonObject{{"commit", m}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check(qPrintable(QString("git_show meta '%1': isError").arg(m)),
r.isError);
check(qPrintable(QString("git_show meta '%1': kind=io_error").arg(m)),
r.errorKind == "io_error");
}
// Nonexistent SHA — passes our regex/metachar gate but git rejects.
// git_show returns git_error in that case (stderr surfaced).
{
AiTools::ToolCall call;
call.id = "gsh_nope";
call.name = "git_show";
call.args = QJsonObject{{"commit", "deadbeefcafebabe"}};
AiTools::ToolResult r = AiTools::execute(call, fx.ws);
check("git_show nonexistent SHA: isError", r.isError);
check("git_show nonexistent SHA: kind=git_error",
r.errorKind == "git_error");
}
}
// ── 7) Real-repo integration ─────────────────────────────────────────────
//
// Read-only sweep against the actual Notepatra repo. Verifies the tools
// return sensible data on a realistic codebase. Uses no setup, no teardown.
static void testRealRepo() {
fprintf(stdout, "[suite] real-repo integration sweep\n");
QString repo = QString::fromLocal8Bit(qgetenv("NOTEPATRA_REPO_ROOT"));
if (repo.isEmpty()) {
fprintf(stdout, " (skipping — NOTEPATRA_REPO_ROOT unset)\n");
return;
}
QFileInfo fi(repo + "/.git");
if (!fi.exists()) {
fprintf(stdout, " (skipping — repo not present at %s)\n",
qPrintable(repo));
return;
}
// git_log → expect a recent commit
{
AiTools::ToolCall call;
call.id = "rl";
call.name = "git_log";
call.args = QJsonObject{{"max_count", 50}};
AiTools::ToolResult r = AiTools::execute(call, repo);
check("real git_log: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QJsonArray commits = result.value("commits").toArray();
check("real git_log: count > 0", commits.size() > 0);
bool sawV58 = false;
for (const auto &v : commits) {
QString subj = v.toObject().value("subject").toString();
if (subj.contains("v0.1.58") || subj.contains("0.1.58")) {
sawV58 = true;
break;
}
}
check("real git_log: contains v0.1.58 commit", sawV58);
}
// git_branch_list → expect main
{
AiTools::ToolCall call;
call.id = "rb";
call.name = "git_branch_list";
AiTools::ToolResult r = AiTools::execute(call, repo);
check("real git_branch_list: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
QJsonArray branches = result.value("branches").toArray();
bool sawMain = false;
for (const auto &v : branches) {
if (v.toObject().value("name").toString() == "main") {
sawMain = true;
break;
}
}
check("real git_branch_list: contains 'main'", sawMain);
}
// git_status → report what's there (we don't enforce clean since
// there might be untracked files).
{
AiTools::ToolCall call;
call.id = "rs";
call.name = "git_status";
AiTools::ToolResult r = AiTools::execute(call, repo);
check("real git_status: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("real git_status: branch present (non-empty)",
!result.value("branch").toString().isEmpty());
QJsonArray staged = result.value("staged").toArray();
QJsonArray modified = result.value("modified").toArray();
QJsonArray untracked = result.value("untracked").toArray();
fprintf(stdout, " (real-repo state: staged=%d modified=%d untracked=%d)\n",
staged.size(), modified.size(), untracked.size());
}
// git_show HEAD → succeeds, contains commit info
{
AiTools::ToolCall call;
call.id = "rsh";
call.name = "git_show";
call.args = QJsonObject{{"commit", "HEAD"}};
AiTools::ToolResult r = AiTools::execute(call, repo);
check("real git_show HEAD: not error", !r.isError);
QJsonObject body = parseToolBody(r);
QJsonObject result = body.value("result").toObject();
check("real git_show HEAD: text non-empty",
!result.value("text").toString().isEmpty());
}
}
// ── 8) Results writer ────────────────────────────────────────────────────
//
// Writes a JSON results blob to tests/comprehensive/results/git_tools.json.
static void writeResultsJson(const QString &outPath, int total,
int passed, int failed) {
QFile f(outPath);
if (!f.open(QFile::WriteOnly | QFile::Truncate)) {
fprintf(stderr, "warn: cannot write %s\n", qPrintable(outPath));
return;
}
QJsonObject doc;
doc["test"] = "git_tools_e2e";
doc["scenarios"] = total;
doc["pass"] = passed;
doc["fail"] = failed;
QJsonArray failures;
for (const FailDetail &d : g_failDetails) {
failures.push_back(d.label);
}
QJsonObject details;
details["failures"] = failures;
details["suites"] = QJsonArray{
"no_workspace_errors",
"not_a_repo",
"git_status (clean/dirty/staged)",
"git_diff (working/staged/path/traversal)",
"git_log (default/n/clamp/path/traversal)",
"git_branch_list (main + feature/x)",
"git_show (sha/short/HEAD/HEAD~1/branch/error paths)",
"real-repo integration sweep"
};
doc["details"] = details;
f.write(QJsonDocument(doc).toJson(QJsonDocument::Indented));
f.close();
}
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
// ── Setup: temp non-repo dir + temp repo dir ─────────────────────────
QTemporaryDir tmpNonRepo;
if (!tmpNonRepo.isValid()) {
fprintf(stderr, "FATAL: cannot create temp non-repo dir\n");
return 1;
}
QTemporaryDir tmpRepo;
if (!tmpRepo.isValid()) {
fprintf(stderr, "FATAL: cannot create temp repo dir\n");
return 1;
}
RepoFixture fx;
if (!buildFixture(tmpRepo, fx)) {
fprintf(stderr, "FATAL: buildFixture failed\n");
return 1;
}
fprintf(stdout, "Fixture built: ws=%s sha1=%s sha2=%s sha3=%s\n",
qPrintable(fx.ws), qPrintable(fx.sha1.left(7)),
qPrintable(fx.sha2.left(7)), qPrintable(fx.sha3.left(7)));
testNoWorkspaceErrors();
testNotARepo(tmpNonRepo.path());
testGitStatus(fx);
testGitDiff(fx);
testGitLog(fx);
testGitBranchList(fx);
testGitShow(fx);
testRealRepo();
int total = g_pass + g_fail;
fprintf(stdout, "\n=========================================\n");
fprintf(stdout, " total: %d pass: %d fail: %d\n",
total, g_pass, g_fail);
fprintf(stdout, "=========================================\n");
// Write results.json. Path is overridable via NOTEPATRA_RESULTS_DIR
// (the comprehensive harness sets this); otherwise default to a
// relative path so checked-in source has no machine-specific paths.
QString resultsDir = QString::fromLocal8Bit(qgetenv("NOTEPATRA_RESULTS_DIR"));
if (resultsDir.isEmpty())
resultsDir = QStringLiteral("tests/comprehensive/results");
QString resultsPath = resultsDir + "/git_tools.json";
QDir().mkpath(QFileInfo(resultsPath).absolutePath());
writeResultsJson(resultsPath, total, g_pass, g_fail);
return g_fail == 0 ? 0 : 1;
}