mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
fix(maps): a place's marker photo is its community picture where it has one
An OSM or index place with a reviewed TREK Community picture showed it in its gallery but only a category pin in the lists and on the map: the marker photo asked Google and Wikimedia, never the community's pictures. It now asks them first, the order the gallery shows them in, and only while the TREK Places API is on.
This commit is contained in:
@@ -12,6 +12,7 @@ import { PoisAlongResolver } from '../../src/nest/maps/pois-along.resolver';
|
||||
import { GooglePlacesClient } from '../../src/nest/maps/providers/google-places.provider';
|
||||
import { OsmClient } from '../../src/nest/maps/providers/osm.client';
|
||||
import { WikimediaClient } from '../../src/nest/maps/providers/wikimedia.client';
|
||||
import type { TrekCommunityService } from '../../src/nest/maps/trek-community.service';
|
||||
import type { PlacePhotoCacheService } from '../../src/nest/place-photos/place-photo-cache.service';
|
||||
import { noGoogleQuota } from './google-quota';
|
||||
import { photonOff } from './photon';
|
||||
@@ -44,7 +45,17 @@ export function buildMapsParts(
|
||||
const osm = new OsmClient(photonOff());
|
||||
const wiki = new WikimediaClient();
|
||||
const selector = new PlacesProviderSelector(appSettings, users, googleQuota);
|
||||
const photos = new PlacePhotoResolver(photoCache, places, google, wiki, selector, passThroughPlaceChanges());
|
||||
// No community pictures here: a hand-built service never reaches the TREK Places API.
|
||||
const community = { photoPaths: async () => [], imageBytes: async () => null } as unknown as TrekCommunityService;
|
||||
const photos = new PlacePhotoResolver(
|
||||
photoCache,
|
||||
places,
|
||||
google,
|
||||
wiki,
|
||||
selector,
|
||||
passThroughPlaceChanges(),
|
||||
community,
|
||||
);
|
||||
const links = new MapsUrlResolver(osm);
|
||||
const details = new PlaceDetailsResolver(placeDetailsCache, google, osm, selector);
|
||||
const along = new PoisAlongResolver(osm);
|
||||
|
||||
@@ -9,10 +9,11 @@ import { PlacePhotoResolver } from '../../../src/nest/maps/place-photo.resolver'
|
||||
import type { PlacesProviderSelector } from '../../../src/nest/maps/places-provider.selector';
|
||||
import type { GooglePlacesClient } from '../../../src/nest/maps/providers/google-places.provider';
|
||||
import type { WikimediaClient } from '../../../src/nest/maps/providers/wikimedia.client';
|
||||
import type { TrekCommunityService } from '../../../src/nest/maps/trek-community.service';
|
||||
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
|
||||
import { passThroughPlaceChanges } from '../../helpers/place-changes';
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
function cache(over: Record<string, unknown> = {}) {
|
||||
return {
|
||||
@@ -35,11 +36,16 @@ function resolver(
|
||||
wikiOutcome: unknown,
|
||||
key: string | null = null,
|
||||
googleOutcome: unknown = { kind: 'none' },
|
||||
communityPhoto: { paths: string[]; bytes: Buffer | null } = { paths: [], bytes: null },
|
||||
) {
|
||||
const places = { setImageUrlIfUnset: vi.fn(async () => 1) };
|
||||
const google = { firstPhoto: vi.fn(async () => googleOutcome) };
|
||||
const wiki = { downloadPhoto: vi.fn(async () => wikiOutcome) };
|
||||
const selector = { getMapsKey: vi.fn(async () => key) };
|
||||
const community = {
|
||||
photoPaths: vi.fn(async () => communityPhoto.paths),
|
||||
imageBytes: vi.fn(async () => communityPhoto.bytes),
|
||||
};
|
||||
const r = new PlacePhotoResolver(
|
||||
c as unknown as PlacePhotoCacheService,
|
||||
places as unknown as PlacesRepository,
|
||||
@@ -47,8 +53,9 @@ function resolver(
|
||||
wiki as unknown as WikimediaClient,
|
||||
selector as unknown as PlacesProviderSelector,
|
||||
passThroughPlaceChanges(),
|
||||
community as unknown as TrekCommunityService,
|
||||
);
|
||||
return { r, places, google, wiki };
|
||||
return { r, places, google, wiki, community };
|
||||
}
|
||||
|
||||
describe('PlacePhotoResolver', () => {
|
||||
@@ -92,4 +99,85 @@ describe('PlacePhotoResolver', () => {
|
||||
expect(wiki.downloadPhoto).toHaveBeenCalledWith(1, 2, undefined);
|
||||
expect(c.markError).toHaveBeenCalledWith('ChIJabc', 'provider-error');
|
||||
});
|
||||
|
||||
describe('the community pictures of an index or OSM place', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it('PHOTO-R-005: lead, the way the place gallery shows them, and no other source is asked', async () => {
|
||||
const c = cache();
|
||||
const { r, google, wiki, community } = resolver(
|
||||
c,
|
||||
{ kind: 'none' },
|
||||
'key',
|
||||
{ kind: 'none' },
|
||||
{
|
||||
paths: ['/images/a.jpg', '/images/b.jpg'],
|
||||
bytes: Buffer.from([1]),
|
||||
},
|
||||
);
|
||||
expect(await r.resolve(1, 'node:410711907', 54.08, 12.12, 'Helgas Kitchen')).toEqual({
|
||||
photoUrl: '/api/maps/place-photo/node%3A410711907/bytes',
|
||||
attribution: 'TREK Community',
|
||||
});
|
||||
expect(community.imageBytes).toHaveBeenCalledWith('/images/a.jpg');
|
||||
expect(c.put).toHaveBeenCalledWith('node:410711907', Buffer.from([1]), 'TREK Community');
|
||||
expect(google.firstPhoto).not.toHaveBeenCalled();
|
||||
expect(wiki.downloadPhoto).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('PHOTO-R-006: a picture that would not load falls back and is remembered only briefly', async () => {
|
||||
const c = cache();
|
||||
const { r, wiki } = resolver(
|
||||
c,
|
||||
{ kind: 'none' },
|
||||
null,
|
||||
{ kind: 'none' },
|
||||
{ paths: ['/images/a.jpg'], bytes: null },
|
||||
);
|
||||
expect(await r.resolve(1, 'way:7', 1, 2)).toEqual({ photoUrl: null, attribution: null });
|
||||
expect(wiki.downloadPhoto).toHaveBeenCalled();
|
||||
expect(c.markError).toHaveBeenCalledWith('way:7', 'provider-error');
|
||||
});
|
||||
|
||||
it('PHOTO-R-007: a picture the disk cache cannot store falls back too', async () => {
|
||||
const c = cache({
|
||||
put: vi.fn(async () => {
|
||||
throw new Error('disk full');
|
||||
}),
|
||||
});
|
||||
const { r } = resolver(
|
||||
c,
|
||||
{ kind: 'none' },
|
||||
null,
|
||||
{ kind: 'none' },
|
||||
{ paths: ['/images/a.jpg'], bytes: Buffer.from([1]) },
|
||||
);
|
||||
expect(await r.resolve(1, 'relation:3', 1, 2)).toEqual({ photoUrl: null, attribution: null });
|
||||
expect(c.markError).toHaveBeenCalledWith('relation:3', 'provider-error');
|
||||
});
|
||||
|
||||
it('PHOTO-R-008: are not asked for with the TREK Places API off, nor for a place that cannot have any', async () => {
|
||||
const off = resolver(
|
||||
cache(),
|
||||
{ kind: 'none' },
|
||||
null,
|
||||
{ kind: 'none' },
|
||||
{ paths: ['/images/a.jpg'], bytes: Buffer.from([1]) },
|
||||
);
|
||||
vi.stubEnv('TREK_PLACES_ENABLED', 'false');
|
||||
await off.r.resolve(1, 'node:1', 1, 2);
|
||||
expect(off.community.photoPaths).not.toHaveBeenCalled();
|
||||
vi.unstubAllEnvs();
|
||||
|
||||
const coords = resolver(cache(), { kind: 'none' });
|
||||
await coords.r.resolve(1, 'coords:1,2', 1, 2);
|
||||
expect(coords.community.photoPaths).not.toHaveBeenCalled();
|
||||
const none = resolver(cache(), { kind: 'none' }, null, { kind: 'none' }, { paths: [], bytes: null });
|
||||
await none.r.resolve(1, 'gers:abc', 1, 2);
|
||||
expect(none.community.photoPaths).toHaveBeenCalledWith('gers:abc');
|
||||
expect(none.community.imageBytes).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user