Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { COVER_SCALE_CAP, coverHasLanded, coverRequestSize, createScrollPace, forgetLandedCovers, markCoverLanded, SETTLE_MS,} from "./covers";
/** * When a cover is worth asking for, and how big to ask for. * * The gallery used to ask per cell, unconditionally, at a flat 2×. A fling down * the collection therefore put several hundred server-side thumbnail resamples in * front of the ~40 covers the reader stopped to look at, and the ones that * mattered arrived a second late. These are the two decisions that replaced it. */
const ROW = 208; // CELL_H: one row of cellsconst FLING = ROW * 10; // ten rows in one event: unambiguously a fling
/** The pace, plus the report it gave its owner — which is what the grid renders on. */function makePace(options: { rowHeight?: number } = {}): { pace: ReturnType<typeof createScrollPace>; seen: boolean[]; held: () => boolean;} { const seen: boolean[] = []; const pace = createScrollPace(options.rowHeight ?? ROW, (v) => seen.push(v)); return { pace, seen, held: () => pace.isFlinging() };}
afterEach(() => forgetLandedCovers());
describe("createScrollPace — the fling guard", () => { beforeEach(() => vi.useFakeTimers()); afterEach(() => vi.useRealTimers());
it("holds nothing while the reader scrolls one album at a time", () => { const { pace, seen, held } = makePace(); // A thumb dragging at four rows a second: reading speed. for (const top of [40, 80, 120]) { vi.advanceTimersByTime(100); pace.onScroll(top); } expect(held()).toBe(false); expect(seen).toEqual([]); // and its owner was never told anything pace.dispose(); });
it("holds while the scroller is moving faster than a reader can look at", () => { const { pace, seen, held } = makePace(); pace.onScroll(0); vi.advanceTimersByTime(16); // one frame pace.onScroll(FLING); expect(held()).toBe(true); expect(seen).toEqual([true]); pace.dispose(); });
it("says so once, not once per event", () => { const { pace, seen } = makePace(); let top = 0; for (let i = 0; i < 10; i++) { vi.advanceTimersByTime(16); top += FLING; pace.onScroll(top); } expect(seen).toEqual([true]); pace.dispose(); });
it("releases a cover that would otherwise be held for ever", () => { // The last scroll event of a fling is followed by nothing at all, so the // release has to be a timer: without it the gallery would go on holding covers // for a reader who stopped a minute ago. const { pace, seen, held } = makePace(); pace.onScroll(0); vi.advanceTimersByTime(16); pace.onScroll(FLING); expect(held()).toBe(true);
vi.advanceTimersByTime(SETTLE_MS - 1); expect(held()).toBe(true); // still moving, as far as it knows vi.advanceTimersByTime(2); expect(held()).toBe(false); expect(seen).toEqual([true, false]); pace.dispose(); });
it("does not release mid-fling, however long the fling", () => { const { pace, held } = makePace(); let top = 0; for (let i = 0; i < 40; i++) { vi.advanceTimersByTime(16); top += FLING; pace.onScroll(top); expect(held()).toBe(true); } pace.dispose(); });
it("measures from the pace's own birth, not from the epoch", () => { // The first scroll of a session is the one that opens the gallery with a // flick; measured against 1970 it would be infinitely slow, and the guard // would be off for exactly the case it exists for. const { pace, held } = makePace(); pace.onScroll(5000); expect(held()).toBe(true); pace.dispose(); });
it("does not call a movement after a pause a fling", () => { // A `PageDown`, or a thumb returning to the wheel after reading: one event, // a long gap since the last. Those are covers the reader means to look at. const { pace, held } = makePace(); pace.onScroll(100); vi.advanceTimersByTime(500); pace.onScroll(100 + FLING); expect(held()).toBe(false); pace.dispose(); });
it("stops holding when the component that was watching goes away", () => { const { pace, seen, held } = makePace(); pace.onScroll(0); vi.advanceTimersByTime(16); pace.onScroll(FLING); expect(held()).toBe(true);
pace.dispose(); expect(held()).toBe(false); expect(seen).toEqual([true, false]); // the owner is told, so it stops holding // …and the pending release does not fire into a disposed pace. vi.advanceTimersByTime(SETTLE_MS * 3); expect(seen).toEqual([true, false]); });});
describe("coverRequestSize — the bytes asked for", () => { it("asks a 1× screen for the tile's own pixels, not four times them", () => { expect(coverRequestSize(160, 1)).toBe(160); });
it("asks a HiDPI screen for the 2× it can show, and stops there", () => { expect(coverRequestSize(160, 2)).toBe(320); expect(coverRequestSize(160, 3)).toBe(160 * COVER_SCALE_CAP); // capped: invisible pixels });
it("never asks below the tile, whatever the ratio claims", () => { // Zooming out reports a ratio below 1; asking for fewer pixels than the tile // is a request to upscale a blur. expect(coverRequestSize(160, 0.5)).toBe(160); expect(coverRequestSize(160, 0)).toBe(160); expect(coverRequestSize(160, Number.NaN)).toBe(160); expect(coverRequestSize(160, -3)).toBe(160); });});
describe("the landed ledger", () => { it("knows a cover this page has already displayed", () => { const url = "https://nav/rest/getCoverArt?id=al-1&size=160"; expect(coverHasLanded(url)).toBe(false); markCoverLanded(url); expect(coverHasLanded(url)).toBe(true); });
it("does not treat 'no cover to ask for' as a cover that landed", () => { markCoverLanded(""); expect(coverHasLanded("")).toBe(false); });
it("stays bounded, dropping the oldest covers first", () => { for (let i = 0; i < 5000; i++) markCoverLanded(`https://nav/rest/getCoverArt?id=a-${i}`); // The first ones are gone; the most recent are what a reader is likely to // scroll back to. expect(coverHasLanded("https://nav/rest/getCoverArt?id=a-0")).toBe(false); expect(coverHasLanded("https://nav/rest/getCoverArt?id=a-4999")).toBe(true); });});