feat(plugins): search plugins can answer the place search as you type

A searchProvider plugin can now implement an optional suggest() beside
search(). Its places then show up in the place search's typed-ahead list,
after the core suggestions and marked with the plugin's name, on desktop
and phone. An index that cannot take a request per keystroke leaves it
out and keeps answering the full search only.

The child reports which functions each hook object carries at load, so
GET /api/plugin-search/suggest only asks the plugins that implement
suggest: from the second character, three rows at most across all of
them, 800 ms to answer. The feed marks plugins holding
hook:search-provider and the client only asks the route while one is
installed. A plugin row carries its whole place, so picking it skips the
details lookup, which knows no plugin id. The phone's search results now
name their source as the desktop ones do, and the desktop dropdown
scrolls instead of running past the dialog.

Addresses discussion #2221.
This commit is contained in:
Maurice
2026-09-25 09:19:11 +02:00
parent 1727732841
commit 2a8071f642
38 changed files with 1248 additions and 96 deletions
@@ -3,6 +3,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
const { pluginsEnabled } = vi.hoisted(() => ({ pluginsEnabled: vi.fn(() => true) }));
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
import { pluginSearchHitSchema, pluginSuggestResultSchema } from '@trek/shared';
import { PluginSearchController } from '../../../src/nest/plugins/contributions/plugin-search.controller';
import type { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
@@ -15,6 +16,7 @@ function controller(over: Partial<PluginHooks> = {}) {
const hooks = {
providersOf: vi.fn(() => ['p1']),
searchPlaces: vi.fn(async () => [hit()]),
suggestPlaces: vi.fn(async () => [hit()]),
...over,
} as unknown as PluginHooks;
return { c: new PluginSearchController(hooks), hooks };
@@ -198,4 +200,75 @@ describe('PluginSearchController', () => {
const { places } = await c.search('milan', undefined, undefined, undefined, undefined, req(5));
expect(places).toHaveLength(20);
});
it('answers in the row the shared contract describes, so the client can check it strictly', async () => {
// The server's SearchHit and the shared schema are two spellings of one row; this
// is what fails when one of them moves without the other.
const { c } = controller({
searchPlaces: vi.fn(async () => [
hit({ address: 'Via Roma 1', rating: 4.5, website: 'ok.example', phone: '+39 02', category: 'restaurant', description: 'Good' }),
hit({ id: 'bare', name: 'Bare' }),
]) as unknown as PluginHooks['searchPlaces'],
});
const { places } = await c.search('milan', undefined, undefined, undefined, undefined, req(5));
for (const place of places) expect(pluginSearchHitSchema.safeParse(place).success).toBe(true);
});
});
describe('PluginSearchController.suggest (#2221)', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); });
it('asks only the providers that implement suggest, never their search', async () => {
const { c, hooks } = controller();
await c.suggest('ich', '35.66', '139.7', 'ja', req(5));
expect(hooks.providersOf).toHaveBeenCalledWith('searchProvider', 'suggest');
expect(hooks.suggestPlaces).toHaveBeenCalledWith(
'p1',
{ query: 'ich', limit: 3, lang: 'ja', near: { lat: 35.66, lng: 139.7 } },
5,
);
expect(hooks.searchPlaces).not.toHaveBeenCalled();
});
it('asks nothing for one letter, a blank box, a caller without a session or a runtime that is off', async () => {
const { c, hooks } = controller();
expect(await c.suggest('i', undefined, undefined, undefined, req(5))).toEqual({ places: [] });
expect(await c.suggest(' ', undefined, undefined, undefined, req(5))).toEqual({ places: [] });
expect(await c.suggest(undefined, undefined, undefined, undefined, req(5))).toEqual({ places: [] });
expect(await c.suggest('ichiran', undefined, undefined, undefined, req(undefined))).toEqual({ places: [] });
pluginsEnabled.mockReturnValue(false);
expect(await c.suggest('ichiran', undefined, undefined, undefined, req(5))).toEqual({ places: [] });
expect(hooks.providersOf).not.toHaveBeenCalled();
expect(hooks.suggestPlaces).not.toHaveBeenCalled();
});
it('answers without reaching any plugin when none implements suggest', async () => {
const { c, hooks } = controller({ providersOf: vi.fn(() => []) });
expect(await c.suggest('ichiran', undefined, undefined, undefined, req(5))).toEqual({ places: [] });
expect(hooks.suggestPlaces).not.toHaveBeenCalled();
});
it('keeps three rows across all providers, interleaved, whatever each one sends', async () => {
const { c } = controller({
providersOf: vi.fn(() => ['a', 'b']),
suggestPlaces: vi.fn(async (id: string) =>
Array.from({ length: 10 }, (_v, i) => hit({ id: `${id}${i}`, name: `${id.toUpperCase()}${i}` })),
) as unknown as PluginHooks['suggestPlaces'],
});
const result = await c.suggest('ichiran', undefined, undefined, undefined, req(5));
expect(result.places.map(p => p.name)).toEqual(['A0', 'B0', 'A1']);
expect(pluginSuggestResultSchema.safeParse(result).success).toBe(true);
});
it('drops a provider that times out and normalizes what the others send', async () => {
const { c } = controller({
providersOf: vi.fn(() => ['slow', 'ok']),
suggestPlaces: vi.fn(async (id: string) => {
if (id === 'slow') throw new Error('plugin invoke timed out');
return [hit({ id: 'r1', website: 'javascript:alert(1)' }), { name: 'no coordinates' }];
}) as unknown as PluginHooks['suggestPlaces'],
});
const { places } = await c.suggest('ichiran', undefined, undefined, undefined, req(5));
expect(places).toEqual([expect.objectContaining({ osm_id: 'plugin:ok:r1', source: 'plugin:ok', website: null })]);
});
});