mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
acc294dd8e
A day of a tour that names nobody now counts for every member of the trip, not only for its owner. In the planner an empty participant list means everyone, and a full member list is stored back as empty, so members got nothing for most shared tours. Only a day that names participants without the user stays theirs. The tours of a shared trip on no day or only on undated days are now named as not counting for members as well. A whole route a multi-day tour's stages were planned from is no longer dropped on the server as soon as any stage has a day. Each entry carries derived_from_place_id and the shared summary leaves the route out only while one of its stages is done, counted and in the period and type. A route walked last year now still counts when a stage of it is planned for next month or was left out because it fell through, in the Atlas and over MCP alike. A tour on several days is dated on the last of them, as is a recording on no day, so it no longer counts while it is still under way. The info text, the get_tour_activity_stats description and the wiki say so.
96 lines
3.2 KiB
TypeScript
96 lines
3.2 KiB
TypeScript
// FE-STA2-REPO-001 to FE-STA2-REPO-004
|
|
// STA-02: the user's own tour activities are read live and parsed against the
|
|
// shared contract; offline, nothing is requested at all.
|
|
import type { TourActivityStatsResponse } from '@trek/shared';
|
|
import { http, HttpResponse } from 'msw';
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
import { server } from '../../tests/helpers/msw/server';
|
|
import { _resetNetworkMode, setForcedOffline } from '../sync/networkMode';
|
|
import { TourActivityStatsOfflineError, tourActivityStatsRepo } from './tourActivityStatsRepo';
|
|
|
|
const BASE = '/api/tours/activity-stats';
|
|
|
|
const stats: TourActivityStatsResponse = {
|
|
entries: [
|
|
{
|
|
place_id: 7,
|
|
name: 'Ridge',
|
|
tour_type: 'hike',
|
|
trip: { id: 5, title: 'Alps' },
|
|
derived_from_place_id: null,
|
|
date: '2026-07-02',
|
|
basis: 'plan',
|
|
track: null,
|
|
day_count: 1,
|
|
distance_km: 12.5,
|
|
ascent_m: 800,
|
|
descent_m: 790,
|
|
excluded: false,
|
|
},
|
|
],
|
|
uncounted: { unassigned: 1, undated: 0 },
|
|
};
|
|
|
|
function setOnline(value: boolean): void {
|
|
Object.defineProperty(navigator, 'onLine', { value, writable: true, configurable: true });
|
|
}
|
|
|
|
beforeEach(() => {
|
|
setOnline(true);
|
|
_resetNetworkMode();
|
|
});
|
|
|
|
afterEach(() => {
|
|
_resetNetworkMode();
|
|
setOnline(true);
|
|
});
|
|
|
|
describe('tourActivityStatsRepo', () => {
|
|
it('FE-STA2-REPO-001: get() reads the sums and parses them', async () => {
|
|
server.use(http.get(BASE, () => HttpResponse.json(stats)));
|
|
expect(await tourActivityStatsRepo.get()).toEqual(stats);
|
|
});
|
|
|
|
it('FE-STA2-REPO-002: setExcluded() sends the tour and the choice and answers the new sums', async () => {
|
|
let sent: unknown = null;
|
|
server.use(
|
|
http.put(`${BASE}/exclusions`, async ({ request }) => {
|
|
sent = await request.json();
|
|
return HttpResponse.json({ ...stats, entries: [{ ...stats.entries[0], excluded: true }] });
|
|
})
|
|
);
|
|
const answer = await tourActivityStatsRepo.setExcluded({ place_id: 7, excluded: true });
|
|
expect(sent).toEqual({ place_id: 7, excluded: true });
|
|
expect(answer.entries[0]?.excluded).toBe(true);
|
|
});
|
|
|
|
it('FE-STA2-REPO-003: offline, by the browser or by the switch, nothing is requested', async () => {
|
|
let reached = false;
|
|
server.use(
|
|
http.all(`${BASE}*`, () => {
|
|
reached = true;
|
|
return HttpResponse.json(stats);
|
|
})
|
|
);
|
|
setOnline(false);
|
|
await expect(tourActivityStatsRepo.get()).rejects.toBeInstanceOf(TourActivityStatsOfflineError);
|
|
setOnline(true);
|
|
setForcedOffline(true);
|
|
await expect(tourActivityStatsRepo.setExcluded({ place_id: 7, excluded: true })).rejects.toBeInstanceOf(
|
|
TourActivityStatsOfflineError
|
|
);
|
|
expect(reached).toBe(false);
|
|
});
|
|
|
|
it('FE-STA2-REPO-004: an answer outside the contract fails in the repo, naming the field', async () => {
|
|
const broken = { ...stats, entries: [{ ...stats.entries[0], basis: 'guess' }] };
|
|
server.use(http.get(BASE, () => HttpResponse.json(broken)));
|
|
const error = await tourActivityStatsRepo.get().catch((err: unknown) => err);
|
|
expect((error as { issues?: Array<{ path: Array<string | number> }> }).issues?.[0]?.path).toEqual([
|
|
'entries',
|
|
0,
|
|
'basis',
|
|
]);
|
|
});
|
|
});
|