1package prompt
2
3import (
4 "os"
5 "path/filepath"
6 "runtime"
7 "strings"
8 "testing"
9)
10
11func TestExpandPath(t *testing.T) {
12 tests := []struct {
13 name string
14 input string
15 expected func() string
16 }{
17 {
18 name: "regular path unchanged",
19 input: "/absolute/path",
20 expected: func() string {
21 return "/absolute/path"
22 },
23 },
24 {
25 name: "tilde expansion",
26 input: "~/documents",
27 expected: func() string {
28 home, _ := os.UserHomeDir()
29 return filepath.Join(home, "documents")
30 },
31 },
32 {
33 name: "tilde only",
34 input: "~",
35 expected: func() string {
36 home, _ := os.UserHomeDir()
37 return home
38 },
39 },
40 {
41 name: "environment variable expansion",
42 input: "$HOME",
43 expected: func() string {
44 return os.Getenv("HOME")
45 },
46 },
47 {
48 name: "relative path unchanged",
49 input: "relative/path",
50 expected: func() string {
51 return "relative/path"
52 },
53 },
54 }
55
56 for _, tt := range tests {
57 t.Run(tt.name, func(t *testing.T) {
58 result := expandPath(tt.input)
59 expected := tt.expected()
60
61 // Skip test if environment variable is not set
62 if strings.HasPrefix(tt.input, "$") && expected == "" {
63 t.Skip("Environment variable not set")
64 }
65
66 if result != expected {
67 t.Errorf("expandPath(%q) = %q, want %q", tt.input, result, expected)
68 }
69 })
70 }
71}
72
73func TestProcessContextPaths(t *testing.T) {
74 // Create a temporary directory and file for testing
75 tmpDir := t.TempDir()
76 testFile := filepath.Join(tmpDir, "test.txt")
77 testContent := "test content"
78
79 err := os.WriteFile(testFile, []byte(testContent), 0o644)
80 if err != nil {
81 t.Fatalf("Failed to create test file: %v", err)
82 }
83
84 // Test with absolute path to file
85 result := processContextPaths("", []string{testFile})
86 expected := "# From:" + testFile + "\n" + testContent
87
88 if result != expected {
89 t.Errorf("processContextPaths with absolute path failed.\nGot: %q\nWant: %q", result, expected)
90 }
91
92 // Test with directory path (should process all files in directory)
93 result = processContextPaths("", []string{tmpDir})
94 if !strings.Contains(result, testContent) {
95 t.Errorf("processContextPaths with directory path failed to include file content")
96 }
97
98 // Test with tilde expansion (if we can create a file in home directory)
99 tmpDir = t.TempDir()
100 setHomeEnv(t, tmpDir)
101 homeTestFile := filepath.Join(tmpDir, "crush_test_file.txt")
102 err = os.WriteFile(homeTestFile, []byte(testContent), 0o644)
103 if err == nil {
104 defer os.Remove(homeTestFile) // Clean up
105
106 tildeFile := "~/crush_test_file.txt"
107 result = processContextPaths("", []string{tildeFile})
108 expected = "# From:" + homeTestFile + "\n" + testContent
109
110 if result != expected {
111 t.Errorf("processContextPaths with tilde expansion failed.\nGot: %q\nWant: %q", result, expected)
112 }
113 }
114}
115
116func setHomeEnv(tb testing.TB, path string) {
117 tb.Helper()
118 key := "HOME"
119 if runtime.GOOS == "windows" {
120 key = "USERPROFILE"
121 }
122 tb.Setenv(key, path)
123}