-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathtest_cli.c
More file actions
2500 lines (2047 loc) · 85.4 KB
/
test_cli.c
File metadata and controls
2500 lines (2047 loc) · 85.4 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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* test_cli.c — Tests for CLI subcommands: install, uninstall, update, version.
*
* Port of Go test files:
* - cmd/codebase-memory-mcp/cli_test.go (11 tests)
* - cmd/codebase-memory-mcp/install_test.go (24 tests)
* - cmd/codebase-memory-mcp/update_test.go (5 tests)
* - internal/selfupdate/selfupdate_test.go (7 tests)
*
* Total: 47 Go tests → 47 C tests
*/
#include "../src/foundation/compat.h"
#include "test_framework.h"
#include <cli/cli.h>
#include <foundation/yaml.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <zlib.h>
/* Helper: create a file with content */
static int write_test_file(const char *path, const char *content) {
FILE *f = fopen(path, "w");
if (!f)
return -1;
fprintf(f, "%s", content);
fclose(f);
return 0;
}
/* Helper: read a file into static buffer */
static const char *read_test_file(const char *path) {
static char buf[8192];
FILE *f = fopen(path, "r");
if (!f)
return NULL;
size_t n = fread(buf, 1, sizeof(buf) - 1, f);
fclose(f);
buf[n] = '\0';
return buf;
}
/* Helper: mkdirp */
static int test_mkdirp(const char *path) {
char tmp[1024];
snprintf(tmp, sizeof(tmp), "%s", path);
for (char *p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = '\0';
cbm_mkdir(tmp);
*p = '/';
}
}
return cbm_mkdir(tmp) == 0 || errno == EEXIST ? 0 : -1;
}
/* Helper: recursive remove */
static void test_rmdir_r(const char *path) {
char cmd[1024];
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", path);
system(cmd);
}
/* Helper: create tar.gz with a single file */
static unsigned char *create_test_targz(const char *filename, const unsigned char *content,
int content_len, int *out_len) {
/* Build tar data: 512-byte header + content padded to 512-byte boundary + 2x512 zero blocks */
int data_blocks = (content_len + 511) / 512;
int tar_size = 512 + data_blocks * 512 + 1024; /* header + data + end-of-archive */
unsigned char *tar = calloc(1, (size_t)tar_size);
if (!tar)
return NULL;
/* Filename (bytes 0-99) */
strncpy((char *)tar, filename, 99);
/* Mode (bytes 100-107): octal 0700 */
memcpy(tar + 100, "0000700\0", 8);
/* UID/GID (bytes 108-123): 0 */
memcpy(tar + 108, "0000000\0", 8);
memcpy(tar + 116, "0000000\0", 8);
/* Size (bytes 124-135): octal */
char size_str[12];
snprintf(size_str, sizeof(size_str), "%011o", content_len);
memcpy(tar + 124, size_str, 11);
/* Mtime (bytes 136-147): 0 */
memcpy(tar + 136, "00000000000\0", 12);
/* Type flag (byte 156): '0' = regular file */
tar[156] = '0';
/* Checksum (bytes 148-155): compute over header with checksum field as spaces */
memset(tar + 148, ' ', 8);
unsigned int checksum = 0;
for (int i = 0; i < 512; i++)
checksum += tar[i];
snprintf((char *)tar + 148, 7, "%06o", checksum);
tar[154] = '\0';
/* File content */
memcpy(tar + 512, content, (size_t)content_len);
/* Compress with gzip */
z_stream strm = {0};
if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 16 + MAX_WBITS, 8,
Z_DEFAULT_STRATEGY) != Z_OK) {
free(tar);
return NULL;
}
size_t gz_cap = (size_t)tar_size + 256;
unsigned char *gz = malloc(gz_cap);
if (!gz) {
deflateEnd(&strm);
free(tar);
return NULL;
}
strm.next_in = tar;
strm.avail_in = (unsigned int)tar_size;
strm.next_out = gz;
strm.avail_out = (unsigned int)gz_cap;
deflate(&strm, Z_FINISH);
*out_len = (int)(gz_cap - strm.avail_out);
deflateEnd(&strm);
free(tar);
return gz;
}
/* ═══════════════════════════════════════════════════════════════════
* Version comparison tests (port of selfupdate_test.go)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_compare_versions) {
/* Port of TestCompareVersions — 13 cases */
ASSERT(cbm_compare_versions("0.2.1", "0.2.0") > 0);
ASSERT_EQ(cbm_compare_versions("0.2.0", "0.2.0"), 0);
ASSERT(cbm_compare_versions("0.1.9", "0.2.0") < 0);
ASSERT(cbm_compare_versions("0.10.0", "0.2.0") > 0);
ASSERT(cbm_compare_versions("1.0.0", "0.99.99") > 0);
ASSERT(cbm_compare_versions("0.0.1", "0.0.2") < 0);
ASSERT_EQ(cbm_compare_versions("v0.2.1", "0.2.1"), 0);
ASSERT_EQ(cbm_compare_versions("0.2.1", "v0.2.1"), 0);
ASSERT(cbm_compare_versions("0.2.1-dev", "0.2.1") < 0);
ASSERT(cbm_compare_versions("0.2.1", "0.2.1-dev") > 0);
ASSERT_EQ(cbm_compare_versions("0.2.1-dev", "0.2.1-dev"), 0);
ASSERT(cbm_compare_versions("0.3.0", "0.2.1-dev") > 0);
ASSERT(cbm_compare_versions("0.2.0", "0.2.1-dev") < 0);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* Version get/set (port of TestCLI_Version)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_version_get_set) {
cbm_cli_set_version("1.2.3");
ASSERT_STR_EQ(cbm_cli_get_version(), "1.2.3");
cbm_cli_set_version("dev");
ASSERT_STR_EQ(cbm_cli_get_version(), "dev");
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* Shell RC detection (port of TestDetectShellRC + BashWithBashrc)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_detect_shell_rc_zsh) {
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-rc-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
/* Save and override SHELL — must strdup because setenv may realloc env block */
const char *raw = getenv("SHELL");
char *old_shell = raw ? strdup(raw) : NULL;
cbm_setenv("SHELL", "/bin/zsh", 1);
const char *rc = cbm_detect_shell_rc(tmpdir);
ASSERT_NOT_NULL(rc);
ASSERT(strstr(rc, ".zshrc") != NULL);
if (old_shell) {
cbm_setenv("SHELL", old_shell, 1);
free(old_shell);
} else
cbm_unsetenv("SHELL");
rmdir(tmpdir);
PASS();
}
TEST(cli_detect_shell_rc_bash) {
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-rc-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
const char *raw = getenv("SHELL");
char *old_shell = raw ? strdup(raw) : NULL;
cbm_setenv("SHELL", "/bin/bash", 1);
/* No .bashrc → falls back to .bash_profile */
const char *rc = cbm_detect_shell_rc(tmpdir);
ASSERT_NOT_NULL(rc);
ASSERT(strstr(rc, ".bash_profile") != NULL);
if (old_shell) {
cbm_setenv("SHELL", old_shell, 1);
free(old_shell);
} else
cbm_unsetenv("SHELL");
rmdir(tmpdir);
PASS();
}
TEST(cli_detect_shell_rc_bash_with_bashrc) {
/* Port of TestDetectShellRC_BashWithBashrc */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-rc-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
const char *raw = getenv("SHELL");
char *old_shell = raw ? strdup(raw) : NULL;
cbm_setenv("SHELL", "/bin/bash", 1);
/* Create .bashrc */
char bashrc[512];
snprintf(bashrc, sizeof(bashrc), "%s/.bashrc", tmpdir);
write_test_file(bashrc, "# test\n");
const char *rc = cbm_detect_shell_rc(tmpdir);
ASSERT_STR_EQ(rc, bashrc);
unlink(bashrc);
if (old_shell) {
cbm_setenv("SHELL", old_shell, 1);
free(old_shell);
} else
cbm_unsetenv("SHELL");
rmdir(tmpdir);
PASS();
}
TEST(cli_detect_shell_rc_fish) {
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-rc-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
const char *raw = getenv("SHELL");
char *old_shell = raw ? strdup(raw) : NULL;
cbm_setenv("SHELL", "/usr/bin/fish", 1);
const char *rc = cbm_detect_shell_rc(tmpdir);
ASSERT(strstr(rc, ".config/fish/config.fish") != NULL);
if (old_shell) {
cbm_setenv("SHELL", old_shell, 1);
free(old_shell);
} else
cbm_unsetenv("SHELL");
rmdir(tmpdir);
PASS();
}
TEST(cli_detect_shell_rc_default) {
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-rc-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
const char *raw = getenv("SHELL");
char *old_shell = raw ? strdup(raw) : NULL;
cbm_setenv("SHELL", "/bin/sh", 1);
const char *rc = cbm_detect_shell_rc(tmpdir);
ASSERT(strstr(rc, ".profile") != NULL);
if (old_shell) {
cbm_setenv("SHELL", old_shell, 1);
free(old_shell);
} else
cbm_unsetenv("SHELL");
rmdir(tmpdir);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* CLI binary detection (port of TestFindCLI_*)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_find_cli_not_found) {
/* Port of TestFindCLI_NotFound */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-find-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
const char *raw = getenv("PATH");
char *old_path = raw ? strdup(raw) : NULL;
cbm_setenv("PATH", tmpdir, 1);
const char *result = cbm_find_cli("nonexistent-binary-xyz", tmpdir);
ASSERT_STR_EQ(result, "");
if (old_path) {
cbm_setenv("PATH", old_path, 1);
free(old_path);
}
rmdir(tmpdir);
PASS();
}
TEST(cli_find_cli_on_path) {
#ifdef _WIN32
SKIP("PATH search differs on Windows");
#endif
/* Port of TestFindCLI_FoundOnPATH */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-find-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char fakecli[512];
snprintf(fakecli, sizeof(fakecli), "%s/fakecli", tmpdir);
write_test_file(fakecli, "#!/bin/sh\n");
chmod(fakecli, 0500);
const char *raw = getenv("PATH");
char *old_path = raw ? strdup(raw) : NULL;
cbm_setenv("PATH", tmpdir, 1);
const char *result = cbm_find_cli("fakecli", tmpdir);
ASSERT(result[0] != '\0');
ASSERT(strstr(result, "fakecli") != NULL);
if (old_path) {
cbm_setenv("PATH", old_path, 1);
free(old_path);
}
unlink(fakecli);
rmdir(tmpdir);
PASS();
}
TEST(cli_find_cli_fallback_paths) {
#ifdef _WIN32
SKIP("shell scripts + chmod not available on Windows");
#endif
/* Port of TestFindCLI_FallbackPaths */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-find-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char localbin[512];
snprintf(localbin, sizeof(localbin), "%s/.local/bin", tmpdir);
test_mkdirp(localbin);
char fakecli[512];
snprintf(fakecli, sizeof(fakecli), "%s/testcli", localbin);
write_test_file(fakecli, "#!/bin/sh\n");
chmod(fakecli, 0500);
const char *raw = getenv("PATH");
char *old_path = raw ? strdup(raw) : NULL;
cbm_setenv("PATH", "/nonexistent", 1);
const char *result = cbm_find_cli("testcli", tmpdir);
ASSERT_STR_EQ(result, fakecli);
if (old_path) {
cbm_setenv("PATH", old_path, 1);
free(old_path);
}
test_rmdir_r(tmpdir);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* Dry-run flag parsing (port of TestDryRun)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_dry_run_flags) {
/* Port of TestDryRun — just verifies the pattern */
bool dry_run = false, force = false;
const char *args[] = {"--dry-run", "--force"};
for (int i = 0; i < 2; i++) {
if (strcmp(args[i], "--dry-run") == 0)
dry_run = true;
if (strcmp(args[i], "--force") == 0)
force = true;
}
ASSERT_TRUE(dry_run);
ASSERT_TRUE(force);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* Skill file management tests (port of install_test.go skill tests)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_skill_creation) {
/* Port of TestInstallSkillCreation */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-skill-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char skills_dir[512];
snprintf(skills_dir, sizeof(skills_dir), "%s/.claude/skills", tmpdir);
int written = cbm_install_skills(skills_dir, false, false);
ASSERT_EQ(written, CBM_SKILL_COUNT);
/* Verify all 4 skills exist and have content */
const cbm_skill_t *sk = cbm_get_skills();
for (int i = 0; i < CBM_SKILL_COUNT; i++) {
char path[1024];
snprintf(path, sizeof(path), "%s/%s/SKILL.md", skills_dir, sk[i].name);
const char *data = read_test_file(path);
ASSERT_NOT_NULL(data);
ASSERT(strlen(data) > 0);
/* Check YAML frontmatter */
ASSERT(strncmp(data, "---\n", 4) == 0);
/* Check name field */
ASSERT(strstr(data, sk[i].name) != NULL);
}
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_skill_idempotent) {
/* Port of TestInstallIdempotent */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-skill-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char skills_dir[512];
snprintf(skills_dir, sizeof(skills_dir), "%s/.claude/skills", tmpdir);
/* Install twice */
cbm_install_skills(skills_dir, false, false);
int second = cbm_install_skills(skills_dir, false, false);
/* Second install should write 0 (skills exist, no force) */
ASSERT_EQ(second, 0);
/* All skills should still exist */
const cbm_skill_t *sk = cbm_get_skills();
for (int i = 0; i < CBM_SKILL_COUNT; i++) {
char path[1024];
snprintf(path, sizeof(path), "%s/%s/SKILL.md", skills_dir, sk[i].name);
struct stat st;
ASSERT_EQ(stat(path, &st), 0);
}
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_skill_force_overwrite) {
/* Port of TestCLI_InstallForceOverwrites */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-skill-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char skills_dir[512];
snprintf(skills_dir, sizeof(skills_dir), "%s/.claude/skills", tmpdir);
cbm_install_skills(skills_dir, false, false);
int force_count = cbm_install_skills(skills_dir, true, false);
/* Force should overwrite all */
ASSERT_EQ(force_count, CBM_SKILL_COUNT);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_uninstall_removes_skills) {
/* Port of TestUninstallRemovesSkills */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-skill-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char skills_dir[512];
snprintf(skills_dir, sizeof(skills_dir), "%s/.claude/skills", tmpdir);
cbm_install_skills(skills_dir, false, false);
int removed = cbm_remove_skills(skills_dir, false);
ASSERT_EQ(removed, CBM_SKILL_COUNT);
/* Verify all removed */
const cbm_skill_t *sk = cbm_get_skills();
for (int i = 0; i < CBM_SKILL_COUNT; i++) {
char path[1024];
snprintf(path, sizeof(path), "%s/%s", skills_dir, sk[i].name);
struct stat st;
ASSERT(stat(path, &st) != 0);
}
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_remove_old_monolithic_skill) {
/* Port of TestRemoveOldMonolithicSkill */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-skill-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char skills_dir[512];
snprintf(skills_dir, sizeof(skills_dir), "%s/.claude/skills", tmpdir);
/* Create old monolithic skill */
char old_dir[1024];
snprintf(old_dir, sizeof(old_dir), "%s/codebase-memory-mcp", skills_dir);
test_mkdirp(old_dir);
char old_file[1024];
snprintf(old_file, sizeof(old_file), "%s/SKILL.md", old_dir);
write_test_file(old_file, "old skill");
bool removed = cbm_remove_old_monolithic_skill(skills_dir, false);
ASSERT_TRUE(removed);
struct stat st;
ASSERT(stat(old_dir, &st) != 0);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_skill_files_content) {
/* Port of TestSkillFilesContent */
const cbm_skill_t *sk = cbm_get_skills();
ASSERT_EQ(CBM_SKILL_COUNT, 4);
/* Check exploring skill */
bool found_exploring = false, found_tracing = false;
bool found_quality = false, found_reference = false;
for (int i = 0; i < CBM_SKILL_COUNT; i++) {
if (strcmp(sk[i].name, "codebase-memory-exploring") == 0) {
found_exploring = true;
ASSERT(strstr(sk[i].content, "search_graph") != NULL);
ASSERT(strstr(sk[i].content, "get_graph_schema") != NULL);
}
if (strcmp(sk[i].name, "codebase-memory-tracing") == 0) {
found_tracing = true;
ASSERT(strstr(sk[i].content, "trace_path") != NULL);
ASSERT(strstr(sk[i].content, "direction") != NULL);
ASSERT(strstr(sk[i].content, "detect_changes") != NULL);
}
if (strcmp(sk[i].name, "codebase-memory-quality") == 0) {
found_quality = true;
ASSERT(strstr(sk[i].content, "max_degree=0") != NULL);
ASSERT(strstr(sk[i].content, "exclude_entry_points") != NULL);
}
if (strcmp(sk[i].name, "codebase-memory-reference") == 0) {
found_reference = true;
ASSERT(strstr(sk[i].content, "query_graph") != NULL);
ASSERT(strstr(sk[i].content, "Cypher") != NULL);
ASSERT(strstr(sk[i].content, "14 total") != NULL);
}
}
ASSERT_TRUE(found_exploring);
ASSERT_TRUE(found_tracing);
ASSERT_TRUE(found_quality);
ASSERT_TRUE(found_reference);
PASS();
}
TEST(cli_codex_instructions) {
/* Port of TestCodexInstructionsCreation */
const char *instr = cbm_get_codex_instructions();
ASSERT_NOT_NULL(instr);
ASSERT(strstr(instr, "Codebase Knowledge Graph") != NULL);
ASSERT(strstr(instr, "trace_path") != NULL);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* Editor MCP config tests (Cursor/Windsurf/Gemini)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_editor_mcp_install) {
/* Port of TestEditorMCPInstall */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.cursor/mcp.json", tmpdir);
int rc = cbm_install_editor_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
ASSERT(strstr(data, "mcpServers") != NULL);
ASSERT(strstr(data, "codebase-memory-mcp") != NULL);
ASSERT(strstr(data, "/usr/local/bin/codebase-memory-mcp") != NULL);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_editor_mcp_idempotent) {
/* Port of TestEditorMCPInstallIdempotent */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.cursor/mcp.json", tmpdir);
cbm_install_editor_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
int rc = cbm_install_editor_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
ASSERT_EQ(rc, 0);
/* Should still parse as valid JSON with only 1 server */
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
/* Count occurrences of "codebase-memory-mcp" (should be exactly 1 in mcpServers) */
int count = 0;
const char *p = data;
while ((p = strstr(p, "\"codebase-memory-mcp\"")) != NULL) {
count++;
p += 20;
}
/* The key appears once as key name */
ASSERT_EQ(count, 1);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_editor_mcp_preserves_others) {
/* Port of TestEditorMCPPreservesOtherServers */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.cursor/mcp.json", tmpdir);
test_mkdirp(tmpdir);
char dir[512];
snprintf(dir, sizeof(dir), "%s/.cursor", tmpdir);
test_mkdirp(dir);
/* Write config with existing server */
write_test_file(configpath,
"{\"mcpServers\": {\"other-server\": {\"command\": \"/usr/bin/other\"}}}");
cbm_install_editor_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
ASSERT(strstr(data, "other-server") != NULL);
ASSERT(strstr(data, "codebase-memory-mcp") != NULL);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_editor_mcp_uninstall) {
/* Port of TestEditorMCPUninstall */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.cursor/mcp.json", tmpdir);
cbm_install_editor_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
int rc = cbm_remove_editor_mcp(configpath);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
/* codebase-memory-mcp should be removed */
ASSERT(strstr(data, "\"codebase-memory-mcp\"") == NULL);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_gemini_mcp_install) {
/* Port of TestGeminiMCPInstall */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.gemini/settings.json", tmpdir);
/* Gemini uses same mcpServers format as Cursor */
int rc = cbm_install_editor_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
ASSERT(strstr(data, "mcpServers") != NULL);
ASSERT(strstr(data, "codebase-memory-mcp") != NULL);
test_rmdir_r(tmpdir);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* VS Code MCP config tests
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_vscode_mcp_install) {
/* Port of TestVSCodeMCPInstall */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/Code/User/mcp.json", tmpdir);
int rc = cbm_install_vscode_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
ASSERT(strstr(data, "\"servers\"") != NULL);
ASSERT(strstr(data, "\"type\"") != NULL);
ASSERT(strstr(data, "\"stdio\"") != NULL);
ASSERT(strstr(data, "codebase-memory-mcp") != NULL);
ASSERT(strstr(data, "/usr/local/bin/codebase-memory-mcp") != NULL);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_vscode_mcp_uninstall) {
/* Port of TestVSCodeMCPUninstall */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/Code/User/mcp.json", tmpdir);
cbm_install_vscode_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
int rc = cbm_remove_vscode_mcp(configpath);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
ASSERT(strstr(data, "\"codebase-memory-mcp\"") == NULL);
test_rmdir_r(tmpdir);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* Zed MCP config tests
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_zed_mcp_install) {
/* Port of TestZedMCPInstall */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.config/zed/settings.json", tmpdir);
int rc = cbm_install_zed_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
ASSERT(strstr(data, "\"context_servers\"") != NULL);
ASSERT(strstr(data, "\"command\"") != NULL);
ASSERT(strstr(data, "\"args\"") != NULL);
ASSERT(strstr(data, "codebase-memory-mcp") != NULL);
ASSERT(strstr(data, "/usr/local/bin/codebase-memory-mcp") != NULL);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_zed_mcp_preserves_settings) {
/* Port of TestZedMCPPreservesSettings */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.config/zed/settings.json", tmpdir);
char dir[512];
snprintf(dir, sizeof(dir), "%s/.config/zed", tmpdir);
test_mkdirp(dir);
/* Pre-existing Zed settings */
write_test_file(configpath, "{\"theme\": \"One Dark\", \"vim_mode\": true}");
cbm_install_zed_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
/* Original settings preserved */
ASSERT(strstr(data, "One Dark") != NULL);
ASSERT(strstr(data, "vim_mode") != NULL);
/* MCP server added */
ASSERT(strstr(data, "context_servers") != NULL);
ASSERT(strstr(data, "codebase-memory-mcp") != NULL);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_zed_mcp_uninstall) {
/* Port of TestZedMCPUninstall */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.config/zed/settings.json", tmpdir);
cbm_install_zed_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
int rc = cbm_remove_zed_mcp(configpath);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
ASSERT(strstr(data, "\"codebase-memory-mcp\"") == NULL);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_zed_mcp_jsonc_comments) {
/* Issue #24: Zed settings.json uses JSONC (comments + trailing commas) */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-mcp-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/.config/zed/settings.json", tmpdir);
char dir[512];
snprintf(dir, sizeof(dir), "%s/.config/zed", tmpdir);
test_mkdirp(dir);
/* JSONC with comments and trailing commas — must not fail */
write_test_file(configpath, "// Zed settings\n"
"{\n"
" \"theme\": \"One Dark\",\n"
" /* multi-line\n"
" comment */\n"
" \"vim_mode\": true,\n" /* trailing comma */
"}\n");
int rc = cbm_install_zed_mcp("/usr/local/bin/codebase-memory-mcp", configpath);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
/* Original settings preserved */
ASSERT(strstr(data, "One Dark") != NULL);
ASSERT(strstr(data, "vim_mode") != NULL);
/* MCP server added */
ASSERT(strstr(data, "codebase-memory-mcp") != NULL);
ASSERT(strstr(data, "context_servers") != NULL);
test_rmdir_r(tmpdir);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* PATH management tests (port of TestCLI_InstallPATHAppend)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_ensure_path_append) {
/* Port of TestCLI_InstallPATHAppend */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-path-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char rcfile[512];
snprintf(rcfile, sizeof(rcfile), "%s/.zshrc", tmpdir);
write_test_file(rcfile, "# existing content\n");
int rc = cbm_ensure_path("/usr/local/bin", rcfile, false);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(rcfile);
ASSERT(strstr(data, "export PATH=\"/usr/local/bin:$PATH\"") != NULL);
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_ensure_path_already_present) {
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-path-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char rcfile[512];
snprintf(rcfile, sizeof(rcfile), "%s/.zshrc", tmpdir);
write_test_file(rcfile, "export PATH=\"/usr/local/bin:$PATH\"\n");
int rc = cbm_ensure_path("/usr/local/bin", rcfile, false);
ASSERT_EQ(rc, 1); /* 1 = already present */
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_ensure_path_dry_run) {
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-path-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char rcfile[512];
snprintf(rcfile, sizeof(rcfile), "%s/.zshrc", tmpdir);
write_test_file(rcfile, "# clean\n");
int rc = cbm_ensure_path("/usr/local/bin", rcfile, true);
ASSERT_EQ(rc, 0);
/* File should NOT be modified */
const char *data = read_test_file(rcfile);
ASSERT(strstr(data, "export PATH") == NULL);
test_rmdir_r(tmpdir);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* File copy tests (port of update_test.go)
* ═══════════════════════════════════════════════════════════════════ */
TEST(cli_copy_file) {
/* Port of TestCopyFile */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-copy-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char src[512], dst[512];
snprintf(src, sizeof(src), "%s/source", tmpdir);
snprintf(dst, sizeof(dst), "%s/dest", tmpdir);
write_test_file(src, "test content for copy");
int rc = cbm_copy_file(src, dst);
ASSERT_EQ(rc, 0);
const char *data = read_test_file(dst);
ASSERT_STR_EQ(data, "test content for copy");
test_rmdir_r(tmpdir);
PASS();
}
TEST(cli_copy_file_source_not_found) {
/* Port of TestCopyFile_SourceNotFound */
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-copy-XXXXXX");
if (!cbm_mkdtemp(tmpdir))
SKIP("cbm_mkdtemp failed");
char src[512], dst[512];
snprintf(src, sizeof(src), "%s/nonexistent", tmpdir);
snprintf(dst, sizeof(dst), "%s/dest", tmpdir);