-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembed_test.go
More file actions
289 lines (264 loc) · 9.52 KB
/
Copy pathembed_test.go
File metadata and controls
289 lines (264 loc) · 9.52 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
package waxtap
import (
"bytes"
"context"
"fmt"
"image/color"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/colespringer/waxlabel"
"github.com/colespringer/waxlabel/tag"
"github.com/colespringer/waxtap/v3/internal/coverart"
"github.com/colespringer/waxtap/v3/internal/cutrange"
"github.com/colespringer/waxtap/v3/internal/media"
"github.com/colespringer/waxtap/v3/internal/mediatest"
"github.com/colespringer/waxtap/v3/youtube"
)
// coverServer serves body at /cover.jpg and returns a Client pointed at it,
// along with a Video whose ladder already reaches 1280x720 so the walk stays
// local: a shorter ladder would prepend i.ytimg.com probes and leave the test
// reaching the real network.
func coverServer(t *testing.T, body []byte) (*Client, *youtube.Video) {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write(body)
}))
t.Cleanup(srv.Close)
c, err := New(Options{HTTPClient: srv.Client()})
if err != nil {
t.Fatal(err)
}
v := &youtube.Video{
ID: "dummyVideo0",
Title: "Fixture Track",
Author: "Fixture Channel",
Thumbnails: []youtube.Thumbnail{{URL: srv.URL + "/cover.jpg", Width: 1280, Height: 720}},
}
return c, v
}
// webpBytes is a RIFF/WEBP header. waxlabel.IsRecognizedImage accepts it, so it
// reaches the square step, where the stdlib cannot decode it.
var webpBytes = append([]byte("RIFF"), append([]byte{0x20, 0, 0, 0}, []byte("WEBP")...)...)
func TestCoverPictureFrameIsByteIdentical(t *testing.T) {
// The default mode must not spend a re-encode generation on a feature nobody
// asked for: the embedded bytes are what the CDN sent.
sent := mediatest.JPEGBytes(mediatest.ArtTrackCover(1280, 720, 720, color.RGBA{A: 255}), 90)
c, v := coverServer(t, sent)
img, note, err := c.coverPicture(context.Background(), v, CoverArtFrame)
if err != nil || note != "" {
t.Fatalf("coverPicture = %v, %q; want no error and no note", err, note)
}
if !bytes.Equal(img, sent) {
t.Error("CoverArtFrame must embed the delivered bytes verbatim")
}
}
func TestCoverPictureDegradesOnAnUndecodableImage(t *testing.T) {
c, v := coverServer(t, webpBytes)
img, note, err := c.coverPicture(context.Background(), v, CoverArtSquare)
if err != nil {
t.Fatalf("an unshapeable picture is still worth embedding: %v", err)
}
if !bytes.Equal(img, webpBytes) {
t.Error("a failed shape must fall back to the fetched bytes")
}
if note == "" {
t.Error("a degraded shape must be reported, never silent")
}
}
func TestDoEmbedReportsAnUnshapeablePicture(t *testing.T) {
c, v := coverServer(t, webpBytes)
path := writeWAVFixture(t)
skip, err := c.doEmbed(context.Background(), path, "wav", v,
embedOptions{thumbnail: true, coverArt: CoverArtSquare})
if err != nil {
t.Fatalf("doEmbed: %v", err)
}
if skip == "" || !strings.Contains(skip, "unshaped") {
t.Errorf("skipReason = %q, want a report that the picture went in unshaped", skip)
}
}
func TestDoEmbedSquaresWithoutWarning(t *testing.T) {
sent := mediatest.JPEGBytes(mediatest.ArtTrackCover(1280, 720, 720, color.RGBA{A: 255}), 90)
c, v := coverServer(t, sent)
path := writeWAVFixture(t)
skip, err := c.doEmbed(context.Background(), path, "wav", v,
embedOptions{thumbnail: true, metadata: true, coverArt: CoverArtSquare})
if err != nil {
t.Fatalf("doEmbed: %v", err)
}
// The over-trim guard and the near-square fast path both deliver a picture the
// user asked for, so nothing on the success path may warn.
if skip != "" {
t.Errorf("skipReason = %q, want empty", skip)
}
// The picture actually landed, and landed square.
out, rerr := os.ReadFile(path)
if rerr != nil {
t.Fatal(rerr)
}
pic := embeddedJPEG(out)
if pic == nil {
t.Fatal("no JPEG cover picture found in the tagged file")
}
w, h, derr := coverart.Dimensions(pic)
if derr != nil {
t.Fatalf("embedded picture: %v", derr)
}
if w != h || w < 716 || w > 724 {
t.Errorf("embedded cover = %dx%d, want a square within 4px of the 720px art", w, h)
}
}
// TestDoEmbedWritesCoverArtIntoWavPack pins the APEv2 arm of the embed pass:
// cover art and tags land in a .wv file's APEv2 block (a Cover Art item), the
// post-pass that a mux-time special case used to bypass, and the chapters
// APEv2 cannot hold are reported rather than skipped silently.
func TestDoEmbedWritesCoverArtIntoWavPack(t *testing.T) {
ctx := context.Background()
sent := mediatest.JPEGBytes(mediatest.ArtTrackCover(1280, 720, 720, color.RGBA{A: 255}), 90)
c, v := coverServer(t, sent)
v.Chapters = []youtube.Chapter{{Title: "One"}, {Start: time.Second, Title: "Two"}}
wav := writeWAVFixture(t)
path := filepath.Join(t.TempDir(), "track.wv")
if _, err := c.engine().Transcode(ctx, wav, path, media.Spec{Codec: media.CodecWavPack}); err != nil {
t.Fatalf("synth wv: %v", err)
}
skip, err := c.doEmbed(ctx, path, "wv", v, embedOptions{thumbnail: true, metadata: true})
if err != nil {
t.Fatalf("doEmbed: %v", err)
}
if !strings.Contains(skip, "chapters cannot be embedded in a WavPack file") {
t.Errorf("skipReason = %q, want the chapter skip reported", skip)
}
doc, err := waxlabel.ParseFile(ctx, path)
if err != nil {
t.Fatalf("parse tagged wv: %v", err)
}
if vs, _ := doc.Get(tag.Title); len(vs) == 0 || vs[0] != "Fixture Track" {
t.Errorf("TITLE = %v, want the video title", vs)
}
pics := doc.Pictures()
if len(pics) != 1 {
t.Fatalf("pictures = %d, want the embedded cover", len(pics))
}
if !bytes.Equal(pics[0].Data, sent) {
t.Error("cover bytes differ from the delivered thumbnail (frame mode embeds verbatim)")
}
}
// TestPictureCapableExt pins the delivered-extension gate. The false rows are
// the names a keep-source download can put mismatched bytes under, where the
// cover-art remux to the codec's native container would misname the result.
func TestPictureCapableExt(t *testing.T) {
exts := append([]string{"webm", "aac", "wav", "aiff", "wv", "ape", "mka", "mkv"}, media.DecodeOnlyExts()...)
for _, ext := range exts {
if pictureCapableExt(ext) {
t.Errorf("pictureCapableExt(%q) = true, want false", ext)
}
}
for _, ext := range []string{"", "opus", "ogg", "flac", "mp3", "m4a"} {
if !pictureCapableExt(ext) {
t.Errorf("pictureCapableExt(%q) = false, want true", ext)
}
}
}
// TestDiscardNotes pins the write-warning relay: discard warnings surface,
// advisory ones stay silent, and a long list is capped.
func TestDiscardNotes(t *testing.T) {
if got := discardNotes(nil); got != "" {
t.Errorf("no warnings = %q, want empty", got)
}
ws := []waxlabel.Warning{
{Code: waxlabel.WarnValueDropped, Message: "d1"},
{Code: waxlabel.WarnFragmented, Message: "advisory"},
{Code: waxlabel.WarnLegacyStripDropped, Message: "d2"},
}
if got := discardNotes(ws); got != "d1; d2" {
t.Errorf("discardNotes = %q, want %q", got, "d1; d2")
}
var many []waxlabel.Warning
for i := range 5 {
many = append(many, waxlabel.Warning{Code: waxlabel.WarnValueDropped, Message: fmt.Sprintf("m%d", i)})
}
if got := discardNotes(many); got != "m0; m1; m2; and 2 more" {
t.Errorf("capped = %q, want %q", got, "m0; m1; m2; and 2 more")
}
}
// writeWAVFixture stages a short WAV, a container WaxLabel writes pictures into
// and WaxFlow probes without a remux.
func writeWAVFixture(t *testing.T) string {
t.Helper()
path := filepath.Join(t.TempDir(), "track.wav")
if err := os.WriteFile(path, mediatest.SineWAV(1, 2), 0o644); err != nil {
t.Fatal(err)
}
return path
}
// embeddedJPEG returns the first embedded JPEG in a tagged file, found by its
// SOI/EOI markers. It avoids re-parsing the container just to read one picture.
func embeddedJPEG(data []byte) []byte {
start := bytes.Index(data, []byte{0xFF, 0xD8, 0xFF})
if start < 0 {
return nil
}
end := bytes.LastIndex(data[start:], []byte{0xFF, 0xD9})
if end < 0 {
return nil
}
return data[start : start+end+2]
}
// TestDoEmbedRemapsChaptersThroughCut pins the download-side chapter fix: with
// a rendered cut attached, --embed-metadata writes chapter marks on the
// delivered timeline, not the source's. Removing [1s,2s) from a 3s track
// erases chapter Two's content and shifts Three from 2s to 1s.
func TestDoEmbedRemapsChaptersThroughCut(t *testing.T) {
ctx := context.Background()
c := newOfflineClient(t)
dir := t.TempDir()
wav := filepath.Join(dir, "in.wav")
if err := os.WriteFile(wav, mediatest.SineWAV(3, 2), 0o644); err != nil {
t.Fatal(err)
}
flac := filepath.Join(dir, "in.flac")
if _, err := c.Process(ctx, ProcessRequest{
Input: wav,
ProcessSpec: ProcessSpec{
Output: ToFile(flac),
Transcode: &TranscodeSpec{Format: FormatFLAC},
},
}); err != nil {
t.Fatalf("fixture transcode: %v", err)
}
v := &youtube.Video{
Title: "Fixture Track",
Chapters: []youtube.Chapter{
{Start: 0, Title: "One"},
{Start: 1 * time.Second, Title: "Two"},
{Start: 2 * time.Second, Title: "Three"},
},
}
cut := &appliedCut{
keeps: []cutrange.Range{{Start: 0, End: 1 * time.Second}, {Start: 2 * time.Second, End: 3 * time.Second}},
total: 3 * time.Second,
}
if _, err := c.doEmbed(ctx, flac, "flac", v, embedOptions{metadata: true, cut: cut}); err != nil {
t.Fatalf("doEmbed: %v", err)
}
doc, err := waxlabel.ParseFile(ctx, flac)
if err != nil {
t.Fatalf("parse output: %v", err)
}
chs := doc.Chapters()
if len(chs) != 2 {
t.Fatalf("chapters = %+v, want One and Three", chs)
}
if chs[0].Title != "One" || chs[0].Start != 0 {
t.Errorf("chapter 0 = %q@%v, want One@0", chs[0].Title, chs[0].Start)
}
if chs[1].Title != "Three" || chs[1].Start != 1*time.Second {
t.Errorf("chapter 1 = %q@%v, want Three@1s", chs[1].Title, chs[1].Start)
}
}