perf(web): warm frequent routes during idle time
This commit is contained in:
@@ -1,11 +1,51 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { routeSectionForPath } from './routeModules';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { routeSectionForPath, scheduleIdleRoutePreloads, shouldPreloadRoutes } from './routeModules';
|
||||
|
||||
describe('route module preloading', () => {
|
||||
async function flushPreloadQueue() {
|
||||
for (let index = 0; index < 6; index += 1) await Promise.resolve();
|
||||
}
|
||||
|
||||
it('maps nested and parameterized URLs to the correct lazy module', () => {
|
||||
expect(routeSectionForPath('/monitor')).toBe('monitor');
|
||||
expect(routeSectionForPath('/vehicles/LTEST001?tab=telemetry')).toBe('vehicles');
|
||||
expect(routeSectionForPath('/alerts/rules#editor')).toBe('alerts');
|
||||
expect(routeSectionForPath('/not-a-platform-route')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('skips background route preloads on save-data and 2G connections', () => {
|
||||
expect(shouldPreloadRoutes({ saveData: true, effectiveType: '4g' })).toBe(false);
|
||||
expect(shouldPreloadRoutes({ effectiveType: 'slow-2g' })).toBe(false);
|
||||
expect(shouldPreloadRoutes({ effectiveType: '2g' })).toBe(false);
|
||||
expect(shouldPreloadRoutes({ effectiveType: '3g' })).toBe(true);
|
||||
expect(shouldPreloadRoutes({ effectiveType: '4g' })).toBe(true);
|
||||
});
|
||||
|
||||
it('warms inactive routes one idle period at a time and stops after cleanup', async () => {
|
||||
const callbacks: Array<() => void> = [];
|
||||
const cancelled: number[] = [];
|
||||
const schedule = vi.fn((callback: () => void) => {
|
||||
const index = callbacks.push(callback) - 1;
|
||||
return () => { cancelled.push(index); };
|
||||
});
|
||||
const preload = vi.fn(async () => undefined);
|
||||
const cleanup = scheduleIdleRoutePreloads({
|
||||
activePathname: '/monitor',
|
||||
connection: { effectiveType: '4g' },
|
||||
preload,
|
||||
schedule
|
||||
});
|
||||
|
||||
expect(schedule).toHaveBeenCalledTimes(1);
|
||||
callbacks[0]();
|
||||
await flushPreloadQueue();
|
||||
expect(preload).toHaveBeenCalledWith('/tracks');
|
||||
expect(schedule).toHaveBeenCalledTimes(2);
|
||||
|
||||
callbacks[1]();
|
||||
await flushPreloadQueue();
|
||||
expect(preload).toHaveBeenLastCalledWith('/history');
|
||||
cleanup();
|
||||
expect(cancelled).toContain(2);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user