Files
TREK/server/tests/helpers/photon.ts
T
Maurice 256f8c08e2 feat(server): ask the TREK services' Photon before Nominatim for search and reverse lookups
An installation that holds a key for the TREK services and has no
NOMINATIM_URL of its own now sends the OpenStreetMap search and the
reverse lookups to photon.liketrek.com first. The answers are read into
Nominatim's shape, so the place records stay the same, and every miss
(refused key, allowance spent, timeout, error, foreign body) falls back
to Nominatim, with a one minute pause after four misses in a row.

TREK_PHOTON_URL points at another copy, and the default of
TREK_SERVICES_HOSTS now lists photon.liketrek.com and
valhalla.liketrek.com beside places.liketrek.com.
2026-10-29 13:34:28 +01:00

61 lines
2.1 KiB
TypeScript

import { deriveServices } from '../../src/app-config/boot-derive';
import { PhotonClient } from '../../src/nest/maps/providers/photon.client';
import { ServiceIdentityPort } from '../../src/nest/service-identity/service-identity.interface';
/** Photon with the TREK services off, so an OsmClient built by hand asks Nominatim as it always did. */
export function photonOff(): PhotonClient {
return new PhotonClient(deriveServices({ TREK_SERVICES: 'off' }));
}
/**
* The installation identity as the service-identity module hands it out, with
* a key to send or none, and the status of every answer it was shown.
*/
export class FakeServiceIdentity extends ServiceIdentityPort {
bearer: string | null = 'tk1.kid.payload.signature';
readonly observed: { host: string; status: number }[] = [];
headersFor(): Record<string, string> {
const headers: Record<string, string> = { 'User-Agent': 'TREK/test' };
if (this.bearer) headers.Authorization = `Bearer ${this.bearer}`;
return headers;
}
observe(url: URL, response: Response): void {
this.observed.push({ host: url.hostname, status: response.status });
}
}
/** The Brandenburg Gate as Photon answers a search for it. */
export function gateFeature(properties: Record<string, unknown> = {}) {
return {
type: 'Feature',
geometry: { type: 'Point', coordinates: [13.3777, 52.5163] },
properties: {
osm_type: 'W',
osm_id: 518071791,
osm_key: 'tourism',
osm_value: 'attraction',
type: 'house',
name: 'Brandenburger Tor',
street: 'Pariser Platz',
postcode: '10117',
district: 'Mitte',
city: 'Berlin',
state: 'Berlin',
country: 'Deutschland',
countrycode: 'DE',
extra: { wikidata: 'Q82425', wikipedia: 'de:Brandenburger Tor' },
...properties,
},
};
}
/** A Photon answer over the given features, as the service sends it. */
export function photonAnswer(features: unknown[], status = 200): Response {
return new Response(JSON.stringify({ type: 'FeatureCollection', features }), {
status,
headers: { 'content-type': 'application/json' },
});
}