mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
c23518dae0
A tester with a server on his home network could not open the Android app
away from it: after closing the app and turning Wi-Fi off it stayed on the
address screen ("No server answered at this address").
- Android keeps the server's UI itself (TrekOfflineShell): the start page
and every file of the build, listed by native/offlineShell.ts from the
server's manifest and downloaded natively. A web view runs no service
worker on a plain-http server, the usual address of a server at home, so
the old "Android has the service worker" assumption did not hold.
- Without a network, or once the server failed to load twice, the web view
serves the copy under the server's origin with Capacitor's bridge; the UI
runs on its own offline data and the API answers again once the server
is back. The address screen opens the copy too (openOfflineCopy).
- The check an update asks for no longer locks the app out offline: on both
platforms a server out of reach is put back on its copy and checked on
the next launch that reaches it. iOS no longer throws its copy away when
the update check runs.
129 lines
5.1 KiB
TypeScript
129 lines
5.1 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('refreshes the stored start page and fetches every file of the build', 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).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('stops after the 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);
|
|
});
|
|
|
|
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('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', 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 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', true);
|
|
await keepOfflineShellFresh(fetchImpl, 'en', true);
|
|
|
|
expect(TrekShell.refreshOfflineCopy).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|