1use std::collections::HashMap;
9use std::fmt::Write as _;
10
11use serde::Serialize;
12
13use crate::config::AudioFormat;
14use crate::consts::SUNO_SONG_BASE_URL;
15use crate::graph::LineageStore;
16use crate::lineage::LineageContext;
17use crate::lyrics::AlignedLyrics;
18use crate::manifest::Manifest;
19use crate::model::Clip;
20use crate::tag::{TrackMetadata, non_empty};
21
22pub const INDEX_SCHEMA_VERSION: u32 = 1;
27
28#[derive(Debug, Clone, Copy)]
39pub struct M3u8Entry<'a> {
40 pub title: &'a str,
41 pub duration_secs: f64,
42 pub relative_path: &'a str,
43}
44
45pub fn render_m3u8(name: &str, entries: &[M3u8Entry<'_>]) -> String {
56 let mut out = String::from("#EXTM3U\n");
57 let _ = writeln!(out, "#PLAYLIST:{}", to_single_line(name));
58 for entry in entries {
59 let title = to_single_line(entry.title);
60 if entry.relative_path.is_empty() {
61 let _ = writeln!(out, "# (not in library) {title}");
64 continue;
65 }
66 let path = to_single_line(entry.relative_path);
67 let seconds = extinf_seconds(entry.duration_secs);
68 let _ = write!(out, "#EXTINF:{seconds},{title}\n{path}\n");
69 }
70 out
71}
72
73#[derive(Debug, Serialize)]
79struct IndexEntry {
80 id: String,
81 path: String,
82 format: AudioFormat,
83 size: u64,
84 title: String,
85 artist: Option<String>,
86 handle: Option<String>,
87 album: String,
88 root_id: String,
89 created_at: Option<String>,
90 duration: Option<f64>,
91 tags: Option<String>,
92}
93
94#[derive(Debug, Serialize)]
96struct LibraryIndex {
97 schema_version: u32,
98 clips: Vec<IndexEntry>,
99}
100
101pub fn render_library_index(
112 manifest: &Manifest,
113 store: &LineageStore,
114 live: &HashMap<&str, &Clip>,
115) -> String {
116 let clips = manifest
117 .iter()
118 .map(|(id, entry)| {
119 let live_clip = live.get(id.as_str()).copied();
120 let title = live_clip
121 .map(|clip| clip.title.clone())
122 .filter(|title| !title.is_empty())
123 .or_else(|| {
124 store
125 .node(id)
126 .map(|node| node.title.clone())
127 .filter(|title| !title.is_empty())
128 })
129 .unwrap_or_else(|| "Untitled".to_owned());
130 let artist =
131 live_clip.map(|clip| non_empty(&clip.display_name).unwrap_or("Suno").to_owned());
132 let handle = live_clip.and_then(|clip| non_empty(&clip.handle).map(str::to_owned));
133 let album = match live_clip {
134 Some(clip) => store.context_for(clip).album(&clip.title),
135 None => store.album_for_id(id),
136 };
137 let root_id = store
138 .get_root(id)
139 .map(|cached| cached.root_id.clone())
140 .filter(|root| !root.is_empty())
141 .unwrap_or_else(|| id.clone());
142 let created_at = store
143 .node(id)
144 .map(|node| node.created_at.clone())
145 .filter(|created| !created.is_empty());
146 let duration = live_clip.map(|clip| clip.duration);
147 let tags = live_clip.map(|clip| clip.tags.clone());
148 IndexEntry {
149 id: id.clone(),
150 path: entry.path.clone(),
151 format: entry.format,
152 size: entry.size,
153 title,
154 artist,
155 handle,
156 album,
157 root_id,
158 created_at,
159 duration,
160 tags,
161 }
162 })
163 .collect();
164 let index = LibraryIndex {
165 schema_version: INDEX_SCHEMA_VERSION,
166 clips,
167 };
168 serde_json::to_string_pretty(&index).expect("library index serialises")
169}
170
171fn extinf_seconds(duration_secs: f64) -> i64 {
175 if duration_secs.is_finite() {
176 duration_secs.round() as i64
177 } else {
178 0
179 }
180}
181fn to_single_line(text: &str) -> String {
184 text.replace('\r', "").replace('\n', " ")
185}
186
187pub fn render_clip_details(clip: &Clip, lineage: &LineageContext) -> String {
200 let meta = TrackMetadata::from_clip(clip, lineage);
201 let url = if clip.id.is_empty() {
202 String::new()
203 } else {
204 format!("{SUNO_SONG_BASE_URL}/{}", clip.id)
205 };
206 let fields: [(&str, &str); 17] = [
207 ("Title", &meta.title),
208 ("Artist", &meta.artist),
209 ("Album", &meta.album),
210 ("Album Artist", &meta.album_artist),
211 ("Date", &meta.date),
212 ("Duration", &format_duration(clip.duration)),
213 ("Model", &meta.model),
214 ("Handle", &meta.handle),
215 ("Style", &meta.style),
216 ("Style Summary", &meta.style_summary),
217 ("Comment", &meta.comment),
218 ("Prompt", &clip.prompt),
219 ("Parent", &meta.parent),
220 ("Root", &meta.root),
221 ("Lineage", &meta.lineage),
222 ("Id", &clip.id),
223 ("Url", &url),
224 ];
225 let mut out = String::new();
226 for (label, value) in fields {
227 if value.is_empty() {
228 continue;
229 }
230 let _ = writeln!(out, "{label}: {}", to_single_line(value));
231 }
232 out
233}
234
235pub fn render_clip_lyrics(clip: &Clip) -> Option<String> {
242 if clip.lyrics.trim().is_empty() {
243 return None;
244 }
245 Some(format!("{}\n", clip.lyrics.trim_end()))
246}
247
248pub fn render_clip_lrc(clip: &Clip, lineage: &LineageContext) -> Option<String> {
259 if clip.lyrics.trim().is_empty() {
260 return None;
261 }
262 let mut out = lrc_headers(clip, lineage);
263 for line in clip.lyrics.trim_end().lines() {
264 let _ = writeln!(out, "{line}");
265 }
266 Some(out)
267}
268
269pub fn render_synced_lrc(
279 clip: &Clip,
280 lineage: &LineageContext,
281 aligned: &AlignedLyrics,
282) -> Option<String> {
283 let body = aligned.lrc_body();
284 if body.is_empty() {
285 return None;
286 }
287 let mut out = lrc_headers(clip, lineage);
288 out.push_str(&body);
289 Some(out)
290}
291
292fn lrc_headers(clip: &Clip, lineage: &LineageContext) -> String {
295 let meta = TrackMetadata::from_clip(clip, lineage);
296 let length = format_duration(clip.duration);
297 let headers: [(&str, &str); 5] = [
298 ("ti", &meta.title),
299 ("ar", &meta.artist),
300 ("al", &meta.album),
301 ("length", &length),
302 ("re", "rs-suno"),
303 ];
304 let mut out = String::new();
305 for (tag, value) in headers {
306 if value.is_empty() {
307 continue;
308 }
309 let _ = writeln!(out, "[{tag}:{}]", to_single_line(value));
310 }
311 out
312}
313
314fn format_duration(secs: f64) -> String {
317 if !secs.is_finite() || secs <= 0.0 {
318 return String::new();
319 }
320 let total = secs.round() as i64;
321 format!("{}:{:02}", total / 60, total % 60)
322}
323
324#[cfg(test)]
325mod tests {
326 use super::*;
327 use crate::lineage::{EdgeType, ResolveStatus};
328
329 fn full_clip() -> Clip {
330 Clip {
331 id: "clip-1234abcd".to_owned(),
332 title: "Electric Storm".to_owned(),
333 tags: "ambient, cinematic".to_owned(),
334 duration: 211.6,
335 created_at: "2024-03-10T14:22:01Z".to_owned(),
336 display_name: "alice".to_owned(),
337 handle: "alice".to_owned(),
338 prompt: "an orchestral storm".to_owned(),
339 gpt_description_prompt: "a moody cinematic build".to_owned(),
340 lyrics: "thunder rolls\nover the plains".to_owned(),
341 model_name: "chirp-v4".to_owned(),
342 major_model_version: "v4".to_owned(),
343 image_large_url: "https://cdn1.suno.ai/signed?token=secret".to_owned(),
344 audio_url: "https://cdn1.suno.ai/clip-1234abcd.mp3".to_owned(),
345 ..Clip::default()
346 }
347 }
348
349 fn full_lineage() -> LineageContext {
350 LineageContext {
351 root_id: "rootid567890".to_owned(),
352 root_title: "Weather Series".to_owned(),
353 parent_id: "parentid1234".to_owned(),
354 edge_type: Some(EdgeType::Extend),
355 status: ResolveStatus::Resolved,
356 }
357 }
358
359 #[test]
360 fn details_render_is_exact_and_fixed_order() {
361 let rendered = render_clip_details(&full_clip(), &full_lineage());
362 let expected = "Title: Electric Storm\n\
363 Artist: alice\n\
364 Album: Weather Series\n\
365 Album Artist: alice\n\
366 Date: 2024-03-10\n\
367 Duration: 3:32\n\
368 Model: chirp-v4 (v4)\n\
369 Handle: alice\n\
370 Style: ambient, cinematic\n\
371 Style Summary: a moody cinematic build\n\
372 Comment: a moody cinematic build\n\
373 Prompt: an orchestral storm\n\
374 Parent: parentid1234\n\
375 Root: rootid567890\n\
376 Lineage: Extended from parentid Root rootid56 (Weather Series)\n\
377 Id: clip-1234abcd\n\
378 Url: https://suno.com/song/clip-1234abcd\n";
379 assert_eq!(rendered, expected);
380 }
381
382 #[test]
383 fn details_omit_empty_fields() {
384 let clip = Clip {
385 id: "only-id".to_owned(),
386 title: "Bare".to_owned(),
387 ..Clip::default()
388 };
389 let rendered = render_clip_details(&clip, &LineageContext::own_root(&clip));
390 let expected = "Title: Bare\n\
394 Artist: Suno\n\
395 Album: Bare\n\
396 Album Artist: Suno\n\
397 Root: only-id\n\
398 Id: only-id\n\
399 Url: https://suno.com/song/only-id\n";
400 assert_eq!(rendered, expected);
401 assert!(!rendered.contains("Duration:"));
402 assert!(!rendered.contains("Prompt:"));
403 }
404
405 #[test]
406 fn details_exclude_signed_cdn_urls() {
407 let rendered = render_clip_details(&full_clip(), &full_lineage());
408 assert!(!rendered.contains("cdn1.suno.ai"));
409 assert!(!rendered.contains("token=secret"));
410 assert!(!rendered.contains(".mp3"));
411 }
412
413 #[test]
414 fn details_use_canonical_song_url() {
415 let rendered = render_clip_details(&full_clip(), &full_lineage());
416 assert!(rendered.contains("Url: https://suno.com/song/clip-1234abcd\n"));
417 }
418
419 #[test]
420 fn details_label_prompt_not_lyrics() {
421 let rendered = render_clip_details(&full_clip(), &full_lineage());
422 assert!(rendered.contains("Prompt: an orchestral storm\n"));
423 assert!(!rendered.contains("Lyrics:"));
426 assert!(!rendered.contains("thunder rolls"));
427 }
428
429 #[test]
430 fn details_use_resolved_lineage_not_feed_fields() {
431 let clip = Clip {
432 id: "child".to_owned(),
433 title: "Child".to_owned(),
434 album_title: "Ignored Feed Album".to_owned(),
435 ..Clip::default()
436 };
437 let lineage = LineageContext {
438 root_id: "root-01".to_owned(),
439 root_title: "Resolved Album".to_owned(),
440 parent_id: "root-01".to_owned(),
441 edge_type: Some(EdgeType::Cover),
442 status: ResolveStatus::Resolved,
443 };
444 let rendered = render_clip_details(&clip, &lineage);
445 assert!(rendered.contains("Album: Resolved Album\n"));
446 assert!(!rendered.contains("Ignored Feed Album"));
447 }
448
449 #[test]
450 fn details_for_a_pure_root_omit_lineage_and_parent() {
451 let clip = Clip {
452 id: "root".to_owned(),
453 title: "Root".to_owned(),
454 ..Clip::default()
455 };
456 let rendered = render_clip_details(&clip, &LineageContext::own_root(&clip));
457 assert!(!rendered.contains("Parent:"));
460 assert!(!rendered.contains("Lineage:"));
461 assert!(rendered.contains("Root: root\n"));
462 }
463
464 #[test]
465 fn lyrics_render_verbatim_with_one_trailing_newline() {
466 let clip = Clip {
467 lyrics: "line one\nline two".to_owned(),
468 ..Clip::default()
469 };
470 assert_eq!(
471 render_clip_lyrics(&clip),
472 Some("line one\nline two\n".to_owned())
473 );
474 }
475
476 #[test]
477 fn lyrics_normalise_trailing_whitespace_to_one_newline() {
478 let clip = Clip {
479 lyrics: "verse\n\n\n".to_owned(),
480 ..Clip::default()
481 };
482 assert_eq!(render_clip_lyrics(&clip), Some("verse\n".to_owned()));
483 }
484
485 #[test]
486 fn lyrics_none_when_empty_or_whitespace_only() {
487 assert_eq!(render_clip_lyrics(&Clip::default()), None);
488 let clip = Clip {
489 lyrics: " \n\t \n".to_owned(),
490 ..Clip::default()
491 };
492 assert_eq!(render_clip_lyrics(&clip), None);
493 }
494
495 #[test]
496 fn lyrics_use_clip_lyrics_not_prompt() {
497 let clip = Clip {
498 prompt: "the generation prompt".to_owned(),
499 lyrics: "the actual sung words".to_owned(),
500 ..Clip::default()
501 };
502 let rendered = render_clip_lyrics(&clip).unwrap();
503 assert!(rendered.contains("the actual sung words"));
504 assert!(!rendered.contains("the generation prompt"));
505 }
506
507 #[test]
508 fn lrc_none_when_lyrics_blank() {
509 let empty = Clip::default();
510 assert_eq!(
511 render_clip_lrc(&empty, &LineageContext::own_root(&empty)),
512 None
513 );
514 let clip = Clip {
515 lyrics: " \n\t \n".to_owned(),
516 ..Clip::default()
517 };
518 assert_eq!(
519 render_clip_lrc(&clip, &LineageContext::own_root(&clip)),
520 None
521 );
522 }
523
524 #[test]
525 fn lrc_renders_untimed_body_with_headers() {
526 let rendered = render_clip_lrc(&full_clip(), &full_lineage()).unwrap();
527 let expected = "[ti:Electric Storm]\n\
528 [ar:alice]\n\
529 [al:Weather Series]\n\
530 [length:3:32]\n\
531 [re:rs-suno]\n\
532 thunder rolls\n\
533 over the plains\n";
534 assert_eq!(rendered, expected);
535 assert!(!rendered.contains("[00:"));
537 }
538
539 #[test]
540 fn lrc_omits_unknown_headers() {
541 let clip = Clip {
542 title: "Bare".to_owned(),
543 lyrics: "one line".to_owned(),
544 ..Clip::default()
545 };
546 let rendered = render_clip_lrc(&clip, &LineageContext::own_root(&clip)).unwrap();
547 assert!(!rendered.contains("[length:"));
550 assert!(rendered.contains("[ti:Bare]\n"));
551 assert!(rendered.contains("[re:rs-suno]\n"));
552 assert!(rendered.ends_with("one line\n"));
553 }
554
555 fn sample_aligned() -> crate::lyrics::AlignedLyrics {
556 crate::lyrics::AlignedLyrics::from_json(&serde_json::json!({
557 "aligned_words": [],
558 "aligned_lyrics": [
559 {"text": "thunder rolls", "start_s": 1.5, "end_s": 2.4, "section": "Verse 1",
560 "words": [
561 {"text": "thunder", "start_s": 1.5, "end_s": 2.0},
562 {"text": "rolls", "start_s": 2.1, "end_s": 2.4}
563 ]}
564 ]
565 }))
566 }
567
568 #[test]
569 fn synced_lrc_has_headers_then_line_stamps() {
570 let rendered = render_synced_lrc(&full_clip(), &full_lineage(), &sample_aligned()).unwrap();
571 let expected = "[ti:Electric Storm]\n\
572 [ar:alice]\n\
573 [al:Weather Series]\n\
574 [length:3:32]\n\
575 [re:rs-suno]\n\
576 [00:01.50]thunder rolls\n";
577 assert_eq!(rendered, expected);
578 }
579
580 #[test]
581 fn synced_lrc_is_none_for_empty_alignment() {
582 let empty = crate::lyrics::AlignedLyrics::default();
585 assert_eq!(
586 render_synced_lrc(&full_clip(), &full_lineage(), &empty),
587 None
588 );
589 }
590
591 #[test]
592 fn m3u8_preserves_order_and_rounds_extinf() {
593 let entries = [
594 M3u8Entry {
595 title: "First",
596 duration_secs: 211.6,
597 relative_path: "Artist/Album/First.flac",
598 },
599 M3u8Entry {
600 title: "Second, Take",
601 duration_secs: 90.5,
602 relative_path: "Artist/Album/Second.flac",
603 },
604 M3u8Entry {
605 title: "Third\nLine",
606 duration_secs: 30.2,
607 relative_path: "Artist/Album/Third.flac",
608 },
609 ];
610
611 let rendered = render_m3u8("Road Trip", &entries);
612
613 let expected = "#EXTM3U\n\
614 #PLAYLIST:Road Trip\n\
615 #EXTINF:212,First\n\
616 Artist/Album/First.flac\n\
617 #EXTINF:91,Second, Take\n\
618 Artist/Album/Second.flac\n\
619 #EXTINF:30,Third Line\n\
620 Artist/Album/Third.flac\n";
621 assert_eq!(rendered, expected);
622 }
623
624 #[test]
625 fn m3u8_strips_newlines_but_keeps_commas() {
626 let entries = [M3u8Entry {
627 title: "Hello, World\r\nSecond, Line",
628 duration_secs: 12.0,
629 relative_path: "Artist/Track.flac",
630 }];
631
632 let rendered = render_m3u8("Mix", &entries);
633
634 assert_eq!(
635 rendered,
636 "#EXTM3U\n#PLAYLIST:Mix\n#EXTINF:12,Hello, World Second, Line\nArtist/Track.flac\n"
637 );
638 assert!(!rendered.contains('\r'));
639 assert_eq!(rendered.lines().count(), 4);
641 }
642
643 #[test]
644 fn m3u8_folds_newlines_in_the_playlist_name() {
645 let rendered = render_m3u8("Road\r\nTrip", &[]);
646 assert_eq!(rendered, "#EXTM3U\n#PLAYLIST:Road Trip\n");
647 }
648
649 #[test]
650 fn m3u8_empty_list_is_header_and_name_only() {
651 assert_eq!(render_m3u8("Empty", &[]), "#EXTM3U\n#PLAYLIST:Empty\n");
652 }
653
654 #[test]
655 fn m3u8_absent_member_renders_a_comment_not_a_path() {
656 let entries = [
659 M3u8Entry {
660 title: "In Library",
661 duration_secs: 60.0,
662 relative_path: "Artist/In.flac",
663 },
664 M3u8Entry {
665 title: "Missing, Song",
666 duration_secs: 42.0,
667 relative_path: "",
668 },
669 M3u8Entry {
670 title: "Also Present",
671 duration_secs: 30.0,
672 relative_path: "Artist/Also.flac",
673 },
674 ];
675
676 let rendered = render_m3u8("Liked Songs", &entries);
677
678 let expected = "#EXTM3U\n\
679 #PLAYLIST:Liked Songs\n\
680 #EXTINF:60,In Library\n\
681 Artist/In.flac\n\
682 # (not in library) Missing, Song\n\
683 #EXTINF:30,Also Present\n\
684 Artist/Also.flac\n";
685 assert_eq!(rendered, expected);
686 assert!(!rendered.contains("#EXTINF:42"));
688 }
689
690 #[test]
691 fn m3u8_non_finite_duration_is_zero() {
692 let entries = [M3u8Entry {
693 title: "Unknown",
694 duration_secs: f64::NAN,
695 relative_path: "Artist/Unknown.flac",
696 }];
697
698 assert_eq!(
699 render_m3u8("Odd", &entries),
700 "#EXTM3U\n#PLAYLIST:Odd\n#EXTINF:0,Unknown\nArtist/Unknown.flac\n"
701 );
702 }
703
704 use crate::lineage::{Resolution, RootInfo};
705 use crate::manifest::ManifestEntry;
706 use serde_json::Value;
707 use std::collections::HashMap as Map;
708
709 fn manifest_entry(path: &str, format: AudioFormat, size: u64) -> ManifestEntry {
710 ManifestEntry {
711 path: path.to_owned(),
712 format,
713 size,
714 ..Default::default()
715 }
716 }
717
718 fn clip(id: &str, title: &str) -> Clip {
719 Clip {
720 id: id.to_owned(),
721 title: title.to_owned(),
722 display_name: "alice".to_owned(),
723 handle: "alice_handle".to_owned(),
724 tags: "ambient, cinematic".to_owned(),
725 duration: 211.0,
726 created_at: "2024-03-10T14:22:01Z".to_owned(),
727 ..Default::default()
728 }
729 }
730
731 fn lineage_store() -> LineageStore {
734 let child = Clip {
735 id: "child".to_owned(),
736 title: "Cover Take".to_owned(),
737 created_at: "2024-05-01T00:00:00Z".to_owned(),
738 clip_type: "gen".to_owned(),
739 task: "cover".to_owned(),
740 cover_clip_id: "root".to_owned(),
741 edited_clip_id: "root".to_owned(),
742 ..Default::default()
743 };
744 let root = Clip {
745 id: "root".to_owned(),
746 title: "Original".to_owned(),
747 created_at: "2024-04-01T00:00:00Z".to_owned(),
748 ..Default::default()
749 };
750 let mut roots = HashMap::new();
751 roots.insert(
752 "child".to_owned(),
753 RootInfo {
754 root_id: "root".to_owned(),
755 root_title: "Original".to_owned(),
756 status: ResolveStatus::Resolved,
757 },
758 );
759 roots.insert(
760 "root".to_owned(),
761 RootInfo {
762 root_id: "root".to_owned(),
763 root_title: "Original".to_owned(),
764 status: ResolveStatus::Resolved,
765 },
766 );
767 let resolution = Resolution {
768 roots,
769 gap_filled: Vec::new(),
770 };
771 let mut store = LineageStore::new();
772 store.update(&[child, root], &resolution, "2024-06-01T00:00:00Z");
773 store
774 }
775
776 fn parse(rendered: &str) -> Value {
777 serde_json::from_str(rendered).expect("index is valid JSON")
778 }
779
780 #[test]
781 fn index_empty_manifest_is_exact() {
782 let rendered = render_library_index(&Manifest::new(), &LineageStore::new(), &Map::new());
783 assert_eq!(rendered, "{\n \"schema_version\": 1,\n \"clips\": []\n}");
784 }
785
786 #[test]
787 fn index_schema_version_matches_constant() {
788 let value = parse(&render_library_index(
789 &Manifest::new(),
790 &LineageStore::new(),
791 &Map::new(),
792 ));
793 assert_eq!(value["schema_version"], INDEX_SCHEMA_VERSION);
794 }
795
796 #[test]
797 fn index_live_clip_uses_live_fields_and_canonical_album() {
798 let mut manifest = Manifest::new();
799 manifest.insert(
800 "child",
801 manifest_entry("Original/Cover Take.flac", AudioFormat::Flac, 99),
802 );
803 let store = lineage_store();
804 let clip = clip("child", "Cover Take");
805 let mut live: Map<&str, &Clip> = Map::new();
806 live.insert("child", &clip);
807
808 let value = parse(&render_library_index(&manifest, &store, &live));
809 let row = &value["clips"][0];
810 assert_eq!(row["id"], "child");
811 assert_eq!(row["path"], "Original/Cover Take.flac");
812 assert_eq!(row["format"], "flac");
813 assert_eq!(row["size"], 99);
814 assert_eq!(row["title"], "Cover Take");
815 assert_eq!(row["artist"], "alice");
816 assert_eq!(row["handle"], "alice_handle");
817 assert_eq!(row["album"], "Original");
819 assert_eq!(row["root_id"], "root");
820 assert_eq!(row["created_at"], "2024-05-01T00:00:00Z");
821 assert_eq!(row["duration"], 211.0);
822 assert_eq!(row["tags"], "ambient, cinematic");
823 }
824
825 #[test]
826 fn index_on_disk_clip_nulls_live_only_fields() {
827 let mut manifest = Manifest::new();
828 manifest.insert(
829 "child",
830 manifest_entry("Original/Cover Take.flac", AudioFormat::Mp3, 7),
831 );
832 let store = lineage_store();
833
834 let value = parse(&render_library_index(&manifest, &store, &Map::new()));
836 let row = &value["clips"][0];
837 assert_eq!(row["format"], "mp3");
839 assert_eq!(row["size"], 7);
840 assert_eq!(row["title"], "Cover Take");
841 assert_eq!(row["album"], "Original");
842 assert_eq!(row["root_id"], "root");
843 assert_eq!(row["created_at"], "2024-05-01T00:00:00Z");
844 assert!(row["artist"].is_null());
846 assert!(row["handle"].is_null());
847 assert!(row["duration"].is_null());
848 assert!(row["tags"].is_null());
849 }
850
851 #[test]
852 fn index_album_resolves_for_both_live_and_on_disk_clips() {
853 let mut manifest = Manifest::new();
854 manifest.insert(
855 "child",
856 manifest_entry("Original/a.flac", AudioFormat::Flac, 1),
857 );
858 let store = lineage_store();
859
860 let live_clip = clip("child", "Cover Take");
861 let mut live: Map<&str, &Clip> = Map::new();
862 live.insert("child", &live_clip);
863 let live_value = parse(&render_library_index(&manifest, &store, &live));
864 let on_disk_value = parse(&render_library_index(&manifest, &store, &Map::new()));
865
866 assert_eq!(live_value["clips"][0]["album"], "Original");
868 assert_eq!(on_disk_value["clips"][0]["album"], "Original");
869 }
870
871 #[test]
872 fn index_album_differs_from_sanitised_path_segment() {
873 let mut manifest = Manifest::new();
877 manifest.insert(
878 "child",
879 manifest_entry("AC-DC Live/song.flac", AudioFormat::Flac, 1),
880 );
881 let raw_root = Clip {
882 id: "root".to_owned(),
883 title: "AC/DC: Live!".to_owned(),
884 created_at: "2024-04-01T00:00:00Z".to_owned(),
885 ..Default::default()
886 };
887 let child = Clip {
888 id: "child".to_owned(),
889 title: "song".to_owned(),
890 clip_type: "gen".to_owned(),
891 task: "cover".to_owned(),
892 cover_clip_id: "root".to_owned(),
893 edited_clip_id: "root".to_owned(),
894 ..Default::default()
895 };
896 let mut roots = HashMap::new();
897 roots.insert(
898 "child".to_owned(),
899 RootInfo {
900 root_id: "root".to_owned(),
901 root_title: "AC/DC: Live!".to_owned(),
902 status: ResolveStatus::Resolved,
903 },
904 );
905 let resolution = Resolution {
906 roots,
907 gap_filled: Vec::new(),
908 };
909 let mut store = LineageStore::new();
910 store.update(&[child, raw_root], &resolution, "2024-06-01T00:00:00Z");
911
912 let value = parse(&render_library_index(&manifest, &store, &Map::new()));
913 let row = &value["clips"][0];
914 assert_eq!(row["album"], "AC/DC: Live!");
915 let album = row["album"].as_str().unwrap();
917 let path_segment = row["path"].as_str().unwrap().split('/').next().unwrap();
918 assert_ne!(album, path_segment);
919 assert_eq!(path_segment, "AC-DC Live");
920 }
921
922 #[test]
923 fn index_iterates_in_clip_id_order() {
924 let mut manifest = Manifest::new();
925 manifest.insert("c", manifest_entry("c.flac", AudioFormat::Flac, 1));
926 manifest.insert("a", manifest_entry("a.flac", AudioFormat::Flac, 1));
927 manifest.insert("b", manifest_entry("b.flac", AudioFormat::Flac, 1));
928
929 let value = parse(&render_library_index(
930 &manifest,
931 &LineageStore::new(),
932 &Map::new(),
933 ));
934 let ids: Vec<&str> = value["clips"]
935 .as_array()
936 .unwrap()
937 .iter()
938 .map(|row| row["id"].as_str().unwrap())
939 .collect();
940 assert_eq!(ids, ["a", "b", "c"]);
941 }
942
943 #[test]
944 fn index_unknown_clip_is_well_formed_with_defaults() {
945 let mut manifest = Manifest::new();
948 manifest.insert("orphan", manifest_entry("orphan.wav", AudioFormat::Wav, 3));
949
950 let value = parse(&render_library_index(
951 &manifest,
952 &LineageStore::new(),
953 &Map::new(),
954 ));
955 let row = &value["clips"][0];
956 assert_eq!(row["id"], "orphan");
957 assert_eq!(row["title"], "Untitled");
958 assert_eq!(row["format"], "wav");
959 assert_eq!(row["album"], "");
960 assert_eq!(row["root_id"], "orphan");
961 assert!(row["created_at"].is_null());
962 assert!(row["artist"].is_null());
963 assert!(row["tags"].is_null());
964 }
965
966 #[test]
967 fn index_title_falls_back_to_store_node_then_untitled() {
968 let mut manifest = Manifest::new();
969 manifest.insert("child", manifest_entry("x.flac", AudioFormat::Flac, 1));
970 let store = lineage_store();
971 let value = parse(&render_library_index(&manifest, &store, &Map::new()));
973 assert_eq!(value["clips"][0]["title"], "Cover Take");
974 }
975
976 #[test]
977 fn index_artist_falls_back_to_suno_when_display_name_empty() {
978 let mut manifest = Manifest::new();
979 manifest.insert("child", manifest_entry("x.flac", AudioFormat::Flac, 1));
980 let mut anon = clip("child", "Cover Take");
981 anon.display_name = String::new();
982 let mut live: Map<&str, &Clip> = Map::new();
983 live.insert("child", &anon);
984 let value = parse(&render_library_index(
985 &manifest,
986 &LineageStore::new(),
987 &live,
988 ));
989 assert_eq!(value["clips"][0]["artist"], "Suno");
991 }
992
993 #[test]
994 fn index_unicode_round_trips() {
995 let mut manifest = Manifest::new();
996 manifest.insert("🎵", manifest_entry("音楽/曲.flac", AudioFormat::Flac, 5));
997 let unicode = clip("🎵", "音楽 \"quoted\"");
998 let mut live: Map<&str, &Clip> = Map::new();
999 live.insert("🎵", &unicode);
1000
1001 let rendered = render_library_index(&manifest, &LineageStore::new(), &live);
1002 let value = parse(&rendered);
1003 let row = &value["clips"][0];
1004 assert_eq!(row["id"], "🎵");
1005 assert_eq!(row["path"], "音楽/曲.flac");
1006 assert_eq!(row["title"], "音楽 \"quoted\"");
1007 }
1008}