mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-05-02 13:06:08 +02:00
refactor: Introduce granular query types with specific policies for improved caching and data freshness.
This commit is contained in:
@@ -1,5 +1,16 @@
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
const mockLogWebRequest = vi.fn();
|
||||
const mockWithQueryLog = vi.fn(async (fn) => fn());
|
||||
|
||||
vi.mock('../../services/appConfig', () => ({
|
||||
AppDebug: {
|
||||
debugWebRequests: false
|
||||
},
|
||||
logWebRequest: (...args) => mockLogWebRequest(...args),
|
||||
withQueryLog: (...args) => mockWithQueryLog(...args)
|
||||
}));
|
||||
|
||||
import {
|
||||
_entityCacheInternals,
|
||||
fetchWithEntityPolicy,
|
||||
@@ -13,6 +24,8 @@ describe('entity query cache helpers', () => {
|
||||
beforeEach(() => {
|
||||
queryClient.clear();
|
||||
vi.restoreAllMocks();
|
||||
mockLogWebRequest.mockClear();
|
||||
mockWithQueryLog.mockClear();
|
||||
});
|
||||
|
||||
test('reports cache hit for fresh data', async () => {
|
||||
@@ -75,6 +88,50 @@ describe('entity query cache helpers', () => {
|
||||
expect(callCount).toBe(2);
|
||||
});
|
||||
|
||||
test('uses label in logs without changing cache behavior', async () => {
|
||||
const queryKey = ['user', 'usr_9'];
|
||||
const policy = {
|
||||
staleTime: 20000,
|
||||
gcTime: 90000,
|
||||
retry: 1,
|
||||
refetchOnWindowFocus: false
|
||||
};
|
||||
const queryFn = vi.fn(async () => ({
|
||||
json: { id: 'usr_9', updated_at: '2026-01-01T00:00:00.000Z' },
|
||||
params: { userId: 'usr_9' },
|
||||
ref: { id: 'usr_9', updated_at: '2026-01-01T00:00:00.000Z' }
|
||||
}));
|
||||
|
||||
const first = await fetchWithEntityPolicy({
|
||||
queryKey,
|
||||
policy,
|
||||
queryFn,
|
||||
label: 'user.dialog'
|
||||
});
|
||||
const second = await fetchWithEntityPolicy({
|
||||
queryKey,
|
||||
policy,
|
||||
queryFn
|
||||
});
|
||||
|
||||
expect(first.cache).toBe(false);
|
||||
expect(second.cache).toBe(true);
|
||||
expect(mockLogWebRequest).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'[QUERY FETCH]',
|
||||
'user.dialog',
|
||||
queryKey,
|
||||
expect.any(Object)
|
||||
);
|
||||
expect(mockLogWebRequest).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'[QUERY CACHE HIT]',
|
||||
'user',
|
||||
queryKey,
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
test('does not overwrite newer data with older payload', () => {
|
||||
const queryKey = ['world', 'wrld_1'];
|
||||
|
||||
|
||||
@@ -26,8 +26,8 @@ describe('query policy configuration', () => {
|
||||
});
|
||||
|
||||
expect(entityQueryPolicies.group).toMatchObject({
|
||||
staleTime: 60000,
|
||||
gcTime: 300000,
|
||||
staleTime: 300000,
|
||||
gcTime: 1800000,
|
||||
retry: 1,
|
||||
refetchOnWindowFocus: false
|
||||
});
|
||||
@@ -89,8 +89,8 @@ describe('query policy configuration', () => {
|
||||
|
||||
test('file-related policies', () => {
|
||||
expect(entityQueryPolicies.fileAnalysis).toMatchObject({
|
||||
staleTime: 120000,
|
||||
gcTime: 600000,
|
||||
staleTime: 600000,
|
||||
gcTime: 3600000,
|
||||
retry: 1,
|
||||
refetchOnWindowFocus: false
|
||||
});
|
||||
@@ -164,8 +164,8 @@ describe('query policy configuration', () => {
|
||||
const options = toQueryOptions(entityQueryPolicies.group);
|
||||
|
||||
expect(options).toEqual({
|
||||
staleTime: 60000,
|
||||
gcTime: 300000,
|
||||
staleTime: 300000,
|
||||
gcTime: 1800000,
|
||||
retry: 1,
|
||||
refetchOnWindowFocus: false
|
||||
});
|
||||
|
||||
@@ -140,10 +140,10 @@ export function patchQueryDataWithRecency({ queryKey, nextData }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{queryKey: unknown[], policy: {staleTime: number, gcTime: number, retry: number, refetchOnWindowFocus: boolean}, queryFn: () => Promise<any>}} options
|
||||
* @param {{queryKey: unknown[], policy: {staleTime: number, gcTime: number, retry: number, refetchOnWindowFocus: boolean}, queryFn: () => Promise<any>, label?: string}} options
|
||||
* @returns {Promise<{data: any, cache: boolean}>}
|
||||
*/
|
||||
export async function fetchWithEntityPolicy({ queryKey, policy, queryFn }) {
|
||||
export async function fetchWithEntityPolicy({ queryKey, policy, queryFn, label }) {
|
||||
const queryState = queryClient.getQueryState(queryKey);
|
||||
const isFresh =
|
||||
Boolean(queryState?.dataUpdatedAt) &&
|
||||
@@ -157,9 +157,9 @@ export async function fetchWithEntityPolicy({ queryKey, policy, queryFn }) {
|
||||
});
|
||||
|
||||
if (isFresh) {
|
||||
logWebRequest('[QUERY CACHE HIT]', queryKey, data);
|
||||
logWebRequest('[QUERY CACHE HIT]', label || queryKey[0], queryKey, data);
|
||||
} else {
|
||||
logWebRequest('[QUERY FETCH]', queryKey, data);
|
||||
logWebRequest('[QUERY FETCH]', label || queryKey[0], queryKey, data);
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user