Files
jellytau/src/lib/stores/favorites.test.ts
dtourolle 62873cab3d feat(search): answer search from a local index; tier downloads by lifetime
Search's instant leg read only downloaded items, so with no downloads it
returned nothing and every keystroke fell through to a full Recursive=true
server query. It now reads the whole synced catalog through the same
availability CTE get_items uses, gated on the same include_catalog_browse
flag so search and browse cannot diverge. (UR-065, DR-108)

Also fixes three defects found while confirming that:

- items_fts grew by a full duplicate index every catalog pass. INSERT OR
  REPLACE fires no AFTER DELETE trigger without recursive_triggers, so the
  old index row was orphaned, and a TEXT PRIMARY KEY meant the replacement
  took a fresh rowid and inserted a second entry. Now a real upsert, with
  migration 021 rebuilding existing indexes. (DR-110)
- DELETE FROM items existed nowhere, so server-side deletions never
  propagated. Adds a post-crawl mark-and-sweep, scoped to crawled types,
  skipping downloaded items, and refusing to run after a partial crawl
  because items.parent_id cascades. (DR-110)
- The index omitted MusicArtist, Playlist and People, which search groups
  results by. Adds them plus people_fts (migration 022). (DR-111)

Re-indexing moves from a frontend startup call to a Rust background task
with a 6h TTL, so a long session no longer searches a stale catalog and a
restart no longer forces a crawl regardless of freshness. (DR-109, IR-030)

Downloads gain a lifetime tier. Eviction selected every completed row by
age with no download_source filter, so hitting the storage limit deleted
the oldest download -- typically one saved deliberately for offline -- to
make room for a precached track. It now reclaims only 'auto' rows, and
expired ones are reclaimed first, before live cache is evicted.
(DR-126, DR-127)

Downloaded video and audio-only handoffs now play from disk instead of
streaming; the video path had never consulted downloads at all. No
transcode is involved: MPV runs video=no and ExoPlayer has no surface for
an Audio item. (DR-123 in part, DR-128)

FTS queries are built as quoted phrases so apostrophes, hyphens and
slashes are data rather than operator syntax, and the item-type filter is
bound rather than interpolated.

Specs: docs/specs/catalog-index-search.md,
docs/specs/read-through-media-cache.md

Includes concurrently-developed favourites browsing and background-audio
stream-end handling; the two workstreams share offline.rs, lib.rs and
online.rs, so no subset of files builds independently.
2026-08-04 17:35:17 +02:00

102 lines
3.4 KiB
TypeScript

// TRACES: UR-067, UR-068 | DR-117, DR-119 | UT-105, UT-106
import { describe, it, expect, beforeEach } from "vitest";
import { get } from "svelte/store";
import type { MediaItem } from "$lib/api/types";
import {
favoriteOverrides,
setFavorite,
clearFavorite,
clearAllFavorites,
resolveIsFavorite,
isFavoriteNow,
retainFavorites,
} from "./favorites";
function item(id: string, isFavorite?: boolean): MediaItem {
return {
id,
name: `Item ${id}`,
type: "Movie",
kind: "movie",
isFolder: false,
serverId: "s1",
userData: isFavorite === undefined ? undefined : { isFavorite },
} as unknown as MediaItem;
}
describe("favorites store", () => {
beforeEach(() => clearAllFavorites());
describe("resolveIsFavorite (UT-105)", () => {
it("falls back to the server's userData when nothing was toggled here", () => {
expect(resolveIsFavorite(item("a", true), new Map())).toBe(true);
expect(resolveIsFavorite(item("a", false), new Map())).toBe(false);
});
it("treats an item with no userData as not favourited", () => {
expect(resolveIsFavorite(item("a"), new Map())).toBe(false);
});
it("lets a session override win over userData", () => {
// The whole point: after tapping the heart on a card, the item object
// still carries the server's stale value until the next fetch.
expect(resolveIsFavorite(item("a", false), new Map([["a", true]]))).toBe(true);
expect(resolveIsFavorite(item("a", true), new Map([["a", false]]))).toBe(false);
});
it("is false for a missing item rather than throwing", () => {
expect(resolveIsFavorite(null, new Map())).toBe(false);
expect(resolveIsFavorite(undefined, new Map())).toBe(false);
});
});
describe("overrides", () => {
it("publishes a toggle to subscribers", () => {
setFavorite("a", true);
expect(get(favoriteOverrides).get("a")).toBe(true);
expect(isFavoriteNow(item("a", false))).toBe(true);
setFavorite("a", false);
expect(isFavoriteNow(item("a", true))).toBe(false);
});
it("clearing an override hands authority back to the item's userData", () => {
setFavorite("a", false);
expect(isFavoriteNow(item("a", true))).toBe(false);
clearFavorite("a");
expect(isFavoriteNow(item("a", true))).toBe(true);
});
it("replaces the map so Svelte sees a new reference", () => {
const before = get(favoriteOverrides);
setFavorite("a", true);
expect(get(favoriteOverrides)).not.toBe(before);
});
});
describe("retainFavorites (UT-106)", () => {
it("drops an item un-favourited during this session", () => {
const items = [item("a", true), item("b", true)];
const kept = retainFavorites(items, new Map([["a", false]]));
expect(kept.map((i) => i.id)).toEqual(["b"]);
});
it("keeps everything when nothing was toggled", () => {
const items = [item("a", true), item("b", true)];
expect(retainFavorites(items, new Map())).toHaveLength(2);
});
it("keeps an item favourited during this session even if the server said otherwise", () => {
const items = [item("a", false)];
expect(retainFavorites(items, new Map([["a", true]]))).toHaveLength(1);
});
it("drops items the server never marked as favourites", () => {
// A listing fetched with a stale scope should not keep non-favourites.
expect(retainFavorites([item("a")], new Map())).toHaveLength(0);
});
});
});