mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
fix(tours): restoring a recorded track and saving a copy wait for the tour's queued edits
Both go straight to the server. While an edit of the same tour still waited in the queue, the restore went out first and its answer replaced the cached edit, and the edit replayed afterwards planned the track over again. A copy copied the server's older line instead of the one on screen. Both now wait the way the actual track writes do: offline, behind a queued write of the tour, or for a tour the server has not got yet they refuse with OnlineOnlyError, and the planner says a connection is needed and the waiting changes go first, in all 27 languages.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import type { TourListItem } from '@trek/shared';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { OnlineOnlyError } from '../../repo/onlineOnly';
|
||||
import type { TourPlannerController } from './planner/useTourPlanner';
|
||||
import { useRecordedTrack } from './useRecordedTrack';
|
||||
|
||||
@@ -87,4 +88,21 @@ describe('useRecordedTrack', () => {
|
||||
expect(controller.returnToNeutral).not.toHaveBeenCalled();
|
||||
expect(onRestored).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('says it needs a connection when it is offline or behind an edit of the tour still waiting', async () => {
|
||||
repo.restoreRecorded.mockRejectedValueOnce(new OnlineOnlyError());
|
||||
const controller = planner(7);
|
||||
const onRestored = vi.fn();
|
||||
const { result } = renderHook(() =>
|
||||
useRecordedTrack({ tripId: 1, planner: controller, tours: [replaced], canEdit: true, onRestored })
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.restore();
|
||||
});
|
||||
|
||||
expect(toast.error).toHaveBeenCalledWith('tours.recorded.offline');
|
||||
expect(controller.returnToNeutral).not.toHaveBeenCalled();
|
||||
expect(onRestored).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { TourListItem } from '@trek/shared';
|
||||
import { useCallback, useState } from 'react';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { OnlineOnlyError } from '../../repo/onlineOnly';
|
||||
import { tourRepo } from '../../repo/tourRepo';
|
||||
import { useToast } from '../shared/Toast';
|
||||
import type { TourPlannerController } from './planner/useTourPlanner';
|
||||
@@ -36,8 +37,9 @@ export function useRecordedTrack({ tripId, planner, tours, canEdit, onRestored }
|
||||
returnToNeutral();
|
||||
await onRestored(result.tour);
|
||||
toast.success(t('tours.recorded.restored'));
|
||||
} catch {
|
||||
toast.error(t('tours.recorded.failed'));
|
||||
} catch (err: unknown) {
|
||||
// Offline, or behind an edit of the tour still waiting to be sent: said as such, not as a failure.
|
||||
toast.error(t(err instanceof OnlineOnlyError ? 'tours.recorded.offline' : 'tours.recorded.failed'));
|
||||
} finally {
|
||||
setRestoring(false);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
// P2D-XD-01: from a tour's packing link to the trip's Lists tab, the tour's detail closed on the way.
|
||||
// P2C-07: a tour saved as a new one, and why not when it cannot be.
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import type { TourListItem } from '@trek/shared';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { useListsSubTab } from '../../hooks/useListsSubTab';
|
||||
import { OnlineOnlyError } from '../../repo/onlineOnly';
|
||||
import { useTourDetailActions } from './useTourDetailActions';
|
||||
import type { useTripPlanner } from './useTripPlanner';
|
||||
|
||||
const repo = vi.hoisted(() => ({ copy: vi.fn() }));
|
||||
vi.mock('../../repo/tourRepo', () => ({ tourRepo: repo }));
|
||||
|
||||
describe('useTourDetailActions', () => {
|
||||
const planner = () => ({ tripId: 5, setSelectedPlaceId: vi.fn(), handleTabChange: vi.fn() });
|
||||
const actions = (p: ReturnType<typeof planner>) =>
|
||||
@@ -31,4 +37,19 @@ describe('useTourDetailActions', () => {
|
||||
act(() => a.onOpenPacking());
|
||||
expect(lists.result.current[0]).toBe('packing');
|
||||
});
|
||||
|
||||
it('FE-TOUR-DETAIL-ACTIONS-004: a copy that cannot go out says it needs a connection, any other failure that it failed', async () => {
|
||||
const toast = { success: vi.fn(), error: vi.fn() };
|
||||
const p = { ...planner(), t: (key: string) => key, toast };
|
||||
const ridge = { place_id: 40, name: 'Ridge walk' } as TourListItem;
|
||||
const a = actions(p);
|
||||
repo.copy.mockRejectedValueOnce(new OnlineOnlyError());
|
||||
await act(() => a.onSaveAsNew(ridge));
|
||||
expect(repo.copy).toHaveBeenCalledWith(5, 40, 'tours.copy.name');
|
||||
expect(toast.error).toHaveBeenLastCalledWith('tours.copy.offline');
|
||||
repo.copy.mockRejectedValueOnce(new Error('Server error'));
|
||||
await act(() => a.onSaveAsNew(ridge));
|
||||
expect(toast.error).toHaveBeenLastCalledWith('tours.copy.failed');
|
||||
expect(toast.success).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { TourListItem } from '@trek/shared';
|
||||
import { rememberListsSubTab } from '../../hooks/useListsSubTab';
|
||||
import { OnlineOnlyError } from '../../repo/onlineOnly';
|
||||
import { tourRepo } from '../../repo/tourRepo';
|
||||
import { useTripStore } from '../../store/tripStore';
|
||||
import type { Place } from '../../types';
|
||||
@@ -49,8 +50,9 @@ export function useTourDetailActions(planner: ReturnType<typeof useTripPlanner>)
|
||||
const { tour: copy } = await tourRepo.copy(tripId, tour.place_id, name);
|
||||
await takeIn(copy);
|
||||
toast.success(t('tours.copy.saved'));
|
||||
} catch {
|
||||
toast.error(t('tours.copy.failed'));
|
||||
} catch (err: unknown) {
|
||||
// Offline, or behind an edit of the tour still waiting to be sent, which the copy would leave out.
|
||||
toast.error(t(err instanceof OnlineOnlyError ? 'tours.copy.offline' : 'tours.copy.failed'));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-REPO-TOUR-001 to FE-REPO-TOUR-014, FE-REPO-TOUR-042 to FE-REPO-TOUR-047
|
||||
// FE-REPO-TOUR-001 to FE-REPO-TOUR-014, FE-REPO-TOUR-042 to FE-REPO-TOUR-048
|
||||
// Tours through the offline core: read-through to Dexie, offline create and
|
||||
// edit written there and queued, and the online paths keeping the copy fresh.
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
@@ -502,6 +502,43 @@ describe('tourRepo provenance and copies (P2C-02, P2C-07)', () => {
|
||||
expect(cached?.activity_tracks).toEqual([{ id: 5, track_place_id: 41, assignment_id: null }])
|
||||
})
|
||||
|
||||
it('FE-REPO-TOUR-048: the restore of a recorded track and a copy wait for a queued edit of the tour, and go out neither offline nor for a tour the server has not got', async () => {
|
||||
let sent = 0
|
||||
server.use(
|
||||
http.post('/api/trips/3/tours/40/restore-recorded', () => {
|
||||
sent++
|
||||
return HttpResponse.json({ tour: tour({ name: 'Recorded', has_waypoints: false }), waypoints: [] })
|
||||
}),
|
||||
http.post('/api/trips/3/tours/40/copy', () => {
|
||||
sent++
|
||||
return HttpResponse.json({ tour: tour({ place_id: 41, name: 'Ridge walk (copy)' }), waypoints: [] }, { status: 201 })
|
||||
}),
|
||||
)
|
||||
// An edit made here offline whose replay failed and waits: it would plan the restored track over again.
|
||||
await offlineDb.tours.put({ ...tour({ name: 'Edited here' }), trip_id: 3 })
|
||||
await offlineDb.mutationQueue.put({
|
||||
id: 'queued-tour', tripId: 3, method: 'PUT', url: '/trips/3/tours/40', body, createdAt: 1,
|
||||
status: 'failed', attempts: 3, lastError: 'Server error', resource: 'tours', entityId: 40,
|
||||
})
|
||||
await expect(tourRepo.restoreRecorded(3, 40)).rejects.toBeInstanceOf(OnlineOnlyError)
|
||||
await expect(tourRepo.copy(3, 40, 'Ridge walk (copy)')).rejects.toBeInstanceOf(OnlineOnlyError)
|
||||
expect(sent).toBe(0)
|
||||
expect((await offlineDb.tours.get(40))?.name).toBe('Edited here')
|
||||
await offlineDb.mutationQueue.clear()
|
||||
|
||||
setOnline(false)
|
||||
await expect(tourRepo.restoreRecorded(3, 40)).rejects.toBeInstanceOf(OnlineOnlyError)
|
||||
await expect(tourRepo.copy(3, 40, 'Ridge walk (copy)')).rejects.toBeInstanceOf(OnlineOnlyError)
|
||||
setOnline(true)
|
||||
await expect(tourRepo.copy(3, -7, 'Draft (copy)')).rejects.toBeInstanceOf(OnlineOnlyError)
|
||||
expect(sent).toBe(0)
|
||||
|
||||
// Once nothing waits, the restore goes out and the cache takes the restored tour in.
|
||||
await tourRepo.restoreRecorded(3, 40)
|
||||
expect(sent).toBe(1)
|
||||
expect(await offlineDb.tours.get(40)).toMatchObject({ name: 'Recorded', has_waypoints: false })
|
||||
})
|
||||
|
||||
it('FE-REPO-TOUR-021: a copy is asked of the server under its name and cached as a tour of the trip', async () => {
|
||||
let sent: unknown = null
|
||||
server.use(http.post('/api/trips/3/tours/40/copy', async ({ request }) => {
|
||||
|
||||
@@ -18,10 +18,13 @@ import { OnlineOnlyError } from './onlineOnly'
|
||||
import type { Place } from '../types'
|
||||
|
||||
/**
|
||||
* ACT-01: an assignment change goes out only for a tour the server has, while
|
||||
* no older write of it waits in the queue; `mustQueue` is true offline too.
|
||||
* A write only the server can make (ACT-01's assignments, the restore of a
|
||||
* recorded track, a copy) goes out only for a tour the server has, while no
|
||||
* older write of it waits in the queue: its answer is the server's tour from
|
||||
* before that write, and the write replayed after it would undo this one.
|
||||
* `mustQueue` is true offline too.
|
||||
*/
|
||||
async function requireActivityWrite(placeId: number): Promise<void> {
|
||||
async function requireServerWrite(placeId: number): Promise<void> {
|
||||
if (placeId < 0 || (await mutationQueue.mustQueue('tours', placeId))) throw new OnlineOnlyError()
|
||||
}
|
||||
|
||||
@@ -350,8 +353,13 @@ export const tourRepo = {
|
||||
return result
|
||||
},
|
||||
|
||||
/** Online only, like the import: the recorded track is kept on the server, not in the cache. */
|
||||
/**
|
||||
* Online only, like the import: the recorded track is kept on the server,
|
||||
* not in the cache. Held back behind a queued edit of the tour, which would
|
||||
* plan it over again once replayed.
|
||||
*/
|
||||
async restoreRecorded(tripId: number | string, placeId: number): Promise<TourDetailResponse> {
|
||||
await requireServerWrite(placeId)
|
||||
const result = await recordedTrackApi.restore(tripId, placeId)
|
||||
await cacheTourAnswer(Number(tripId), result)
|
||||
return result
|
||||
@@ -367,7 +375,7 @@ export const tourRepo = {
|
||||
* changes, so only the recordings of the cached tour take the answer in.
|
||||
*/
|
||||
async assignActivity(tripId: number | string, placeId: number, body: TourActivityAssignRequest): Promise<TourDetailResponse> {
|
||||
await requireActivityWrite(placeId)
|
||||
await requireServerWrite(placeId)
|
||||
const result = await tourActivitiesApi.assign(tripId, placeId, body)
|
||||
await cacheActivities(tripId, placeId, result)
|
||||
return result
|
||||
@@ -375,7 +383,7 @@ export const tourRepo = {
|
||||
|
||||
/** ACT-01: a recorded track taken off the tour again; online only and held back like the assignment. */
|
||||
async unassignActivity(tripId: number | string, placeId: number, activityId: number): Promise<TourDetailResponse> {
|
||||
await requireActivityWrite(placeId)
|
||||
await requireServerWrite(placeId)
|
||||
const result = await tourActivitiesApi.unassign(tripId, placeId, activityId)
|
||||
await cacheActivities(tripId, placeId, result)
|
||||
return result
|
||||
@@ -397,9 +405,11 @@ export const tourRepo = {
|
||||
|
||||
/**
|
||||
* P2C-07, online only like the restore: the copy is made from the server's
|
||||
* stored line and control points, so it needs the server to answer.
|
||||
* stored line and control points, so it needs the server to answer, and
|
||||
* waits for a queued edit of the tour so that it copies what the user sees.
|
||||
*/
|
||||
async copy(tripId: number | string, placeId: number, name: string): Promise<TourCreateResponse> {
|
||||
await requireServerWrite(placeId)
|
||||
const result = await tourCopiesApi.copy(tripId, placeId, { name })
|
||||
await cacheTourAnswer(Number(tripId), result)
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user