import { describe, expect, it } from "vitest"; import type { LibrarySong } from "../harvest/types"; import { albumRuns, buildRows, codecKey, durationParts, fmtDuration, nameKey, trackBadges, } from "./rows"; const song = (over: Partial): LibrarySong => ({ id: Math.random().toString(36).slice(2), title: "t", source: "navidrome", ...over, }); describe("albumRuns", () => { it("slices the list into the albums a listener hears", () => { const runs = albumRuns([ song({ artist: "A", album: "One", albumId: "a", track: 1 }), song({ artist: "A", album: "One", albumId: "a", track: 2 }), song({ artist: "A", album: "Two", albumId: "b", track: 1 }), ]); expect(runs.map((r) => r.map((s) => s.title))).toEqual([["t", "t"], ["t"]]); expect(runs.map((r) => r.length)).toEqual([2, 1]); }); it("two records called Live are not one album", () => { // the id decides when the server has one; the title only ever falls back const runs = albumRuns([ song({ album: "Live", albumId: "one", artist: "X" }), song({ album: "Live", albumId: "two", artist: "Y" }), ]); expect(runs).toHaveLength(2); }); it("is contiguous, not grouped: a run broken in the list stays broken", () => { // the list never does this (it sorts by album), but the function's contract // is "contiguous runs", and repeat + random album both depend on matching // what the rows actually show const a = song({ album: "A", albumId: "a" }); const b = song({ album: "B", albumId: "b" }); expect(albumRuns([a, b, a])).toHaveLength(3); }); it("has nothing to say about an empty list", () => { expect(albumRuns([])).toEqual([]); }); it("does not cut a run in half over a space in the tag", () => { // Without ids the name decides, and the list sorts by the folded name: two // spellings of one record must be one run, or repeat=album stops early. const runs = albumRuns([ song({ artist: "Luis Hernandez", album: "JAZZPUNK", track: 1 }), song({ artist: "luis hernandez ", album: "jazzpunk ", track: 2 }), ]); expect(runs).toHaveLength(1); expect(runs[0]).toHaveLength(2); }); }); describe("nameKey", () => { it("loses the difference between two spellings of one name", () => { expect(nameKey("Luis Hernandez ")).toBe(nameKey("Luis Hernandez")); expect(nameKey(" Luis\u00a0Hernandez")).toBe(nameKey("Luis Hernandez")); expect(nameKey("luis hernandez")).toBe(nameKey("Luis Hernandez")); expect(nameKey("JAZZPUNK")).toBe(nameKey("jazzpunk")); // a compatibility fold, so a full-width spelling is the same record too expect(nameKey("JAZZPUNK")).toBe(nameKey("jazzpunk")); }); it("still separates names that are actually different", () => { expect(nameKey("Live")).not.toBe(nameKey("Live!")); expect(nameKey(undefined)).toBe(""); expect(nameKey(" ")).toBe(""); // whitespace-only is no artist at all }); }); describe("buildRows", () => { it("groups tracks under artist and album section rows", () => { const rows = buildRows([ song({ artist: "Boards of Canada", album: "Music Has the Right", track: 2, title: "B" }), song({ artist: "Aphex Twin", album: "Selected", track: 1, title: "Xtal" }), song({ artist: "Aphex Twin", album: "Selected", track: 2, title: "Q1" }), song({ artist: "Aphex Twin", album: " drukqs", track: 1, title: "Rug" }), ]); expect(rows.map((r) => r.kind)).toEqual([ "artist", // aphex twin (case-insensitive alpha order) "album", // drukqs "track", "album", // selected "track", "track", "artist", // boards of canada "album", "track", ]); const first = rows[0]; expect(first).toMatchObject({ kind: "artist", label: "Aphex Twin", songs: 3, albums: 2 }); const album = rows[1]; expect(album).toMatchObject({ kind: "album", label: " drukqs", songs: 1 }); }); it("carries the artist's id down from the tracks that have one", () => { // The id an artist row is located by, and nothing else: /api/song, which is where this // library is built from, names the artist id on the *track*, never on a row for the // artist, so aggregating it here is the only place a client can pick it up. const rows = buildRows([ song({ artist: "Autechre", artistId: "ar-9", album: "Amadja", albumId: "al-1", track: 1 }), song({ artist: "Autechre", artistId: "ar-9", album: "Amadja", albumId: "al-1", track: 2 }), song({ artist: "Kokoko!", artistId: "ar-8", album: "Amadja", albumId: "al-2", track: 1 }), ]); const artists = rows.filter((r) => r.kind === "artist"); expect(artists.map((r) => (r.kind === "artist" ? r.artistId : undefined))).toEqual([ "ar-9", // Autechre sorts before Kokoko! "ar-8", ]); }); it("gives an artist assembled from tracks without ids no id", () => { // An untagged library (or a server that omits artistId): the row exists, the id does // not, and nothing here should guess one off the label. const rows = buildRows([song({ artist: "Autechre", album: "Amadja", track: 1 })]); const artist = rows.find((r) => r.kind === "artist"); expect(artist).toMatchObject({ kind: "artist", label: "Autechre" }); expect(artist!.kind === "artist" && artist!.artistId).toBeUndefined(); }); it("orders tracks by disc then track then title", () => { const rows = buildRows([ song({ artist: "a", album: "b", disc: 2, track: 1, title: "late disc" }), song({ artist: "a", album: "b", disc: 1, track: 5, title: "five" }), song({ artist: "a", album: "b", disc: 1, track: 1, title: "one" }), song({ artist: "a", album: "b", disc: 2, track: undefined, title: "zz unnumbered" }), ]); const tracks = rows.filter((r) => r.kind === "track").map((r) => (r as any).song.title); expect(tracks).toEqual(["one", "five", "late disc", "zz unnumbered"]); }); it("buckets unknown artist and missing album to the end with labels", () => { const rows = buildRows([ song({ title: "loose" }), song({ artist: "Zeta", title: "z" }), song({ artist: "alpha", title: "a" }), ]); const artists = rows.filter((r) => r.kind === "artist").map((r) => (r as any).label); expect(artists).toEqual(["alpha", "Zeta", "(unknown artist)"]); const albumRow = rows.filter((r) => r.kind === "album").at(-1) as any; expect(albumRow.label).toBe("(no album)"); }); it("keys rows stably for keyed For", () => { const songs = [song({ artist: "a", album: "b", id: "id-1", title: "x" })]; const a = buildRows(songs).map((r) => r.key); const b = buildRows(songs).map((r) => r.key); expect(a).toEqual(b); expect(new Set(a).size).toBe(a.length); }); it("gives one artist one section, however the tags are padded", () => { // A re-rip whose ALBUM ARTIST is `"Luis Hernandez "` used to land in a second // `Luis Hernandez` header — and the album sections inside it are keyed by // artist too, so the same record appeared under both. const rows = buildRows([ song({ artist: "Luis Hernandez", album: "JAZZPUNK", track: 1, title: "one" }), song({ artist: "Luis Hernandez ", album: "JAZZPUNK", track: 2, title: "two" }), song({ artist: "luis\u00a0hernandez", album: "jazzpunk ", track: 3, title: "three" }), ]); const artists = rows.filter((r) => r.kind === "artist"); const albums = rows.filter((r) => r.kind === "album"); expect(artists).toHaveLength(1); expect(artists[0]).toMatchObject({ songs: 3, albums: 1 }); expect(albums).toHaveLength(1); // the label is the server's own text, not the folded key it grouped by expect(artists[0]!.kind === "artist" && artists[0]!.label).toBe("Luis Hernandez"); }); }); describe("trackBadges", () => { it("leads with the codec, then bitrate / bit depth / sample rate", () => { expect( trackBadges(song({ suffix: "FLAC", bitRate: 1411, bitDepth: 24, sampleRate: 88200 })), ).toEqual([ { label: "flac", kind: "codec", codec: "flac" }, // normalized id drives the hue { label: "1411k", kind: "bitrate" }, { label: "24b", kind: "depth" }, { label: "88.2hz", kind: "rate" }, ]); }); it("colors the format alone — measurement chips carry no hue key", () => { const measurements = trackBadges(song({ bitRate: 320, bitDepth: 16, sampleRate: 44100 })); expect(measurements.map((b) => b.kind)).toEqual(["bitrate", "depth", "rate"]); for (const b of measurements) expect(b.codec).toBeUndefined(); }); it("folds format spellings onto one hue key so a format cannot diverge in color", () => { expect(codecKey("Vorbis")).toBe("ogg"); expect(codecKey("WAVE")).toBe("wav"); expect(codecKey(" AIF ")).toBe("aiff"); expect(codecKey("mp4a")).toBe("m4a"); expect(codecKey("apple_lossless")).toBe("alac"); expect(codecKey("FLAC")).toBe("flac"); expect(codecKey(undefined)).toBeUndefined(); expect(codecKey(" ")).toBeUndefined(); // an unknown container still gets a chip, it just holds no hue of its own expect(codecKey("wv")).toBe("wv"); expect(trackBadges(song({ suffix: "wv" }))).toEqual([ { label: "wv", kind: "codec", codec: "wv" }, ]); }); it("believes the probe over the filename", () => { // FLAC audio filed as `.mp3`: Navidrome's own chip prints the extension and // calls it MP3, but it probed `flac` and stored that. Where the server has // probed, the chip says what is in the file. expect(trackBadges(song({ suffix: "mp3", codec: "flac", bitRate: 320 }))).toEqual([ { label: "flac", kind: "codec", codec: "flac" }, { label: "320k", kind: "bitrate" }, ]); }); it("falls back to the container when the server probed nothing", () => { // the portable Subsonic crawl carries `suffix` and no codec at all, so the // extension is the best available answer — and the only one on those servers expect(trackBadges(song({ suffix: "flac", bitRate: 900 }))[0]).toEqual({ label: "flac", kind: "codec", codec: "flac", }); // an empty probe is not a probe: it must not swallow the container chip expect(trackBadges(song({ suffix: "flac", codec: " " }))[0]!.codec).toBe("flac"); }); it("folds ffprobe's bit-qualified names onto the format they name", () => { expect(codecKey("pcm_s24le")).toBe("wav"); expect(codecKey("PCM_F32BE")).toBe("wav"); expect(codecKey("dsd_msbf_planar")).toBe("dsd"); expect(trackBadges(song({ suffix: "dff", codec: "dsd_lsbf_planar" }))).toEqual([ { label: "dsd", kind: "codec", codec: "dsd" }, ]); }); it("omits missing fields rather than rendering empty chips", () => { expect(trackBadges(song({ suffix: "mp3" }))).toEqual([ { label: "mp3", kind: "codec", codec: "mp3" }, ]); expect(trackBadges(song({ sampleRate: 44100 }))).toEqual([{ label: "44.1hz", kind: "rate" }]); expect(trackBadges(song({}))).toEqual([]); }); it("rounds fractional bitrates and drops zero values", () => { expect(trackBadges(song({ bitRate: 319.6 }))).toEqual([{ label: "320k", kind: "bitrate" }]); expect(trackBadges(song({ bitRate: 0, sampleRate: 0, suffix: "ogg" }))).toEqual([ { label: "ogg", kind: "codec", codec: "ogg" }, ]); }); }); describe("durationParts", () => { it("fades only the leading segments that carry no information", () => { expect(durationParts(7)).toEqual({ faded: "00:00:", strong: "07" }); expect(durationParts(175)).toEqual({ faded: "00:", strong: "02:55" }); expect(durationParts(0)).toEqual({ faded: "00:00:", strong: "00" }); // once the hours are real they are the news, so nothing fades expect(durationParts(3600)).toEqual({ faded: "", strong: "01:00:00" }); expect(durationParts(4325)).toEqual({ faded: "", strong: "01:12:05" }); expect(durationParts(undefined)).toEqual({ faded: "", strong: "" }); }); it("joins back to exactly what fmtDuration renders", () => { for (const s of [0, 7, 175, 600, 3600, 3723, 43_250]) { const p = durationParts(s); expect(p.faded + p.strong).toBe(fmtDuration(s)); expect(p.faded + p.strong).toHaveLength(8); } }); }); describe("fmtDuration", () => { it("always renders the 8-char hh:mm:ss form so the time column stays rigid", () => { expect(fmtDuration(0)).toBe("00:00:00"); expect(fmtDuration(7)).toBe("00:00:07"); expect(fmtDuration(175)).toBe("00:02:55"); expect(fmtDuration(600)).toBe("00:10:00"); expect(fmtDuration(3600)).toBe("01:00:00"); expect(fmtDuration(3723)).toBe("01:02:03"); expect(fmtDuration(43_250)).toBe("12:00:50"); // long live sets keep 2-digit hours expect(fmtDuration(undefined)).toBe(""); }); });