mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
8fa75b3c4e
A multi-day tour in the planner's list of the trip's tours now unfolds into its stages (MDT-02), so the stages can be seen together and fitted into the trip without leaving the one route they are cut from. - The fold beside a multi-day tour lists its stages in route order, each from stop to stop with its distance, climb, descent and moving time, read off the saved tour the way the planner reads them, and below them what the whole tour adds up to. A value no stage tells is left out, a total some stages do not tell is marked as at least that much and says for how many, and a stage whose route is not planned yet says so. - Each stage has its own day: a pill opens the trip's days, moves the stage to another one or takes it off again. A stage without a day shows a dashed "No day" pill. A day holds a tour, or one of its stages, once, and a stage is on one day at most; the plan's own offline path writes it. - A stage opens in the planner: the tour opens as any tour does and the stage is shown once its points are in. - The day plan names the stage a day holds with its own figures, and the day's route runs to that stage's start and on from its end; the trip map draws that stretch only. The tour list still counts a tour once, however many days hold it or its stages. - A saved change that takes a stage away (its stage end gone, the tour a day tour again, back to the recorded track) takes it off its day in the same write and tells every client; the planner says so before the save. - REST and the assign_place_to_day tool take the stage by the key of the stop it starts at, with the same checks; list_tours names the stops between the stages and the stage each day holds. A day assignment remembers its stage by that key (migration 046010); every assignment made before holds the whole tour, as it did.
179 lines
7.4 KiB
TypeScript
179 lines
7.4 KiB
TypeScript
// FE-REPO-ASSIGN-OFF-001 to FE-REPO-ASSIGN-OFF-010
|
|
// Offline writes of a day's visits (#2601): put on a day, taken off, reordered,
|
|
// moved, who joins and how a leg is travelled.
|
|
import 'fake-indexeddb/auto';
|
|
import { http, HttpResponse } from 'msw';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { buildAssignment, buildDay, buildPlace } from '../../tests/helpers/factories';
|
|
import { server } from '../../tests/helpers/msw/server';
|
|
import type { QueuedMutation } from '../db/offlineDb';
|
|
import { clearAll, offlineDb } from '../db/offlineDb';
|
|
import { setAuthed } from '../sync/authGate';
|
|
import { mutationQueue } from '../sync/mutationQueue';
|
|
import { assignmentRepo } from './assignmentRepo';
|
|
|
|
function setOnline(v: boolean): void {
|
|
Object.defineProperty(navigator, 'onLine', { value: v, writable: true, configurable: true });
|
|
}
|
|
|
|
async function queue(): Promise<QueuedMutation[]> {
|
|
return (await offlineDb.mutationQueue.toArray()).sort((a, b) => a.createdAt - b.createdAt);
|
|
}
|
|
|
|
const place = buildPlace({ id: 300, trip_id: 4, name: 'Pantheon' });
|
|
const first = buildAssignment({ id: 500, day_id: 70, order_index: 0, place: buildPlace({ id: 301, trip_id: 4 }) });
|
|
|
|
beforeEach(async () => {
|
|
await clearAll();
|
|
mutationQueue._resetFlushing();
|
|
setOnline(false);
|
|
await offlineDb.days.bulkPut([
|
|
buildDay({ id: 70, trip_id: 4, assignments: [first] }),
|
|
buildDay({ id: 71, trip_id: 4, assignments: [] }),
|
|
]);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
setAuthed(false);
|
|
setOnline(true);
|
|
});
|
|
|
|
describe('assignmentRepo, offline', () => {
|
|
it('FE-REPO-ASSIGN-OFF-001: a place put on a day lands there under a temp id and the POST waits', async () => {
|
|
const visit = await assignmentRepo.create(4, 70, place);
|
|
|
|
expect(visit.id).toBeLessThan(0);
|
|
expect(visit).toMatchObject({ day_id: 70, place_id: 300, order_index: 1, place: { name: 'Pantheon' } });
|
|
expect((await offlineDb.days.get(70))?.assignments?.map((a) => a.id)).toEqual([500, visit.id]);
|
|
expect((await queue())[0]).toMatchObject({
|
|
method: 'POST',
|
|
url: '/trips/4/days/70/assignments',
|
|
body: { place_id: 300 },
|
|
resource: 'assignments',
|
|
tempId: visit.id,
|
|
});
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-002: a place created offline is put on a day through the queue even online', async () => {
|
|
setOnline(true);
|
|
vi.spyOn(mutationQueue, 'sendSoon').mockImplementation(() => {});
|
|
const visit = await assignmentRepo.create(4, 70, buildPlace({ id: -42, trip_id: 4 }));
|
|
|
|
expect(visit.id).toBeLessThan(0);
|
|
expect((await queue())[0]).toMatchObject({ method: 'POST', body: { place_id: -42 } });
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-003: taking an unsynced visit off waits for its real id', async () => {
|
|
const visit = await assignmentRepo.create(4, 70, place);
|
|
await assignmentRepo.remove(4, 70, visit.id);
|
|
|
|
expect((await offlineDb.days.get(70))?.assignments?.map((a) => a.id)).toEqual([500]);
|
|
expect((await queue())[1]).toMatchObject({
|
|
method: 'DELETE',
|
|
url: '/trips/4/days/70/assignments/{id}',
|
|
tempEntityId: visit.id,
|
|
resource: 'assignments',
|
|
});
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-004: a new order is cached and queued with the temp ids in it', async () => {
|
|
const visit = await assignmentRepo.create(4, 70, place);
|
|
await assignmentRepo.reorder(4, 70, [visit.id, 500]);
|
|
|
|
expect((await offlineDb.days.get(70))?.assignments?.map((a) => [a.id, a.order_index])).toEqual([
|
|
[visit.id, 0],
|
|
[500, 1],
|
|
]);
|
|
expect((await queue())[1]).toMatchObject({
|
|
method: 'PUT',
|
|
url: '/trips/4/days/70/assignments/reorder',
|
|
body: { orderedIds: [visit.id, 500] },
|
|
});
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-005: a move takes the visit to the other day and queues the move, then the order there', async () => {
|
|
await offlineDb.days.update(71, { assignments: [buildAssignment({ id: 510, day_id: 71, order_index: 0 })] });
|
|
|
|
await assignmentRepo.move(4, first, 71, 0, [500, 510]);
|
|
|
|
expect((await offlineDb.days.get(70))?.assignments).toEqual([]);
|
|
expect((await offlineDb.days.get(71))?.assignments?.map((a) => [a.id, a.day_id, a.order_index])).toEqual([
|
|
[500, 71, 0],
|
|
[510, 71, 1],
|
|
]);
|
|
const [move, order] = await queue();
|
|
expect(move).toMatchObject({
|
|
url: '/trips/4/assignments/500/move',
|
|
body: { new_day_id: 71, order_index: 0 },
|
|
entityId: 500,
|
|
});
|
|
expect(order).toMatchObject({ url: '/trips/4/days/71/assignments/reorder', body: { orderedIds: [500, 510] } });
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-006: who joins a visit is named from the cached members of the trip', async () => {
|
|
await offlineDb.tripMembers.bulkPut([
|
|
{ tripId: 4, id: 3, username: 'maurice', avatar: null },
|
|
{ tripId: 4, id: 5, username: 'tom', avatar: 'tom.png' },
|
|
]);
|
|
|
|
const participants = await assignmentRepo.setParticipants(4, 500, [5]);
|
|
|
|
expect(participants).toEqual([{ user_id: 5, username: 'tom', avatar: 'tom.png' }]);
|
|
expect((await offlineDb.days.get(70))?.assignments?.[0].participants).toEqual(participants);
|
|
expect((await queue())[0]).toMatchObject({ url: '/trips/4/assignments/500/participants', body: { user_ids: [5] } });
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-007: the travel mode of the leg entering a visit is cached and queued with its direction', async () => {
|
|
await assignmentRepo.setLegMode(4, 500, 'driving', 'incoming');
|
|
|
|
expect((await offlineDb.days.get(70))?.assignments?.[0].incoming_leg_transport_mode).toBe('driving');
|
|
expect((await queue())[0]).toMatchObject({
|
|
url: '/trips/4/assignments/500/transport',
|
|
body: { transport_mode: 'driving', direction: 'incoming' },
|
|
entityId: 500,
|
|
});
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-008: online with nothing queued, a place goes on its day straight away', async () => {
|
|
setOnline(true);
|
|
server.use(
|
|
http.post('/api/trips/4/days/70/assignments', () =>
|
|
HttpResponse.json({ assignment: buildAssignment({ id: 520, day_id: 70, order_index: 1, place }) })
|
|
)
|
|
);
|
|
|
|
const visit = await assignmentRepo.create(4, 70, place);
|
|
|
|
expect(visit.id).toBe(520);
|
|
expect(await offlineDb.mutationQueue.count()).toBe(0);
|
|
expect((await offlineDb.days.get(70))?.assignments?.map((a) => a.id)).toEqual([500, 520]);
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-009: a stage of a multi-day tour goes on its day offline with its key, in the queued write too (MDT-02)', async () => {
|
|
const visit = await assignmentRepo.create(4, 71, place, 'stop-hut');
|
|
|
|
expect(visit).toMatchObject({ day_id: 71, place_id: 300, tour_stage_key: 'stop-hut' });
|
|
expect((await offlineDb.days.get(71))?.assignments?.[0].tour_stage_key).toBe('stop-hut');
|
|
expect((await queue())[0]).toMatchObject({ body: { place_id: 300, tour_stage_key: 'stop-hut' } });
|
|
});
|
|
|
|
it('FE-REPO-ASSIGN-OFF-010: online, the stage key goes with the request (MDT-02)', async () => {
|
|
setOnline(true);
|
|
let sent: unknown = null;
|
|
server.use(
|
|
http.post('/api/trips/4/days/71/assignments', async ({ request }) => {
|
|
sent = await request.json();
|
|
return HttpResponse.json({
|
|
assignment: buildAssignment({ id: 530, day_id: 71, order_index: 0, place, tour_stage_key: 'stop-hut' }),
|
|
});
|
|
})
|
|
);
|
|
|
|
const visit = await assignmentRepo.create(4, 71, place, 'stop-hut');
|
|
|
|
expect(sent).toEqual({ place_id: 300, tour_stage_key: 'stop-hut' });
|
|
expect(visit).toMatchObject({ id: 530, tour_stage_key: 'stop-hut' });
|
|
});
|
|
});
|