Files
TREK/client/src/components/Journey/JourneyRecordingDialog.test.tsx
T
Maurice 65bb2d95eb feat(journey): keep the route recorded on a journey day from Dawarich, previewed first, with its gaps
A day header in the journey timeline now carries the Dawarich mark for an
editor whose Dawarich answers (dimmed with the reason while offline). It opens
a dialog that names the journey and the day and reads that one local day from
the caller's own connection: a sketch of the route, distance, first and last
time, pieces, gaps without recording, modes and whether tracks or thinned
points answered. The day is cut in the zone Dawarich records in where its
answer shows one, else in this device's, and a picker cuts it in any other.
Nothing is kept before "Keep route". On a phone the same sheet opens from the
journey's More options for the day of the card shown.

What is kept is the preview that was seen: the save reads the day again and
refuses with recording_changed when its hash moved, and the dialog shows the
new version. The same day taken over twice keeps one version and announces
nothing; a different version is replaced only on "Replace", never touching an
entry or text. A failed read throws before any write, so the kept version
stays. Gaps stay gaps: points are split where silence ends elsewhere, and no
day is joined to the one before. The dialog says how many other people will
see the route; editors may keep their own recording.

Kept days show in the day header (distance, whose recording, since when,
still running) and on the desktop and phone journey maps in the colour of
their day's pins, winning over the live trail of their date, and stay drawn
without Dawarich. A trip's GPX track is now drawn dashed as the plan, recorded
routes solid. Shared links do not show recorded days. Remove route asks first
and works without a connection.

Server: migration 046160 adds journey_recorded_days (one row per journey,
local day and source, segments as JSON, zone, window, fetch time, hash,
importer kept past their account). The journey domain owns the rows through
JourneyRecordedDaysService; the new journey-recordings domain takes a day
over through DawarichTracksService.fetchDay, which reads past the cache, asks
a window wide enough for every zone and refuses with DomainErrors. REST under
/api/journeys/:id/recordings (list, preview, PUT and DELETE /:date) with
@trek/shared contracts, and MCP list_journey_recorded_days,
preview_journey_recorded_day, save_journey_recorded_day and
delete_journey_recorded_day on the same service and access. A new user event
journey:recording:updated keeps every open map current. JourneyTrack gains an
optional kind. All 27 locales carry the strings; the Journey and Dawarich wiki
pages and the MCP tool list say what it does.
2026-10-21 15:05:21 +02:00

104 lines
5.0 KiB
TypeScript

// FE-JRN-RECORDING-DIALOG-001 to FE-JRN-RECORDING-DIALOG-005
//
// JRN-01: the desktop dialog that takes one day's recorded route over. It names
// the journey and the day, shows what would be kept, and keeps it only on its
// button.
import userEvent from '@testing-library/user-event';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { buildRecordedDay, buildRecordingPreview } from '../../../tests/helpers/journeyRecording';
import { render, screen, waitFor } from '../../../tests/helpers/render';
import JourneyRecordingDialog from './JourneyRecordingDialog';
const repo = vi.hoisted(() => ({ preview: vi.fn(), save: vi.fn(), remove: vi.fn() }));
vi.mock('../../repo/journeyRecordingsRepo', () => ({ journeyRecordingsRepo: repo }));
const toast = { success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() };
vi.mock('../shared/Toast', () => ({ useToast: () => toast }));
const httpError = (code: string) => Object.assign(new Error(code), { response: { data: { error: code, code } } });
function renderDialog(stored?: ReturnType<typeof buildRecordedDay>) {
const props = { onKept: vi.fn(), onRemoved: vi.fn(), onClose: vi.fn() };
render(<JourneyRecordingDialog journeyId={7} journeyTitle="Venice" date="2026-05-14" stored={stored} {...props} />);
return props;
}
beforeEach(() => {
for (const fn of [...Object.values(repo), ...Object.values(toast)]) fn.mockReset();
});
describe('JourneyRecordingDialog', () => {
it('FE-JRN-RECORDING-DIALOG-001: names the journey and the day and shows what the day covers before anything is kept', async () => {
repo.preview.mockResolvedValue(buildRecordingPreview({ audience: 2 }));
renderDialog();
expect(await screen.findByTestId('recording-sketch')).toBeInTheDocument();
expect(screen.getByText('Venice')).toBeInTheDocument();
expect(screen.getByText(/May 14, 2026/)).toBeInTheDocument();
expect(screen.getByText('1.5 km')).toBeInTheDocument();
expect(screen.getByText('1 piece')).toBeInTheDocument();
expect(screen.getByText('Walking')).toBeInTheDocument();
expect(screen.getByText(/2 other people on this journey will see the route/)).toBeInTheDocument();
expect(repo.save).not.toHaveBeenCalled();
});
it('FE-JRN-RECORDING-DIALOG-002: keeps the day on its button and closes', async () => {
repo.preview.mockResolvedValue(buildRecordingPreview());
const day = buildRecordedDay();
repo.save.mockResolvedValue({ day, outcome: 'created' });
const props = renderDialog();
await userEvent.click(await screen.findByRole('button', { name: 'Keep route' }));
await waitFor(() => expect(props.onKept).toHaveBeenCalledWith(day));
expect(props.onClose).toHaveBeenCalled();
});
it('FE-JRN-RECORDING-DIALOG-003: gaps and an unfinished day are said out loud, a kept twin cannot be kept again', async () => {
const preview = buildRecordingPreview();
repo.preview.mockResolvedValue({
...preview,
provisional: true,
stats: { ...preview.stats, gapCount: 2 },
stored: buildRecordedDay(),
sameAsStored: true,
});
renderDialog(buildRecordedDay());
expect(await screen.findByText('2 gaps')).toBeInTheDocument();
expect(screen.getByText(/not over yet in this time zone/)).toBeInTheDocument();
expect(screen.getByText('This journey keeps exactly this recording already.')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Already kept' })).toBeDisabled();
});
it('FE-JRN-RECORDING-DIALOG-004: when Dawarich cannot be read it says why, that the kept route stays, and offers to try again', async () => {
repo.preview.mockRejectedValueOnce(httpError('unauthorized'));
renderDialog(buildRecordedDay());
expect(await screen.findByText(/Dawarich rejected the API key\./)).toBeInTheDocument();
expect(screen.getByText(/The route this journey keeps stays as it is\./)).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Keep route' })).toBeDisabled();
repo.preview.mockResolvedValue(buildRecordingPreview());
await userEvent.click(screen.getByRole('button', { name: 'Try again' }));
expect(await screen.findByTestId('recording-sketch')).toBeInTheDocument();
});
it('FE-JRN-RECORDING-DIALOG-005: removing the kept route asks first, also when Dawarich does not answer', async () => {
repo.preview.mockRejectedValue(httpError('not_connected'));
repo.remove.mockResolvedValue(undefined);
const props = renderDialog(buildRecordedDay());
await screen.findByText(/No Dawarich instance is connected yet\./);
await userEvent.click(screen.getByRole('button', { name: 'Remove route' }));
expect(repo.remove).not.toHaveBeenCalled();
expect(await screen.findByText('Remove the kept route?')).toBeInTheDocument();
const buttons = screen.getAllByRole('button', { name: 'Remove route' });
const confirm = buttons[buttons.length - 1];
if (!confirm) throw new Error('no confirm button');
await userEvent.click(confirm);
await waitFor(() => expect(props.onRemoved).toHaveBeenCalledWith('2026-05-14'));
});
});