mirror of
https://github.com/sst/opencode.git
synced 2025-08-04 13:30:52 +00:00
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package prompt
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/sst/opencode/internal/config"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetContextFromPaths(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
lvl := new(slog.LevelVar)
|
|
lvl.Set(slog.LevelDebug)
|
|
|
|
tmpDir := t.TempDir()
|
|
_, err := config.Load(tmpDir, false, lvl)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load config: %v", err)
|
|
}
|
|
cfg := config.Get()
|
|
cfg.WorkingDir = tmpDir
|
|
cfg.ContextPaths = []string{
|
|
"file.txt",
|
|
"directory/",
|
|
}
|
|
testFiles := []string{
|
|
"file.txt",
|
|
"directory/file_a.txt",
|
|
"directory/file_b.txt",
|
|
"directory/file_c.txt",
|
|
}
|
|
|
|
createTestFiles(t, tmpDir, testFiles)
|
|
|
|
context := getContextFromPaths()
|
|
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)
|
|
assert.Equal(t, expectedContext, context)
|
|
}
|
|
|
|
func createTestFiles(t *testing.T, tmpDir string, testFiles []string) {
|
|
t.Helper()
|
|
for _, path := range testFiles {
|
|
fullPath := filepath.Join(tmpDir, path)
|
|
if path[len(path)-1] == '/' {
|
|
err := os.MkdirAll(fullPath, 0755)
|
|
require.NoError(t, err)
|
|
} else {
|
|
dir := filepath.Dir(fullPath)
|
|
err := os.MkdirAll(dir, 0755)
|
|
require.NoError(t, err)
|
|
err = os.WriteFile(fullPath, []byte(path+": test content"), 0644)
|
|
require.NoError(t, err)
|
|
}
|
|
}
|
|
}
|