|
| 1 | +package prompt |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/opencode-ai/opencode/internal/config" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestGetContextFromPaths(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + tmpDir := t.TempDir() |
| 18 | + _, err := config.Load(tmpDir, false) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Failed to load config: %v", err) |
| 21 | + } |
| 22 | + cfg := config.Get() |
| 23 | + cfg.WorkingDir = tmpDir |
| 24 | + cfg.ContextPaths = []string{ |
| 25 | + "file.txt", |
| 26 | + "directory/", |
| 27 | + } |
| 28 | + testFiles := []string{ |
| 29 | + "file.txt", |
| 30 | + "directory/file_a.txt", |
| 31 | + "directory/file_b.txt", |
| 32 | + "directory/file_c.txt", |
| 33 | + } |
| 34 | + |
| 35 | + createTestFiles(t, tmpDir, testFiles) |
| 36 | + |
| 37 | + context := getContextFromPaths() |
| 38 | + expectedContext := fmt.Sprintf("# From:%s/file.txt\nfile.txt: test content\n# From:%s/directory/file_a.txt\ndirectory/file_a.txt: test content\n# From:%s/directory/file_b.txt\ndirectory/file_b.txt: test content\n# From:%s/directory/file_c.txt\ndirectory/file_c.txt: test content", tmpDir, tmpDir, tmpDir, tmpDir) |
| 39 | + assert.Equal(t, expectedContext, context) |
| 40 | +} |
| 41 | + |
| 42 | +func createTestFiles(t *testing.T, tmpDir string, testFiles []string) { |
| 43 | + t.Helper() |
| 44 | + for _, path := range testFiles { |
| 45 | + fullPath := filepath.Join(tmpDir, path) |
| 46 | + if path[len(path)-1] == '/' { |
| 47 | + err := os.MkdirAll(fullPath, 0755) |
| 48 | + require.NoError(t, err) |
| 49 | + } else { |
| 50 | + dir := filepath.Dir(fullPath) |
| 51 | + err := os.MkdirAll(dir, 0755) |
| 52 | + require.NoError(t, err) |
| 53 | + err = os.WriteFile(fullPath, []byte(path+": test content"), 0644) |
| 54 | + require.NoError(t, err) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments