Repository navigation
Expand file tree
/
Copy pathhtml_processing_test.go
More file actions
179 lines (154 loc) · 4.62 KB
/
Copy pathhtml_processing_test.go
File metadata and controls
179 lines (154 loc) · 4.62 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
package main
import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestProcessHTMLFile(t *testing.T) {
// Set up temporary directories for testing
tempDir := t.TempDir()
config.Output = filepath.Join(tempDir, "output")
config.Originals = filepath.Join(tempDir, "originals")
config.Name = "Test Gallery"
config.Copyright = "© 2025 Test"
config.GalleryPath = "/gallery"
config.ImageOrder = "new"
// Create a mock template file
templateDir := filepath.Join(tempDir, "templates", "default")
err := os.MkdirAll(templateDir, 0755)
assert.NoError(t, err)
templateFile := filepath.Join(templateDir, "index.go.html")
err = os.WriteFile(templateFile, []byte(`
<html>
<head><title>{{.Name}}</title></head>
<body>
<h1>{{.Name}}</h1>
<ul>
{{range .Images}}
<li>{{.}}</li>
{{end}}
</ul>
<p>{{.Copyright}}</p>
</body>
</html>
`), 0644)
assert.NoError(t, err)
// Set up channels and WaitGroup
htmlTasks := make(chan Dir)
done := make(chan struct{})
var wg sync.WaitGroup
// Start the processHTMLFile function in a goroutine
wg.Add(1)
go processHTMLFile(htmlTasks, &wg, done)
// Create a mock HTML task
htmlTask := Dir{
Path: "/test",
Files: map[string]File{
"image1.jpg": {Name: "image1.jpg"},
"image2.jpg": {Name: "image2.jpg"},
},
SubDirs: map[string]SubDir{
"subdir1": {Name: "subdir1"},
},
}
// Send the task to the channel
htmlTasks <- htmlTask
close(done)
// Wait for the goroutine to finish
wg.Wait()
// Verify the output directory was created
outputDir := filepath.Join(config.Output, "test")
_, err = os.Stat(outputDir)
assert.NoError(t, err)
// Verify the output file was created
outputFile := filepath.Join(outputDir, "index.html")
_, err = os.Stat(outputFile)
assert.NoError(t, err)
// Verify the content of the output file
content, err := os.ReadFile(outputFile)
assert.NoError(t, err)
assert.Contains(t, string(content), "Test Gallery")
assert.Contains(t, string(content), "© 2025 Test")
assert.Contains(t, string(content), "image1.jpg")
assert.Contains(t, string(content), "image2.jpg")
}
func TestProcessHTMLFileWithNewestFirst(t *testing.T) {
// Set up temporary directories for testing
tempDir := t.TempDir()
config.Output = filepath.Join(tempDir, "output")
config.Originals = filepath.Join(tempDir, "originals")
config.Name = "Test Gallery"
config.Copyright = "© 2025 Test"
config.GalleryPath = "/gallery"
config.ImageOrder = "new"
// Create a mock template file
templateDir := filepath.Join(tempDir, "templates", "default")
err := os.MkdirAll(templateDir, 0755)
assert.NoError(t, err)
templateFile := filepath.Join(templateDir, "index.go.html")
err = os.WriteFile(templateFile, []byte(`
<html>
<head><title>{{.Name}}</title></head>
<body>
<h1>{{.Name}}</h1>
<ul>
{{range .Images}}
<li>{{.}}</li>
{{end}}
</ul>
<p>{{.Copyright}}</p>
</body>
</html>
`), 0644)
assert.NoError(t, err)
// Set up channels and WaitGroup
htmlTasks := make(chan Dir)
done := make(chan struct{})
var wg sync.WaitGroup
// Start the processHTMLFile function in a goroutine
wg.Add(1)
go processHTMLFile(htmlTasks, &wg, done)
// Create a mock HTML task
htmlTask := Dir{
Path: "/test",
Files: map[string]File{
"image1.jpg": {
Name: "image1.jpg",
ModTime: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
},
"image2.jpg": {
Name: "image2.jpg",
ModTime: time.Date(2025, 2, 1, 0, 0, 0, 0, time.UTC),
},
},
SubDirs: map[string]SubDir{
"subdir1": {Name: "subdir1"},
},
}
// Send the task to the channel
htmlTasks <- htmlTask
close(done)
// Wait for the goroutine to finish
wg.Wait()
// Verify the output directory was created
outputDir := filepath.Join(config.Output, "test")
_, err = os.Stat(outputDir)
assert.NoError(t, err)
// Verify the output file was created
outputFile := filepath.Join(outputDir, "index.html")
_, err = os.Stat(outputFile)
assert.NoError(t, err)
// Verify the content of the output file
content, err := os.ReadFile(outputFile)
assert.NoError(t, err)
assert.Contains(t, string(content), "Test Gallery")
assert.Contains(t, string(content), "© 2025 Test")
assert.Contains(t, string(content), "image1.jpg")
assert.Contains(t, string(content), "image2.jpg")
// Verify the order of images (newest first)
assert.True(t, strings.Index(string(content), "image2.jpg") < strings.Index(string(content), "image1.jpg"))
}