-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkcdj_test.go
207 lines (164 loc) · 5.28 KB
/
mkcdj_test.go
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
package mkcdj_test
import (
"context"
"encoding/json"
"fmt"
"io"
"io/fs"
"mkcdj"
"os"
"path/filepath"
"strings"
"testing"
)
func TestPresets(t *testing.T) {
t.Run("it should load the preset by its name", func(t *testing.T) {
p, err := mkcdj.PresetFromName("dnb")
noerr(t, err)
assert(t, "dnb", p.Name)
})
t.Run("it should return an error and the default preset if the name is not found", func(t *testing.T) {
p, err := mkcdj.PresetFromName("foo")
assert(t, true, err != nil)
assert(t, "default", p.Name)
})
t.Run("it should load the preset by its bpm range, using the narrowest encountered range", func(t *testing.T) {
p, err := mkcdj.PresetFromBPM(174)
noerr(t, err)
assert(t, "dnb", p.Name)
})
t.Run("it should return an error and the default preset for unsupported BPM ranges", func(t *testing.T) {
p, err := mkcdj.PresetFromBPM(20)
assert(t, true, err != nil)
assert(t, "default", p.Name)
})
}
func TestSerialization(t *testing.T) {
t.Run("it should unserialize and serialize a playlist", func(t *testing.T) {
data := `[{"path":"/foo","hash":"bar","preset":"dnb","bpm":100}]`
var tracks []mkcdj.Track
noerr(t, json.Unmarshal([]byte(data), &tracks))
assert(t, "/foo", tracks[0].Path)
assert(t, "bar", tracks[0].Hash)
assert(t, "dnb", tracks[0].Preset.Name)
assert(t, 100, tracks[0].BPM)
got, err := json.Marshal(&tracks)
noerr(t, err)
assert(t, data, string(got))
})
t.Run("it should use the default preset if the track preset is empty", func(t *testing.T) {
data := `[{"path":"/foo","hash":"bar","preset":"","bpm":100}]`
var tracks []mkcdj.Track
noerr(t, json.Unmarshal([]byte(data), &tracks))
assert(t, "default", tracks[0].Preset.Name)
})
}
func TestAnalyze(t *testing.T) {
SUT, params := setup(t)
// Do the analysis twice to check duplication.
noerr(t, SUT.Analyze(context.Background(), params.SourceFilePath, mkcdj.Presets[0]))
noerr(t, SUT.Analyze(context.Background(), params.SourceFilePath, mkcdj.Presets[0]))
tracks := loadPlaylist(t, params.PlaylistFilePath)
assert(t, 1, len(tracks))
assert(t, params.SourceFilePath, tracks[0].Path)
assert(t, "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03", tracks[0].Hash)
assert(t, 100, tracks[0].BPM)
}
func TestRefresh(t *testing.T) {
SUT, params := setup(t)
noerr(t, SUT.Refresh(context.Background()))
tracks := loadPlaylist(t, params.PlaylistFilePath)
assert(t, 1, len(tracks))
assert(t, params.SourceFilePath, tracks[0].Path)
assert(t, "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03", tracks[0].Hash)
assert(t, 100, tracks[0].BPM)
}
func TestCompile(t *testing.T) {
SUT, params := setup(t)
noerr(t, SUT.Compile(context.Background(), params.OutDirPath))
files := listFiles(t, params.OutDirPath)
t.Log(files)
base, ext := filepath.Base(params.SourceFilePath), filepath.Ext(params.SourceFilePath)
want := fmt.Sprintf("100 - %s", base[:len(base)-len(ext)])
assert(t, 3, len(files))
assert(t, want+".wav", filepath.Base(files[0]))
assert(t, "default", filepath.Base(filepath.Dir(files[0])))
checkFile(t, params.OutDirPath, filepath.Dir(files[0]), want+".wav")
checkFile(t, params.OutDirPath, filepath.Dir(files[1]), want+".png")
checkFile(t, params.OutDirPath, filepath.Dir(files[2]), want+".png")
}
type params struct {
SourceFilePath string
OutDirPath string
PlaylistFilePath string
}
func setup(t *testing.T) (*mkcdj.Playlist, params) {
t.Helper()
dir := t.TempDir()
source := filepath.Join(dir, "mkcdj-source.flac")
noerr(t, os.WriteFile(source, []byte("hello\n"), 0666))
tracks := []mkcdj.Track{{
Path: source,
Hash: "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03",
BPM: 100,
Preset: mkcdj.Presets[0],
}}
payload, err := json.Marshal(tracks)
noerr(t, err)
playlist := filepath.Join(os.TempDir(), "/mkcdj.json")
noerr(t, os.WriteFile(playlist, payload, 0666))
SUT := mkcdj.New(
mkcdj.WithRepository(playlist),
mkcdj.WithPipeline(mkcdj.Convert, writeOk),
mkcdj.WithPipeline(mkcdj.Analyze, writeOk),
mkcdj.WithPipeline(mkcdj.Waveform, writeOk),
mkcdj.WithPipeline(mkcdj.Spectrum, writeOk),
mkcdj.WithBPMScanFunc(stubBPMScanner),
)
res := params{
SourceFilePath: source,
OutDirPath: dir,
PlaylistFilePath: playlist,
}
return SUT, res
}
func loadPlaylist(t *testing.T, path string) []mkcdj.Track {
t.Helper()
tracks := make([]mkcdj.Track, 0)
data, err := os.ReadFile(path)
noerr(t, err)
noerr(t, json.Unmarshal(data, &tracks))
return tracks
}
func listFiles(t *testing.T, path string) []string {
t.Helper()
files, err := fs.Glob(os.DirFS(path), "mkcdj-*/*/*/*")
noerr(t, err)
return files
}
func checkFile(t *testing.T, components ...string) {
t.Helper()
content, err := os.ReadFile(filepath.Join(components...))
noerr(t, err)
assert(t, "ok", strings.TrimSpace(string(content)))
}
func assert[T comparable](t *testing.T, want, got T) {
t.Helper()
if want != got {
t.Errorf("want: %v, got: %v", want, got)
}
}
func noerr(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
var writeOk = mkcdj.PipelineFunc(stubCmd)
func stubCmd(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) error {
_, err := stdout.Write([]byte("ok"))
return err
}
func stubBPMScanner(r io.Reader, min, max float64) (float64, error) {
return 100, nil
}