Files
TREK/server/tests/unit/nest/maps.place-photo-resolver.test.ts
T
Maurice 07d54d6f6e 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.
2026-11-30 13:14:23 +01:00

184 lines
7.4 KiB
TypeScript

/**
* PlacePhotoResolver on its own: the outcomes of the two photo sources mapped
* onto the negative cache. The full getPlacePhoto flow (cache hit, in-flight
* dedupe, Google then Wikimedia) runs through MapsService in
* maps.service.test.ts; this pins the cases a source answer alone decides.
*/
import type { PlacesRepository } from '../../../src/db/repositories/Places.repository';
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 { afterEach, describe, expect, it, vi } from 'vitest';
function cache(over: Record<string, unknown> = {}) {
return {
get: vi.fn(async () => null),
getErrored: vi.fn(async () => false),
getInFlight: vi.fn(() => undefined),
setInFlight: vi.fn(),
markError: vi.fn(async () => {}),
put: vi.fn(async (id: string, _bytes: Buffer, attribution: string | null) => ({
photoUrl: `/p/${id}`,
filePath: '/x',
attribution,
})),
...over,
};
}
function resolver(
c: ReturnType<typeof cache>,
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,
google as unknown as GooglePlacesClient,
wiki as unknown as WikimediaClient,
selector as unknown as PlacesProviderSelector,
passThroughPlaceChanges(),
community as unknown as TrekCommunityService,
);
return { r, places, google, wiki, community };
}
describe('PlacePhotoResolver', () => {
it('PHOTO-R-001: a Wikimedia picture the disk cache cannot store counts as a provider failure', async () => {
const c = cache({
put: vi.fn(async () => {
throw new Error('disk full');
}),
});
const { r } = resolver(c, { kind: 'photo', bytes: Buffer.from([1]), attribution: 'A' });
expect(await r.resolve(1, 'coords:1,2', 1, 2, 'X')).toEqual({ photoUrl: null, attribution: null });
expect(c.markError).toHaveBeenCalledWith('coords:1,2', 'provider-error');
});
it('PHOTO-R-002: nothing anywhere is remembered as a plain miss', async () => {
const c = cache();
const { r } = resolver(c, { kind: 'none' });
expect(await r.resolve(1, 'coords:1,2', 1, 2)).toEqual({ photoUrl: null, attribution: null });
expect(c.markError).toHaveBeenCalledWith('coords:1,2', 'no-photo');
});
it('PHOTO-R-003: a Google photo is stored, its proxy URL persisted, and Wikimedia never asked', async () => {
const c = cache();
const { r, places, wiki } = resolver(c, { kind: 'none' }, 'key', {
kind: 'photo',
bytes: Buffer.from([1]),
attribution: 'G',
});
expect(await r.resolve(1, 'ChIJabc', 1, 2)).toEqual({
photoUrl: '/api/maps/place-photo/ChIJabc/bytes',
attribution: 'G',
});
expect(places.setImageUrlIfUnset).toHaveBeenCalledWith('ChIJabc', '/p/ChIJabc');
expect(wiki.downloadPhoto).not.toHaveBeenCalled();
});
it('PHOTO-R-004: a Google failure falls back to Wikimedia and is remembered briefly when that has nothing', async () => {
const c = cache();
const { r, wiki } = resolver(c, { kind: 'none' }, 'key', { kind: 'failed' });
expect(await r.resolve(1, 'ChIJabc', 1, 2)).toEqual({ photoUrl: null, attribution: null });
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();
});
});
});