mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
feat(tours): link the track a tour was actually done with, and show its own values (ACT-01)
A planned tour can now be given the track it was actually done with. In the tour's detail, "Add actual track" opens a dialog that asks for the day the tour was done on (or none) and a recording of the trip: an import nobody planned over, or a new one through the existing GPX import, its preview preset to the tour's type. What the choice measures is shown before anything is written, and only "Link" writes. The linked track keeps its own name and source; the detail shows its distance, heights, profile and, only where the file carried time stamps, the time it took, marked as actual values next to the untouched plan. A track can be changed or unlinked again. Server: a tour_activities table points a tour's execution (a day assignment, or none) at a recording of the same trip, and the tours GPX import keeps the first and last time of a track whose every point carries one. The values are read from the recording's own line on every read, so a changed recording updates them while the plan's line, measures and revision stay as they were. Every check runs inside the write: the recording must be an import of this trip not planned over, documenting no other tour; the day must be one of this tour; a refusal names its reason and changes nothing. PUT and DELETE /api/trips/:tripId/tours/:placeId/activities answer the detail, which now carries the actual tracks; the list names them per tour and per recording. MCP: assign_tour_track and unassign_tour_track over the same service and checks, get_tour_plan reads the values without the line. A recording linked as a tour's actual track is no longer counted as a tour of its own in the trip's tour summary, and the GPX preview imports a city walk as one instead of as a hike.
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
// FE-ACT-ASSIGN-001 to FE-ACT-ASSIGN-004
|
||||
// ACT-01, the link dialog's state: the day it starts on, the recordings of the
|
||||
// trip to choose from, what a choice replaces, and a recording imported here
|
||||
// chosen for the user but never linked by itself.
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import type { TourActivity, TourListItem } from '@trek/shared';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { TourExecution } from './tourActivityModel';
|
||||
import { useTrackAssignment } from './useTrackAssignment';
|
||||
|
||||
const repo = vi.hoisted(() => ({ list: vi.fn() }));
|
||||
interface ImportOptions {
|
||||
onImported?: (tours: TourListItem[]) => void;
|
||||
onToursChanged?: (ids?: number[]) => unknown;
|
||||
}
|
||||
const gpx = vi.hoisted(() => ({ options: null as null | ImportOptions }));
|
||||
vi.mock('../../../repo/tourRepo', () => ({ tourRepo: repo }));
|
||||
vi.mock('../useTourGpxImport', () => ({
|
||||
useTourGpxImport: (options: ImportOptions) => {
|
||||
gpx.options = options;
|
||||
return { importing: false, preview: null, pick: vi.fn(), confirm: vi.fn(), cancel: vi.fn(), importFiles: vi.fn() };
|
||||
},
|
||||
}));
|
||||
|
||||
const tour = { place_id: 7, name: 'Ridge', tour_type: 'hike', origin: 'trek', has_waypoints: true } as TourListItem;
|
||||
const recording = (placeId: number, name: string, extra: Partial<TourListItem> = {}) =>
|
||||
({ place_id: placeId, name, tour_type: 'hike', origin: 'gpx', has_waypoints: false, ...extra }) as TourListItem;
|
||||
const executions: TourExecution[] = [
|
||||
{ assignmentId: 11, day: null, index: 0 },
|
||||
{ assignmentId: 31, day: null, index: 2 },
|
||||
];
|
||||
const linked = (assignmentId: number | null, trackPlaceId: number, name: string) =>
|
||||
({
|
||||
id: trackPlaceId,
|
||||
assignment_id: assignmentId,
|
||||
track: { place_id: trackPlaceId, name },
|
||||
}) as unknown as TourActivity;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
repo.list.mockResolvedValue({ tours: [tour, recording(20, 'Watch'), recording(21, 'Phone', { activity_of: 99 })] });
|
||||
});
|
||||
|
||||
function render(activities: TourActivity[], target: { assignmentId?: number | null } = {}, onToursChanged = vi.fn()) {
|
||||
return renderHook(() =>
|
||||
useTrackAssignment({ tripId: 1, tour, executions, activities, target, canEdit: true, onToursChanged })
|
||||
);
|
||||
}
|
||||
|
||||
describe('useTrackAssignment', () => {
|
||||
it('FE-ACT-ASSIGN-001: starts on the first day without an actual track, or on the day asked for', async () => {
|
||||
expect(render([]).result.current.assignmentId).toBe(11);
|
||||
expect(render([linked(11, 20, 'Watch')]).result.current.assignmentId).toBe(31);
|
||||
expect(render([linked(11, 20, 'Watch'), linked(31, 21, 'Phone')]).result.current.assignmentId).toBe(11);
|
||||
const asked = render([linked(31, 21, 'Phone')], { assignmentId: 31 });
|
||||
expect(asked.result.current.assignmentId).toBe(31);
|
||||
// A change starts on the track the day has, which therefore replaces nothing.
|
||||
expect(asked.result.current.selected).toBe(21);
|
||||
expect(asked.result.current.replaces).toBeNull();
|
||||
await waitFor(() => expect(asked.result.current.candidates).not.toBeNull());
|
||||
});
|
||||
|
||||
it('FE-ACT-ASSIGN-002: lists the recordings of the trip, and only a free one can be linked', async () => {
|
||||
const { result } = render([]);
|
||||
expect(result.current.candidates).toBeNull();
|
||||
expect(result.current.ready).toBe(false);
|
||||
await waitFor(() => expect(result.current.candidates?.map((c) => c.tour.place_id)).toEqual([20, 21]));
|
||||
expect(repo.list).toHaveBeenCalledWith(1);
|
||||
|
||||
act(() => result.current.choose(21));
|
||||
expect(result.current.choice?.takenBy).toBe('');
|
||||
expect(result.current.ready).toBe(false);
|
||||
act(() => result.current.choose(20));
|
||||
expect(result.current.ready).toBe(true);
|
||||
expect(result.current.importFields).toEqual({ tour_type: 'hike' });
|
||||
});
|
||||
|
||||
it('FE-ACT-ASSIGN-003: names the track a choice replaces on its day, and none on a day without one', async () => {
|
||||
const { result } = render([linked(11, 21, 'Phone')], { assignmentId: 11 });
|
||||
await waitFor(() => expect(result.current.candidates).not.toBeNull());
|
||||
act(() => result.current.choose(20));
|
||||
expect(result.current.replaces).toBe('Phone');
|
||||
act(() => result.current.chooseExecution(null));
|
||||
expect(result.current.execution.day).toBeNull();
|
||||
expect(result.current.replaces).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-ACT-ASSIGN-004: a recording imported here is chosen and the list read again; the lists are told', async () => {
|
||||
const onToursChanged = vi.fn();
|
||||
const { result } = render([], {}, onToursChanged);
|
||||
await waitFor(() => expect(result.current.candidates).not.toBeNull());
|
||||
repo.list.mockResolvedValue({ tours: [tour, recording(20, 'Watch'), recording(40, 'Imported')] });
|
||||
|
||||
await act(async () => gpx.options?.onToursChanged?.([40]));
|
||||
expect(onToursChanged).toHaveBeenCalledWith([40]);
|
||||
await act(async () => gpx.options?.onToursChanged?.());
|
||||
expect(onToursChanged).toHaveBeenLastCalledWith([]);
|
||||
act(() => gpx.options?.onImported?.([recording(40, 'Imported')]));
|
||||
expect(result.current.selected).toBe(40);
|
||||
await waitFor(() => expect(result.current.choice?.tour.name).toBe('Imported'));
|
||||
expect(repo.list).toHaveBeenCalledTimes(2);
|
||||
act(() => gpx.options?.onImported?.([]));
|
||||
expect(result.current.selected).toBe(40);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user