suno-core 0.36.0

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
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
use super::*;

#[test]
fn animated_covers_defaults_off_and_follows_precedence() {
    // Compiled default is off.
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();
    let eff = cfg.resolve("alice", None, &no_env(), &no_flags()).unwrap();
    assert!(!eff.animated_covers);

    // File default on; per-source off; env on; flag off — flag wins.
    let toml = r#"
        [defaults]
        animated_covers = true

        [accounts.alice.sources.liked]
        animated_covers = false
    "#;
    let cfg = Config::from_toml(toml).unwrap();

    // File default (defaults) turns it on for an unscoped resolve.
    let eff = cfg.resolve("alice", None, &no_env(), &no_flags()).unwrap();
    assert!(eff.animated_covers);

    // Per-source file setting overrides the file default.
    let eff = cfg
        .resolve("alice", Some("liked"), &no_env(), &no_flags())
        .unwrap();
    assert!(!eff.animated_covers);

    // Env overrides file (even the per-source off).
    let env: HashMap<String, String> = [("SUNO_ANIMATED_COVERS".into(), "true".into())]
        .into_iter()
        .collect();
    let eff = cfg
        .resolve("alice", Some("liked"), &env, &no_flags())
        .unwrap();
    assert!(eff.animated_covers);

    // Flag overrides env.
    let flags = FlagOverrides {
        settings: Settings {
            animated_covers: Some(false),
            ..Default::default()
        },
        ..Default::default()
    };
    let eff = cfg.resolve("alice", Some("liked"), &env, &flags).unwrap();
    assert!(!eff.animated_covers);
}

#[test]
fn video_mp4_defaults_off_and_follows_precedence() {
    // Compiled default is off.
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();
    let eff = cfg.resolve("alice", None, &no_env(), &no_flags()).unwrap();
    assert!(!eff.video_mp4);

    // File default on; per-source off; env on; flag off — flag wins.
    let toml = r#"
        [defaults]
        video_mp4 = true

        [accounts.alice.sources.liked]
        video_mp4 = false
    "#;
    let cfg = Config::from_toml(toml).unwrap();
    assert!(
        cfg.resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
            .video_mp4
    );
    assert!(
        !cfg.resolve("alice", Some("liked"), &no_env(), &no_flags())
            .unwrap()
            .video_mp4
    );

    let env: HashMap<String, String> = [("SUNO_VIDEO_MP4".into(), "true".into())]
        .into_iter()
        .collect();
    assert!(
        cfg.resolve("alice", Some("liked"), &env, &no_flags())
            .unwrap()
            .video_mp4
    );

    let flags = FlagOverrides {
        settings: Settings {
            video_mp4: Some(false),
            ..Default::default()
        },
        ..Default::default()
    };
    assert!(
        !cfg.resolve("alice", Some("liked"), &env, &flags)
            .unwrap()
            .video_mp4
    );
}

#[test]
fn download_stems_defaults_off_and_follows_precedence() {
    // Compiled default is off (bulk stem mirroring never spends, but it is
    // opt-in so it never runs unless asked).
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();
    assert!(
        !cfg.resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
            .download_stems
    );

    // File default on; per-source off; env on; flag off — flag wins.
    let toml = r#"
        [defaults]
        download_stems = true

        [accounts.alice.sources.liked]
        download_stems = false
    "#;
    let cfg = Config::from_toml(toml).unwrap();
    assert!(
        cfg.resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
            .download_stems
    );
    assert!(
        !cfg.resolve("alice", Some("liked"), &no_env(), &no_flags())
            .unwrap()
            .download_stems
    );

    let env: HashMap<String, String> = [("SUNO_DOWNLOAD_STEMS".into(), "true".into())]
        .into_iter()
        .collect();
    assert!(
        cfg.resolve("alice", Some("liked"), &env, &no_flags())
            .unwrap()
            .download_stems
    );

    let flags = FlagOverrides {
        settings: Settings {
            download_stems: Some(false),
            ..Default::default()
        },
        ..Default::default()
    };
    assert!(
        !cfg.resolve("alice", Some("liked"), &env, &flags)
            .unwrap()
            .download_stems
    );
}

#[test]
fn stem_format_defaults_to_wav_and_follows_precedence() {
    // Compiled default is WAV (lossless, the safe default for stems).
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();
    assert_eq!(
        cfg.resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
            .stem_format,
        StemFormat::Wav
    );

    // File default mp3; per-source wav; env mp3; flag wav — flag wins.
    let toml = r#"
        [defaults]
        stem_format = "mp3"

        [accounts.alice.sources.liked]
        stem_format = "wav"
    "#;
    let cfg = Config::from_toml(toml).unwrap();
    assert_eq!(
        cfg.resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
            .stem_format,
        StemFormat::Mp3
    );
    assert_eq!(
        cfg.resolve("alice", Some("liked"), &no_env(), &no_flags())
            .unwrap()
            .stem_format,
        StemFormat::Wav
    );

    let env: HashMap<String, String> = [("SUNO_STEM_FORMAT".into(), "mp3".into())]
        .into_iter()
        .collect();
    assert_eq!(
        cfg.resolve("alice", Some("liked"), &env, &no_flags())
            .unwrap()
            .stem_format,
        StemFormat::Mp3
    );

    let flags = FlagOverrides {
        settings: Settings {
            stem_format: Some(StemFormat::Wav),
            ..Default::default()
        },
        ..Default::default()
    };
    assert_eq!(
        cfg.resolve("alice", Some("liked"), &env, &flags)
            .unwrap()
            .stem_format,
        StemFormat::Wav
    );
}

#[test]
fn stem_format_rejects_flac_and_unknown() {
    // FLAC is deliberately unrepresentable for stems: parsing it is an error,
    // so a config or flag can never ask for a FLAC stem.
    assert!("flac".parse::<StemFormat>().is_err());
    assert!("aac".parse::<StemFormat>().is_err());
    assert_eq!("WAV".parse::<StemFormat>().unwrap(), StemFormat::Wav);
    assert_eq!("Mp3".parse::<StemFormat>().unwrap(), StemFormat::Mp3);
    // A FLAC stem_format in config is a config error, not a silent fallback.
    assert!(Config::from_toml("[defaults]\nstem_format = \"flac\"\n").is_err());
}

#[test]
fn video_cover_retention_drives_cover_artifacts_not_the_music_video() {
    let resolve = |retention: &str| {
        let toml = format!("[accounts.alice]\nvideo_cover_retention = \"{retention}\"\n");
        Config::from_toml(&toml)
            .unwrap()
            .resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
    };

    let neither = resolve("neither");
    assert!(!neither.animated_covers && !neither.raw_animated_cover);
    assert_eq!(neither.video_cover_retention, VideoCoverRetention::Neither);

    let webp = resolve("webp");
    assert!(webp.animated_covers && !webp.raw_animated_cover);
    assert_eq!(webp.video_cover_retention, VideoCoverRetention::Webp);

    // `mp4` keeps the raw album cover (`video_cover_url` verbatim); it does
    // NOT switch on the standalone music-video toggle (`video_url`).
    let mp4 = resolve("mp4");
    assert!(!mp4.animated_covers && mp4.raw_animated_cover);
    assert!(!mp4.video_mp4);
    assert_eq!(mp4.video_cover_retention, VideoCoverRetention::Mp4);

    let both = resolve("both");
    assert!(both.animated_covers && both.raw_animated_cover);
    assert!(!both.video_mp4);
    assert_eq!(both.video_cover_retention, VideoCoverRetention::Both);
}

#[test]
fn video_mp4_is_independent_of_cover_retention() {
    // The standalone music video (`video_url`) has its own toggle and is
    // never implied by a `video_cover_retention` mode, nor vice versa.
    let toml = "[accounts.alice]\nvideo_mp4 = true\nvideo_cover_retention = \"webp\"\n";
    let cfg = Config::from_toml(toml).unwrap();
    let eff = cfg.resolve("alice", None, &no_env(), &no_flags()).unwrap();
    assert!(eff.video_mp4);
    assert!(eff.animated_covers);
    assert!(!eff.raw_animated_cover);
    assert_eq!(eff.video_cover_retention, VideoCoverRetention::Webp);
}

#[test]
fn animated_cover_webp_knobs_follow_precedence_and_validate_ranges() {
    let toml = r#"
        [defaults]
        animated_cover_quality = 80
        animated_cover_max_fps = 20
        animated_cover_max_width = 640
        animated_cover_compression_level = 3

        [accounts.alice.sources.liked]
        animated_cover_quality = 75
    "#;
    let cfg = Config::from_toml(toml).unwrap();
    let eff = cfg
        .resolve("alice", Some("liked"), &no_env(), &no_flags())
        .unwrap();
    assert_eq!(eff.animated_cover_webp.quality, 75);
    assert_eq!(eff.animated_cover_webp.max_fps, 20);
    assert_eq!(eff.animated_cover_webp.max_width, Some(640));
    assert_eq!(eff.animated_cover_webp.compression_level, 3);

    let env: HashMap<String, String> = [("SUNO_ANIMATED_COVER_QUALITY".into(), "90".into())]
        .into_iter()
        .collect();
    let eff = cfg
        .resolve("alice", Some("liked"), &env, &no_flags())
        .unwrap();
    assert_eq!(eff.animated_cover_webp.quality, 90);

    let flags = FlagOverrides {
        settings: Settings {
            animated_cover_quality: Some(95),
            animated_cover_max_width: Some(512),
            animated_cover_compression_level: Some(4),
            ..Default::default()
        },
        ..Default::default()
    };
    let eff = cfg.resolve("alice", Some("liked"), &env, &flags).unwrap();
    assert_eq!(eff.animated_cover_webp.quality, 95);
    assert_eq!(eff.animated_cover_webp.max_width, Some(512));
    assert_eq!(eff.animated_cover_webp.compression_level, 4);

    let bad_env: HashMap<String, String> = [("SUNO_ANIMATED_COVER_QUALITY".into(), "101".into())]
        .into_iter()
        .collect();
    assert!(cfg.resolve("alice", None, &bad_env, &no_flags()).is_err());
}

#[test]
fn video_cover_retention_parses_formats_and_reports_kept_artifacts() {
    // FromStr is case-insensitive across every variant.
    assert_eq!(
        "NEITHER".parse::<VideoCoverRetention>().unwrap(),
        VideoCoverRetention::Neither
    );
    assert_eq!(
        "WebP".parse::<VideoCoverRetention>().unwrap(),
        VideoCoverRetention::Webp
    );
    assert_eq!(
        "mp4".parse::<VideoCoverRetention>().unwrap(),
        VideoCoverRetention::Mp4
    );
    assert_eq!(
        "Both".parse::<VideoCoverRetention>().unwrap(),
        VideoCoverRetention::Both
    );
    // An unknown mode is a config error, not a silent fallback.
    assert!("mkv".parse::<VideoCoverRetention>().is_err());

    // Display round-trips back to a token FromStr accepts.
    for mode in [
        VideoCoverRetention::Neither,
        VideoCoverRetention::Webp,
        VideoCoverRetention::Mp4,
        VideoCoverRetention::Both,
    ] {
        assert_eq!(
            mode.to_string().parse::<VideoCoverRetention>().unwrap(),
            mode
        );
    }

    // keeps_webp / keeps_mp4 truth table.
    assert!(!VideoCoverRetention::Neither.keeps_webp());
    assert!(!VideoCoverRetention::Neither.keeps_mp4());
    assert!(VideoCoverRetention::Webp.keeps_webp());
    assert!(!VideoCoverRetention::Webp.keeps_mp4());
    assert!(!VideoCoverRetention::Mp4.keeps_webp());
    assert!(VideoCoverRetention::Mp4.keeps_mp4());
    assert!(VideoCoverRetention::Both.keeps_webp());
    assert!(VideoCoverRetention::Both.keeps_mp4());
}

#[test]
fn video_cover_retention_resolves_from_env_and_rejects_unknown() {
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();

    // A valid env value overrides the legacy toggles, just like a flag.
    let env: HashMap<String, String> = [("SUNO_VIDEO_COVER_RETENTION".into(), "both".into())]
        .into_iter()
        .collect();
    let eff = cfg.resolve("alice", None, &env, &no_flags()).unwrap();
    assert_eq!(eff.video_cover_retention, VideoCoverRetention::Both);
    assert!(eff.animated_covers);
    assert!(eff.raw_animated_cover);

    // An unknown env value is a config error rather than a silent default.
    let bad_env: HashMap<String, String> = [("SUNO_VIDEO_COVER_RETENTION".into(), "mkv".into())]
        .into_iter()
        .collect();
    assert!(cfg.resolve("alice", None, &bad_env, &no_flags()).is_err());
}

#[test]
fn animated_cover_compression_level_enforces_zero_to_four() {
    // The top of the valid range is accepted from the config file. Effort is
    // capped at 4 because level 6 costs many times the time for no size gain.
    let cfg =
        Config::from_toml("[defaults]\nanimated_cover_compression_level = 4\n[accounts.alice]\n")
            .unwrap();
    assert_eq!(
        cfg.resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
            .animated_cover_webp
            .compression_level,
        4
    );

    // One past the top is rejected.
    let cfg =
        Config::from_toml("[defaults]\nanimated_cover_compression_level = 5\n[accounts.alice]\n")
            .unwrap();
    assert!(cfg.resolve("alice", None, &no_env(), &no_flags()).is_err());

    // The same ceiling is enforced for an env override.
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();
    let bad_env: HashMap<String, String> =
        [("SUNO_ANIMATED_COVER_COMPRESSION_LEVEL".into(), "5".into())]
            .into_iter()
            .collect();
    assert!(cfg.resolve("alice", None, &bad_env, &no_flags()).is_err());

    // A non-integer env value is a config error, not a panic.
    let junk_env: HashMap<String, String> = [("SUNO_ANIMATED_COVER_MAX_FPS".into(), "abc".into())]
        .into_iter()
        .collect();
    assert!(cfg.resolve("alice", None, &junk_env, &no_flags()).is_err());
}

#[test]
fn animated_cover_lossless_defaults_off_and_follows_precedence() {
    // The compiled default is a bounded lossy encode that fits the FLAC
    // picture cap: quality 90, effort 4, lossy (not lossless).
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();
    let eff = cfg.resolve("alice", None, &no_env(), &no_flags()).unwrap();
    assert_eq!(eff.animated_cover_webp.quality, 90);
    assert_eq!(eff.animated_cover_webp.compression_level, 4);
    assert!(!eff.animated_cover_webp.lossless);

    // Config opts in; an env value and a flag each override in turn.
    let cfg = Config::from_toml("[defaults]\nanimated_cover_lossless = true\n[accounts.alice]\n")
        .unwrap();
    assert!(
        cfg.resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
            .animated_cover_webp
            .lossless
    );
    let env: HashMap<String, String> = [("SUNO_ANIMATED_COVER_LOSSLESS".into(), "false".into())]
        .into_iter()
        .collect();
    assert!(
        !cfg.resolve("alice", None, &env, &no_flags())
            .unwrap()
            .animated_cover_webp
            .lossless
    );
    let flags = FlagOverrides {
        settings: Settings {
            animated_cover_lossless: Some(true),
            ..Default::default()
        },
        ..Default::default()
    };
    assert!(
        cfg.resolve("alice", None, &env, &flags)
            .unwrap()
            .animated_cover_webp
            .lossless
    );

    // A non-boolean value is a config error, not a silent default.
    let bad_env: HashMap<String, String> =
        [("SUNO_ANIMATED_COVER_LOSSLESS".into(), "maybe".into())]
            .into_iter()
            .collect();
    assert!(cfg.resolve("alice", None, &bad_env, &no_flags()).is_err());
}

#[test]
fn animated_cover_max_width_defaults_to_bounded() {
    // With nothing configured, the width cap is the bounded default (640 px)
    // so the embedded animated cover reliably fits the FLAC picture cap.
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();
    assert_eq!(
        cfg.resolve("alice", None, &no_env(), &no_flags())
            .unwrap()
            .animated_cover_webp
            .max_width,
        Some(640)
    );
}

#[test]
fn text_sidecars_default_off_and_follow_precedence() {
    // Both compiled defaults are off.
    let cfg = Config::from_toml("[accounts.alice]\n").unwrap();
    let eff = cfg.resolve("alice", None, &no_env(), &no_flags()).unwrap();
    assert!(!eff.details_sidecar);
    assert!(!eff.lyrics_sidecar);

    let toml = r#"
        [defaults]
        details_sidecar = true

        [accounts.alice.sources.liked]
        details_sidecar = false
    "#;
    let cfg = Config::from_toml(toml).unwrap();

    // File default turns details on for an unscoped resolve; lyrics stays off.
    let eff = cfg.resolve("alice", None, &no_env(), &no_flags()).unwrap();
    assert!(eff.details_sidecar);
    assert!(!eff.lyrics_sidecar);

    // Per-source file setting overrides the file default.
    let eff = cfg
        .resolve("alice", Some("liked"), &no_env(), &no_flags())
        .unwrap();
    assert!(!eff.details_sidecar);

    // Env overrides file (both flags), and the flag overrides env.
    let env: HashMap<String, String> = [
        ("SUNO_DETAILS_SIDECAR".into(), "true".into()),
        ("SUNO_LYRICS_SIDECAR".into(), "true".into()),
    ]
    .into_iter()
    .collect();
    let eff = cfg
        .resolve("alice", Some("liked"), &env, &no_flags())
        .unwrap();
    assert!(eff.details_sidecar);
    assert!(eff.lyrics_sidecar);

    let flags = FlagOverrides {
        settings: Settings {
            lyrics_sidecar: Some(false),
            ..Default::default()
        },
        ..Default::default()
    };
    let eff = cfg.resolve("alice", Some("liked"), &env, &flags).unwrap();
    assert!(eff.details_sidecar);
    assert!(!eff.lyrics_sidecar);
}