-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_test.go
More file actions
205 lines (175 loc) · 4.49 KB
/
Copy pathpreview_test.go
File metadata and controls
205 lines (175 loc) · 4.49 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestNewPreviewService(t *testing.T) {
// Default max lines
p := NewPreviewService(0)
if p.maxLines != 500 {
t.Errorf("expected maxLines=500 for 0 input, got %d", p.maxLines)
}
p = NewPreviewService(-1)
if p.maxLines != 500 {
t.Errorf("expected maxLines=500 for -1 input, got %d", p.maxLines)
}
// Custom max lines
p = NewPreviewService(100)
if p.maxLines != 100 {
t.Errorf("expected maxLines=100, got %d", p.maxLines)
}
// Style and formatter should not be nil
if p.style == nil {
t.Error("style should not be nil")
}
if p.formatter == nil {
t.Error("formatter should not be nil")
}
}
func TestReadLinesLimited(t *testing.T) {
dir := t.TempDir()
// Create a test file with known content
lines := []string{"line1", "line2", "line3", "line4", "line5"}
content := strings.Join(lines, "\n")
path := filepath.Join(dir, "test.txt")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
// Read all lines
got, truncated, err := readLinesLimited(path, 100)
if err != nil {
t.Fatal(err)
}
if truncated {
t.Error("should not be truncated")
}
if len(got) != 5 {
t.Errorf("expected 5 lines, got %d", len(got))
}
// Read with limit
got, truncated, err = readLinesLimited(path, 3)
if err != nil {
t.Fatal(err)
}
if !truncated {
t.Error("should be truncated")
}
if len(got) != 3 {
t.Errorf("expected 3 lines, got %d", len(got))
}
// Non-existent file
_, _, err = readLinesLimited(filepath.Join(dir, "nonexistent.txt"), 100)
if err == nil {
t.Error("expected error for non-existent file")
}
}
func TestGenerateBinaryFile(t *testing.T) {
dir := t.TempDir()
p := NewPreviewService(500)
// Create a fake binary file
path := filepath.Join(dir, "test.bin")
if err := os.WriteFile(path, []byte{0x00, 0x01, 0x02}, 0644); err != nil {
t.Fatal(err)
}
result, err := p.Generate(path, 80)
if err != nil {
t.Fatal(err)
}
if result != "[Binary file - no preview]" {
t.Errorf("expected binary file message, got: %s", result)
}
}
func TestGenerateTextFile(t *testing.T) {
dir := t.TempDir()
p := NewPreviewService(500)
// Create a Go source file
content := `package main
import "fmt"
func main() {
fmt.Println("hello")
}
`
path := filepath.Join(dir, "main.go")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
result, err := p.Generate(path, 80)
if err != nil {
t.Fatal(err)
}
if result == "" {
t.Error("preview should not be empty")
}
if result == "[Binary file - no preview]" {
t.Error("Go file should not be detected as binary")
}
}
func TestGeneratePlainFile(t *testing.T) {
dir := t.TempDir()
p := NewPreviewService(500)
// Create a plain text file (no syntax highlighting)
content := "hello world\nfoo bar"
path := filepath.Join(dir, "notes.txt")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
result, err := p.Generate(path, 80)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(result, "hello world") {
t.Errorf("preview should contain file content, got: %s", result)
}
}
func TestGenerateTruncated(t *testing.T) {
dir := t.TempDir()
p := NewPreviewService(3) // Only 3 lines
// Create a file with 5 lines
content := "line1\nline2\nline3\nline4\nline5"
path := filepath.Join(dir, "long.txt")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
result, err := p.Generate(path, 80)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(result, "truncated") {
t.Error("should contain truncation notice")
}
}
func TestGenerateNonExistent(t *testing.T) {
p := NewPreviewService(500)
result, err := p.Generate("/nonexistent/path/file.txt", 80)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(result, "Error") {
t.Errorf("should contain error message, got: %s", result)
}
}
func TestSupportedLangs(t *testing.T) {
expected := []string{".go", ".py", ".js", ".ts", ".rs", ".c", ".cpp", ".h", ".hpp", ".java", ".md"}
for _, lang := range expected {
if !supportedLangs[lang] {
t.Errorf("expected %s to be supported", lang)
}
}
}
func TestGenerateAsync(t *testing.T) {
dir := t.TempDir()
p := NewPreviewService(500)
// Create a test file
content := "hello world"
path := filepath.Join(dir, "test.txt")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
// GenerateAsync should return a function
cmd := p.GenerateAsync(path, 80)
if cmd == nil {
t.Error("GenerateAsync returned nil cmd")
}
}