Files
TREK/client/src/native/offlineShell.test.ts
T
Maurice b79d104349 fix(ios): verify the shell before saving offline trips
Recheck cached assets during explicit offline preparation even when the build is marked warm. Keep the existing start page, trip cache, and pin if this check fails. Keep the ordinary launch fast path for warm builds.

Refs #3318
2026-12-05 12:35:18 +01:00

193 lines
8.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { keepOfflineShellFresh, manifestFiles } from './offlineShell';
import { TrekShell } from './trekShell';
vi.mock('./trekShell', () => ({ TrekShell: { refreshOfflineCopy: vi.fn() } }));
const manifest = {
'index.html': {
file: 'assets/index-a1b2c3d4.js',
css: ['assets/index-e5f6a7b8.css'],
assets: ['assets/poppins-11223344.woff2'],
},
'src/pages/AtlasPage.tsx': { file: 'assets/AtlasPage-99887766.js', css: ['assets/index-e5f6a7b8.css'] },
_shared: { file: 'assets/shared-55443322.js', assets: 'not-a-list' },
broken: null,
};
beforeEach(() => {
vi.mocked(TrekShell.refreshOfflineCopy).mockReset().mockResolvedValue({ saved: true });
window.localStorage.clear();
});
describe('manifestFiles', () => {
it('collects every chunk, stylesheet and asset once', () => {
expect(manifestFiles(manifest).sort()).toEqual([
'assets/AtlasPage-99887766.js',
'assets/index-a1b2c3d4.js',
'assets/index-e5f6a7b8.css',
'assets/poppins-11223344.woff2',
'assets/shared-55443322.js',
]);
});
it('returns nothing for something that is not a manifest', () => {
expect(manifestFiles(null)).toEqual([]);
expect(manifestFiles('x')).toEqual([]);
});
});
describe('keepOfflineShellFresh', () => {
it('tries every asset even when one fails and does not replace the stored start page', async () => {
const fetched: string[] = [];
const fetchImpl = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
if (url === '/asset-manifest.json') {
expect(init).toEqual({ cache: 'no-store' });
return new Response(JSON.stringify(manifest));
}
fetched.push(url);
if (url.includes('shared')) throw new Error('offline for a moment');
return new Response('x');
}) as unknown as typeof fetch;
await keepOfflineShellFresh(fetchImpl);
expect(TrekShell.refreshOfflineCopy).not.toHaveBeenCalled();
expect(fetched.sort()).toEqual([
'/assets/AtlasPage-99887766.js',
'/assets/index-a1b2c3d4.js',
'/assets/index-e5f6a7b8.css',
'/assets/poppins-11223344.woff2',
'/assets/shared-55443322.js',
]);
});
it('keeps the previous start page when the build has no manifest', async () => {
const fetchImpl = vi.fn(async () => new Response('', { status: 404 })) as unknown as typeof fetch;
await keepOfflineShellFresh(fetchImpl);
expect(fetchImpl).toHaveBeenCalledTimes(1);
expect(TrekShell.refreshOfflineCopy).not.toHaveBeenCalled();
});
it('fetches at low priority, only the own language, and once per build', async () => {
const withLocales = {
...manifest,
'../shared/dist/i18n/de/index.mjs': { file: 'assets/de-aaaa1111.js' },
'../shared/dist/i18n/fr/index.mjs': { file: 'assets/fr-bbbb2222.js' },
'../shared/dist/i18n/en/index.mjs': { file: 'assets/en-cccc3333.js' },
};
const inits: (RequestInit | undefined)[] = [];
const fetched: string[] = [];
const fetchImpl = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
if (url === '/asset-manifest.json') return new Response(JSON.stringify(withLocales));
fetched.push(url);
inits.push(init);
return new Response('x');
}) as unknown as typeof fetch;
await keepOfflineShellFresh(fetchImpl, 'de');
expect(fetched).toContain('/assets/de-aaaa1111.js');
expect(fetched).toContain('/assets/en-cccc3333.js');
expect(fetched).not.toContain('/assets/fr-bbbb2222.js');
expect(inits.every((i) => (i as { priority?: string } | undefined)?.priority === 'low')).toBe(true);
const before = fetched.length;
await keepOfflineShellFresh(fetchImpl, 'de');
expect(fetched.length).toBe(before);
await keepOfflineShellFresh(fetchImpl, 'fr');
expect(fetched).toContain('/assets/fr-bbbb2222.js');
});
it('retries the iOS warm-up when a route chunk returned an HTTP error', async () => {
const fetchImpl = vi.fn(async (input: RequestInfo | URL) => {
if (String(input) === '/asset-manifest.json') return new Response(JSON.stringify(manifest));
if (String(input) === '/assets/AtlasPage-99887766.js') return new Response(null, { status: 503 });
return new Response('asset');
});
await keepOfflineShellFresh(fetchImpl);
expect(fetchImpl).toHaveBeenCalledWith('/assets/AtlasPage-99887766.js', { priority: 'low' });
fetchImpl.mockClear();
await keepOfflineShellFresh(fetchImpl);
expect(fetchImpl).toHaveBeenCalledWith('/assets/AtlasPage-99887766.js', { priority: 'low' });
});
it('rechecks the marked iOS shell on demand and keeps the previous start page if an asset is missing', async () => {
const chunk = '/assets/AtlasPage-99887766.js';
let chunkAvailable = true;
const fetchImpl = vi.fn(async (input: RequestInfo | URL) => {
if (String(input) === '/asset-manifest.json') return new Response(JSON.stringify(manifest));
if (String(input) === chunk && !chunkAvailable) return new Response(null, { status: 503 });
return new Response('asset');
});
expect(await keepOfflineShellFresh(fetchImpl)).toBe(true);
expect(await keepOfflineShellFresh(fetchImpl)).toBe(true);
expect(fetchImpl.mock.calls.filter(([url]) => url === chunk)).toHaveLength(1);
vi.mocked(TrekShell.refreshOfflineCopy).mockClear();
chunkAvailable = false;
expect(await keepOfflineShellFresh(fetchImpl, 'en', { verifyAssets: true })).toBe(false);
expect(fetchImpl.mock.calls.filter(([url]) => url === chunk)).toHaveLength(2);
expect(TrekShell.refreshOfflineCopy).not.toHaveBeenCalled();
chunkAvailable = true;
expect(await keepOfflineShellFresh(fetchImpl)).toBe(true);
expect(fetchImpl.mock.calls.filter(([url]) => url === chunk)).toHaveLength(3);
expect(TrekShell.refreshOfflineCopy).toHaveBeenCalledOnce();
});
it('on Android hands the list to the native store instead of fetching it here', async () => {
vi.mocked(TrekShell.refreshOfflineCopy).mockResolvedValue({ saved: true, complete: true });
const fetchImpl = vi.fn(async (input: RequestInfo | URL) => {
if (String(input) === '/asset-manifest.json') return new Response(JSON.stringify(manifest));
throw new Error('the native side fetches the files');
}) as unknown as typeof fetch;
await keepOfflineShellFresh(fetchImpl, 'en', { nativeStore: true });
expect(fetchImpl).toHaveBeenCalledTimes(1);
expect(TrekShell.refreshOfflineCopy).toHaveBeenCalledTimes(1);
const { files } = vi.mocked(TrekShell.refreshOfflineCopy).mock.calls[0][0] as { files: string[] };
expect(files).toContain('assets/index-a1b2c3d4.js');
expect(files).toContain('assets/AtlasPage-99887766.js');
});
it('on Android also hands over the classic scripts the start page loads from the root', async () => {
vi.mocked(TrekShell.refreshOfflineCopy).mockResolvedValue({ saved: true, complete: true });
document.head.innerHTML = [
'<script src="/theme-boot.js"></script>',
'<script src="/shell-guard.js"></script>',
'<script type="module" src="/assets/index-a1b2c3d4.js"></script>',
'<script src="/plugins/demo/widget.js"></script>',
'<script src="https://elsewhere.example/tracker.js"></script>',
].join('');
const fetchImpl = vi.fn(async () => new Response(JSON.stringify(manifest))) as unknown as typeof fetch;
try {
await keepOfflineShellFresh(fetchImpl, 'en', { nativeStore: true });
} finally {
document.head.innerHTML = '';
}
const { files } = vi.mocked(TrekShell.refreshOfflineCopy).mock.calls[0][0] as { files: string[] };
expect(files).toContain('theme-boot.js');
expect(files).toContain('shell-guard.js');
expect(files.filter((f) => f === 'assets/index-a1b2c3d4.js')).toHaveLength(1);
expect(files.some((f) => f.includes('plugins') || f.includes('tracker'))).toBe(false);
});
it('on Android asks again on the next launch while the store is incomplete', async () => {
vi.mocked(TrekShell.refreshOfflineCopy).mockResolvedValue({ saved: true, complete: false });
const fetchImpl = vi.fn(async () => new Response(JSON.stringify(manifest))) as unknown as typeof fetch;
await keepOfflineShellFresh(fetchImpl, 'en', { nativeStore: true });
await keepOfflineShellFresh(fetchImpl, 'en', { nativeStore: true });
expect(TrekShell.refreshOfflineCopy).toHaveBeenCalledTimes(2);
});
});