mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
feat(roadtrip): what a charger costs, before it is a stop
A tester found the charging panel once a station was on the trip and asked for it while still deciding. The availability and the published tariffs were reachable only for a saved place: the route takes a places id, and a hit found along the route has no row yet. ChargingService.load already needed nothing but a coordinate and a name, so it now has a public method that says so, behind a trip-scoped route with the same guard stack, a Zod contract in shared, and the parallel MCP tool in this same change. Same service, same caches, so a station looked at in the dialog and then added keys the entry already fetched. Trip-scoped even though the trip is not part of the question: without :tripId the trip access guard goes too, and what is left is an open proxy in front of somebody else's public API. It renders in the add dialog, and only while the selected kind is charging, so a fuel or food stop costs nothing and one opened dialog is one lookup. Deliberately not per result row: the corridor search publishes box by box, every mounted panel owns a refresh timer, a cache miss can cost seven upstream requests, and a station in a dense charging park is the case the upstream matcher refuses as ambiguous. Power and socket counts are not in this answer and were not asked of it. They are OSM tag data and already ride along on the corridor row for free.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { Body, Controller, HttpCode, Post, UseGuards } from '@nestjs/common';
|
||||
import { chargingLookupSchema } from '@trek/shared';
|
||||
import { createZodDto } from 'nestjs-zod';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import { AddonGuard } from '../addons/addon.guard';
|
||||
import { RequireAddon } from '../addons/require-addon.decorator';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { TripAccessGuard } from '../permissions/trip-access.guard';
|
||||
import { ChargingService } from './charging.service';
|
||||
|
||||
/** Exported so the parity test can hold the route and the MCP tool to the same contract. */
|
||||
export class ChargingLookupDto extends createZodDto(chargingLookupSchema) {}
|
||||
|
||||
/**
|
||||
* Charging availability for a station nobody has added yet.
|
||||
*
|
||||
* A sibling of `ChargingController` rather than another verb on it, because the two
|
||||
* ask in different ways: that one names a row in `places`, this one hands over a
|
||||
* coordinate. Same service, same cache, same answer.
|
||||
*
|
||||
* Trip-scoped on purpose, even though the trip is not part of the question. Leaving
|
||||
* `:tripId` out would take `TripAccessGuard` with it, and what is left is an open
|
||||
* proxy in front of somebody else's public API: anyone with the URL could aim it
|
||||
* anywhere on the planet at whatever rate they liked. Nothing asks this while not
|
||||
* planning a trip, so the scope costs the caller nothing.
|
||||
*
|
||||
* A POST for a read follows `RoadtripSearchController`: the input is a coordinate pair
|
||||
* and a name, which belongs in a body behind a contract rather than in a query string.
|
||||
*/
|
||||
@Controller('api/trips/:tripId/roadtrip/charging-lookup')
|
||||
@UseGuards(AddonGuard, JwtAuthGuard, TripAccessGuard)
|
||||
@RequireAddon(ADDON_IDS.ROADTRIP, 'Road trip')
|
||||
export class ChargingLookupController {
|
||||
constructor(private readonly charging: ChargingService) {}
|
||||
@Post()
|
||||
@HttpCode(200)
|
||||
lookup(@Body() input: ChargingLookupDto) { return this.charging.lookup(input.lat, input.lng, input.name); }
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { chargingLookupSchema } from '@trek/shared';
|
||||
import { z } from 'zod';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import { noAccess } from '../../mcp/tools/_shared';
|
||||
@@ -17,4 +18,11 @@ export class ChargingMcp {
|
||||
if (!this.db.canAccessTrip(tripId, ctx.userId)) return noAccess();
|
||||
return ok(await this.charging.read(tripId, placeId));
|
||||
}
|
||||
@Tool({ name: 'lookup_roadtrip_charging_info', description: 'Read open charging availability and published tariffs for a charging station by coordinate, before it is added to a trip. Needs a trip only for access control. No API key required. Every caveat of get_roadtrip_charging_info applies unchanged: coverage varies by region and operator, unknown or stale status is not available capacity, prices are gross per-component figures that can carry conditions and extra fees and are not personalized roaming quotes. Report the source, licence and timestamp alongside any figure from it. Charging power, socket types and socket counts are not in this answer; they come from the place search.',
|
||||
inputSchema: { tripId: z.number().int().positive(), ...chargingLookupSchema.shape },
|
||||
annotations: TOOL_ANNOTATIONS_READONLY, access: { group: 'trips', mode: 'read' }, when: addonGate(ADDON_IDS.ROADTRIP) })
|
||||
async lookup({ tripId, lat, lng, name }: { tripId: number; lat: number; lng: number; name: string }, ctx: McpContext) {
|
||||
if (!this.db.canAccessTrip(tripId, ctx.userId)) return noAccess();
|
||||
return ok(await this.charging.lookup(lat, lng, name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ import { DatabaseService } from '../database/database.service';
|
||||
import { readCappedJson } from '../../utils/cappedFetch';
|
||||
import { chargingLocation, chargingSource, chargingTariff, matchChargingLocation, normalizeCharging, type ChargingSource, type ChargingTariff } from './charging.helpers';
|
||||
|
||||
const empty = (status: ChargingInfo['status']): ChargingInfo => ({ checkedAt: new Date().toISOString(), status, station: null, source: null, sourceUrl: null, license: null, updatedAt: null, stale: false, available: null, total: 0, unknown: 0, tariffs: [], pricesUnavailable: false });
|
||||
/** The shape of "we cannot say", exported so nothing has to restate its thirteen fields. */
|
||||
export const empty = (status: ChargingInfo['status']): ChargingInfo => ({ checkedAt: new Date().toISOString(), status, station: null, source: null, sourceUrl: null, license: null, updatedAt: null, stale: false, available: null, total: 0, unknown: 0, tariffs: [], pricesUnavailable: false });
|
||||
async function request(path: string) {
|
||||
const fetched = await fetch(`https://api.mobidata-bw.de/ocpdb${path}`, { headers: { 'User-Agent': 'TREK Roadtrip (https://github.com/liketrek/TREK)' }, signal: AbortSignal.timeout(12000), redirect: 'error' });
|
||||
if (!fetched.ok) { await fetched.body?.cancel(); throw new Error('Charging source unavailable'); }
|
||||
@@ -22,11 +23,28 @@ export class ChargingService {
|
||||
const place = this.db.get<{ name: string; lat: number | null; lng: number | null; stop_type: string }>('SELECT name, lat, lng, stop_type FROM places WHERE id = ? AND trip_id = ?', placeId, tripId);
|
||||
if (!place) throw new HttpException({ error: 'Place not found' }, 404);
|
||||
if (place.stop_type !== 'charging' || place.lat == null || place.lng == null) return empty('unknown');
|
||||
const key = `${place.lat},${place.lng}:${place.name}`;
|
||||
return this.lookup(place.lat, place.lng, place.name);
|
||||
}
|
||||
/**
|
||||
* The same answer for a station the trip has not saved yet.
|
||||
*
|
||||
* A hit found along the route has no `places` row, so it cannot be asked for by id,
|
||||
* but the match never needed one: a coordinate and a name is the whole input. Sharing
|
||||
* `cached` with the saved-stop path matters twice over. The answer a charger gives
|
||||
* while it is being considered is the answer it gives once it is on the trip, and the
|
||||
* minute of cover is what keeps a second look from becoming a second round of
|
||||
* upstream requests.
|
||||
*
|
||||
* The cache is keyed on the coordinate exactly as it arrived, not on a rounded one:
|
||||
* a stop added from a corridor hit carries the coordinate the hit had, so the two
|
||||
* paths land on the same entry without either of them knowing about the other.
|
||||
*/
|
||||
lookup(lat: number, lng: number, name: string): Promise<ChargingInfo> {
|
||||
const key = `${lat},${lng}:${name}`;
|
||||
const cached = this.cached.get(key);
|
||||
if (cached && cached.expires > Date.now()) return cached.value;
|
||||
if (this.cached.size >= 300) this.cached.delete(this.cached.keys().next().value!);
|
||||
const value = this.load(place.lat, place.lng, place.name).catch(() => empty('unavailable'));
|
||||
const value = this.load(lat, lng, name).catch(() => empty('unavailable'));
|
||||
this.cached.set(key, { expires: Date.now() + 60000, value });
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ChargingMcp } from './charging.mcp';
|
||||
import { ChargingService } from './charging.service';
|
||||
import { ChargingController } from './charging.controller';
|
||||
import { ChargingLookupController } from './charging-lookup.controller';
|
||||
import { GoogleRouteService } from './google-route.service';
|
||||
import { GoogleRouteController } from './google-route.controller';
|
||||
import { GoogleRouteMcp } from './google-route.mcp';
|
||||
@@ -47,7 +48,7 @@ import { RoadtripHazardsMcp } from './roadtrip-hazards.mcp';
|
||||
PluginsRuntimeModule,
|
||||
MapsModule, PlacesModule, AssignmentsModule,
|
||||
],
|
||||
controllers: [ChargingController, GoogleRouteController, RoadtripSearchController, RoadtripPreferencesController, RoadtripController, DayBoundariesController, RoadtripHazardsController],
|
||||
controllers: [ChargingController, ChargingLookupController, GoogleRouteController, RoadtripSearchController, RoadtripPreferencesController, RoadtripController, DayBoundariesController, RoadtripHazardsController],
|
||||
providers: [ChargingMcp, ChargingService, GoogleRouteService, GoogleRouteMcp,
|
||||
RoadtripSearchService,
|
||||
RoadtripHazardsService,
|
||||
|
||||
@@ -146,6 +146,26 @@ describe('Roadtrip e2e (real guard chain + temp SQLite)', () => {
|
||||
expect(read).toHaveBeenCalledTimes(1);
|
||||
} finally { read.mockRestore(); }
|
||||
});
|
||||
it('gates the coordinate lookup like the saved stop and validates before fetching', async () => {
|
||||
const lookup = vi.spyOn(app.get(ChargingService), 'lookup').mockResolvedValue({} as never);
|
||||
const body = { lat: 48.137, lng: 11.575, name: 'Ladepark Nord' };
|
||||
const post = () => request(server).post('/api/trips/5/roadtrip/charging-lookup').set('Cookie', cookie());
|
||||
try {
|
||||
await request(server).post('/api/trips/5/roadtrip/charging-lookup').send(body).expect(401);
|
||||
await request(server).post('/api/trips/6/roadtrip/charging-lookup').set('Cookie', cookie()).send(body).expect(404);
|
||||
await post().send({ ...body, lat: 91 }).expect(400);
|
||||
// A list is the shape a caller would reach for to fan a whole corridor out in one
|
||||
// request. There is no rate limiter here and each miss costs the upstream registry
|
||||
// several requests, so the contract refuses it before anything leaves the server.
|
||||
await post().send([body]).expect(400);
|
||||
expect(lookup).not.toHaveBeenCalled();
|
||||
await post().send(body).expect(200);
|
||||
expect(lookup).toHaveBeenCalledWith(48.137, 11.575, 'Ladepark Nord');
|
||||
setAddon(false);
|
||||
await post().send(body).expect(404);
|
||||
expect(lookup).toHaveBeenCalledTimes(1);
|
||||
} finally { lookup.mockRestore(); }
|
||||
});
|
||||
it('validates Google route preview and import before executing either service', async () => {
|
||||
const routes = app.get(GoogleRouteService);
|
||||
const preview = vi.spyOn(routes, 'preview').mockResolvedValue({ stops: [] });
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { ChargingService } from '../../../src/nest/roadtrip/charging.service';
|
||||
import { chargingLookupSchema } from '@trek/shared';
|
||||
import { ChargingService, empty } from '../../../src/nest/roadtrip/charging.service';
|
||||
import { ChargingLookupController, ChargingLookupDto } from '../../../src/nest/roadtrip/charging-lookup.controller';
|
||||
import { ChargingMcp } from '../../../src/nest/roadtrip/charging.mcp';
|
||||
import { getEntry, type ClassRef } from '../../../src/nest-mcp/metadata';
|
||||
import { matchChargingLocation, normalizeCharging, type ChargingLocation, type ChargingSource, type ChargingTariff } from '../../../src/nest/roadtrip/charging.helpers';
|
||||
|
||||
const now = Date.parse('2026-09-12T12:00:00Z');
|
||||
@@ -59,3 +63,86 @@ describe('Open charging data', () => {
|
||||
expect(fetcher).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* The same data for a station nobody has added yet (#1797 feedback: "it would be useful
|
||||
* to see this data also before the add"). One service, one cache, one answer: the only
|
||||
* difference is how the station is named.
|
||||
*/
|
||||
describe('Open charging data before the stop exists', () => {
|
||||
const answers = (tariffs: unknown) => vi.fn(async (url: string) => {
|
||||
if (url.includes('/sources')) return new Response(JSON.stringify({ items: [source] }));
|
||||
if (url.includes('/locations')) return new Response(JSON.stringify({ items: [station], total_count: 1 }));
|
||||
return new Response(JSON.stringify(tariffs));
|
||||
});
|
||||
|
||||
it('answers for a coordinate and lets the saved stop reuse that answer', async () => {
|
||||
const fetcher = answers({ items: [tariff], total_count: 1 });
|
||||
vi.stubGlobal('fetch', fetcher);
|
||||
const service = new ChargingService({ get: () => ({ name: 'Test', lat: 48, lng: 11, stop_type: 'charging' }) } as never);
|
||||
|
||||
const ahead = await service.lookup(48, 11, 'Test');
|
||||
expect(ahead.status).toBe('ok');
|
||||
// The count of individual charge points, which is what "how many chargers" means.
|
||||
expect(ahead).toMatchObject({ total: 3, available: 1, unknown: 1 });
|
||||
expect(ahead.tariffs[0].components[0]).toMatchObject({ kind: 'ENERGY', taxIncluded: true });
|
||||
// Added to the trip, the same coordinate and name key the same cache entry, so the
|
||||
// stop on the rail does not pay for the answer the dialog already got.
|
||||
expect(await service.read(1, 2)).toEqual(ahead);
|
||||
expect(fetcher).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('never reports a station it could not identify as free', async () => {
|
||||
// Two operators within the radius is the dense case a station found along a route
|
||||
// routinely is. Returning the nearest one would attach another operator's prices.
|
||||
vi.stubGlobal('fetch', vi.fn(async (url: string) => {
|
||||
if (url.includes('/sources')) return new Response(JSON.stringify({ items: [source] }));
|
||||
return new Response(JSON.stringify({ items: [station, { ...station, id: 'other', operator: { name: 'Other' } }], total_count: 2 }));
|
||||
}));
|
||||
const info = await new ChargingService({ get: () => null } as never).lookup(48, 11, 'Charging');
|
||||
expect(info.status).toBe('ambiguous');
|
||||
expect(info.available).toBeNull();
|
||||
});
|
||||
|
||||
it('turns an unreachable source into an empty answer, not a failed request', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => new Response('', { status: 503 })));
|
||||
const info = await new ChargingService({ get: () => null } as never).lookup(48, 11, 'Test');
|
||||
expect(info).toEqual({ ...empty('unavailable'), checkedAt: info.checkedAt });
|
||||
});
|
||||
|
||||
it('does not go to the database for a station that is not in it', async () => {
|
||||
vi.stubGlobal('fetch', answers({ items: [], total_count: 0 }));
|
||||
const get = vi.fn();
|
||||
await new ChargingService({ get } as never).lookup(48, 11, 'Test');
|
||||
expect(get).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('serves the route and the MCP tool from one contract and one access check', async () => {
|
||||
const charging = { read: vi.fn(), lookup: vi.fn(async () => empty('unknown')) };
|
||||
const db = { canAccessTrip: vi.fn(() => false) };
|
||||
const tool = new ChargingMcp(charging as never, db as never, {} as never);
|
||||
const input = { lat: 48.1, lng: 11.5, name: 'Ladepark Nord' };
|
||||
|
||||
// A trip the caller cannot reach is refused before anything is fetched, exactly as
|
||||
// TripAccessGuard refuses it on the route.
|
||||
expect((await tool.lookup({ tripId: 3, ...input }, { userId: 5 } as never)).isError).toBe(true);
|
||||
expect(charging.lookup).not.toHaveBeenCalled();
|
||||
db.canAccessTrip.mockReturnValue(true);
|
||||
await tool.lookup({ tripId: 3, ...input }, { userId: 5 } as never);
|
||||
await new ChargingLookupController(charging as never).lookup(input as unknown as ChargingLookupDto);
|
||||
|
||||
expect(charging.lookup.mock.calls).toEqual([[48.1, 11.5, 'Ladepark Nord'], [48.1, 11.5, 'Ladepark Nord']]);
|
||||
// Both surfaces validate through the shared contract rather than a restatement of
|
||||
// it, so a bound or a length limit cannot be tightened on one side only.
|
||||
for (const bad of [{ ...input, lat: 91 }, { ...input, lng: -181 }, { ...input, name: 'x'.repeat(201) }]) {
|
||||
expect(ChargingLookupDto.schema.safeParse(bad).success).toBe(false);
|
||||
expect(chargingLookupSchema.safeParse(bad).success).toBe(false);
|
||||
}
|
||||
expect(ChargingLookupDto.schema.safeParse(input).success).toBe(true);
|
||||
const declared = getEntry(ChargingMcp as unknown as ClassRef, 'lookup')?.options as { inputSchema: Record<string, unknown> };
|
||||
expect(Object.keys(declared.inputSchema).sort()).toEqual(['lat', 'lng', 'name', 'tripId']);
|
||||
for (const [field, type] of Object.entries(chargingLookupSchema.shape)) {
|
||||
expect(declared.inputSchema[field]).toBe(type);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user