-
Notifications
You must be signed in to change notification settings - Fork 7
perf(waves): stop sending an observer on anonymous waves requests #1648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+191
−18
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
de7fd30
perf(waves): stop sending an observer on anonymous waves requests
feruzm 0ae9000
review: cover what the waves views send as observer
feruzm ccff42e
docs: correct what an observer does to a bridge comment thread
feruzm dd95eb0
review: build the spec's mocked query keys from QueryKeys
feruzm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import React from "react"; | ||
| import { render } from "@testing-library/react"; | ||
| import "@testing-library/jest-dom"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
| import { QueryKeys } from "@ecency/sdk"; | ||
|
|
||
| /** | ||
| * What the waves views send as `observer`. | ||
| * | ||
| * The parameter carries the *viewer's* mute list, and esync treats any request | ||
| * that has one as personalised, skipping its shared response cache. Logged out | ||
| * there is no viewer, so it must be absent: a fallback there costs every | ||
| * anonymous visitor a cache entry nobody else can reuse, and filters nothing | ||
| * that esync does not already filter server-side. | ||
| * | ||
| * Logged in it must still be the viewer's own name, or their personal mutes | ||
| * stop being applied at all. | ||
| */ | ||
|
|
||
| const getWavesFeedQueryOptions = vi.hoisted(() => vi.fn()); | ||
| const getShortsFeedQueryOptions = vi.hoisted(() => vi.fn()); | ||
|
|
||
| /** | ||
| * Keys come from `QueryKeys` rather than a literal, so a mocked feed lands on | ||
| * the same cache entry production would give it. That matters here: the key | ||
| * carries the observer, so anonymous and logged-in renders are distinct | ||
| * entries, exactly as they are in the app. A literal would collapse them onto | ||
| * one and quietly hide a cache-sharing bug from any later test that seeds or | ||
| * asserts on cache state. | ||
| */ | ||
| const stubInfiniteQuery = (queryKey: readonly unknown[]) => ({ | ||
| queryKey, | ||
| initialPageParam: undefined, | ||
| queryFn: async () => [], | ||
| getNextPageParam: () => undefined, | ||
| enabled: false | ||
| }); | ||
|
|
||
| vi.mock("@/utils", async () => ({ | ||
| ...(await vi.importActual<typeof import("@/utils")>("@/utils")), | ||
| random: vi.fn(), | ||
| getAccessToken: vi.fn(() => "mock-token") | ||
| })); | ||
|
|
||
| vi.mock("@ecency/sdk", async () => ({ | ||
| ...(await vi.importActual<Record<string, unknown>>("@ecency/sdk")), | ||
| getWavesFeedQueryOptions: getWavesFeedQueryOptions.mockImplementation( | ||
| (params: Parameters<typeof QueryKeys.posts.wavesFeed>[0] = {}) => | ||
| stubInfiniteQuery(QueryKeys.posts.wavesFeed(params)) | ||
| ), | ||
| getShortsFeedQueryOptions: getShortsFeedQueryOptions.mockImplementation( | ||
| (params: Parameters<typeof QueryKeys.posts.shortsFeed>[0] = {}) => | ||
| stubInfiniteQuery(QueryKeys.posts.shortsFeed(params)) | ||
| ), | ||
| getPromotedPostsQuery: vi.fn((type: string = "feed") => ({ | ||
| queryKey: QueryKeys.posts.promoted(type), | ||
| queryFn: async () => [], | ||
| enabled: false | ||
| })) | ||
| })); | ||
|
|
||
| vi.mock("@/app/waves/_context", () => ({ | ||
| useWavesTagFilter: () => ({ selectedTag: null, selectedSource: null }) | ||
| })); | ||
| vi.mock("@/app/waves/_hooks", () => ({ | ||
| useWavesAutoRefresh: () => ({ newWaves: [], clear: vi.fn(), now: 0 }) | ||
| })); | ||
| vi.mock("@/app/waves/_components", () => ({ WavesRefreshPopup: () => null })); | ||
| vi.mock("@/app/waves/_components/waves-list-item", () => ({ WavesListItem: () => null })); | ||
| vi.mock("@/app/waves/_components/waves-list-loader", () => ({ WavesListLoader: () => null })); | ||
| vi.mock("@/app/waves/_components/waves-reel-item", () => ({ WavesReelItem: () => null })); | ||
| vi.mock("@/app/waves/_components/waves-fast-reply-dialog", () => ({ | ||
| WavesFastReplyDialog: () => null | ||
| })); | ||
| vi.mock("@/features/shared", () => ({ DetectBottom: () => null })); | ||
| vi.mock("@/core/hooks", () => ({ useBottomPagination: () => [vi.fn(), vi.fn()] })); | ||
|
|
||
| import { WavesListView } from "@/app/waves/_components/waves-list-view"; | ||
| import { WavesReelsView } from "@/app/waves/_components/waves-reels-view"; | ||
|
|
||
| function renderView(node: React.ReactElement) { | ||
| const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); | ||
| return render(<QueryClientProvider client={queryClient}>{node}</QueryClientProvider>); | ||
| } | ||
|
|
||
| describe("waves observer", () => { | ||
| beforeEach(() => { | ||
| getWavesFeedQueryOptions.mockClear(); | ||
| getShortsFeedQueryOptions.mockClear(); | ||
| }); | ||
|
|
||
| it("omits the observer on the anonymous waves feed", () => { | ||
| renderView(<WavesListView feedType="for-you" />); | ||
|
|
||
| expect(getWavesFeedQueryOptions).toHaveBeenCalled(); | ||
| for (const [params] of getWavesFeedQueryOptions.mock.calls) { | ||
| expect(params.observer).toBeUndefined(); | ||
| } | ||
| }); | ||
|
|
||
| it("sends the logged-in viewer as the observer on the waves feed", () => { | ||
| renderView(<WavesListView feedType="for-you" username="viewer" />); | ||
|
|
||
| expect(getWavesFeedQueryOptions).toHaveBeenCalled(); | ||
| for (const [params] of getWavesFeedQueryOptions.mock.calls) { | ||
| expect(params.observer).toBe("viewer"); | ||
| } | ||
| }); | ||
|
|
||
| it("omits the observer on the anonymous shorts feed", () => { | ||
| renderView(<WavesReelsView />); | ||
|
|
||
| expect(getShortsFeedQueryOptions).toHaveBeenCalled(); | ||
| for (const [params] of getShortsFeedQueryOptions.mock.calls) { | ||
| expect(params.observer).toBeUndefined(); | ||
| } | ||
| }); | ||
|
|
||
| it("sends the logged-in viewer as the observer on the shorts feed", () => { | ||
| renderView(<WavesReelsView username="viewer" />); | ||
|
|
||
| expect(getShortsFeedQueryOptions).toHaveBeenCalled(); | ||
| for (const [params] of getShortsFeedQueryOptions.mock.calls) { | ||
| expect(params.observer).toBe("viewer"); | ||
| } | ||
| }); | ||
|
|
||
| it("puts the anonymous and logged-in feeds on different cache keys", () => { | ||
| // The whole point of dropping the observer is the cache tier, so the key | ||
| // has to actually differ. Also guards the mocks above: if they went back to | ||
| // a literal key, the two renders would share one entry and every | ||
| // cache-sensitive test written after this would be testing a fiction. | ||
| renderView(<WavesListView feedType="for-you" />); | ||
| const anonymous = getWavesFeedQueryOptions.mock.results[0].value.queryKey; | ||
|
|
||
| getWavesFeedQueryOptions.mockClear(); | ||
| renderView(<WavesListView feedType="for-you" username="viewer" />); | ||
| const signedIn = getWavesFeedQueryOptions.mock.results[0].value.queryKey; | ||
|
|
||
| expect(anonymous).not.toEqual(signedIn); | ||
| expect(signedIn).toContain("viewer"); | ||
| }); | ||
|
|
||
| it("never substitutes Ecency's moderation account for a missing viewer", () => { | ||
| // The regression this guards: `username || DEFAULT_OBSERVER` reads as a | ||
| // harmless default, and the response is identical either way, so nothing | ||
| // visible breaks when it comes back -- only the cache tier changes. | ||
| renderView(<WavesListView feedType="for-you" />); | ||
| renderView(<WavesReelsView />); | ||
|
|
||
| const observers = [ | ||
| ...getWavesFeedQueryOptions.mock.calls, | ||
| ...getShortsFeedQueryOptions.mock.calls | ||
| ].map(([params]) => params.observer); | ||
|
|
||
| expect(observers.length).toBeGreaterThan(0); | ||
| expect(observers).not.toContain("ecency"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Missing observer regression coverage
📘 Rule violation▣ TestabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools