Repository navigation
Expand file tree
/
Copy pathintegration_test.go
More file actions
250 lines (234 loc) · 6.61 KB
/
Copy pathintegration_test.go
File metadata and controls
250 lines (234 loc) · 6.61 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
package deck_test
import (
"bytes"
"context"
"fmt"
"image/png"
"os"
"path/filepath"
"testing"
"time"
"github.com/corona10/goimagehash"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/k1LoW/deck"
"github.com/k1LoW/deck/md"
"github.com/lestrrat-go/backoff/v2"
)
var testCodeBlockToImageCmd = func() string {
abs, err := filepath.Abs("testdata/txt2img/main.go")
if err != nil {
return ""
}
return fmt.Sprintf("go run -mod=mod %s", abs)
}()
func TestApplyMarkdown(t *testing.T) {
if os.Getenv("TEST_INTEGRATION") == "" {
t.Skip("skipping integration test, set TEST_INTEGRATION=1 to run")
}
ctx := context.Background()
browserClose := deck.InitChrome(ctx)
t.Cleanup(browserClose)
tests := []struct {
in string
}{
{"testdata/slide.md"},
{"testdata/cap.md"},
{"testdata/br.md"},
{"testdata/list_simple.md"},
{"testdata/list_and_paragraph.md"},
{"testdata/paragraph_and_list.md"},
{"testdata/paragraphs.md"},
{"testdata/breaks_enabled.md"},
{"testdata/breaks_default.md"},
{"testdata/bold_and_italic.md"},
{"testdata/emoji.md"},
{"testdata/code.md"},
{"testdata/style.md"},
{"testdata/empty_list.md"},
{"testdata/empty_link.md"},
{"testdata/link.md"},
{"testdata/single_list.md"},
{"testdata/nested_list.md"},
{"testdata/images.md"},
{"testdata/blockquote.md"},
{"testdata/codeblock.md"},
{"testdata/defaults.md"},
{"testdata/tables.md"},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
t.Parallel()
// Acquire a presentation from the pool
presentationID := deck.AcquirePresentation(t)
b, err := os.ReadFile(tt.in)
if err != nil {
t.Fatal(err)
}
m, err := md.Parse("testdata", b, nil)
if err != nil {
t.Fatal(err)
}
fromMd, err := m.ToSlides(ctx, testCodeBlockToImageCmd)
if err != nil {
t.Fatal(err)
}
opts := append([]deck.Option{deck.WithPresentationID(presentationID)}, deck.BuildTestOptions()...)
d, err := deck.New(ctx, opts...)
if err != nil {
t.Fatal(err)
}
if err := d.DeletePageAfter(ctx, 0); err != nil {
t.Fatal(err)
}
if err := d.Apply(ctx, fromMd); err != nil {
t.Fatal(err)
}
urls := d.ListSlideURLs()
p := backoff.Exponential(
backoff.WithMinInterval(time.Second),
backoff.WithMaxInterval(5*time.Second),
backoff.WithJitterFactor(0.05),
backoff.WithMaxRetries(5),
)
for i, url := range urls {
page := i + 1
b := p.Start(ctx)
if err := func() (errr error) {
for backoff.Continue(b) {
got := deck.Screenshot(t, url)
p := fmt.Sprintf("%s-%d.golden.png", tt.in, page)
if os.Getenv("UPDATE_GOLDEN") != "" {
if err := os.WriteFile(p, got, 0600); err != nil {
t.Fatalf("failed to update golden file %s: %v", p, err)
}
return nil
}
want, err := os.ReadFile(p)
if err != nil {
t.Fatalf("failed to read golden file %s: %v", p, err)
}
gotImage, err := png.Decode(bytes.NewReader(got))
if err != nil {
t.Fatalf("failed to decode screenshot: %v", err)
}
gotPHash, err := goimagehash.PerceptionHash(gotImage)
if err != nil {
t.Fatalf("failed to compute perception hash for screenshot: %v", err)
}
wantImage, err := png.Decode(bytes.NewReader(want))
if err != nil {
t.Fatalf("failed to decode golden screenshot: %v", err)
}
wantPHash, err := goimagehash.PerceptionHash(wantImage)
if err != nil {
t.Fatalf("failed to compute perception hash for golden screenshot: %v", err)
}
distance, err := gotPHash.Distance(wantPHash)
if err != nil {
t.Fatalf("failed to compute distance between screenshots: %v", err)
}
if distance > 2 { // threshold for similarity
diffpath := fmt.Sprintf("%s-%d.diff.png", tt.in, page)
if err := os.WriteFile(diffpath, got, 0600); err != nil {
t.Fatalf("failed to write diff file %s: %v", diffpath, err)
}
errr = fmt.Errorf("screenshot %s does not match golden file %s: distance %d, see %s for diff", p, tt.in, distance, diffpath)
continue
}
return nil
}
return
}(); err != nil {
t.Error(err)
}
}
})
}
}
func TestRoundTripSlidesToGoogleSlidesPresentationAndBack(t *testing.T) {
if os.Getenv("TEST_INTEGRATION") == "" {
t.Skip("skipping integration test, set TEST_INTEGRATION=1 to run")
}
ctx := context.Background()
tests := []struct {
in string
}{
{"testdata/slide.md"},
{"testdata/cap.md"},
{"testdata/br.md"},
{"testdata/list_simple.md"},
{"testdata/list_and_paragraph.md"},
{"testdata/paragraph_and_list.md"},
{"testdata/paragraphs.md"},
//{"testdata/breaks_enabled.md"}, // FIXME: fragment merge processing is required
//{"testdata/breaks_default.md"},
{"testdata/bold_and_italic.md"},
{"testdata/emoji.md"},
{"testdata/code.md"},
//{"testdata/style.md"}, // FIXME: class is not supported yet
{"testdata/empty_list.md"},
{"testdata/empty_link.md"},
{"testdata/link.md"},
{"testdata/nested_list.md"},
{"testdata/images.md"},
//{"testdata/blockquote.md"},
//{"testdata/tables.md"},
}
cmpopts := cmp.Options{
cmpopts.IgnoreFields(deck.Fragment{}, "StyleName"),
cmpopts.IgnoreFields(deck.Slide{}, "TitleBodies", "SubtitleBodies"),
cmpopts.IgnoreUnexported(deck.Slide{}, deck.Image{}),
cmpopts.EquateEmpty(),
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
t.Parallel()
// Acquire a presentation from the pool
presentationID := deck.AcquirePresentation(t)
b, err := os.ReadFile(tt.in)
if err != nil {
t.Fatal(err)
}
m, err := md.Parse("testdata", b, nil)
if err != nil {
t.Fatal(err)
}
base, err := m.ToSlides(ctx, "")
if err != nil {
t.Fatal(err)
}
opts := append([]deck.Option{deck.WithPresentationID(presentationID)}, deck.BuildTestOptions()...)
d, err := deck.New(ctx, opts...)
if err != nil {
t.Fatal(err)
}
// Clear existing slides before applying new ones
if err := d.DeletePageAfter(ctx, 0); err != nil {
t.Fatal(err)
}
if err := d.Apply(ctx, base); err != nil {
t.Fatal(err)
}
applied, err := d.DumpSlides(ctx)
if err != nil {
t.Fatal(err)
}
if !base.Equal(applied) {
diff := cmp.Diff(base, applied, cmpopts...)
t.Errorf("slides after apply do not match base: %s", diff)
}
if err := d.Apply(ctx, applied); err != nil {
t.Fatal(err)
}
applied2, err := d.DumpSlides(ctx)
if err != nil {
t.Fatal(err)
}
if !applied.Equal(applied2) {
diff := cmp.Diff(applied, applied2, cmpopts...)
t.Errorf("slides after apply do not match base: %s", diff)
}
})
}
}