The FE fired /feed/count (1 pass) + /facets (3 passes) on every filter change =
4 full scans of the subscribed set. New /api/feed/summary does ONE grouped query
(the base filter with tags stripped, GROUP BY channel_id -> per-channel video
counts) and derives the total count + every tag's distinct-channel facet count in
Python on the ~few-hundred-row channel map, faithfully mirroring filtered_query's
channel-based tag logic + the /facets drill-down (own category dropped, unless a
conjunctive topic-AND). Returns {count, facets} — the same numbers the two old
endpoints returned.
FE: a single shared query (Feed reads .count, Sidebar reads .facets); same key +
filters, so React Query dedupes to ONE request. Invalidations repointed to
qk.feedSummary. The old /feed/count + /facets endpoints (and their now-unused FE
api/qk) are kept as the equivalence-test oracle + safety net; a later cleanup can
drop them.
New equivalence test (test_feed_views, 8 filter combos incl. cross-category,
disjunctive/conjunctive topic modes, search): /feed/summary count == /feed/count
and facets == /facets, byte-identical. Structural win: 2 requests / 4-6 queries
-> 1 request / 2-3 queries (the review measured 530 -> 144 ms). Gate green:
344 pytest, frontend tsc/eslint/knip/prettier/222 vitest.
88 lines
3.9 KiB
TypeScript
88 lines
3.9 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: [] },
|
|
]);
|
|
});
|
|
});
|