Files
TREK/client/src/native/tileCache.test.ts
T
Maurice 3beb5c4223 feat(native): the Android app's widgets, location, tile store, Play Billing and Google Wallet
The UI now turns these on by feature on Android as well as iOS:

- widgets, widgetTrips and the travel day (liveActivity) write the same
  snapshot to the Android app's Glance widgets; the travel day also sends
  the next stop's minutes and the stops' colours for the Live Update.
- geolocation answers navigator.geolocation from the app's location stream.
- tileCache: Android keeps Mapbox's resources in its WebView client, so the
  map is offline-capable there too, without the iOS trek-tile:// rewrite.
- playBilling: the purchase client buys through Google Play on Android,
  with the purchase token as the receipt the control plane verifies.
- googleWallet: a booking's button adds it to Google Wallet on Android,
  through the save link the server asks the TREK services for.
2026-11-07 14:11:59 +01:00

72 lines
3.2 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { hasNativeFeature } from './capabilities';
import { isNativeApp, isNativeIos } from './platform';
import { mapboxTileCacheRequest, nativeRasterTemplate, tileCacheActive, toTileCacheUrl } from './tileCache';
vi.mock('./platform', () => ({ isNativeIos: vi.fn(), isNativeApp: vi.fn() }));
vi.mock('./capabilities', () => ({ hasNativeFeature: vi.fn() }));
beforeEach(() => {
vi.mocked(isNativeIos).mockReturnValue(true);
vi.mocked(isNativeApp).mockReturnValue(true);
vi.mocked(hasNativeFeature).mockReturnValue(true);
});
describe('tileCache', () => {
it('is on only in an app binary that has the store', () => {
expect(tileCacheActive()).toBe(true);
vi.mocked(hasNativeFeature).mockReturnValue(false);
expect(tileCacheActive()).toBe(false);
vi.mocked(hasNativeFeature).mockReturnValue(true);
vi.mocked(isNativeIos).mockReturnValue(false);
vi.mocked(isNativeApp).mockReturnValue(false);
expect(tileCacheActive()).toBe(false);
});
it('on Android keeps the store but leaves every URL as it is: the WebView client takes the requests', () => {
vi.mocked(isNativeIos).mockReturnValue(false);
expect(tileCacheActive()).toBe(true);
expect(nativeRasterTemplate('https://{s}.tiles.example.com/{z}/{x}/{y}.png')).toBe(
'https://{s}.tiles.example.com/{z}/{x}/{y}.png'
);
expect(mapboxTileCacheRequest('https://a.tiles.mapbox.com/v4/x/1/2/3.vector.pbf', 'Tile').url).toBe(
'https://a.tiles.mapbox.com/v4/x/1/2/3.vector.pbf'
);
});
it('swaps only the scheme, so the store knows where the tile lives', () => {
expect(toTileCacheUrl('https://a.tile.example.com/1/2/3.png?k=v')).toBe(
'trek-tile://a.tile.example.com/1/2/3.png?k=v'
);
expect(toTileCacheUrl('http://insecure.example.com/x')).toBe('http://insecure.example.com/x');
});
it('routes Leaflet raster templates through the store only where there is one', () => {
expect(nativeRasterTemplate('https://{s}.tiles.example.com/{z}/{x}/{y}.png')).toBe(
'trek-tile://{s}.tiles.example.com/{z}/{x}/{y}.png'
);
vi.mocked(hasNativeFeature).mockReturnValue(false);
expect(nativeRasterTemplate('https://{s}.tiles.example.com/{z}/{x}/{y}.png')).toBe(
'https://{s}.tiles.example.com/{z}/{x}/{y}.png'
);
});
it('sends what a Mapbox map draws through the store, never its telemetry', () => {
expect(
mapboxTileCacheRequest('https://api.mapbox.com/styles/v1/mapbox/streets-v12?access_token=pk', 'Style').url
).toBe('trek-tile://api.mapbox.com/styles/v1/mapbox/streets-v12?access_token=pk');
expect(mapboxTileCacheRequest('https://a.tiles.mapbox.com/v4/x/1/2/3.vector.pbf', 'Tile').url).toMatch(
/^trek-tile:/
);
expect(mapboxTileCacheRequest('https://events.mapbox.com/events/v2', 'Unknown').url).toBe(
'https://events.mapbox.com/events/v2'
);
expect(mapboxTileCacheRequest('https://api.mapbox.com/map-sessions/v1', 'Unknown').url).toBe(
'https://api.mapbox.com/map-sessions/v1'
);
expect(mapboxTileCacheRequest('https://tiles.openfreemap.org/planet/1/2/3.pbf', 'Tile').url).toBe(
'https://tiles.openfreemap.org/planet/1/2/3.pbf'
);
});
});