-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcue_test.go
More file actions
656 lines (545 loc) · 19.4 KB
/
Copy pathcue_test.go
File metadata and controls
656 lines (545 loc) · 19.4 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
package xtractr_test
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"unicode/utf16"
"github.com/mewkiz/flac"
"github.com/mewkiz/flac/meta"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golift.io/xtractr"
)
func TestCueParseCueSheet(t *testing.T) {
t.Parallel()
assert.True(t, xtractr.IsArchiveFile("test.cue"), ".cue should be recognized as an archive file")
assert.True(t, xtractr.IsArchiveFile("TEST.CUE"), ".CUE (uppercase) should be recognized")
assert.True(t, xtractr.IsArchiveFile("album.cue.txt"), ".cue.txt should be recognized as an archive file")
assert.True(t, xtractr.IsArchiveFile("ALBUM.CUE.TXT"), ".CUE.TXT (uppercase) should be recognized")
}
func TestCueExtractCUE(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(3 * 60 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
// Open the file ourselves and close it explicitly. flac.Open/ParseFile wrap
// the reader in bufio.NewReader which prevents Stream.Close from releasing
// the file handle (the io.Closer interface is lost by the wrapper).
verifyFile, err := os.Open(flacPath)
require.NoError(t, err, "opening generated FLAC file for verification")
verifyStream, err := flac.New(verifyFile)
require.NoError(t, err, "parsing generated FLAC file")
assert.Equal(t, uint32(testSampleRate), verifyStream.Info.SampleRate)
assert.Equal(t, uint8(testNChannels), verifyStream.Info.NChannels)
require.NoError(t, verifyFile.Close())
cueContent := strings.Join([]string{
`PERFORMER "Test Artist"`,
`TITLE "Test Album"`,
`FILE "album.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "First Song"`,
` INDEX 01 00:00:00`,
` TRACK 02 AUDIO`,
` TITLE "Second Song"`,
` INDEX 01 01:00:00`,
` TRACK 03 AUDIO`,
` TITLE "Third Song"`,
` INDEX 01 02:00:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "album.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
size, files, archiveList, err := xtractr.ExtractCUE(xFile)
require.NoError(t, err, "extracting CUE+FLAC")
assert.Len(t, files, 4, "expected 3 track files + CUE sheet")
assert.Positive(t, size, "total size should be > 0")
assert.Len(t, archiveList, 2, "archive list should contain cue and flac files")
assert.Contains(t, archiveList, cuePath)
assert.Contains(t, archiveList, flacPath)
expectedNames := []string{
"01 - First Song.flac",
"02 - Second Song.flac",
"03 - Third Song.flac",
}
expectedTitles := []string{"First Song", "Second Song", "Third Song"}
for idx, expectedName := range expectedNames {
assert.Equal(t, filepath.Join(outputDir, expectedName), files[idx])
trackFile, err := os.Open(files[idx])
require.NoError(t, err, "opening track FLAC file: %s", files[idx])
trackStream, err := flac.Parse(trackFile)
require.NoError(t, err, "parsing track FLAC file: %s", files[idx])
assert.Equal(t, uint32(testSampleRate), trackStream.Info.SampleRate)
assert.Equal(t, uint8(testNChannels), trackStream.Info.NChannels)
assert.Positive(t, trackStream.Info.NSamples, "track should have samples")
// Split tracks should include VorbisComment with ALBUM, ARTIST, TITLE, TRACKNUMBER for Lidarr/import.
var vorbis *meta.VorbisComment
for _, blk := range trackStream.Blocks {
if vc, ok := blk.Body.(*meta.VorbisComment); ok {
vorbis = vc
break
}
}
require.NotNil(t, vorbis, "track %s should have VorbisComment metadata", files[idx])
tagMap := make(map[string]string)
for _, pair := range vorbis.Tags {
tagMap[pair[0]] = pair[1]
}
assert.Equal(t, "Test Album", tagMap["ALBUM"], "ALBUM tag from CUE TITLE")
assert.Equal(t, "Test Artist", tagMap["ARTIST"], "ARTIST tag from CUE PERFORMER")
assert.Equal(t, expectedTitles[idx], tagMap["TITLE"], "TITLE tag from track")
assert.Equal(t, strconv.Itoa(idx+1), tagMap["TRACKNUMBER"], "TRACKNUMBER")
require.NoError(t, trackFile.Close())
}
}
func TestCueExtractCUE_ReplacesTrackSymlink(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
err := os.Symlink("target", filepath.Join(tmpDir, "symlink-probe"))
if err != nil {
t.Skipf("symlinks unavailable on this platform: %v", err)
}
outputDir := filepath.Join(tmpDir, "output")
require.NoError(t, os.MkdirAll(outputDir, 0o750))
victim := filepath.Join(tmpDir, "victim")
require.NoError(t, os.WriteFile(victim, []byte("secret"), 0o600))
trackPath := filepath.Join(outputDir, "01 - First Song.flac")
require.NoError(t, os.Symlink(victim, trackPath))
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, uint64(testBlockSize)*2)
cuePath := filepath.Join(tmpDir, "album.cue")
cueContent := strings.Join([]string{
`FILE "album.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "First Song"`,
` INDEX 01 00:00:00`,
}, "\n") + "\n"
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
_, files, _, err := xtractr.ExtractCUE(&xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
})
require.NoError(t, err)
require.Contains(t, files, trackPath)
got, err := os.ReadFile(victim)
require.NoError(t, err)
assert.Equal(t, []byte("secret"), got, "must not follow planted track symlink")
info, err := os.Lstat(trackPath)
require.NoError(t, err)
assert.Equal(t, os.FileMode(0), info.Mode()&os.ModeSymlink)
}
func TestCueExtractViaExtractFile(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(30 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
cueContent := strings.Join([]string{
`PERFORMER "Artist"`,
`TITLE "Album"`,
`FILE "album.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Track One"`,
` INDEX 01 00:00:00`,
` TRACK 02 AUDIO`,
` TITLE "Track Two"`,
` INDEX 01 00:15:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "album.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
size, files, archiveList, err := xtractr.ExtractFile(xFile)
require.NoError(t, err, "ExtractFile with .cue")
assert.Len(t, files, 3, "expected 2 track files + CUE sheet")
assert.Len(t, archiveList, 2)
assert.Positive(t, size)
}
// TestCueTxtExtractViaExtractFile covers release groups that ship the sheet as .cue.txt.
func TestCueTxtExtractViaExtractFile(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(30 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
cueContent := strings.Join([]string{
`PERFORMER "Artist"`,
`TITLE "Album"`,
`FILE "album.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Track One"`,
` INDEX 01 00:00:00`,
` TRACK 02 AUDIO`,
` TITLE "Track Two"`,
` INDEX 01 00:15:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "album.cue.txt")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
found := xtractr.FindCompressedFiles(xtractr.Filter{Path: tmpDir})
require.Contains(t, found.List(), cuePath)
size, files, archiveList, err := xtractr.ExtractFile(&xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
})
require.NoError(t, err, "ExtractFile with .cue.txt")
assert.Len(t, files, 3, "expected 2 track files + CUE sheet")
assert.Len(t, archiveList, 2)
assert.Positive(t, size)
assert.Contains(t, files, filepath.Join(outputDir, "album.cue.txt"))
}
// TestCueTxtBasenameFallback ensures album.cue.txt still finds album.flac when
// the FILE line does not match (filepath.Ext would otherwise leave a ".cue" stem).
func TestCueTxtBasenameFallback(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(60 * testSampleRate)
flacPath := filepath.Join(tmpDir, "Album.flac")
generateTestFLAC(t, flacPath, totalSamples)
cueContent := strings.Join([]string{
`PERFORMER "Artist"`,
`TITLE "Album"`,
`FILE "Other Name.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Track One"`,
` INDEX 01 00:00:00`,
` TRACK 02 AUDIO`,
` TITLE "Track Two"`,
` INDEX 01 00:30:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "Album.cue.txt")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
size, files, archiveList, err := xtractr.ExtractCUE(&xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
})
require.NoError(t, err, "ExtractCUE should find Album.flac via same-basename fallback")
assert.Len(t, files, 3)
assert.Len(t, archiveList, 2)
assert.Positive(t, size)
assert.Contains(t, archiveList, flacPath)
}
func TestCueMissingFlac(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
cueContent := strings.Join([]string{
`FILE "nonexistent.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Track"`,
` INDEX 01 00:00:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "test.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
_, _, _, err := xtractr.ExtractCUE(xFile) //nolint:dogsled
assert.ErrorIs(t, err, xtractr.ErrAudioNotFound)
}
func TestCueUnsupportedFormat(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
wavPath := filepath.Join(tmpDir, "album.wav")
require.NoError(t, os.WriteFile(wavPath, []byte("fake"), 0o600))
cueContent := strings.Join([]string{
`FILE "album.wav" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Track"`,
` INDEX 01 00:00:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "test.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
_, _, _, err := xtractr.ExtractCUE(xFile) //nolint:dogsled
assert.ErrorIs(t, err, xtractr.ErrUnsupportedAudio)
}
// TestCueWavReferenceFlacFile verifies that when the CUE says FILE "album.wav" WAVE
// but only album.flac exists on disk, we use the .flac file (common mislabeling).
func TestCueWavReferenceFlacFile(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(60 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
// CUE references .wav but we only have .flac
cueContent := strings.Join([]string{
`PERFORMER "Artist"`,
`TITLE "Album"`,
`FILE "album.wav" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Track One"`,
` INDEX 01 00:00:00`,
` TRACK 02 AUDIO`,
` TITLE "Track Two"`,
` INDEX 01 00:30:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "album.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
size, files, archiveList, err := xtractr.ExtractCUE(xFile)
require.NoError(t, err, "ExtractCUE with CUE referencing .wav but .flac on disk")
assert.Len(t, files, 3, "expected 2 track files + CUE sheet")
assert.Len(t, archiveList, 2)
assert.Positive(t, size)
// Archive list should include the actual flac path we used, not the .wav path
assert.Contains(t, archiveList, flacPath)
}
// TestCueFallbackSameBasename verifies that when the FILE line does not match any file
// (e.g. O vs Ö or encoding mismatch), we use the FLAC with the same base name as the CUE file.
func TestCueFallbackSameBasename(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(60 * testSampleRate)
// FLAC on disk has same base name as CUE, but CUE FILE line references a different name.
flacPath := filepath.Join(tmpDir, "Album.flac")
generateTestFLAC(t, flacPath, totalSamples)
cueContent := strings.Join([]string{
`PERFORMER "Artist"`,
`TITLE "Album"`,
`FILE "Other Name.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Track One"`,
` INDEX 01 00:00:00`,
` TRACK 02 AUDIO`,
` TITLE "Track Two"`,
` INDEX 01 00:30:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "Album.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
size, files, archiveList, err := xtractr.ExtractCUE(xFile)
require.NoError(t, err, "ExtractCUE should find Album.flac via same-basename fallback")
assert.Len(t, files, 3)
assert.Len(t, archiveList, 2)
assert.Positive(t, size)
assert.Contains(t, archiveList, flacPath)
}
// TestCueUTF16LE verifies that a CUE file encoded as UTF-16 LE with BOM is parsed correctly.
func TestCueUTF16LE(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(60 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
//nolint:lll // it's ok.
cueContent := "PERFORMER \"Artist\"\r\nTITLE \"Album\"\r\nFILE \"album.flac\" WAVE\r\n TRACK 01 AUDIO\r\n TITLE \"Track One\"\r\n INDEX 01 00:00:00\r\n"
// Encode as UTF-16 LE with BOM (common for CUE sheets from Windows).
u16 := utf16.Encode([]rune(cueContent))
buf := make([]byte, 0, 2+len(u16)*2)
buf = append(buf, 0xFF, 0xFE)
for _, v := range u16 {
buf = append(buf, byte(v), byte(v>>8))
}
cuePath := filepath.Join(tmpDir, "album.cue")
require.NoError(t, os.WriteFile(cuePath, buf, 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
size, files, _, err := xtractr.ExtractCUE(xFile)
require.NoError(t, err, "UTF-16 LE CUE should parse and extract")
assert.Positive(t, size)
assert.Len(t, files, 2, "expected 1 track + CUE sheet")
}
func TestCueTimestampConversion(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(6 * 60 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
cueContent := strings.Join([]string{
`FILE "album.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "A"`,
` INDEX 01 00:00:00`,
` TRACK 02 AUDIO`,
` TITLE "B"`,
` INDEX 01 05:15:37`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "test.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
_, files, _, err := xtractr.ExtractCUE(xFile)
require.NoError(t, err)
assert.Len(t, files, 3, "expected 2 track files + CUE sheet")
file1, err := os.Open(files[0])
require.NoError(t, err)
stream1, err := flac.New(file1)
require.NoError(t, err)
expectedTrack1Samples := uint64((5*60+15)*testSampleRate) + uint64(37*testSampleRate/75)
assert.Equal(t, expectedTrack1Samples, stream1.Info.NSamples,
"Track 1 should have the expected number of samples based on CUE timestamp")
require.NoError(t, file1.Close())
file2, err := os.Open(files[1])
require.NoError(t, err)
stream2, err := flac.New(file2)
require.NoError(t, err)
expectedTrack2Samples := totalSamples - expectedTrack1Samples
assert.Equal(t, expectedTrack2Samples, stream2.Info.NSamples,
"Track 2 should have the remaining samples")
require.NoError(t, file2.Close())
}
func TestCueSpecialCharacters(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(10 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
cueContent := strings.Join([]string{
`PERFORMER "Test/Artist"`,
`TITLE "Test: Album?"`,
`FILE "album.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Song With / Slash"`,
` INDEX 01 00:00:00`,
` TRACK 02 AUDIO`,
` TITLE "Song: With <Special> Chars?"`,
` INDEX 01 00:05:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "test.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
_, files, _, err := xtractr.ExtractCUE(xFile)
require.NoError(t, err)
assert.Len(t, files, 3, "expected 2 track files + CUE sheet")
assert.Equal(t, "01 - Song With - Slash.flac", filepath.Base(files[0]))
assert.Equal(t, "02 - Song- With Special Chars.flac", filepath.Base(files[1]))
}
// TestCueSmartQuoteInTitle verifies that track titles with Unicode smart quote (U+2019)
// are sanitized to ASCII apostrophe in output filenames so tools like Lidarr can find files.
func TestCueSmartQuoteInTitle(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(60 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
// U+2019 is RIGHT SINGLE QUOTATION MARK (curly apostrophe), often from CUE sheets.
cueContent := strings.Join([]string{
`FILE "album.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "It's Hard to Find a Way"`,
` INDEX 01 00:00:00`,
}, "\n") + "\n"
// Replace straight apostrophe with U+2019 in the CUE content.
cueContent = strings.ReplaceAll(cueContent, "It's", "It\u2019s")
cuePath := filepath.Join(tmpDir, "test.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
_, files, _, err := xtractr.ExtractCUE(xFile)
require.NoError(t, err)
// Output filename must use ASCII apostrophe so Lidarr can find the file.
assert.Equal(t, "01 - It's Hard to Find a Way.flac", filepath.Base(files[0]))
}
func TestCueREMComments(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
outputDir := filepath.Join(tmpDir, "output")
totalSamples := uint64(10 * testSampleRate)
flacPath := filepath.Join(tmpDir, "album.flac")
generateTestFLAC(t, flacPath, totalSamples)
cueContent := strings.Join([]string{
`REM GENRE "Rock"`,
`REM DATE 2024`,
`REM DISCID 12345678`,
`PERFORMER "Artist"`,
`TITLE "Album"`,
`FILE "album.flac" WAVE`,
` TRACK 01 AUDIO`,
` TITLE "Song"`,
` INDEX 01 00:00:00`,
}, "\n") + "\n"
cuePath := filepath.Join(tmpDir, "test.cue")
require.NoError(t, os.WriteFile(cuePath, []byte(cueContent), 0o600))
xFile := &xtractr.XFile{
FilePath: cuePath,
OutputDir: outputDir,
FileMode: 0o600,
DirMode: 0o755,
}
_, files, _, err := xtractr.ExtractCUE(xFile)
require.NoError(t, err)
assert.Len(t, files, 2, "expected 1 track file + CUE sheet")
}
func TestCueSupportedExtensions(t *testing.T) {
t.Parallel()
extensions := xtractr.SupportedExtensions()
found := false
for _, ext := range extensions {
if strings.EqualFold(ext, ".cue") {
found = true
break
}
}
assert.True(t, found, ".cue should be in supported extensions list")
foundTxt := false
for _, ext := range extensions {
if strings.EqualFold(ext, ".cue.txt") {
foundTxt = true
break
}
}
assert.True(t, foundTxt, ".cue.txt should be in supported extensions list")
}