Files
siftlode/frontend/src/lib/queryKeys.test.ts
T
peter 70a74f8629 fix(plex): round-3 deep-review follow-ups
Third review round, scoped to the round-2 fix commit. The production ternary was
confirmed correct; two sibling-surface findings:

- The sidebar CollectionPicker's clear-all still wrote collectionTitles:{} while the
  chip-row remove (fixed in round 2) wrote undefined — a divergence that leaves the
  {}-vs-absent cache split on the picker path. Collapse an empty map to undefined there
  too, so both no-collection routes key-match the pristine state.
- The queryKeys lang test was near-tautological (a variadic-spread factory can't drop a
  passed arg), and nothing asserted the CALL SITE keys carry lang. Add a real guard in
  PlexDiscover.dom.test.tsx that inspects the registered query keys for scope+lang, and
  reword the factory test's comment to say it only shape-pins the segment pass-through.

Gate: tsc/lint/knip/prettier clean, 413 vitest.
2026-08-23 02:09:14 +02:00

108 lines
4.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { qk } from "./queryKeys";
import type { FeedFilters } from "./api";
// The factory is the single source of truth for ~200 React Query keys; these tests lock the exact
// array shapes it produces, because TanStack invalidation is prefix-MATCHING on those arrays — a
// silent shape change (a flipped guard, a dropped spread) would break refetching with no type error.
const FILTERS = { scope: "my", tags: [] } as unknown as FeedFilters;
describe("qk — bare vs parameterized branch", () => {
it("collapses to the bare prefix key only when no arg is given", () => {
expect(qk.feed()).toEqual(["feed"]);
expect(qk.feed(FILTERS)).toEqual(["feed", FILTERS]);
expect(qk.feedCount()).toEqual(["feed-count"]);
expect(qk.feedCount(FILTERS)).toEqual(["feed-count", FILTERS]);
expect(qk.facets()).toEqual(["facets"]);
expect(qk.facets(FILTERS)).toEqual(["facets", FILTERS]);
expect(qk.feedSummary()).toEqual(["feed-summary"]);
expect(qk.feedSummary(FILTERS)).toEqual(["feed-summary", FILTERS]);
expect(qk.playlist()).toEqual(["playlist"]);
expect(qk.plexShow()).toEqual(["plex-show"]);
expect(qk.plexShow("s1")).toEqual(["plex-show", "s1"]);
expect(qk.ytSearch()).toEqual(["yt-search"]);
});
it("preserves a NULL segment instead of collapsing it to the prefix", () => {
// `["playlist", null]` must stay 2-element — a null id is a real (distinct) key, not "no arg".
expect(qk.playlist(null)).toEqual(["playlist", null]);
expect(qk.playlist(7)).toEqual(["playlist", 7]);
expect(qk.ytSearch(null, 3)).toEqual(["yt-search", null, 3]);
expect(qk.ytSearch("cats", 3)).toEqual(["yt-search", "cats", 3]);
expect(qk.thread(null)).toEqual(["thread", null]);
expect(qk.thread(42)).toEqual(["thread", 42]);
});
});
describe("qk — single-id and scalar keys", () => {
it("returns [root, id]", () => {
expect(qk.channel("c1")).toEqual(["channel", "c1"]);
expect(qk.videoDetail("v1")).toEqual(["video-detail", "v1"]);
expect(qk.playlistMembership("v1")).toEqual(["playlist-membership", "v1"]);
expect(qk.plexItem("i1")).toEqual(["plex-item", "i1"]);
expect(qk.plexPlaylist(9)).toEqual(["plex-playlist", 9]);
expect(qk.downloadLinks(5)).toEqual(["download-links", 5]);
expect(qk.dlQuota(3)).toEqual(["dl-quota", 3]);
expect(qk.storyboard(5)).toEqual(["storyboard", 5]);
expect(qk.adminQuota(30)).toEqual(["admin-quota", 30]);
expect(qk.plexPeopleCards("both", ["Actor"])).toEqual(["plex-people-cards", "both", ["Actor"]]);
});
it("returns the bare root for no-arg keys", () => {
expect(qk.me()).toEqual(["me"]);
expect(qk.myStatus()).toEqual(["my-status"]);
expect(qk.channels()).toEqual(["channels"]);
expect(qk.playlists()).toEqual(["playlists"]);
expect(qk.conversations()).toEqual(["conversations"]);
expect(qk.downloads()).toEqual(["downloads"]);
expect(qk.adminUsers()).toEqual(["admin-users"]);
expect(qk.plex()).toEqual(["plex"]);
});
});
describe("qk — variadic composite plex keys", () => {
it("spreads the rest args after the root, and is bare with none", () => {
expect(qk.plexLibrary()).toEqual(["plex-library"]);
expect(qk.plexLibrary("movie", "q", "title", "s", FILTERS)).toEqual([
"plex-library",
"movie",
"q",
"title",
"s",
FILTERS,
]);
expect(qk.plexCollections()).toEqual(["plex-collections"]);
expect(qk.plexCollections("lib")).toEqual(["plex-collections", "lib"]);
expect(qk.plexCollections("union", "term")).toEqual(["plex-collections", "union", "term"]);
expect(qk.plexPlaylists()).toEqual(["plex-playlists"]);
expect(qk.plexPlaylists("group", ["1", "2"])).toEqual(["plex-playlists", "group", ["1", "2"]]);
expect(qk.plexFacets("both", "s", { genres: [] })).toEqual([
"plex-facets",
"both",
"s",
{ genres: [] },
]);
});
it("preserves scope+lang+filters as distinct Discover/Surprise key segments", () => {
// Shape-pinning only: these lock that the variadic factory carries every arg through (a "tidy-up"
// that drops a param would break cache separation). That the CALL SITE actually passes lang is
// guarded separately in PlexDiscover.dom.test.tsx (it inspects the registered query keys).
expect(qk.plexDiscover("movie", "hu", FILTERS)).toEqual([
"plex-discover",
"movie",
"hu",
FILTERS,
]);
expect(qk.plexSurprise("movie", "hu", FILTERS)).toEqual([
"plex-surprise",
"movie",
"hu",
FILTERS,
]);
// A bare call stays a prefix for the reshuffle/watched-toggle invalidations.
expect(qk.plexSurprise()).toEqual(["plex-surprise"]);
});
});