mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
feat(maps): bill a place search as one session instead of per keystroke
Google charges autocomplete per request unless the requests carry a session token. With one, the keystrokes of a single search and the details lookup that ends it are charged together, which is also how a user experiences it: they looked up one place. TREK sent no token at all, so every debounced keystroke was its own billable request. On a busy trip that is the largest line on the Places bill, and it is paid by whoever owns the key, which for a self-hosted instance is the person running it. A session opens with the first suggestion request and closes when the user picks a result. It has to close on the abandoned paths too: a token carried into the next search would merge two searches into one billed session and scope the suggestions wrongly. So the search box clearing it, the text-search fallback and the error paths all end it. The token is validated on the way in. The autocomplete body goes through the Zod pipe, the details query does not, so the controller shape-checks it there. Anything that does not match Google's URL-safe 36-character form is dropped rather than forwarded, which falls back to per-request billing instead of breaking a lookup. A cached details hit never reaches Google and so never closes its session. That is billing-neutral: an unclosed session is charged as a plain autocomplete session, which is still cheaper than the per-request behaviour it replaces.
This commit is contained in:
@@ -115,7 +115,16 @@ describe('MapsController (parity with the legacy /api/maps route)', () => {
|
||||
const autocomplete = vi.fn().mockResolvedValue({ suggestions: [], source: 'osm' });
|
||||
const bias = { low: { lat: 1, lng: 2 }, high: { lat: 3, lng: 4 } };
|
||||
await makeController({ autocompleteDisabled: () => false, autocomplete }).autocomplete(user, { input: 'be', lang: 'en', locationBias: bias });
|
||||
expect(autocomplete).toHaveBeenCalledWith(3, 'be', 'en', bias);
|
||||
expect(autocomplete).toHaveBeenCalledWith(3, 'be', 'en', bias, undefined);
|
||||
});
|
||||
|
||||
// Session tokens tie a search's keystrokes and its details lookup into one
|
||||
// Google billing session instead of charging each request.
|
||||
it('passes a session token through to the service', async () => {
|
||||
const autocomplete = vi.fn().mockResolvedValue({ suggestions: [], source: 'google' });
|
||||
await makeController({ autocompleteDisabled: () => false, autocomplete })
|
||||
.autocomplete(user, { input: 'be', sessionToken: 'abc123' });
|
||||
expect(autocomplete).toHaveBeenCalledWith(3, 'be', undefined, undefined, 'abc123');
|
||||
});
|
||||
|
||||
it('maps a service error', async () => {
|
||||
@@ -145,7 +154,23 @@ describe('MapsController (parity with the legacy /api/maps route)', () => {
|
||||
it('uses the plain lookup without expand', async () => {
|
||||
const details = vi.fn().mockResolvedValue({ place: { id: 'p1' } });
|
||||
await makeController({ detailsDisabled: () => false, details }).details(user, 'p1', undefined, 'de');
|
||||
expect(details).toHaveBeenCalledWith(3, 'p1', 'de');
|
||||
expect(details).toHaveBeenCalledWith(3, 'p1', 'de', undefined);
|
||||
});
|
||||
|
||||
// The details query is not Zod-validated, so the token is shape-checked here
|
||||
// and a junk value degrades to per-request billing instead of reaching Google.
|
||||
it('forwards a well-formed session token and drops a malformed one', async () => {
|
||||
const details = vi.fn().mockResolvedValue({ place: { id: 'p1' } });
|
||||
const c = makeController({ detailsDisabled: () => false, details });
|
||||
|
||||
await c.details(user, 'p1', undefined, 'de', undefined, 'a-b_C9');
|
||||
expect(details).toHaveBeenLastCalledWith(3, 'p1', 'de', 'a-b_C9');
|
||||
|
||||
await c.details(user, 'p1', undefined, 'de', undefined, 'has spaces & symbols!');
|
||||
expect(details).toHaveBeenLastCalledWith(3, 'p1', 'de', undefined);
|
||||
|
||||
await c.details(user, 'p1', undefined, 'de', undefined, 'x'.repeat(37));
|
||||
expect(details).toHaveBeenLastCalledWith(3, 'p1', 'de', undefined);
|
||||
});
|
||||
|
||||
it('maps a service error', async () => {
|
||||
|
||||
Reference in New Issue
Block a user