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
|
package push
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync"
"testing"
)
// fakeServer stands in for a remote podkit server with ingest enabled.
type fakeServer struct {
mu sync.Mutex
existing map[string]int64
received map[string]string
srv *httptest.Server
}
func newFakeServer(t *testing.T, existing map[string]int64) *fakeServer {
t.Helper()
f := &fakeServer{existing: existing, received: map[string]string{}}
if f.existing == nil {
f.existing = map[string]int64{}
}
mux := http.NewServeMux()
mux.HandleFunc("/manifest", func(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
_ = json.NewEncoder(w).Encode(map[string]any{"files": f.existing})
})
mux.HandleFunc("/ingest", func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
f.mu.Lock()
defer f.mu.Unlock()
f.received[r.URL.Query().Get("name")] = string(body)
w.WriteHeader(http.StatusOK)
})
f.srv = httptest.NewServer(mux)
t.Cleanup(f.srv.Close)
return f
}
func libraryDir(t *testing.T, files map[string]string) string {
t.Helper()
dir := t.TempDir()
for name, body := range files {
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
return dir
}
func TestPushUploadsAudio(t *testing.T) {
dir := libraryDir(t, map[string]string{
"01 - April.mp3": "aaa",
"02 - May.mp3": "bbbb",
})
f := newFakeServer(t, nil)
results, err := Run(context.Background(), Options{Dir: dir, Server: f.srv.URL, Token: "t"})
if err != nil {
t.Fatal(err)
}
uploaded, skipped, failed := Summarise(results)
if uploaded != 2 || skipped != 0 || failed != 0 {
t.Fatalf("uploaded=%d skipped=%d failed=%d", uploaded, skipped, failed)
}
if f.received["01 - April.mp3"] != "aaa" || f.received["02 - May.mp3"] != "bbbb" {
t.Errorf("server received %v", f.received)
}
}
// Re-running a push must not re-send what is already there; a lecture series is
// gigabytes and the second run should be nearly free.
func TestPushSkipsFilesTheServerHas(t *testing.T) {
dir := libraryDir(t, map[string]string{
"01 - April.mp3": "aaa",
"02 - May.mp3": "bbbb",
})
f := newFakeServer(t, map[string]int64{"01 - April.mp3": 3})
results, err := Run(context.Background(), Options{Dir: dir, Server: f.srv.URL})
if err != nil {
t.Fatal(err)
}
uploaded, skipped, _ := Summarise(results)
if uploaded != 1 || skipped != 1 {
t.Fatalf("uploaded=%d skipped=%d", uploaded, skipped)
}
if _, sent := f.received["01 - April.mp3"]; sent {
t.Error("a file the server already had was re-uploaded")
}
}
// A same-named file of a different size is a different file — most likely a
// truncated earlier upload — and must be re-sent rather than assumed good.
func TestPushResendsOnSizeMismatch(t *testing.T) {
dir := libraryDir(t, map[string]string{"01 - April.mp3": "aaa"})
f := newFakeServer(t, map[string]int64{"01 - April.mp3": 99})
results, err := Run(context.Background(), Options{Dir: dir, Server: f.srv.URL})
if err != nil {
t.Fatal(err)
}
uploaded, skipped, _ := Summarise(results)
if uploaded != 1 || skipped != 0 {
t.Fatalf("uploaded=%d skipped=%d", uploaded, skipped)
}
}
func TestPushForceIgnoresManifest(t *testing.T) {
dir := libraryDir(t, map[string]string{"01 - April.mp3": "aaa"})
f := newFakeServer(t, map[string]int64{"01 - April.mp3": 3})
results, err := Run(context.Background(), Options{Dir: dir, Server: f.srv.URL, Force: true})
if err != nil {
t.Fatal(err)
}
if uploaded, _, _ := Summarise(results); uploaded != 1 {
t.Fatalf("uploaded=%d, want 1 under --force", uploaded)
}
}
func TestPushIgnoresNonAudio(t *testing.T) {
dir := libraryDir(t, map[string]string{
"01 - April.mp3": "aaa",
"feed.xml": "<rss/>",
"cover.jpg": "img",
"notes.txt": "hello",
})
f := newFakeServer(t, nil)
if _, err := Run(context.Background(), Options{Dir: dir, Server: f.srv.URL}); err != nil {
t.Fatal(err)
}
if len(f.received) != 1 {
t.Errorf("server received %v, want only the mp3", f.received)
}
}
func TestPushSendsToken(t *testing.T) {
dir := libraryDir(t, map[string]string{"01 - April.mp3": "aaa"})
var gotAuth string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/manifest" {
gotAuth = r.Header.Get("Authorization")
_ = json.NewEncoder(w).Encode(map[string]any{"files": map[string]int64{}})
return
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
if _, err := Run(context.Background(), Options{Dir: dir, Server: srv.URL, Token: "sekrit"}); err != nil {
t.Fatal(err)
}
if gotAuth != "Bearer sekrit" {
t.Errorf("Authorization header was %q", gotAuth)
}
}
// A 404 from the manifest means either a bad token or a server without
// --ingest. The error has to say so, since a bare "404" is unactionable.
func TestPushExplainsManifest404(t *testing.T) {
dir := libraryDir(t, map[string]string{"01 - April.mp3": "aaa"})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}))
defer srv.Close()
_, err := Run(context.Background(), Options{Dir: dir, Server: srv.URL})
if err == nil {
t.Fatal("expected an error")
}
for _, want := range []string{"token", "--ingest"} {
if !contains(err.Error(), want) {
t.Errorf("error should mention %q; got %q", want, err)
}
}
}
func TestPushReportsUploadFailure(t *testing.T) {
dir := libraryDir(t, map[string]string{"01 - April.mp3": "aaa"})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/manifest" {
_ = json.NewEncoder(w).Encode(map[string]any{"files": map[string]int64{}})
return
}
http.Error(w, "disk full", http.StatusInsufficientStorage)
}))
defer srv.Close()
results, err := Run(context.Background(), Options{Dir: dir, Server: srv.URL})
if err != nil {
t.Fatal(err)
}
if _, _, failed := Summarise(results); failed != 1 {
t.Fatalf("failed=%d, want 1", failed)
}
if !contains(results[0].Err.Error(), "disk full") {
t.Errorf("server's reason was lost: %v", results[0].Err)
}
}
func TestPushRejectsBadServerURL(t *testing.T) {
dir := libraryDir(t, map[string]string{"01 - April.mp3": "aaa"})
for _, s := range []string{"", "not-a-url", "/relative/path"} {
if _, err := Run(context.Background(), Options{Dir: dir, Server: s}); err == nil {
t.Errorf("server %q was accepted", s)
}
}
}
func TestPushRejectsEmptyLibrary(t *testing.T) {
f := newFakeServer(t, nil)
if _, err := Run(context.Background(), Options{Dir: t.TempDir(), Server: f.srv.URL}); err == nil {
t.Error("expected an error for a directory with no audio")
}
}
// The base URL may include a path, since the server will usually sit behind a
// reverse proxy at something like /podkit.
func TestPushHonoursServerPathPrefix(t *testing.T) {
dir := libraryDir(t, map[string]string{"01 - April.mp3": "aaa"})
var paths []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
paths = append(paths, r.URL.Path)
if r.URL.Path == "/podkit/manifest" {
_ = json.NewEncoder(w).Encode(map[string]any{"files": map[string]int64{}})
return
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
if _, err := Run(context.Background(), Options{Dir: dir, Server: srv.URL + "/podkit"}); err != nil {
t.Fatal(err)
}
if len(paths) != 2 || paths[0] != "/podkit/manifest" || paths[1] != "/podkit/ingest" {
t.Errorf("requested %v", paths)
}
}
func contains(h, n string) bool {
for i := 0; i+len(n) <= len(h); i++ {
if h[i:i+len(n)] == n {
return true
}
}
return false
}
|