-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit_test.go
More file actions
225 lines (194 loc) · 5.38 KB
/
Copy pathlimit_test.go
File metadata and controls
225 lines (194 loc) · 5.38 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
package main
import (
"os"
"strings"
"testing"
)
func TestGenerateOutputFilename_URL(t *testing.T) {
tests := []struct {
name string
url string
format string
expected string
}{
{
name: "simple URL with markdown",
url: "https://example.com",
format: "markdown",
expected: "example-com.md",
},
{
name: "URL with path",
url: "https://example.com/foo/bar",
format: "markdown",
expected: "example-com-foo-bar.md",
},
{
name: "URL with HTML format",
url: "https://example.com/test",
format: "html",
expected: "example-com-test.html",
},
{
name: "URL with trailing slash",
url: "https://example.com/foo/",
format: "markdown",
expected: "example-com-foo.md",
},
{
name: "URL with query params",
url: "https://example.com/search?q=test&page=1",
format: "markdown",
expected: "example-com-search-q-test-page-1.md",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GenerateOutputFilename(tt.url, "", tt.format)
if result != tt.expected {
t.Errorf("GenerateOutputFilename() = %v, want %v", result, tt.expected)
}
})
}
}
func TestGenerateOutputFilename_File(t *testing.T) {
tests := []struct {
name string
filePath string
format string
expected string
}{
{
name: "simple filename",
filePath: "test.html",
format: "markdown",
expected: "test-cleaned.md",
},
{
name: "file with path",
filePath: "/path/to/file.html",
format: "markdown",
expected: "file-cleaned.md",
},
{
name: "file with HTML format",
filePath: "myfile.html",
format: "html",
expected: "myfile-cleaned.html",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GenerateOutputFilename("", tt.filePath, tt.format)
if result != tt.expected {
t.Errorf("GenerateOutputFilename() = %v, want %v", result, tt.expected)
}
})
}
}
func TestProcessInput_OutputLimit_NotExceeded(t *testing.T) {
tmpfile, err := os.CreateTemp("", "small*.html")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := "<html><body><p>Small content</p></body></html>"
if _, err := tmpfile.Write([]byte(content)); err != nil {
t.Fatal(err)
}
tmpfile.Close()
config := Config{
File: tmpfile.Name(),
Format: "markdown",
MaxTokens: 100000, // Much larger than content
}
result := processInput(config)
if result.Error != "" {
t.Errorf("processInput() error = %v, want nil", result.Error)
}
// Content should be returned directly
if !strings.Contains(result.Content, "Small content") {
t.Error("processInput() should return content directly when under limit")
}
// Should NOT contain "Output exceeded limit"
if strings.Contains(result.Content, "Output exceeded limit") {
t.Error("processInput() should not write to file when under limit")
}
}
func TestProcessInput_OutputLimit_Exceeded(t *testing.T) {
tmpfile, err := os.CreateTemp("", "large*.html")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
// Create large content (> 300 bytes for 100 token limit)
largeContent := "<html><body>"
for i := 0; i < 100; i++ {
largeContent += "<p>This is a very long paragraph with lots of content to exceed the token limit. "
largeContent += "We need to make sure this content is large enough to trigger file writing.</p>"
}
largeContent += "</body></html>"
if _, err := tmpfile.Write([]byte(largeContent)); err != nil {
t.Fatal(err)
}
tmpfile.Close()
config := Config{
File: tmpfile.Name(),
Format: "markdown",
MaxTokens: 100, // Very small limit (300 bytes)
}
result := processInput(config)
if result.Error != "" {
t.Errorf("processInput() error = %v, want nil", result.Error)
}
// processInput should signal over-limit without doing I/O
if !result.OverLimit {
t.Error("processInput() should set OverLimit = true when content exceeds limit")
}
if result.TokenCount <= 0 {
t.Error("processInput() should set TokenCount > 0 when over limit")
}
if result.RawContent == "" {
t.Error("processInput() should set RawContent when over limit")
}
if !strings.Contains(result.RawContent, "very long paragraph") {
t.Error("processInput() RawContent should contain the cleaned content")
}
// Content should be empty — callers handle file-write
if result.Content != "" {
t.Errorf("processInput() Content should be empty when over limit, got: %s", result.Content)
}
}
func TestProcessInput_OutputLimit_URLFilename(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test*.html")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
// Create content that exceeds limit
content := "<html><body>"
for i := 0; i < 50; i++ {
content += "<p>Content paragraph " + string(rune('0'+i%10)) + " with some text.</p>"
}
content += "</body></html>"
if _, err := tmpfile.Write([]byte(content)); err != nil {
t.Fatal(err)
}
tmpfile.Close()
config := Config{
File: tmpfile.Name(),
Format: "markdown",
MaxTokens: 50, // Small limit
}
result := processInput(config)
if result.Error != "" {
t.Errorf("processInput() error = %v, want nil", result.Error)
}
// processInput should signal over-limit
if !result.OverLimit {
t.Error("processInput() should set OverLimit = true")
}
if result.RawContent == "" {
t.Error("processInput() should set RawContent when over limit")
}
}