Repository navigation
Expand file tree
/
Copy pathsbox_test.go
More file actions
1392 lines (1223 loc) · 37.1 KB
/
Copy pathsbox_test.go
File metadata and controls
1392 lines (1223 loc) · 37.1 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
package sbox
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestProjectHash(t *testing.T) {
tests := []struct {
name string
path string
wantLen int
wantSame bool
compareTo string
}{
{
name: "generates 12 char hash",
path: "/tmp/test-project",
wantLen: 12,
},
{
name: "same path produces same hash",
path: "/tmp/same-project",
wantSame: true,
compareTo: "/tmp/same-project",
},
{
name: "different paths produce different hashes",
path: "/tmp/project-a",
wantSame: false,
compareTo: "/tmp/project-b",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hash, err := ProjectHash(tt.path)
if err != nil {
t.Fatalf("ProjectHash(%q) error = %v", tt.path, err)
}
if tt.wantLen > 0 && len(hash) != tt.wantLen {
t.Errorf("ProjectHash(%q) = %q, want length %d, got %d", tt.path, hash, tt.wantLen, len(hash))
}
if tt.compareTo != "" {
hash2, err := ProjectHash(tt.compareTo)
if err != nil {
t.Fatalf("ProjectHash(%q) error = %v", tt.compareTo, err)
}
if tt.wantSame && hash != hash2 {
t.Errorf("ProjectHash(%q) = %q, want same as ProjectHash(%q) = %q", tt.path, hash, tt.compareTo, hash2)
}
if !tt.wantSame && hash == hash2 {
t.Errorf("ProjectHash(%q) = %q, want different from ProjectHash(%q) = %q", tt.path, hash, tt.compareTo, hash2)
}
}
})
}
}
func TestDiscoverMDFiles(t *testing.T) {
// Create a temporary directory structure
tempDir := t.TempDir()
// Create nested directory structure
level1 := filepath.Join(tempDir, "level1")
level2 := filepath.Join(level1, "level2")
level3 := filepath.Join(level2, "level3")
if err := os.MkdirAll(level3, 0755); err != nil {
t.Fatalf("Failed to create test directories: %v", err)
}
// Create CLAUDE.md files at different levels
rootClaude := filepath.Join(tempDir, "CLAUDE.md")
level1Claude := filepath.Join(level1, "CLAUDE.md")
level3Claude := filepath.Join(level3, "CLAUDE.md")
// Create AGENTS.md at level2
level2Agents := filepath.Join(level2, "AGENTS.md")
testFiles := map[string]string{
rootClaude: "# Root CLAUDE.md",
level1Claude: "# Level 1 CLAUDE.md",
level2Agents: "# Level 2 AGENTS.md",
level3Claude: "# Level 3 CLAUDE.md",
}
for path, content := range testFiles {
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("Failed to create test file %s: %v", path, err)
}
}
// Test discovery from level3
files, err := DiscoverMDFiles(level3)
if err != nil {
t.Fatalf("DiscoverMDFiles(%q) error = %v", level3, err)
}
// Should find files in order from root to level3
if len(files) != 4 {
t.Errorf("DiscoverMDFiles(%q) found %d files, want 4", level3, len(files))
}
// Verify order: root -> level1 -> level2 agents -> level3
expectedOrder := []string{rootClaude, level1Claude, level2Agents, level3Claude}
for i, expected := range expectedOrder {
if i >= len(files) {
break
}
if files[i] != expected {
t.Errorf("DiscoverMDFiles files[%d] = %q, want %q", i, files[i], expected)
}
}
}
func TestConcatenateMDFiles(t *testing.T) {
tempDir := t.TempDir()
// Create test files
file1 := filepath.Join(tempDir, "file1.md")
file2 := filepath.Join(tempDir, "file2.md")
require.NoError(t, os.WriteFile(file1, []byte("Content 1"), 0644))
require.NoError(t, os.WriteFile(file2, []byte("Content 2"), 0644))
// Test concatenation with sandbox backend
result, err := ConcatenateMDFiles([]string{file1, file2}, BackendSandbox, "")
require.NoError(t, err)
assert.NotEmpty(t, result)
// Embedded sandbox backend context should be first
assert.Contains(t, result, "sbox (embedded sandbox backend instructions)")
assert.Contains(t, result, "Docker Sandbox Backend")
// Check for source markers and content
assert.Contains(t, result, "Source: "+file1)
assert.Contains(t, result, "Source: "+file2)
assert.Contains(t, result, "Content 1")
assert.Contains(t, result, "Content 2")
}
func TestConcatenateMDFiles_ContainerBackend(t *testing.T) {
tempDir := t.TempDir()
file1 := filepath.Join(tempDir, "file1.md")
require.NoError(t, os.WriteFile(file1, []byte("Content 1"), 0644))
// Test concatenation with container backend
result, err := ConcatenateMDFiles([]string{file1}, BackendContainer, "")
require.NoError(t, err)
// Should have container backend context
assert.Contains(t, result, "sbox (embedded container backend instructions)")
assert.Contains(t, result, "Docker Container Backend")
assert.Contains(t, result, "Content 1")
}
func TestConcatenateMDFiles_Empty(t *testing.T) {
result, err := ConcatenateMDFiles([]string{}, BackendSandbox, "")
require.NoError(t, err)
// Even with no files, we should have the embedded backend context
assert.Contains(t, result, "sbox (embedded sandbox backend instructions)")
assert.Contains(t, result, "Docker Sandbox Backend")
}
func TestLoadConfig_Defaults(t *testing.T) {
// Save and restore HOME to test with clean state
origHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", origHome)
config, err := LoadConfig()
if err != nil {
t.Fatalf("LoadConfig() error = %v", err)
}
// Check defaults
if config.DockerSocket != "auto" {
t.Errorf("config.DockerSocket = %q, want %q", config.DockerSocket, "auto")
}
expectedClaudeHome := filepath.Join(tempDir, ".claude")
if config.ClaudeHome != expectedClaudeHome {
t.Errorf("config.ClaudeHome = %q, want %q", config.ClaudeHome, expectedClaudeHome)
}
expectedSboxDir := filepath.Join(tempDir, ".config", "sbox")
if config.SboxDataDir != expectedSboxDir {
t.Errorf("config.SboxDataDir = %q, want %q", config.SboxDataDir, expectedSboxDir)
}
}
func TestGetProjectConfig_Defaults(t *testing.T) {
// Save and restore HOME
origHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", origHome)
workspaceDir := filepath.Join(tempDir, "my-project")
if err := os.MkdirAll(workspaceDir, 0755); err != nil {
t.Fatalf("Failed to create workspace dir: %v", err)
}
projectConfig, projectHash, err := GetProjectConfig(workspaceDir)
if err != nil {
t.Fatalf("GetProjectConfig() error = %v", err)
}
if projectHash == "" {
t.Error("GetProjectConfig returned empty project hash")
}
if len(projectConfig.Profiles) != 0 {
t.Errorf("projectConfig.Profiles = %v, want empty slice", projectConfig.Profiles)
}
}
func TestSaveAndLoadProjectConfig(t *testing.T) {
// Save and restore HOME
origHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", origHome)
workspaceDir := filepath.Join(tempDir, "test-project")
if err := os.MkdirAll(workspaceDir, 0755); err != nil {
t.Fatalf("Failed to create workspace dir: %v", err)
}
// Save project config with profiles
projectConfig := &ProjectConfig{
Profiles: []string{"go", "rust"},
}
if err := SaveProjectConfig(workspaceDir, projectConfig); err != nil {
t.Fatalf("SaveProjectConfig() error = %v", err)
}
// Load it back
loadedConfig, _, err := GetProjectConfig(workspaceDir)
if err != nil {
t.Fatalf("GetProjectConfig() error = %v", err)
}
if len(loadedConfig.Profiles) != 2 {
t.Fatalf("loadedConfig.Profiles has %d items, want 2", len(loadedConfig.Profiles))
}
if loadedConfig.Profiles[0] != "go" || loadedConfig.Profiles[1] != "rust" {
t.Errorf("loadedConfig.Profiles = %v, want [go, rust]", loadedConfig.Profiles)
}
}
func TestGetProfile(t *testing.T) {
// Test builtin profiles exist
profiles := []string{"go", "rust", "docker", "bash-utils", "sql", "cpp"}
for _, name := range profiles {
profile, ok := GetProfile(name)
if !ok {
t.Errorf("GetProfile(%q) not found", name)
continue
}
if profile.Name != name {
t.Errorf("GetProfile(%q).Name = %q, want %q", name, profile.Name, name)
}
if profile.Description == "" {
t.Errorf("GetProfile(%q).Description is empty", name)
}
if profile.DockerfileSnippet == "" {
t.Errorf("GetProfile(%q).DockerfileSnippet is empty", name)
}
}
// Test non-existent profile
_, ok := GetProfile("nonexistent")
if ok {
t.Error("GetProfile(\"nonexistent\") should return false")
}
}
func TestListProfiles(t *testing.T) {
profiles := ListProfiles()
if len(profiles) < 4 {
t.Errorf("ListProfiles() returned %d profiles, want at least 4", len(profiles))
}
// Check that expected profiles are included
expected := map[string]bool{"go": false, "rust": false, "docker": false, "bash-utils": false}
for _, p := range profiles {
if _, ok := expected[p]; ok {
expected[p] = true
}
}
for name, found := range expected {
if !found {
t.Errorf("ListProfiles() missing expected profile %q", name)
}
}
}
func TestPrepareMDForSandbox(t *testing.T) {
// Save and restore HOME
origHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", origHome)
// Create workspace with CLAUDE.md
workspaceDir := filepath.Join(tempDir, "workspace")
require.NoError(t, os.MkdirAll(workspaceDir, 0755))
claudeMD := filepath.Join(workspaceDir, "CLAUDE.md")
require.NoError(t, os.WriteFile(claudeMD, []byte("# Test CLAUDE.md\nSome content"), 0644))
// Test with sandbox backend
outputPath, err := PrepareMDForSandbox(workspaceDir, BackendSandbox, "")
require.NoError(t, err)
// Check output file exists
_, err = os.Stat(outputPath)
require.NoError(t, err, "output file should exist")
// Check output path is per-project (contains project hash)
projectHash, _ := ProjectHash(workspaceDir)
assert.Contains(t, outputPath, projectHash)
// Read and verify content
content, err := os.ReadFile(outputPath)
require.NoError(t, err)
// Should contain user content
assert.Contains(t, string(content), "Test CLAUDE.md")
// Should contain sandbox backend context
assert.Contains(t, string(content), "Docker Sandbox Backend")
assert.Contains(t, string(content), "MicroVM")
}
func TestPrepareMDForSandbox_ContainerBackend(t *testing.T) {
// Save and restore HOME
origHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", origHome)
// Create workspace with CLAUDE.md
workspaceDir := filepath.Join(tempDir, "workspace")
require.NoError(t, os.MkdirAll(workspaceDir, 0755))
claudeMD := filepath.Join(workspaceDir, "CLAUDE.md")
require.NoError(t, os.WriteFile(claudeMD, []byte("# Test CLAUDE.md\nSome content"), 0644))
// Test with container backend
outputPath, err := PrepareMDForSandbox(workspaceDir, BackendContainer, "")
require.NoError(t, err)
// Read and verify content
content, err := os.ReadFile(outputPath)
require.NoError(t, err)
// Should contain user content
assert.Contains(t, string(content), "Test CLAUDE.md")
// Should contain container backend context
assert.Contains(t, string(content), "Docker Container Backend")
assert.Contains(t, string(content), "--docker-socket")
}
func containsString(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsSubstring(s, substr))
}
func containsSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
func TestSplitFrontmatter(t *testing.T) {
tests := []struct {
name string
content string
wantFrontmatter string
wantBody string
wantHasFrontmatter bool
}{
{
name: "valid frontmatter",
content: `---
name: test-agent
description: "Test agent"
---
This is the body content.`,
wantFrontmatter: "name: test-agent\ndescription: \"Test agent\"",
wantBody: "This is the body content.",
wantHasFrontmatter: true,
},
{
name: "no frontmatter",
content: "Just body content without frontmatter.",
wantFrontmatter: "",
wantBody: "Just body content without frontmatter.",
wantHasFrontmatter: false,
},
{
name: "unclosed frontmatter",
content: "---\nname: test\nno closing delimiter",
wantFrontmatter: "",
wantBody: "---\nname: test\nno closing delimiter",
wantHasFrontmatter: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
frontmatter, body, err := splitFrontmatter(tt.content)
if err != nil {
t.Fatalf("splitFrontmatter() error = %v", err)
}
if tt.wantHasFrontmatter && frontmatter == "" {
t.Error("splitFrontmatter() expected frontmatter but got empty")
}
if !tt.wantHasFrontmatter && frontmatter != "" {
t.Errorf("splitFrontmatter() expected no frontmatter but got %q", frontmatter)
}
if !containsString(body, tt.wantBody) {
t.Errorf("splitFrontmatter() body = %q, want to contain %q", body, tt.wantBody)
}
})
}
}
func TestParseAgentMarkdown(t *testing.T) {
tempDir := t.TempDir()
// Create a test agent file
agentContent := `---
color: cyan
description: "Test agent for testing"
model: opus
name: test-agent
---
You are a test agent. Do testing things.
## Instructions
- Test stuff
- More testing
`
agentPath := filepath.Join(tempDir, "test-agent.md")
if err := os.WriteFile(agentPath, []byte(agentContent), 0644); err != nil {
t.Fatalf("Failed to create test agent file: %v", err)
}
agent, name, err := parseAgentMarkdown(agentPath)
if err != nil {
t.Fatalf("parseAgentMarkdown() error = %v", err)
}
if name != "test-agent" {
t.Errorf("parseAgentMarkdown() name = %q, want %q", name, "test-agent")
}
if agent.Description != "Test agent for testing" {
t.Errorf("parseAgentMarkdown() description = %q, want %q", agent.Description, "Test agent for testing")
}
if agent.Model != "opus" {
t.Errorf("parseAgentMarkdown() model = %q, want %q", agent.Model, "opus")
}
if !containsString(agent.Prompt, "You are a test agent") {
t.Errorf("parseAgentMarkdown() prompt should contain 'You are a test agent', got %q", agent.Prompt)
}
}
func TestBuildAgentsJSON(t *testing.T) {
tempDir := t.TempDir()
// Create agents directory
agentsDir := filepath.Join(tempDir, "agents")
if err := os.MkdirAll(agentsDir, 0755); err != nil {
t.Fatalf("Failed to create agents directory: %v", err)
}
// Create test agent files
agent1 := `---
name: agent-one
description: "First test agent"
model: sonnet
---
You are agent one.
`
agent2 := `---
name: agent-two
description: "Second test agent"
---
You are agent two.
`
if err := os.WriteFile(filepath.Join(agentsDir, "agent-one.md"), []byte(agent1), 0644); err != nil {
t.Fatalf("Failed to create agent-one.md: %v", err)
}
if err := os.WriteFile(filepath.Join(agentsDir, "agent-two.md"), []byte(agent2), 0644); err != nil {
t.Fatalf("Failed to create agent-two.md: %v", err)
}
// Build agents JSON
jsonStr, err := buildAgentsJSON(tempDir)
if err != nil {
t.Fatalf("buildAgentsJSON() error = %v", err)
}
if jsonStr == "" {
t.Fatal("buildAgentsJSON() returned empty string")
}
// Check that JSON contains expected keys
if !containsString(jsonStr, "agent-one") {
t.Error("buildAgentsJSON() JSON missing 'agent-one'")
}
if !containsString(jsonStr, "agent-two") {
t.Error("buildAgentsJSON() JSON missing 'agent-two'")
}
if !containsString(jsonStr, "First test agent") {
t.Error("buildAgentsJSON() JSON missing 'First test agent' description")
}
}
func TestBuildClaudeCLIArgs(t *testing.T) {
flags := &ClaudeFlags{
AgentsJSON: `{"test": {"description": "test"}}`,
PluginDirs: []string{"/mnt/plugins1", "/mnt/plugins2"},
SettingsJSON: `{"key": "value"}`,
}
args := buildClaudeCLIArgs(flags)
// Should have: --agents, --plugin-dir (x2), --settings
expectedCount := 8 // 2 + 4 + 2
if len(args) != expectedCount {
t.Errorf("buildClaudeCLIArgs() returned %d args, want %d", len(args), expectedCount)
}
// Check --agents flag
foundAgents := false
for i, arg := range args {
if arg == "--agents" && i+1 < len(args) {
foundAgents = true
if args[i+1] != flags.AgentsJSON {
t.Errorf("--agents value = %q, want %q", args[i+1], flags.AgentsJSON)
}
}
}
if !foundAgents {
t.Error("buildClaudeCLIArgs() missing --agents flag")
}
// Check --plugin-dir flags
pluginDirCount := 0
for _, arg := range args {
if arg == "--plugin-dir" {
pluginDirCount++
}
}
if pluginDirCount != 2 {
t.Errorf("buildClaudeCLIArgs() has %d --plugin-dir flags, want 2", pluginDirCount)
}
}
func TestGetInstalledPluginPaths(t *testing.T) {
tempDir := t.TempDir()
// Create plugins directory structure with actual plugin directories
pluginsDir := filepath.Join(tempDir, "plugins")
cacheDir := filepath.Join(pluginsDir, "cache")
require.NoError(t, os.MkdirAll(pluginsDir, 0755))
// Create the actual plugin install paths (they must exist)
plugin1Path := filepath.Join(cacheDir, "official", "test-plugin", "abc123")
plugin2Path := filepath.Join(cacheDir, "official", "other-plugin", "def456")
require.NoError(t, os.MkdirAll(plugin1Path, 0755))
require.NoError(t, os.MkdirAll(plugin2Path, 0755))
// Create installed_plugins.json
installedPlugins := InstalledPlugins{
Version: 2,
Plugins: map[string][]InstalledPluginEntry{
"test-plugin@official": {
{
Scope: "local",
ProjectPath: "/Users/testuser/project",
InstallPath: plugin1Path,
Version: "abc123",
InstalledAt: "2026-01-15T00:00:00.000Z",
LastUpdated: "2026-01-19T00:00:00.000Z",
IsLocal: true,
},
},
"other-plugin@official": {
{
Scope: "global",
InstallPath: plugin2Path,
Version: "def456",
InstalledAt: "2026-01-15T00:00:00.000Z",
LastUpdated: "2026-01-19T00:00:00.000Z",
},
},
},
}
jsonBytes, err := json.MarshalIndent(installedPlugins, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(pluginsDir, "installed_plugins.json"), jsonBytes, 0644))
// Call the function
paths, err := GetInstalledPluginPaths(tempDir)
require.NoError(t, err)
require.NotNil(t, paths)
// Check host cache path
assert.Equal(t, cacheDir, paths.HostCachePath)
// Check container paths
require.Len(t, paths.ContainerPaths, 2)
for _, cp := range paths.ContainerPaths {
assert.True(t, strings.HasPrefix(cp, PluginCacheMountPath))
}
}
func TestGetInstalledPluginPaths_NoFile(t *testing.T) {
tempDir := t.TempDir()
// No plugins directory exists
paths, err := GetInstalledPluginPaths(tempDir)
require.NoError(t, err)
assert.Nil(t, paths)
}
func TestGetInstalledPluginPaths_NoCacheDir(t *testing.T) {
tempDir := t.TempDir()
// Create plugins directory but no cache
pluginsDir := filepath.Join(tempDir, "plugins")
require.NoError(t, os.MkdirAll(pluginsDir, 0755))
// Create installed_plugins.json
installedPlugins := InstalledPlugins{
Version: 2,
Plugins: map[string][]InstalledPluginEntry{},
}
jsonBytes, err := json.MarshalIndent(installedPlugins, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(pluginsDir, "installed_plugins.json"), jsonBytes, 0644))
// Call the function - should return nil since cache dir doesn't exist
paths, err := GetInstalledPluginPaths(tempDir)
require.NoError(t, err)
assert.Nil(t, paths)
}
func TestGetInstalledPluginPaths_MissingPluginDir(t *testing.T) {
tempDir := t.TempDir()
// Create plugins directory with cache
pluginsDir := filepath.Join(tempDir, "plugins")
cacheDir := filepath.Join(pluginsDir, "cache")
require.NoError(t, os.MkdirAll(cacheDir, 0755))
// Create installed_plugins.json with a path that doesn't exist
installedPlugins := InstalledPlugins{
Version: 2,
Plugins: map[string][]InstalledPluginEntry{
"missing-plugin@official": {
{
Scope: "local",
InstallPath: filepath.Join(cacheDir, "nonexistent", "plugin"),
Version: "abc123",
InstalledAt: "2026-01-15T00:00:00.000Z",
LastUpdated: "2026-01-19T00:00:00.000Z",
},
},
},
}
jsonBytes, err := json.MarshalIndent(installedPlugins, "", " ")
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(pluginsDir, "installed_plugins.json"), jsonBytes, 0644))
// Call the function - should return nil (no valid plugins found)
paths, err := GetInstalledPluginPaths(tempDir)
require.NoError(t, err)
assert.Nil(t, paths)
}
func TestEnvName(t *testing.T) {
tests := []struct {
spec string
want string
}{
{"FOO", "FOO"},
{"FOO=bar", "FOO"},
{"FOO=", "FOO"},
{"FOO=bar=baz", "FOO"},
}
for _, tt := range tests {
t.Run(tt.spec, func(t *testing.T) {
assert.Equal(t, tt.want, EnvName(tt.spec))
})
}
}
func TestProjectConfigEnvs(t *testing.T) {
// Save and restore HOME
origHome := os.Getenv("HOME")
tempDir := t.TempDir()
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", origHome)
workspaceDir := filepath.Join(tempDir, "env-project")
require.NoError(t, os.MkdirAll(workspaceDir, 0755))
// Save project config with envs
projectConfig := &ProjectConfig{
Envs: []string{"FOO=bar", "BAZ"},
}
require.NoError(t, SaveProjectConfig(workspaceDir, projectConfig))
// Load it back
loaded, _, err := GetProjectConfig(workspaceDir)
require.NoError(t, err)
assert.Equal(t, []string{"FOO=bar", "BAZ"}, loaded.Envs)
}
func TestMergeProjectConfigEnvs(t *testing.T) {
projectConfig := &ProjectConfig{
Envs: []string{"FOO=bar", "SHARED=old"},
}
sboxFile := &SboxFileLocation{
Path: "/tmp/sbox.yaml",
Dir: "/tmp",
Config: &SboxFileConfig{
Envs: []string{"BAZ=qux", "SHARED=new_ignored"},
},
}
merged, err := MergeProjectConfig(projectConfig, sboxFile)
require.NoError(t, err)
// BAZ should be added, SHARED should NOT be duplicated (project wins)
assert.Equal(t, []string{"FOO=bar", "SHARED=old", "BAZ=qux"}, merged.Envs)
}
func TestMergeEnvs(t *testing.T) {
tests := []struct {
name string
global []string
project []string
sboxFile []string
wantMerged []string
wantSources []string // parallel to wantMerged
}{
{
name: "empty",
wantMerged: nil,
},
{
name: "global only",
global: []string{"FOO=bar"},
wantMerged: []string{"FOO=bar"},
wantSources: []string{"global"},
},
{
name: "project overrides global",
global: []string{"FOO=global", "BAZ=keep"},
project: []string{"FOO=project"},
wantMerged: []string{"FOO=project", "BAZ=keep"},
wantSources: []string{"project", "global"},
},
{
name: "sbox overrides global, project overrides sbox",
global: []string{"A=global"},
sboxFile: []string{"A=sbox", "B=sbox"},
project: []string{"B=project", "C=project"},
wantMerged: []string{"A=sbox", "B=project", "C=project"},
wantSources: []string{".sbox", "project", "project"},
},
{
name: "passthrough preserved",
global: []string{"TOKEN"},
project: []string{"DEBUG=1"},
wantMerged: []string{"TOKEN", "DEBUG=1"},
wantSources: []string{"global", "project"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
merged, resolved := MergeEnvs(tt.global, tt.project, tt.sboxFile)
assert.Equal(t, tt.wantMerged, merged)
if tt.wantSources != nil {
require.Len(t, resolved, len(tt.wantSources))
for i, r := range resolved {
assert.Equal(t, tt.wantSources[i], r.Source, "source mismatch for %s", r.Spec)
}
}
})
}
}
func TestResolveProfiles(t *testing.T) {
tests := []struct {
name string
profiles []string
want []string
}{
{
name: "no profiles",
profiles: nil,
want: nil,
},
{
name: "single profile without dependencies",
profiles: []string{"go"},
want: []string{"go"},
},
{
name: "profile with dependencies",
profiles: []string{"substreams"},
want: []string{"rust", "substreams"},
},
{
name: "multiple profiles with shared dependency",
profiles: []string{"substreams", "rust"},
want: []string{"rust", "substreams"}, // rust only appears once
},
{
name: "dependency already listed first",
profiles: []string{"rust", "substreams"},
want: []string{"rust", "substreams"},
},
{
name: "multiple independent profiles",
profiles: []string{"go", "docker"},
want: []string{"go", "docker"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &Config{}
builder := NewTemplateBuilder(config, tt.profiles, DefaultAgent)
got := builder.ResolveProfiles()
if tt.want == nil {
assert.Nil(t, got)
} else {
assert.Equal(t, tt.want, got)
}
})
}
}
func TestSubstreamsProfile(t *testing.T) {
profile, ok := GetProfile("substreams")
require.True(t, ok, "substreams profile should exist")
assert.Equal(t, "substreams", profile.Name)
assert.Contains(t, profile.Description, "Substreams")
assert.Contains(t, profile.Dependencies, "rust")
assert.Contains(t, profile.DockerfileSnippet, "ghcr.io/streamingfast/substreams")
assert.Contains(t, profile.DockerfileSnippet, "ghcr.io/streamingfast/firehose-core")
assert.Contains(t, profile.DockerfileSnippet, "buf")
assert.Contains(t, profile.DockerfileSnippet, "protoc")
}
func TestCppProfile(t *testing.T) {
profile, ok := GetProfile("cpp")
require.True(t, ok, "cpp profile should exist")
assert.Equal(t, "cpp", profile.Name)
assert.Contains(t, profile.Description, "C/C++")
assert.Empty(t, profile.Dependencies)
assert.Contains(t, profile.DockerfileSnippet, "build-essential")
assert.Contains(t, profile.DockerfileSnippet, "libboost-all-dev")
assert.Contains(t, profile.DockerfileSnippet, "libc++-dev")
assert.Contains(t, profile.DockerfileSnippet, "libstdc++")
assert.Contains(t, profile.DockerfileSnippet, "autoconf")
assert.Contains(t, profile.DockerfileSnippet, "automake")
assert.Contains(t, profile.DockerfileSnippet, "libtool")
assert.Contains(t, profile.DockerfileSnippet, "ninja-build")
assert.Contains(t, profile.DockerfileSnippet, "libzstd-dev")
assert.Contains(t, profile.DockerfileSnippet, "zlib1g-dev")
}
func TestParseSandboxLsOutput(t *testing.T) {
tests := []struct {
name string
output string
expected []DockerSandbox
}{
{
name: "single sandbox",
output: `SANDBOX AGENT STATUS WORKSPACE
claude-sbox claude running /Users/maoueh/work/sf/sbox
`,
expected: []DockerSandbox{
{
ID: "claude-sbox",
Name: "claude-sbox",
Image: "claude",
Status: "running",
Workspace: "/Users/maoueh/work/sf/sbox",
},
},
},
{
name: "empty output",
output: "SANDBOX AGENT STATUS WORKSPACE\n",
expected: nil,
},
{
name: "multiple sandboxes",
output: `SANDBOX AGENT STATUS WORKSPACE
claude-sbox claude running /Users/maoueh/work/sf/sbox
claude-substreams-benchmarks claude stopped -
sbox-claude-substreams-rs claude running -
`,
expected: []DockerSandbox{
{
ID: "claude-sbox",
Name: "claude-sbox",
Image: "claude",
Status: "running",
Workspace: "/Users/maoueh/work/sf/sbox",
},
{
ID: "claude-substreams-benchmarks",
Name: "claude-substreams-benchmarks",
Image: "claude",
Status: "stopped",
Workspace: "",
},
{
ID: "sbox-claude-substreams-rs",
Name: "sbox-claude-substreams-rs",
Image: "claude",
Status: "running",
Workspace: "",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sandboxes, err := parseSandboxLsOutput(tt.output)
require.NoError(t, err)
assert.Equal(t, tt.expected, sandboxes)
})
}
}
func TestIsSensitiveEnvName(t *testing.T) {
tests := []struct {
name string
envName string
expected bool
}{
{"API_KEY is sensitive", "API_KEY", true},
{"ANTHROPIC_API_KEY is sensitive", "ANTHROPIC_API_KEY", true},
{"api_key lowercase is sensitive", "api_key", true},
{"TOKEN is sensitive", "AUTH_TOKEN", true},
{"SECRET is sensitive", "MY_SECRET", true},
{"PASSWORD is sensitive", "DB_PASSWORD", true},