mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
2e1dca9fe9
Settings, Integrations, wanderer gains an optional background check of the trails you imported, off until you switch it on. While on, TREK reads the short record of each such trail at most once a day (never its GPX file) and leaves a hint when the trail may have moved on: "may have changed" when only wanderer's change time moved, "changed" when its name or description proves it, and "not checkable" when the trail is gone, no longer yours or carries no change time. Nothing is ever said to be unchanged. A new hint arrives as a notification that opens the tour (a new ?place= link in the planner); the tour's provenance shows it with the source check right beside it, and the card lists all open hints in a dialog, each leading to its tour. The check reads the source in full as before and spends the hint; taking a change in stays the confirmed reimport. No tour is ever changed or deleted. The check only asks through your own connection, only about trails your account imported into trips you can still open, and stops when you switch it off, remove the connection, wanderer refuses the token (until a test passes or a new token is saved) or the admin switches wanderer off; hints and snapshots stay. A run asks at most 25 connections for at most 10 records each, one after another, and an instance that fails (an outage, a rate limit) is left alone for an hour, doubling up to two days. Server: migration 046120 adds the switch and the run bookkeeping to wanderer_connections and the tour_source_hints table (one row per snapshot, for its importer, gone with it). WandererWatchService and an hourly wanderer-watch job on the CronRegistrar live in the tours domain and read through WandererService; GET and PUT /api/integrations/wanderer/watch and GET /api/trips/:tripId/tours/wanderer/sources/:placeId/hint (place_edit, like the check) answer with @trek/shared contracts. A new user notification event, wanderer_source_hint, can be muted per channel. Saving a new token now clears what the last check said about the old one. There is no MCP tool, as for the rest of the personal wanderer connection. All 27 locales carry the strings, and the admin wiki page says what the check does.
96 lines
4.1 KiB
TypeScript
96 lines
4.1 KiB
TypeScript
import { http, HttpResponse } from 'msw';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { server } from '../../tests/helpers/msw/server';
|
|
import { offlineDb } from '../db/offlineDb';
|
|
import { isEffectivelyOffline } from '../sync/networkMode';
|
|
import { wandererErrorCode, WandererOfflineError, wandererRepo } from './wandererRepo';
|
|
|
|
vi.mock('../sync/networkMode', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('../sync/networkMode')>()),
|
|
isEffectivelyOffline: vi.fn(() => false),
|
|
}));
|
|
|
|
const tour = {
|
|
place_id: 70,
|
|
name: 'Ridge',
|
|
tour_type: 'hike',
|
|
distance: 5,
|
|
elevation_gain: 300,
|
|
elevation_loss: 300,
|
|
duration: null,
|
|
difficulty: 'easy',
|
|
wanderer_ref: 'https://w.test#abc',
|
|
match_confidence: 1,
|
|
max_hiking_difficulty: 1,
|
|
planned: false,
|
|
caution: false,
|
|
has_waypoints: false,
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
vi.mocked(isEffectivelyOffline).mockReturnValue(false);
|
|
await offlineDb.tours.clear();
|
|
});
|
|
|
|
describe('wandererRepo', () => {
|
|
it('RS-04: searches and previews through the server, with the trail id kept safe in the path', async () => {
|
|
const seen: string[] = [];
|
|
server.use(
|
|
http.post('/api/trips/3/tours/wanderer/search', async ({ request }) => {
|
|
seen.push(JSON.stringify(await request.json()));
|
|
return HttpResponse.json({ instance: 'https://w.test', trails: [], total: 0, pageSize: 20 });
|
|
}),
|
|
http.get('/api/trips/3/tours/wanderer/trails/:id', ({ params }) => {
|
|
seen.push(String(params.id));
|
|
return HttpResponse.json({ ok: true });
|
|
})
|
|
);
|
|
await expect(wandererRepo.search(3, { q: 'ridge', page: 1 })).resolves.toMatchObject({ total: 0 });
|
|
await wandererRepo.preview(3, 'abc');
|
|
expect(seen).toEqual(['{"q":"ridge","page":1}', 'abc']);
|
|
});
|
|
|
|
it('RS-05: an import is cached like every other tour', async () => {
|
|
server.use(http.post('/api/trips/3/tours/wanderer/import', () => HttpResponse.json({ tour })));
|
|
await wandererRepo.importTrail(3, { trailId: 'abc', contentHash: '0123456789abcdef' });
|
|
expect(await offlineDb.tours.get(70)).toMatchObject({ place_id: 70, trip_id: 3 });
|
|
});
|
|
|
|
it('RS-04: is online only and says so instead of sending', async () => {
|
|
vi.mocked(isEffectivelyOffline).mockReturnValue(true);
|
|
await expect(wandererRepo.search(3, { q: '', page: 1 })).rejects.toBeInstanceOf(WandererOfflineError);
|
|
await expect(wandererRepo.preview(3, 'abc')).rejects.toBeInstanceOf(WandererOfflineError);
|
|
await expect(
|
|
wandererRepo.importTrail(3, { trailId: 'abc', contentHash: '0123456789abcdef' })
|
|
).rejects.toBeInstanceOf(WandererOfflineError);
|
|
});
|
|
|
|
it('WAN-02: reads and switches the watch, and a snapshot hint only while online', async () => {
|
|
const watch = { enabled: true, connected: true, lastRunAt: null, pausedUntil: null, blocked: false, hints: [] };
|
|
const sent: unknown[] = [];
|
|
const hint = { state: 'possible_change', changes: [], reason: null, externalUpdated: null, checkedAt: 'x' };
|
|
server.use(
|
|
http.get('/api/integrations/wanderer/watch', () => HttpResponse.json(watch)),
|
|
http.put('/api/integrations/wanderer/watch', async ({ request }) => {
|
|
sent.push(await request.json());
|
|
return HttpResponse.json(watch);
|
|
}),
|
|
http.get('/api/trips/3/tours/wanderer/sources/70/hint', () => HttpResponse.json({ hint }))
|
|
);
|
|
await expect(wandererRepo.getWatch()).resolves.toEqual(watch);
|
|
await wandererRepo.setWatch(false);
|
|
expect(sent).toEqual([{ enabled: false }]);
|
|
await expect(wandererRepo.sourceHint(3, 70)).resolves.toEqual(hint);
|
|
|
|
vi.mocked(isEffectivelyOffline).mockReturnValue(true);
|
|
await expect(wandererRepo.sourceHint(3, 70)).resolves.toBeNull();
|
|
});
|
|
|
|
it('RS-03: reads the reason of a failed call from the server, the offline case or nothing', () => {
|
|
expect(wandererErrorCode(new WandererOfflineError())).toBe('offline');
|
|
expect(wandererErrorCode({ response: { data: { code: 'not_own_trail' } } })).toBe('not_own_trail');
|
|
expect(wandererErrorCode({ response: { data: {} } })).toBe('unknown');
|
|
expect(wandererErrorCode(new Error('boom'))).toBe('unknown');
|
|
});
|
|
});
|