Smaller fixes: the redirects from / and /<slug> keep the navigation state, so the index hides the dump DumpEdit just deleted; Index's mainFetchDone ref, which blocked nothing, is gone; DumpEdit refuses someone else's dump up front, as the API does; the Dump page takes a handed-over dump once and clears it from the history entry, so a reload fetches; a newly upvoted dump is fetched with the viewer's token; the notifications page sends read-all once per notification; the profile's followed and invitees tabs finish loading when left mid-fetch; UserListPopover no longer closes when its own list scrolls; the vote and like counts open their lists for visitors too (a disabled button swallowed the click; they are aria-disabled now); the removal of a playlist's dump forgets it (an edit brought it back), a failed removal puts it back where it was, an undo waits for the removal in flight, and a refused undo says so; PlaylistCreateForm tells whether the dump made it into the new playlist; the cover preview's object URL is revoked; the muted icon has its cross; the bell no longer stays rung after two notifications within 700 ms; a stale invite check can't overwrite a newer one; a stored theme or color scheme is checked; the default feed tab is one store every reader follows, across tabs; the file fields enforce the size they state, derived from DUMP_MAX_FILE_SIZE_BYTES; formatBytes, friendlyFetchError and ImagePicker are translated (Intl names the units), and ImagePicker ignores keys while uploading; visited dumps and playlists keep a week of visits and are parsed once; a chime is claimed under a Web Lock, so two tabs can't both play it; a date in the future reads as "now". Dead code goes: PageShell's centerSlot, overLimit, the exports of isSafeHex and hueFromString, the comment tree's token prop and ChatContext's isChatOpen and closeChat. The French catalogue has the 11 new messages. 214 backend and 632 frontend tests pass; tsc, deno lint, ESLint and doc:lint are clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
4.1 KiB
TypeScript
98 lines
4.1 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import { act, renderHook } from "@testing-library/react";
|
|
import { type ReactNode, useState } from "react";
|
|
import {
|
|
defaultWSValue,
|
|
WSContext,
|
|
type WSContextValue,
|
|
} from "../../../contexts/ws/WSContext.ts";
|
|
import type { Dump } from "../../../../shared/dump.ts";
|
|
import type { Playlist } from "../../../../shared/playlist.ts";
|
|
import { useDumpListSync } from "../../../hooks/dump/useDumpListSync.ts";
|
|
import { usePositionAwareSync } from "../../../hooks/feed/usePositionAwareSync.ts";
|
|
import { useWS } from "../../../hooks/ws/useWS.ts";
|
|
import { makeDump, makePlaylist } from "../../fixtures.ts";
|
|
|
|
/** A WS context whose value a test can push new events into. */
|
|
function liveWS() {
|
|
let push: (patch: Partial<WSContextValue>) => void = () => {};
|
|
const base = defaultWSValue;
|
|
function Wrapper({ children }: { children: ReactNode }) {
|
|
const [value, setValue] = useState(base);
|
|
push = (patch) => setValue((v) => ({ ...v, ...patch }));
|
|
return <WSContext.Provider value={value}>{children}</WSContext.Provider>;
|
|
}
|
|
return {
|
|
Wrapper,
|
|
emit: (patch: Partial<WSContextValue>) => act(() => push(patch)),
|
|
};
|
|
}
|
|
|
|
function useList<T>(initial: T[]) {
|
|
const [items, setItems] = useState(initial);
|
|
return { items, setItems };
|
|
}
|
|
|
|
const ids = (xs: { id: string }[]) => xs.map((x) => x.id);
|
|
|
|
describe("usePositionAwareSync", () => {
|
|
test("a dump that goes private and back returns to its old place", () => {
|
|
const ws = liveWS();
|
|
const initial = ["a", "b", "c"].map((id) => makeDump({ id, userId: "me" }));
|
|
// Wire the pair exactly as UserPublicProfile does.
|
|
const both = renderHook(() => {
|
|
const { items, setItems } = useList(initial);
|
|
const [event, setEvent] = useState<Dump | null>(null);
|
|
usePositionAwareSync(items, setItems, event, (d) => !d.isPrivate);
|
|
useDumpListSync(setItems);
|
|
return { items, setEvent };
|
|
}, { wrapper: ws.Wrapper });
|
|
|
|
const b = initial[1];
|
|
act(() => both.result.current.setEvent({ ...b, isPrivate: true }));
|
|
ws.emit({ lastDumpEvent: { ...b, isPrivate: true } });
|
|
expect(ids(both.result.current.items)).toEqual(["a", "c"]);
|
|
|
|
act(() => both.result.current.setEvent({ ...b, isPrivate: false }));
|
|
ws.emit({ lastDumpEvent: { ...b, isPrivate: false } });
|
|
expect(ids(both.result.current.items)).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test("a dump that goes private for others, as a deletion, comes back where it was", () => {
|
|
const ws = liveWS();
|
|
const initial = ["a", "b", "c"].map((id) => makeDump({ id, userId: "me" }));
|
|
const both = renderHook(() => {
|
|
const { items, setItems } = useList(initial);
|
|
const { lastDumpEvent } = useWS();
|
|
usePositionAwareSync(items, setItems, lastDumpEvent, (d) => !d.isPrivate);
|
|
useDumpListSync(setItems);
|
|
return { items };
|
|
}, { wrapper: ws.Wrapper });
|
|
ws.emit({ deletedDumpIds: new Set(["b"]) });
|
|
expect(ids(both.result.current.items)).toEqual(["a", "c"]);
|
|
ws.emit({ deletedDumpIds: new Set(), lastDumpEvent: initial[1] });
|
|
expect(ids(both.result.current.items)).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test("a position is only used once, and clamps to the list end", () => {
|
|
const initial = ["a", "b", "c"].map((id) => makePlaylist({ id }));
|
|
const hook = renderHook(() => {
|
|
const { items, setItems } = useList(initial);
|
|
const [event, setEvent] = useState<Playlist | null>(null);
|
|
usePositionAwareSync(items, setItems, event, (p) => p.isPublic);
|
|
return { items, setItems, setEvent };
|
|
});
|
|
const c = initial[2];
|
|
// It leaves, whatever takes it out, then the list shrinks further.
|
|
act(() => hook.result.current.setItems((prev) => prev.slice(0, 2)));
|
|
act(() => hook.result.current.setItems((prev) => prev.slice(0, 1)));
|
|
act(() => hook.result.current.setEvent({ ...c, isPublic: true }));
|
|
expect(ids(hook.result.current.items)).toEqual(["a", "c"]);
|
|
// Used once: the same event again adds nothing.
|
|
act(() =>
|
|
hook.result.current.setEvent({ ...c, isPublic: true, title: "x" })
|
|
);
|
|
expect(ids(hook.result.current.items)).toEqual(["a", "c"]);
|
|
});
|
|
});
|