Files
TREK/client/src/repo/pluginPoiRepo.test.ts
T
Maurice 1727732841 feat(plugins): plugins can add POI categories to the map and answer them
A new manifest capability poiCategories (up to 4, icon from an allow-list,
strict #rrggbb color) and the hook poiCategoryProvider.getPois behind the
hook:poi-category-provider permission. GET /api/plugin-pois in the plugin
contributions module answers them in the /api/maps/pois shape, with
search_plugin_pois as the MCP twin. plugin: ids no longer count as Google
place ids. SDK 1.8.0 carries the types, validation and docs.

On the map, one hook merges the built-in categories with the ones active
plugins declare, and the pill shows the plugin chips after a divider on
desktop and phone, scrolling sideways instead of shrinking a chip below
its touch target. A plugin chip searches through a repo that refuses
offline and parses the answer strictly. Its pins, on Leaflet and GL, use
the category's own icon and color, both checked again before they reach
any markup, and the hover card lists the plugin's details as escaped
text. A plugin POI can be added as a place like any other. The admin
panels on desktop and phone show a chip for the new permission and the
categories a plugin would add, from one shared module.

Addresses discussion #1781.
2026-09-25 01:24:55 +00:00

69 lines
3.8 KiB
TypeScript

// FE-REPO-PLUGINPOI-001 to FE-REPO-PLUGINPOI-005
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { PluginPoi } from '@trek/shared'
import { pluginPoiRepo } from './pluginPoiRepo'
import { isEffectivelyOffline } from '../sync/networkMode'
import { pluginPoisApi } from '../api/pluginPois'
vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: vi.fn(() => false) }))
vi.mock('../api/pluginPois', () => ({ pluginPoisApi: { search: vi.fn() } }))
const BBOX = { south: 47.1, west: 11.2, north: 47.3, east: 11.5 }
const row: PluginPoi = {
osm_id: 'plugin:trail-finder:th-1', name: 'Hochalm trailhead', lat: 47.2, lng: 11.3,
category: 'plugin:trail-finder/trailheads', poi_type: 'plugin:trail-finder/trailheads',
address: 'Almweg 1', website: null, phone: null,
opening_hours: null, cuisine: null, brand: null, brand_wikidata: null, charging: null,
source: 'plugin:trail-finder', pluginId: 'trail-finder', rating: 4.5,
details: [{ label: 'Parking', value: '40 spaces' }], icon: 'Signpost', color: '#2f855a',
}
const reply = { pois: [row], source: 'plugin:trail-finder', truncated: false, clamped: true }
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(isEffectivelyOffline).mockReturnValue(false)
vi.mocked(pluginPoisApi.search).mockResolvedValue(reply)
})
describe('pluginPoiRepo.search', () => {
it('FE-REPO-PLUGINPOI-001: asks the api for the one plugin and category, with the signal', async () => {
const signal = new AbortController().signal
const found = await pluginPoiRepo.search('trail-finder', 'trailheads', BBOX, 'de', signal)
expect(pluginPoisApi.search).toHaveBeenCalledWith('trail-finder', 'trailheads', BBOX, 'de', signal)
expect(found).toEqual(reply)
})
it('FE-REPO-PLUGINPOI-002: offline it refuses without making the request', async () => {
vi.mocked(isEffectivelyOffline).mockReturnValue(true)
await expect(pluginPoiRepo.search('trail-finder', 'trailheads', BBOX, 'en', new AbortController().signal))
.rejects.toThrow('requires a connection')
expect(pluginPoisApi.search).not.toHaveBeenCalled()
})
it('FE-REPO-PLUGINPOI-003: an answer off the contract is rejected, not drawn', async () => {
// A colour that is not #rrggbb would end up inside marker markup.
vi.mocked(pluginPoisApi.search).mockResolvedValue({ ...reply, pois: [{ ...row, color: 'red;background:url(x)' }] })
await expect(pluginPoiRepo.search('trail-finder', 'trailheads', BBOX, 'en', new AbortController().signal)).rejects.toThrow()
vi.mocked(pluginPoisApi.search).mockResolvedValue({ ...reply, pois: [{ ...row, icon: 'Skull' }] } as never)
await expect(pluginPoiRepo.search('trail-finder', 'trailheads', BBOX, 'en', new AbortController().signal)).rejects.toThrow()
})
it('FE-REPO-PLUGINPOI-004: an api failure reaches the caller unchanged', async () => {
const failure = Object.assign(new Error('Request failed with status code 404'), { response: { status: 404 } })
vi.mocked(pluginPoisApi.search).mockRejectedValue(failure)
await expect(pluginPoiRepo.search('trail-finder', 'trailheads', BBOX, 'en', new AbortController().signal)).rejects.toBe(failure)
})
it('FE-REPO-PLUGINPOI-005: hands back the rows as the map draws them, contact fields kept or null', async () => {
const contact = { ...row, osm_id: 'plugin:trail-finder:th-2', address: null, website: 'https://example.test/th-2', phone: '+43 1 234' }
vi.mocked(pluginPoisApi.search).mockResolvedValue({ ...reply, pois: [row, contact] })
const { pois } = await pluginPoiRepo.search('trail-finder', 'trailheads', BBOX, 'en', new AbortController().signal)
expect(pois.map(p => [p.address, p.website, p.phone])).toEqual([
['Almweg 1', null, null],
[null, 'https://example.test/th-2', '+43 1 234'],
])
expect(pois[1]).toMatchObject({ details: row.details, icon: 'Signpost', color: '#2f855a', pluginId: 'trail-finder' })
})
})