-
Notifications
You must be signed in to change notification settings - Fork 4
Request caching #1636
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
Open
Oksamies
wants to merge
4
commits into
master
Choose a base branch
from
11-20-test_implemenation_of_cross-clientloader-request_dedupe
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Request caching #1636
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
adbeb49
Implement request caching with deduplication and abort handling
Oksamies 6d4afee
Add dapperSingleton module with client initialization and request han…
Oksamies 0642cbe
Enhance Sentry initialization with session tools and Dapper client in…
Oksamies 8869b58
Remove unnecessary check from requestCache
Oksamies 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
Some comments aren't visible on the classic Files Changed page.
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
198 changes: 198 additions & 0 deletions
198
apps/cyberstorm-remix/cyberstorm/utils/__tests__/dapperSingleton.test.ts
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,198 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
| import { | ||
| initializeClientDapper, | ||
| getClientDapper, | ||
| getDapperForRequest, | ||
| resetDapperSingletonForTest, | ||
| } from "../dapperSingleton"; | ||
| import { deduplicatePromiseForRequest } from "../requestCache"; | ||
| import { DapperTs } from "@thunderstore/dapper-ts"; | ||
| import * as publicEnvVariables from "../../security/publicEnvVariables"; | ||
| import type { Community } from "../../../../../packages/thunderstore-api/src"; | ||
|
|
||
| // Mock getSessionTools | ||
| vi.mock("../../security/publicEnvVariables", () => ({ | ||
| getSessionTools: vi.fn().mockReturnValue({ | ||
| getConfig: vi.fn().mockReturnValue({ | ||
| apiHost: "http://localhost", | ||
| sessionId: "test-session", | ||
| }), | ||
| }), | ||
| })); | ||
|
|
||
| describe("dapperSingleton", () => { | ||
| beforeEach(() => { | ||
| // Reset window.Dapper | ||
| if (typeof window !== "undefined") { | ||
| // @ts-expect-error Dapper is not optional on Window | ||
| delete window.Dapper; | ||
| } | ||
| resetDapperSingletonForTest(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("initializeClientDapper", () => { | ||
| it("initializes window.Dapper if it does not exist", () => { | ||
| initializeClientDapper(); | ||
| expect(window.Dapper).toBeDefined(); | ||
| expect(window.Dapper).toBeInstanceOf(DapperTs); | ||
| }); | ||
|
|
||
| it("uses provided factory if supplied", () => { | ||
| const factory = vi.fn().mockReturnValue({ apiHost: "custom" }); | ||
| initializeClientDapper(factory); | ||
| expect(window.Dapper).toBeDefined(); | ||
| expect(window.Dapper.config()).toEqual({ apiHost: "custom" }); | ||
| expect(factory).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("updates existing window.Dapper config if called again with factory", () => { | ||
| // First initialization | ||
| initializeClientDapper(); | ||
| const originalDapper = window.Dapper; | ||
| expect(originalDapper).toBeDefined(); | ||
|
|
||
| // Second initialization with new factory | ||
| const newFactory = vi.fn().mockReturnValue({ apiHost: "updated" }); | ||
| initializeClientDapper(newFactory); | ||
|
|
||
| expect(window.Dapper).toBe(originalDapper); // Should be same instance | ||
| expect(window.Dapper.config()).toEqual({ apiHost: "updated" }); | ||
| }); | ||
|
|
||
| it("resolves config factory from session tools if no factory provided", () => { | ||
| initializeClientDapper(); | ||
| expect(publicEnvVariables.getSessionTools).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getClientDapper", () => { | ||
| it("returns window.Dapper if it exists", () => { | ||
| initializeClientDapper(); | ||
| const dapper = window.Dapper; | ||
| expect(getClientDapper()).toBe(dapper); | ||
| }); | ||
|
|
||
| it("initializes and returns window.Dapper if it does not exist", () => { | ||
| expect(window.Dapper).toBeUndefined(); | ||
| const dapper = getClientDapper(); | ||
| expect(dapper).toBeDefined(); | ||
| expect(window.Dapper).toBe(dapper); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getDapperForRequest", () => { | ||
| it("returns client dapper if no request is provided", () => { | ||
| initializeClientDapper(); | ||
| const dapper = getDapperForRequest(); | ||
| expect(dapper).toBe(window.Dapper); | ||
| }); | ||
|
|
||
| it("returns a proxy if request is provided", () => { | ||
| initializeClientDapper(); | ||
| const request = new Request("http://localhost"); | ||
| const dapper = getDapperForRequest(request); | ||
| expect(dapper).not.toBe(window.Dapper); | ||
| // It should be a proxy | ||
| expect(dapper).toBeInstanceOf(DapperTs); | ||
| }); | ||
|
|
||
| it("caches the proxy for the same request", () => { | ||
| initializeClientDapper(); | ||
| const request = new Request("http://localhost"); | ||
| const dapper1 = getDapperForRequest(request); | ||
| const dapper2 = getDapperForRequest(request); | ||
| expect(dapper1).toBe(dapper2); | ||
| }); | ||
|
|
||
| it("creates different proxies for different requests", () => { | ||
| initializeClientDapper(); | ||
| const request1 = new Request("http://localhost"); | ||
| const request2 = new Request("http://localhost"); | ||
| const dapper1 = getDapperForRequest(request1); | ||
| const dapper2 = getDapperForRequest(request2); | ||
| expect(dapper1).not.toBe(dapper2); | ||
| }); | ||
|
|
||
| it("intercepts 'get' methods and caches promises", async () => { | ||
| initializeClientDapper(); | ||
| const request = new Request("http://localhost"); | ||
| const dapper = getDapperForRequest(request); | ||
|
|
||
| // Mock the underlying method on window.Dapper | ||
| const mockGetCommunities = vi | ||
| .spyOn(window.Dapper, "getCommunities") | ||
| .mockResolvedValue({ count: 0, results: [], hasMore: false }); | ||
|
|
||
| const result1 = await dapper.getCommunities(); | ||
| const result2 = await dapper.getCommunities(); | ||
|
|
||
| expect(result1).toEqual({ count: 0, results: [], hasMore: false }); | ||
| expect(result2).toEqual({ count: 0, results: [], hasMore: false }); | ||
|
|
||
| // Should be called only once due to caching | ||
| expect(mockGetCommunities).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("does not intercept non-'get' methods", async () => { | ||
| initializeClientDapper(); | ||
| const request = new Request("http://localhost"); | ||
| const dapper = getDapperForRequest(request); | ||
|
|
||
| // Mock a non-get method | ||
| // postTeamCreate is a good candidate | ||
| const mockPostTeamCreate = vi | ||
| .spyOn(window.Dapper, "postTeamCreate") | ||
| .mockResolvedValue({ | ||
| identifier: 1, | ||
| name: "test", | ||
| donation_link: null, | ||
| }); | ||
|
|
||
| await dapper.postTeamCreate("test"); | ||
| await dapper.postTeamCreate("test"); | ||
|
|
||
| // Should be called twice (no caching) | ||
| expect(mockPostTeamCreate).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("shares cache between proxy calls and manual deduplicatePromiseForRequest calls", async () => { | ||
| initializeClientDapper(); | ||
| const request = new Request("http://localhost"); | ||
| const dapper = getDapperForRequest(request); | ||
|
|
||
| // Mock the underlying method on window.Dapper | ||
| const mockGetCommunity = vi | ||
| .spyOn(window.Dapper, "getCommunity") | ||
| .mockResolvedValue({ | ||
| identifier: "1", | ||
| name: "Test Community", | ||
| } as Community); | ||
|
|
||
| // 1. Call via proxy | ||
| const dapperResult = await dapper.getCommunity("1"); | ||
|
|
||
| // 2. Call manually with same key and args | ||
| const manualFunc = vi.fn().mockResolvedValue("manual result"); | ||
| const manualResult = await deduplicatePromiseForRequest( | ||
| "getCommunity", | ||
| manualFunc, | ||
| ["1"], | ||
| request | ||
| ); | ||
|
|
||
| // Assertions | ||
| expect(dapperResult).toEqual({ identifier: "1", name: "Test Community" }); | ||
| // Should return the cached result from the first call, NOT "manual result" | ||
| expect(manualResult).toBe(dapperResult); | ||
| // The manual function should NOT have been called | ||
| expect(manualFunc).not.toHaveBeenCalled(); | ||
| // The underlying dapper method should have been called once | ||
| expect(mockGetCommunity).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.