-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
351 lines (283 loc) · 8.26 KB
/
Copy pathintegration_test.go
File metadata and controls
351 lines (283 loc) · 8.26 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/danjdewhurst/go-toc/internal/parser"
"github.com/danjdewhurst/go-toc/internal/scanner"
"github.com/danjdewhurst/go-toc/internal/toc"
"github.com/danjdewhurst/go-toc/internal/worker"
)
// TestIntegration tests the full workflow from scanning to generation.
func TestIntegration(t *testing.T) {
tmpDir := setupIntegrationTestDir(t)
defer os.RemoveAll(tmpDir)
// 1. Scan directory
scanConfig := scanner.Config{
RootPath: tmpDir,
UseGitignore: true,
MaxDepth: 0,
}
s := scanner.New(scanConfig)
tree, err := s.Scan()
if err != nil {
t.Fatalf("scan failed: %v", err)
}
// 2. Get markdown files for summary extraction
files, err := s.GetMarkdownFiles()
if err != nil {
t.Fatalf("GetMarkdownFiles failed: %v", err)
}
// Verify we found the expected files (not ignored ones)
if len(files) < 3 {
t.Errorf("expected at least 3 markdown files, got %d", len(files))
}
// Verify ignored files are not included
for _, f := range files {
if strings.Contains(f, "ignored") || strings.Contains(f, "node_modules") {
t.Errorf("should not include ignored file: %s", f)
}
}
// 3. Extract summaries using worker pool
jobs := make([]worker.Job, len(files))
for i, file := range files {
jobs[i] = worker.Job{FilePath: file, Data: 100}
}
processFunc := func(job worker.Job) worker.Result {
maxChars := job.Data.(int)
summary, err := parser.ExtractSummary(job.FilePath, maxChars)
return worker.Result{
FilePath: job.FilePath,
Summary: summary,
Error: err,
}
}
results := worker.ProcessAll(jobs, 4, processFunc)
// Verify summaries were extracted
summaryCount := 0
for _, r := range results {
if r.Error == nil && r.Summary != "" {
summaryCount++
}
}
if summaryCount == 0 {
t.Error("expected at least one summary to be extracted")
}
// 4. Convert results to summaries map
summaries := make(map[string]string)
for filePath, result := range results {
if result.Error == nil && result.Summary != "" {
relPath, _ := filepath.Rel(tmpDir, filePath)
summaries[relPath] = result.Summary
}
}
// 5. Generate ToC
genConfig := toc.GeneratorConfig{
Title: "Integration Test ToC",
IncludeSummary: true,
Summaries: summaries,
}
gen := toc.NewGenerator(genConfig)
output := gen.Generate(tree)
// 6. Verify output
expectedStrings := []string{
"# Integration Test ToC",
"README.md",
"docs/",
"guide.md",
"├──",
"└──",
}
for _, expected := range expectedStrings {
if !strings.Contains(output, expected) {
t.Errorf("output should contain %q", expected)
}
}
// Should contain at least one summary blockquote
if !strings.Contains(output, ">") {
t.Error("output should contain summary blockquotes")
}
// Should not contain ignored content
if strings.Contains(output, "ignored") {
t.Error("output should not contain ignored files")
}
if strings.Contains(output, "node_modules") {
t.Error("output should not contain node_modules")
}
}
// TestIntegrationSingleThreaded tests single-threaded processing.
func TestIntegrationSingleThreaded(t *testing.T) {
tmpDir := setupIntegrationTestDir(t)
defer os.RemoveAll(tmpDir)
scanConfig := scanner.Config{
RootPath: tmpDir,
}
s := scanner.New(scanConfig)
files, err := s.GetMarkdownFiles()
if err != nil {
t.Fatalf("GetMarkdownFiles failed: %v", err)
}
jobs := make([]worker.Job, len(files))
for i, file := range files {
jobs[i] = worker.Job{FilePath: file, Data: 50}
}
processFunc := func(job worker.Job) worker.Result {
maxChars := job.Data.(int)
summary, err := parser.ExtractSummary(job.FilePath, maxChars)
return worker.Result{
FilePath: job.FilePath,
Summary: summary,
Error: err,
}
}
// Single-threaded processing
results := worker.ProcessSequential(jobs, processFunc)
if len(results) != len(files) {
t.Errorf("expected %d results, got %d", len(files), len(results))
}
}
// TestIntegrationFrontmatter tests that frontmatter is properly skipped.
func TestIntegrationFrontmatter(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "go-toc-frontmatter-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Create file with frontmatter
content := `---
title: Test Document
date: 2024-01-01
tags:
- test
- example
---
# Main Heading
This is the actual content that should be extracted as the summary.
`
if err := os.WriteFile(filepath.Join(tmpDir, "doc.md"), []byte(content), 0644); err != nil {
t.Fatal(err)
}
summary, err := parser.ExtractSummary(filepath.Join(tmpDir, "doc.md"), 100)
if err != nil {
t.Fatalf("ExtractSummary failed: %v", err)
}
if strings.Contains(summary, "title:") || strings.Contains(summary, "2024-01-01") {
t.Error("summary should not contain frontmatter content")
}
if !strings.Contains(summary, "actual content") {
t.Error("summary should contain the actual paragraph content")
}
}
// TestIntegrationEmptyDir tests handling of empty directories.
func TestIntegrationEmptyDir(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "go-toc-empty-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
scanConfig := scanner.Config{
RootPath: tmpDir,
}
s := scanner.New(scanConfig)
tree, err := s.Scan()
if err != nil {
t.Fatalf("scan failed: %v", err)
}
gen := toc.NewGenerator(toc.GeneratorConfig{Title: "Empty"})
output := gen.Generate(tree)
// Should just have the title, no tree content
if !strings.Contains(output, "# Empty") {
t.Error("output should contain title")
}
// Should not have any tree characters
if strings.Contains(output, "├──") || strings.Contains(output, "└──") {
t.Error("output should not have tree structure for empty dir")
}
}
// TestIntegrationDeepNesting tests deeply nested directories.
func TestIntegrationDeepNesting(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "go-toc-deep-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Create deeply nested structure
deepPath := filepath.Join(tmpDir, "a", "b", "c", "d", "e")
if err := os.MkdirAll(deepPath, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(deepPath, "deep.md"), []byte("# Deep\n\nDeep content."), 0644); err != nil {
t.Fatal(err)
}
scanConfig := scanner.Config{
RootPath: tmpDir,
}
s := scanner.New(scanConfig)
tree, err := s.Scan()
if err != nil {
t.Fatalf("scan failed: %v", err)
}
gen := toc.NewGenerator(toc.GeneratorConfig{Title: "Deep Test"})
output := gen.Generate(tree)
if !strings.Contains(output, "deep.md") {
t.Errorf("output should contain deeply nested file, got:\n%s", output)
}
// Should have proper tree structure
if !strings.Contains(output, "└──") {
t.Errorf("output should have tree structure, got:\n%s", output)
}
}
// Helper function to set up test directory
func setupIntegrationTestDir(t *testing.T) string {
t.Helper()
tmpDir, err := os.MkdirTemp("", "go-toc-integration-test")
if err != nil {
t.Fatal(err)
}
// Create .gitignore
gitignore := `
ignored/
node_modules/
*.log
`
writeFile(t, tmpDir, ".gitignore", gitignore)
// Create markdown files with various content
writeFile(t, tmpDir, "README.md", `# Project README
This is the main project readme with important information.
## Features
- Feature 1
- Feature 2
`)
writeFile(t, tmpDir, "docs/guide.md", `---
title: Getting Started
---
# Getting Started Guide
Welcome to the getting started guide for this project.
## Installation
Run the installer.
`)
writeFile(t, tmpDir, "docs/api/reference.md", `# API Reference
Complete API reference documentation for developers.
## Endpoints
Various endpoints are documented here.
`)
writeFile(t, tmpDir, "docs/api/examples.md", `# API Examples
Code examples showing how to use the API effectively.
`)
// Create ignored content
writeFile(t, tmpDir, "ignored/secret.md", "# Secret\n\nThis should be ignored.")
writeFile(t, tmpDir, "node_modules/pkg/readme.md", "# Package\n\nThis should be ignored.")
return tmpDir
}
func writeFile(t *testing.T, base, path, content string) {
t.Helper()
fullPath := filepath.Join(base, path)
dir := filepath.Dir(fullPath)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("failed to create directory: %v", err)
}
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
t.Fatalf("failed to write file: %v", err)
}
}