From 8b9de0496043894cd04ea4b9e2fa73434d4bfe43 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:13:35 +0100 Subject: [PATCH] fix(wanderer): offer the connection and run the watch where a trip switched Tours on With Tours off for the instance, a trip that switched it on opened the wanderer import, but Settings hid the connection and the watch cron never ran. The addon feed now tells each user whether wanderer applies to them, and the watch looks for every user who has Tours on somewhere; the others only take their turn. --- server/src/nest/addons/addons.controller.ts | 6 ++++-- server/src/nest/addons/addons.service.ts | 8 ++++++-- server/src/nest/tours/wanderer-watch.service.ts | 13 ++++++++++++- .../tests/unit/nest/addons.controller.test.ts | 5 +++-- .../tests/unit/nest/trip-addons.service.test.ts | 12 ++++++++++++ .../unit/nest/wanderer-watch.service.test.ts | 17 ++++++++++++++++- 6 files changed, 53 insertions(+), 8 deletions(-) diff --git a/server/src/nest/addons/addons.controller.ts b/server/src/nest/addons/addons.controller.ts index 4c6e3a245..982f0031b 100644 --- a/server/src/nest/addons/addons.controller.ts +++ b/server/src/nest/addons/addons.controller.ts @@ -1,3 +1,5 @@ +import type { User } from '../../types'; +import { CurrentUser } from '../auth-core/current-user.decorator'; import { JwtAuthGuard } from '../auth-core/jwt-auth.guard'; import { AddonsService } from './addons.service'; import { Controller, Get, UseGuards } from '@nestjs/common'; @@ -16,7 +18,7 @@ export class AddonsController { constructor(private readonly addons: AddonsService) {} @Get() - async list() { - return this.addons.list(); + async list(@CurrentUser() user: User) { + return this.addons.list(user.id); } } diff --git a/server/src/nest/addons/addons.service.ts b/server/src/nest/addons/addons.service.ts index ed3f15eff..822cea5eb 100644 --- a/server/src/nest/addons/addons.service.ts +++ b/server/src/nest/addons/addons.service.ts @@ -258,7 +258,8 @@ export class AddonsService { return { features: after, changed }; } - async list() { + /** `userId` decides the wanderer flag for that user: allowed, and Tours on for them somewhere. */ + async list(userId?: number) { const addonRows = await this.addons.listEnabled(); // Photo providers surface only inside journeys, so with the journey addon // off they are unavailable no matter what their own rows say. Deriving that @@ -280,7 +281,10 @@ export class AddonsService { return { collabFeatures: await this.getCollabFeatures(), bagTracking: (await this.getBagTracking()).enabled, - toursWanderer: await this.isWandererAvailable(), + toursWanderer: + userId === undefined + ? await this.isWandererAvailable() + : (await this.isWandererAllowed()) && (await this.isEnabledForUserAnywhere(userId, ADDON_IDS.TOURS)), toursHikingMap: (await this.isAddonEnabled(ADDON_IDS.TOURS)) && (await this.getToursHikingMap()).enabled, toursCycleMap: (await this.isAddonEnabled(ADDON_IDS.TOURS)) && (await this.getToursCycleMap()).enabled, // The same three options without the instance Tours switch: a trip may switch Tours on diff --git a/server/src/nest/tours/wanderer-watch.service.ts b/server/src/nest/tours/wanderer-watch.service.ts index 67a776456..7c493497e 100644 --- a/server/src/nest/tours/wanderer-watch.service.ts +++ b/server/src/nest/tours/wanderer-watch.service.ts @@ -1,3 +1,4 @@ +import { ADDON_IDS } from '../../addons'; import { TourSourceHints } from '../../db/entities/TourSourceHints.entity'; import { TOUR_SOURCE_HINT_CHANGES } from '../../db/json-columns'; import type { @@ -92,7 +93,7 @@ export class WandererWatchService { * one user that fails keeps no other from theirs. */ async run(now: Date = new Date()): Promise { - if (this.running || !(await this.addons.isWandererAvailable())) return; + if (this.running || !(await this.addons.isWandererAllowed())) return; this.running = true; try { const userIds = await this.wanderer.watchableUserIds(dbNow(now), WATCH_CONNECTIONS_PER_RUN); @@ -113,6 +114,16 @@ export class WandererWatchService { * after each failure in a row; the hints already there stay as they are. */ private async watchUser(userId: number, now: Date): Promise { + // Tours off for the instance and in every trip of the user: the turn passes without a look. + if (!(await this.addons.isEnabledForUserAnywhere(userId, ADDON_IDS.TOURS))) { + const state = await this.wanderer.watchState(userId); + await this.wanderer.noteWatchRun(userId, { + lastRunAt: dbNow(now), + failures: state.failures, + pausedUntil: state.pausedUntil, + }); + return; + } const instance = await this.wanderer.instanceOf(userId); const checkedBefore = dbNow(new Date(now.getTime() - WATCH_RECHECK_MS)); const due = await this.hints.listDue(userId, instance, checkedBefore, WATCH_SOURCES_PER_RUN); diff --git a/server/tests/unit/nest/addons.controller.test.ts b/server/tests/unit/nest/addons.controller.test.ts index add770e87..da885ddd4 100644 --- a/server/tests/unit/nest/addons.controller.test.ts +++ b/server/tests/unit/nest/addons.controller.test.ts @@ -1,5 +1,6 @@ import { AddonsController } from '../../../src/nest/addons/addons.controller'; import type { AddonsService } from '../../../src/nest/addons/addons.service'; +import type { User } from '../../../src/types'; import { describe, it, expect, vi } from 'vitest'; @@ -20,8 +21,8 @@ describe('AddonsController (parity with the legacy GET /api/addons route)', () = const list = vi.fn().mockReturnValue(feed); const svc = makeService({ list } as Partial); - expect(await new AddonsController(svc).list()).toBe(feed); + expect(await new AddonsController(svc).list({ id: 4 } as User)).toBe(feed); expect(list).toHaveBeenCalledTimes(1); - expect(list).toHaveBeenCalledWith(); + expect(list).toHaveBeenCalledWith(4); }); }); diff --git a/server/tests/unit/nest/trip-addons.service.test.ts b/server/tests/unit/nest/trip-addons.service.test.ts index 7346880fa..9951831e8 100644 --- a/server/tests/unit/nest/trip-addons.service.test.ts +++ b/server/tests/unit/nest/trip-addons.service.test.ts @@ -145,6 +145,18 @@ describe('TripAddonsService: routes without a trip', () => { expect(await addons.isEnabledAnywhere('roadtrip')).toBe(true); }); + it('TRIPADDON-SVC-016: the feed offers wanderer to a user once a trip of theirs switched Tours on', async () => { + const { owner, trip } = await ownedTrip(); + const { user: stranger } = await makeUser(orm()); + await setAddonEnabled(orm(), 'tours', false); + await addons.updateToursWanderer(true); + expect((await addons.list(owner.id)).toursWanderer).toBe(false); + await svc.setTripOverride(trip.id, owner.id, 'tours', true); + expect((await addons.list(owner.id)).toursWanderer).toBe(true); + expect((await addons.list(stranger.id)).toursWanderer).toBe(false); + expect((await addons.list()).toursWanderer).toBe(false); + }); + it('TRIPADDON-SVC-013: tripsWithAddon keeps the trips the addon applies to, in their order, each as isEnabledForTrip says', async () => { const { owner, trip: off } = await ownedTrip(); const on = await makeTrip(orm(), owner.id); diff --git a/server/tests/unit/nest/wanderer-watch.service.test.ts b/server/tests/unit/nest/wanderer-watch.service.test.ts index a97276ba9..008dd0e01 100644 --- a/server/tests/unit/nest/wanderer-watch.service.test.ts +++ b/server/tests/unit/nest/wanderer-watch.service.test.ts @@ -18,7 +18,7 @@ import { WandererClient, WandererError } from '../../../src/nest/wanderer/wander import { createSnapshotTestDb } from '../../helpers/db-mock'; import { makePlace } from '../../helpers/factories/places'; import { countRows, deleteRows, findRow, insertRow, updateRows } from '../../helpers/factories/rows'; -import { setAddonEnabled, setAppSetting } from '../../helpers/factories/settings'; +import { setAddonEnabled, setAppSetting, setTripAddon } from '../../helpers/factories/settings'; import { makeTour, makeTourSource } from '../../helpers/factories/tours'; import { makeTrip } from '../../helpers/factories/trips'; import { makeUser } from '../../helpers/factories/users'; @@ -129,6 +129,21 @@ describe('WandererWatchService.run', () => { expect(await connection(user.id)).toMatchObject({ watch_last_run_at: null }); }); + it('WWATCH-014: with Tours off for the instance, a user whose trip switched it on is watched, the others only take their turn', async () => { + await setAddonEnabled(orm(), 'tours', false); + const opened = await fixture(); + const closed = await fixture(); + await setTripAddon(orm(), opened.trip.id, 'tours', true); + await watch.run(NOW); + expect(client.trail).toHaveBeenCalledTimes(1); + expect(await hintRow(opened.placeId)).toMatchObject({ checked_at: '2026-10-20 10:00:00' }); + expect(await hintRow(closed.placeId)).toBeNull(); + expect(await connection(closed.user.id)).toMatchObject({ + watch_last_run_at: '2026-10-20 10:00:00', + watch_failures: 0, + }); + }); + it('WWATCH-002: a record as imported writes a quiet look, tells nobody and never fetches a GPX', async () => { const { user, placeId } = await fixture(); const before = await countRows(orm(), Places);