suno-core 0.36.3

Engine for a download-only Suno.ai library tool: feed selection, sync reconciliation, and audio tagging.
Documentation
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
use super::*;

#[test]
fn failed_write_leaves_the_prior_file_intact() {
    let c = clip("f");
    let d = desired(c.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![Action::Download {
            clip: c.clone(),
            lineage: LineageContext::own_root(&c),
            path: d.path.clone(),
            format: AudioFormat::Mp3,
        }],
    };
    let http = ScriptedHttp::new().route("f.mp3", Reply::ok(b"new-body".to_vec()));
    let fs = MemFs::new()
        .with_file("f.mp3", b"OLD-CONTENT".to_vec())
        .fail_write("f.mp3");
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &RecordingClock::new(),
        &ExecOptions::default(),
    );

    assert_eq!(outcome.downloaded, 0);
    assert_eq!(outcome.failed(), 1);
    assert_eq!(fs.read_file("f.mp3").unwrap(), b"OLD-CONTENT");
    assert!(manifest.get("f").is_none());
}

#[test]
fn size_mismatch_after_write_is_a_failure() {
    let c = clip("g");
    let d = desired(c.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![Action::Download {
            clip: c.clone(),
            lineage: LineageContext::own_root(&c),
            path: d.path.clone(),
            format: AudioFormat::Mp3,
        }],
    };
    let http = ScriptedHttp::new().route("g.mp3", Reply::ok(b"body".to_vec()));
    let fs = MemFs::new().corrupt_write("g.mp3");
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &RecordingClock::new(),
        &ExecOptions::default(),
    );

    assert_eq!(outcome.downloaded, 0);
    assert_eq!(outcome.failed(), 1);
    assert!(outcome.failures[0].reason.contains("expected"));
    assert!(manifest.get("g").is_none());
}

#[test]
fn transient_failure_is_retried_then_skipped() {
    let c = clip("h");
    let d = desired(c.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![Action::Download {
            clip: c.clone(),
            lineage: LineageContext::own_root(&c),
            path: d.path.clone(),
            format: AudioFormat::Mp3,
        }],
    };
    let http = ScriptedHttp::new().route("h.mp3", Reply::status(500));
    let fs = MemFs::new();
    let clock = RecordingClock::new();
    let opts = ExecOptions {
        max_retries: 2,
        ..ExecOptions::default()
    };
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &clock,
        &opts,
    );

    assert_eq!(outcome.downloaded, 0);
    assert_eq!(outcome.failed(), 1);
    assert_eq!(http.count("h.mp3"), 3);
    assert_eq!(clock.sleeps().len(), 2);
}

#[test]
fn truncated_download_is_retried_then_succeeds() {
    let c = clip("i");
    let d = desired(c.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![Action::Download {
            clip: c.clone(),
            lineage: LineageContext::own_root(&c),
            path: d.path.clone(),
            format: AudioFormat::Mp3,
        }],
    };
    let http = ScriptedHttp::new().route_seq(
        "i.mp3",
        vec![
            Reply::ok(b"short".to_vec()).with_content_length(999),
            Reply::ok(b"good-body".to_vec()),
        ],
    );
    let fs = MemFs::new();
    let clock = RecordingClock::new();
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &clock,
        &ExecOptions::default(),
    );

    assert_eq!(outcome.downloaded, 1);
    assert_eq!(http.count("i.mp3"), 2);
    assert_eq!(clock.sleeps().len(), 1);
}

#[test]
fn rate_limit_backs_off_using_retry_after() {
    let c = clip("j");
    let d = desired(c.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![Action::Download {
            clip: c.clone(),
            lineage: LineageContext::own_root(&c),
            path: d.path.clone(),
            format: AudioFormat::Mp3,
        }],
    };
    let http = ScriptedHttp::new().route_seq(
        "j.mp3",
        vec![
            Reply::status(429).with_retry_after(7),
            Reply::ok(b"body".to_vec()),
        ],
    );
    let fs = MemFs::new();
    let clock = RecordingClock::new();
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &clock,
        &ExecOptions::default(),
    );

    assert_eq!(outcome.downloaded, 1);
    assert_eq!(clock.sleeps(), vec![Duration::from_secs(7)]);
}

#[test]
fn auth_failure_aborts_the_run() {
    let c1 = clip("k1");
    let c2 = clip("k2");
    let d1 = desired(c1.clone(), AudioFormat::Flac);
    let d2 = desired(c2.clone(), AudioFormat::Flac);
    let plan = Plan {
        actions: vec![
            Action::Download {
                clip: c1.clone(),
                lineage: LineageContext::own_root(&c1),
                path: d1.path.clone(),
                format: AudioFormat::Flac,
            },
            Action::Download {
                clip: c2.clone(),
                lineage: LineageContext::own_root(&c2),
                path: d2.path.clone(),
                format: AudioFormat::Flac,
            },
        ],
    };
    // The authenticated WAV-render endpoint rejects auth even after a JWT
    // refresh: that is a bad token, so the whole run aborts rather than
    // hammering every clip. A CDN media rejection, by contrast, does not.
    let http = ScriptedHttp::new()
        .with_auth()
        .route("/wav_file/", Reply::status(401));
    let fs = MemFs::new();
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d1, d2],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &RecordingClock::new(),
        &small_poll(),
    );

    assert_eq!(outcome.status, RunStatus::AuthAborted);
    assert_eq!(outcome.failed(), 1);
    assert_eq!(outcome.failures[0].clip_id, "k1");
    assert_eq!(outcome.downloaded, 0);
}

#[test]
fn disk_full_primary_write_aborts_the_run() {
    // Two MP3 downloads; the first write is out of space. That is systemic,
    // so the run aborts before the second is even attempted: exactly one
    // failure is recorded and its reason names the disk-full cause.
    let c1 = clip("d1");
    let c2 = clip("d2");
    let d1 = desired(c1.clone(), AudioFormat::Mp3);
    let d2 = desired(c2.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![
            Action::Download {
                clip: c1.clone(),
                lineage: LineageContext::own_root(&c1),
                path: d1.path.clone(),
                format: AudioFormat::Mp3,
            },
            Action::Download {
                clip: c2.clone(),
                lineage: LineageContext::own_root(&c2),
                path: d2.path.clone(),
                format: AudioFormat::Mp3,
            },
        ],
    };
    let http = ScriptedHttp::new()
        .route("d1.mp3", Reply::ok(b"body-1".to_vec()))
        .route("d2.mp3", Reply::ok(b"body-2".to_vec()));
    let fs = MemFs::new().fail_write_out_of_space("d1.mp3");
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d1, d2],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &RecordingClock::new(),
        &ExecOptions::default(),
    );

    assert_eq!(outcome.status, RunStatus::DiskFull);
    assert_eq!(outcome.failed(), 1);
    assert_eq!(outcome.failures[0].clip_id, "d1");
    assert!(outcome.failures[0].reason.contains("disk full"));
    assert_eq!(outcome.downloaded, 0);
    // The second clip was never fetched: the run aborted first.
    assert_eq!(http.count("d2.mp3"), 0);
    assert!(!fs.exists("d2.mp3"));
}

#[test]
fn disk_full_flac_transcode_aborts_the_run() {
    // The scratch disk fills during the FLAC re-encode; a WAV rendered, but
    // there is nowhere to stage the transcode, so the run aborts.
    let c1 = clip("d1");
    let c2 = clip("d2");
    let d1 = desired(c1.clone(), AudioFormat::Flac);
    let d2 = desired(c2.clone(), AudioFormat::Flac);
    let plan = Plan {
        actions: vec![
            Action::Download {
                clip: c1.clone(),
                lineage: LineageContext::own_root(&c1),
                path: d1.path.clone(),
                format: AudioFormat::Flac,
            },
            Action::Download {
                clip: c2.clone(),
                lineage: LineageContext::own_root(&c2),
                path: d2.path.clone(),
                format: AudioFormat::Flac,
            },
        ],
    };
    let http = ScriptedHttp::new()
        .with_auth()
        .route(
            "/wav_file/",
            Reply::json(r#"{"wav_file_url": "https://cdn1.suno.ai/d1.wav"}"#),
        )
        .route(".wav", Reply::ok(b"wav".to_vec()));
    let fs = MemFs::new();
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d1, d2],
        &http,
        &fs,
        &StubFfmpeg::out_of_space(),
        &RecordingClock::new(),
        &ExecOptions::default(),
    );

    assert_eq!(outcome.status, RunStatus::DiskFull);
    assert_eq!(outcome.failed(), 1);
    assert_eq!(outcome.failures[0].clip_id, "d1");
    assert!(outcome.failures[0].reason.contains("disk full"));
    assert_eq!(outcome.downloaded, 0);
}

#[test]
fn disk_full_artifact_write_aborts_the_run() {
    // A sidecar write (not a primary download) also aborts on a full disk:
    // the owning audio is present, the cover fetch succeeds, but the sidecar
    // cannot be written.
    let mut manifest = Manifest::new();
    manifest.insert("a", entry("a.mp3", AudioFormat::Mp3));
    let plan = Plan {
        actions: vec![Action::WriteArtifact {
            kind: ArtifactKind::CoverJpg,
            path: "a/cover.jpg".to_owned(),
            source_url: "https://art.suno.ai/a/large.jpg".to_owned(),
            hash: "h1".to_owned(),
            owner_id: "a".to_owned(),
            content: None,
        }],
    };
    let http = ScriptedHttp::new().route("a/large.jpg", Reply::ok(b"jpg-bytes".to_vec()));
    let fs = MemFs::new().fail_write_out_of_space("a/cover.jpg");

    let outcome = run(
        &plan,
        &mut manifest,
        &[],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &RecordingClock::new(),
        &ExecOptions::default(),
    );

    assert_eq!(outcome.status, RunStatus::DiskFull);
    assert_eq!(outcome.failed(), 1);
    assert!(outcome.failures[0].reason.contains("disk full"));
    assert_eq!(outcome.artifacts_written, 0);
    // The sidecar slot was never recorded: the write failed before it.
    assert_eq!(manifest.get("a").unwrap().cover_jpg, None);
}

#[test]
fn disk_full_leaves_the_failed_clips_manifest_entry_unchanged() {
    // write_verify fails before any manifest insert, so a re-download that
    // hits a full disk leaves the prior entry (and file) exactly as it was.
    let c = clip("m");
    let d = desired(c.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![Action::Download {
            clip: c.clone(),
            lineage: LineageContext::own_root(&c),
            path: d.path.clone(),
            format: AudioFormat::Mp3,
        }],
    };
    let http = ScriptedHttp::new().route("m.mp3", Reply::ok(b"new-body".to_vec()));
    let fs = MemFs::new()
        .with_file("m.mp3", b"OLD-CONTENT".to_vec())
        .fail_write_out_of_space("m.mp3");
    let mut manifest = Manifest::new();
    let before = entry("m.mp3", AudioFormat::Mp3);
    manifest.insert("m", before.clone());

    let outcome = run(
        &plan,
        &mut manifest,
        &[d],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &RecordingClock::new(),
        &ExecOptions::default(),
    );

    assert_eq!(outcome.status, RunStatus::DiskFull);
    assert_eq!(manifest.get("m"), Some(&before));
    assert_eq!(fs.read_file("m.mp3").unwrap(), b"OLD-CONTENT");
}

#[test]
fn cdn_download_rejection_skips_the_clip_without_aborting() {
    let c1 = clip("k1");
    let c2 = clip("k2");
    let d1 = desired(c1.clone(), AudioFormat::Mp3);
    let d2 = desired(c2.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![
            Action::Download {
                clip: c1.clone(),
                lineage: LineageContext::own_root(&c1),
                path: d1.path.clone(),
                format: AudioFormat::Mp3,
            },
            Action::Download {
                clip: c2.clone(),
                lineage: LineageContext::own_root(&c2),
                path: d2.path.clone(),
                format: AudioFormat::Mp3,
            },
        ],
    };
    // A CDN media fetch is unauthenticated, so a 403 is a per-asset
    // rejection (often transient), not a bad token: the clip is retried
    // then recorded and skipped, and the run carries on to the rest.
    let http = ScriptedHttp::new()
        .route("k1.mp3", Reply::status(403))
        .route("k2.mp3", Reply::ok(b"body".to_vec()));
    let fs = MemFs::new();
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d1, d2],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &RecordingClock::new(),
        &ExecOptions::default(),
    );

    assert_ne!(outcome.status, RunStatus::AuthAborted);
    assert_eq!(outcome.downloaded, 1);
    assert_eq!(outcome.failed(), 1);
    assert_eq!(outcome.failures[0].clip_id, "k1");
}

#[test]
fn one_clip_failure_does_not_abort_the_run() {
    let c1 = clip("l1");
    let c2 = clip("l2");
    let d1 = desired(c1.clone(), AudioFormat::Mp3);
    let d2 = desired(c2.clone(), AudioFormat::Mp3);
    let plan = Plan {
        actions: vec![
            Action::Download {
                clip: c1.clone(),
                lineage: LineageContext::own_root(&c1),
                path: d1.path.clone(),
                format: AudioFormat::Mp3,
            },
            Action::Download {
                clip: c2.clone(),
                lineage: LineageContext::own_root(&c2),
                path: d2.path.clone(),
                format: AudioFormat::Mp3,
            },
        ],
    };
    let http = ScriptedHttp::new()
        .route("l1.mp3", Reply::status(404))
        .route("l2.mp3", Reply::ok(b"body".to_vec()));
    let fs = MemFs::new();
    let mut manifest = Manifest::new();

    let outcome = run(
        &plan,
        &mut manifest,
        &[d1, d2],
        &http,
        &fs,
        &StubFfmpeg::flac(),
        &RecordingClock::new(),
        &ExecOptions::default(),
    );

    assert_eq!(outcome.status, RunStatus::Completed);
    assert_eq!(outcome.downloaded, 1);
    assert_eq!(outcome.failed(), 1);
    assert_eq!(outcome.failures[0].clip_id, "l1");
    assert!(fs.exists("l2.mp3"));
    assert!(manifest.get("l2").is_some());
    assert!(manifest.get("l1").is_none());
}