import { describe, expect, it } from "vitest"; import type { LibrarySong } from "../harvest/types"; import { albumCells, gridColumns, gridRows, revealRowIndex, sameCell, sameRows, type GridAlbum, } from "./grid"; import { buildRows } from "./rows"; const song = (over: Partial): LibrarySong => ({ id: Math.random().toString(36).slice(2), title: "t", source: "navidrome", ...over, }); const library_: LibrarySong[] = [ song({ artist: "Aphex Twin", album: "Selected Ambient", albumId: "al-1", coverArt: "art-1" }), song({ artist: "Aphex Twin", album: "Richard D. James", albumId: "al-2" }), // no artwork id song({ artist: "Autechre", album: "Amber", albumId: "al-3", coverArt: "art-3" }), ]; describe("albumCells", () => { const rows = buildRows(library_); const cells = albumCells(rows); it("yields one cell per album section, in list order", () => { expect(cells.map((c) => c.name)).toEqual([ "Richard D. James", // sorted by album within the artist "Selected Ambient", "Amber", ]); }); it("carries the row index its album section occupies in the same rows array", () => { // this is what makes click-to-reveal a direct scroll, not a search for (const cell of cells) { expect(rows[cell.rowIndex]?.kind).toBe("album"); expect(rows[cell.rowIndex]).toMatchObject({ key: cell.key, label: cell.name }); } }); it("prefers the album's art id and falls back to the album id", () => { expect(cells.find((c) => c.name === "Amber")?.coverId).toBe("art-3"); expect(cells.find((c) => c.name === "Richard D. James")?.coverId).toBe("al-2"); expect(albumCells(buildRows([song({ album: "X", artist: "Y" })]))[0]?.coverId).toBeUndefined(); }); }); describe("revealRowIndex", () => { const rows = buildRows(library_); const cells = albumCells(rows); it("locates the album row a cell was built from", () => { for (const cell of cells) expect(revealRowIndex(rows, cell)).toBe(cell.rowIndex); }); it("still lands on the album when the keys have diverged", () => { // a harvest can rebuild the rows between the cover rendering and the click const stale = { ...cells[0]!, key: "al:stale\u0000keys" }; expect(revealRowIndex(rows, stale)).toBe(cells[0]!.rowIndex); }); it("returns -1 rather than scrolling somewhere wrong", () => { // key AND identity must both miss — the key is the primary lookup, so an // unknown album with a live key would (correctly) still resolve to it const stranger = { ...cells[0]!, key: "al:gone", name: "Never Harvested", artist: "Nobody" }; expect(revealRowIndex(rows, stranger)).toBe(-1); // an artist row shares the label space and must not be mistaken for an album const artistNamed = { ...cells[0]!, key: "al:gone", name: "Autechre", artist: "Autechre" }; expect(revealRowIndex(rows, artistNamed)).toBe(-1); }); }); describe("gridColumns", () => { it("fits whole cells and never returns fewer than one column", () => { expect(gridColumns(1000, 170, 10)).toBe(5); // 5×170 + 4×10 = 890, a 6th needs 1070 expect(gridColumns(200, 170, 10)).toBe(1); expect(gridColumns(0, 170, 10)).toBe(1); // unmeasured / hidden container expect(gridColumns(Number.NaN, 170, 10)).toBe(1); }); }); describe("gridRows", () => { it("chunks cells into rows of n, last row short", () => { const cells = albumCells(buildRows(library_)); expect(gridRows(cells, 2).map((r) => r.length)).toEqual([2, 1]); expect(gridRows([], 4)).toEqual([]); expect(gridRows(cells, 0).map((r) => r.length)).toEqual([1, 1, 1]); // guarded }); }); describe("sameCell / sameRows — the equality is protected by", () => { const cellsOf = () => albumCells(buildRows(library_)); it("calls a fresh derivation of the same library the same window", () => { // The whole point: `albumCells` returns new objects every pass, so this is // the case that used to tear the gallery down and re-ask for every cover. expect(sameRows(gridRows(cellsOf(), 2), gridRows(cellsOf(), 2))).toBe(true); expect( sameRows( gridRows(cellsOf(), 2), gridRows(cellsOf(), 2).map((r) => r.slice()), ), ).toBe(true); }); it("notices a cover that arrived, since that is the tile changing", () => { const before = gridRows(cellsOf(), 2); // `albumCells` falls back to the album id, so every cell here already has a // cover — the change has to be to a different picture, not to having one. const withArt = gridRows( cellsOf().map((c) => ({ ...c, coverId: "art-new" })), 2, ); expect(sameRows(before, withArt)).toBe(false); const gained = gridRows( cellsOf().map((c, i) => (i === 0 ? { ...c, coverId: undefined } : c)), 2, ); expect(sameRows(before, gained)).toBe(false); }); it("notices a caption, a count, a year, and where the album now sits", () => { const base = cellsOf()[0]!; expect(sameCell(base, { ...base })).toBe(true); for (const over of [ { name: "Renamed" }, { artist: "Someone else" }, { year: 1998 }, { songCount: 99 }, { coverId: "art-other" }, { rowIndex: base.rowIndex + 1 }, { key: "al:different" }, ] as Partial[]) { expect(sameCell(base, { ...base, ...over })).toBe(false); } }); it("sees one changed cell anywhere in the window, and a column count that moved", () => { const cells = cellsOf(); const last = cells.length - 1; const edited = cells.map((c, i) => (i === last ? { ...c, name: "Tail edited" } : c)); expect(sameRows(gridRows(cells, 2), gridRows(edited, 2))).toBe(false); // same cells, different shape: 3 cells are [2,1] at two columns and [3] at three expect(sameRows(gridRows(cells, 2), gridRows(cells, 3))).toBe(false); }); });