Marked watched in the player (by hand, or by the player at its end), a card left an Unwatched list at once, then came back as "new" when the player closed, until a reload. The player's closing position patch writes into the cached feed; the feed took the bumped dataUpdatedAt for fresh server data, dropped its optimistic overrides, and nothing refetched. The overrides now clear only when a refetch lands, and a player session that changed a state refetches the list and its count by the view's filters on close, once its writes have landed.
193 lines
6.8 KiB
TypeScript
193 lines
6.8 KiB
TypeScript
// @vitest-environment jsdom
|
|
//
|
|
// A video marked watched in the player (by hand, or by the player at its end) left an Unwatched view
|
|
// at once, then came back as "new" when the player closed, until a reload (owner, on a channel page).
|
|
// The player's closing position patch writes into the cached feed, which bumped `dataUpdatedAt`; the
|
|
// feed took that for fresh server data and dropped its optimistic overrides, and nothing refetched.
|
|
// The grid and the player are stubs that show what Feed hands them.
|
|
import { describe, it, expect, vi, beforeAll, afterEach } from "vitest";
|
|
import { render, screen, fireEvent, waitFor, cleanup, act } from "@testing-library/react";
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
|
vi.mock("react-i18next", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("react-i18next")>()),
|
|
useTranslation: () => ({ t: (key: string) => key, i18n: { language: "en" } }),
|
|
}));
|
|
vi.mock("./wizardContext", () => ({ useWizard: () => ({ openWizard: vi.fn() }) }));
|
|
// What the grid was handed for its card menu's channel-filter row.
|
|
const grid: { channelFilter?: { ids: string[]; set: (ids: string[]) => void } } = {};
|
|
vi.mock("./VirtualFeed", () => ({
|
|
default: ({
|
|
items,
|
|
onOpen,
|
|
channelFilter,
|
|
}: {
|
|
items: { id: string }[];
|
|
onOpen: (v: unknown) => void;
|
|
channelFilter?: { ids: string[]; set: (ids: string[]) => void };
|
|
}) => (
|
|
(grid.channelFilter = channelFilter),
|
|
(
|
|
<ol aria-label="results">
|
|
{items.map((v) => (
|
|
<li key={v.id}>
|
|
{v.id}
|
|
<button onClick={() => onOpen(v)}>open {v.id}</button>
|
|
</li>
|
|
))}
|
|
</ol>
|
|
)
|
|
),
|
|
}));
|
|
vi.mock("./PlayerModal", () => ({
|
|
default: ({
|
|
video,
|
|
onState,
|
|
onClose,
|
|
}: {
|
|
video: { id: string };
|
|
onState: (id: string, status: string) => void;
|
|
onClose: () => void;
|
|
}) => (
|
|
<div>
|
|
<button onClick={() => onState(video.id, "watched")}>mark</button>
|
|
<button onClick={onClose}>close</button>
|
|
</div>
|
|
),
|
|
}));
|
|
|
|
import Feed from "./Feed";
|
|
import { api, DEFAULT_TAG_MODES, type FeedFilters, type Video } from "../lib/api";
|
|
import { qk } from "../lib/queryKeys";
|
|
import { patchCachedProgress } from "../lib/progressCache";
|
|
|
|
beforeAll(() => {
|
|
class Stub {
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
takeRecords() {
|
|
return [];
|
|
}
|
|
}
|
|
globalThis.ResizeObserver ??= Stub as unknown as typeof ResizeObserver;
|
|
globalThis.IntersectionObserver ??= Stub as unknown as typeof IntersectionObserver;
|
|
});
|
|
afterEach(() => {
|
|
cleanup();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
const FILTERS: FeedFilters = {
|
|
tags: [],
|
|
tagModes: DEFAULT_TAG_MODES,
|
|
channelIds: ["UCx"],
|
|
q: "",
|
|
sort: "newest",
|
|
scope: "my",
|
|
includeNormal: true,
|
|
includeShorts: false,
|
|
includeLive: false,
|
|
show: "unwatched",
|
|
};
|
|
|
|
const video = (status: string, position = 0) =>
|
|
({
|
|
id: "v1",
|
|
title: "v1",
|
|
channel_id: "UCx",
|
|
channel_title: "Channel",
|
|
status,
|
|
position_seconds: position,
|
|
duration_seconds: 600,
|
|
published_at: "2026-01-01T00:00:00Z",
|
|
watch_url: "https://www.youtube.com/watch?v=v1",
|
|
}) as unknown as Video;
|
|
|
|
function mount({
|
|
channelScoped = true,
|
|
setFilters = () => {},
|
|
}: { channelScoped?: boolean; setFilters?: (f: FeedFilters) => void } = {}) {
|
|
const feed = vi
|
|
.spyOn(api, "feed")
|
|
.mockResolvedValueOnce({ items: [video("new")], next_cursor: null } as never)
|
|
// The server after the mark: watched, so an Unwatched view leaves it out.
|
|
.mockResolvedValue({ items: [], next_cursor: null } as never);
|
|
const summary = vi.spyOn(api, "feedSummary").mockResolvedValue({ count: 1, facets: {} } as never);
|
|
vi.spyOn(api, "setState").mockResolvedValue({ ok: true } as never);
|
|
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
qc.setQueryData(qk.me(), { id: 1, can_write: true, prefs: {} });
|
|
render(
|
|
<QueryClientProvider client={qc}>
|
|
<Feed
|
|
filters={FILTERS}
|
|
setFilters={setFilters}
|
|
view="cards"
|
|
setView={() => {}}
|
|
canRead
|
|
ytSearch={null}
|
|
onYtSearch={() => {}}
|
|
onExitYtSearch={() => {}}
|
|
channelScoped={channelScoped}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
return { qc, feed, summary };
|
|
}
|
|
|
|
describe("Feed — a video marked watched in the player", () => {
|
|
it("stays out of an Unwatched view when the player writes its position into the cache", async () => {
|
|
const { qc } = mount();
|
|
fireEvent.click(await screen.findByText("open v1"));
|
|
fireEvent.click(await screen.findByText("mark"));
|
|
await waitFor(() => expect(screen.queryByText("open v1")).toBeNull());
|
|
// What the player does as it closes: the server's stored position, written into the cached list.
|
|
act(() => patchCachedProgress(qc, new Map([["v1", 10]])));
|
|
await new Promise((r) => setTimeout(r, 20));
|
|
expect(screen.queryByText("open v1")).toBeNull();
|
|
});
|
|
|
|
it("refetches the list and its count by the view's filters when the player closes", async () => {
|
|
const { feed, summary } = mount();
|
|
fireEvent.click(await screen.findByText("open v1"));
|
|
fireEvent.click(await screen.findByText("mark"));
|
|
const before = { feed: feed.mock.calls.length, summary: summary.mock.calls.length };
|
|
fireEvent.click(screen.getByText("close"));
|
|
await waitFor(() => expect(feed.mock.calls.length).toBeGreaterThan(before.feed));
|
|
await waitFor(() => expect(summary.mock.calls.length).toBeGreaterThan(before.summary));
|
|
// The refetch is the channel's Unwatched list, and the server's answer keeps the card out.
|
|
expect(feed.mock.calls[feed.mock.calls.length - 1]?.[0]).toMatchObject({
|
|
channelIds: ["UCx"],
|
|
show: "unwatched",
|
|
});
|
|
await new Promise((r) => setTimeout(r, 20));
|
|
expect(screen.queryByText("open v1")).toBeNull();
|
|
});
|
|
|
|
it("does not refetch on a close with nothing changed", async () => {
|
|
const { feed } = mount();
|
|
fireEvent.click(await screen.findByText("open v1"));
|
|
const before = feed.mock.calls.length;
|
|
fireEvent.click(await screen.findByText("close"));
|
|
await new Promise((r) => setTimeout(r, 30));
|
|
expect(feed.mock.calls.length).toBe(before);
|
|
});
|
|
});
|
|
|
|
describe("Feed — the card menu's channel filter", () => {
|
|
it("is offered on the feed, as its own channel list, and sets it", async () => {
|
|
const setFilters = vi.fn();
|
|
mount({ channelScoped: false, setFilters });
|
|
await screen.findByText("open v1");
|
|
expect(grid.channelFilter?.ids).toEqual(["UCx"]);
|
|
grid.channelFilter?.set(["UCx", "UCy"]);
|
|
expect(setFilters).toHaveBeenCalledWith({ ...FILTERS, channelIds: ["UCx", "UCy"] });
|
|
});
|
|
|
|
it("is not offered on a channel page, whose view is that channel already", async () => {
|
|
mount({ channelScoped: true });
|
|
await screen.findByText("open v1");
|
|
expect(grid.channelFilter).toBeUndefined();
|
|
});
|
|
});
|