mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
feat(roadtrip): leave a stop at its End and show the stay that makes
A visit's End could be edited and was shown in road trip mode, but the schedule never read it: the drive always left a stop after its stay, so a traveller who knew when they had to leave but not how long they would stand there had no way to say so. The End is now the time the drive leaves. RoadtripStop.leaveAt is filled from the visit's End (or its place's) on the client by useRoadtripRoutes and on the server by roadtrip-plan.service, so calculate_roadtrip and get_roadtrip_context agree with the planner, and computeSchedule takes it as the departure it already knew how to hold. With Start and End both set the stay is the time between them. With daily travel times on, the stay is the time until then, and an End before the window start starts the day early the way a Start does. Before the first fixed time the chain works backwards through an End as it does through a pinned Start. An End is read against the drive: the last passing of that clock time counts as missed when the drive went past it on the road or earlier the same day, anything else is the next one, so a drive across midnight neither waits a whole day nor pretends to be on time. A drive that gets there after the End leaves on arrival and files a missedLeave finding, shown beside the late badge in the rail and on the phone. The rail's STAY badge, the inspector's Stay tile, the phone stop sheet and the phone chain show the stay the End makes, with an "until 14:00" hint. It is read off the schedule and never written to duration_minutes: the stay belongs to the place, the End to one visit, and a stored copy would go stale as soon as the arrival moved. For such a stop the stay dialog, desktop and phone, shows that stay read only and offers to remove the End, through the visit's time route with an offline path (assignmentRepo.setTimes, setAssignmentTimes). While the road trip is on, the End field in both place forms says the drive leaves at that time. The dead checkoutAt in the plan service is gone. New strings in all 23 locales.
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* RoadtripPlanService against the worker's real SQLite schema.
|
||||
*
|
||||
* The planning SELECT is the one place the server reads a visit's times for the road
|
||||
* trip, and the tests beside the MCP tools hand the service its rows by hand, so a
|
||||
* column the statement never selects would go unnoticed there. These run the statement.
|
||||
*/
|
||||
import { db } from '../../../src/db/database';
|
||||
import { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
import { RoadtripPlanService } from '../../../src/nest/roadtrip/roadtrip-plan.service';
|
||||
import { createDay, createDayAssignment, createPlace, createTrip, createUser } from '../../helpers/factories';
|
||||
import { resetTestDb } from '../../helpers/test-db';
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
/** Every leg an hour and 60 km, whatever it joins. */
|
||||
function hourlyRouter() {
|
||||
return {
|
||||
profiles: () => ['driving'],
|
||||
route: vi.fn(async (_user: number, _trip: number, _day: number, points: { lat: number; lng: number }[]) => ({
|
||||
parts: points.slice(1).map(() => ({ distance: 60000, duration: 3600 })),
|
||||
avoidMissed: [],
|
||||
leg: {
|
||||
line: points.map((p) => [p.lat, p.lng]),
|
||||
vias: [],
|
||||
seg: {
|
||||
from: [points[0].lat, points[0].lng],
|
||||
to: [points[points.length - 1].lat, points[points.length - 1].lng],
|
||||
mid: [points[0].lat, points[0].lng],
|
||||
distance: 60000 * (points.length - 1),
|
||||
duration: 3600 * (points.length - 1),
|
||||
mode: 'driving',
|
||||
distanceText: '',
|
||||
drivingText: '',
|
||||
walkingText: '',
|
||||
},
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
function setup() {
|
||||
const { user } = createUser(db);
|
||||
const trip = createTrip(db, user.id);
|
||||
const day = createDay(db, trip.id);
|
||||
const visits = ['Hamburg', 'Lueneburg', 'Celle'].map((name, i) => {
|
||||
const place = createPlace(db, trip.id, { name, lat: 53 - i * 0.3, lng: 10 });
|
||||
db.prepare('UPDATE places SET duration_minutes = ? WHERE id = ?').run(i === 0 ? 0 : 30, place.id);
|
||||
return createDayAssignment(db, day.id, place.id);
|
||||
});
|
||||
db.prepare("UPDATE day_assignments SET assignment_time = '09:00' WHERE id = ?").run(visits[0].id);
|
||||
const plans = new RoadtripPlanService(
|
||||
new DatabaseService(db),
|
||||
{ getUserSettings: () => ({}) } as never,
|
||||
{ read: () => ({}) } as never,
|
||||
hourlyRouter() as never,
|
||||
{ listForTrip: () => [], tracksForTrip: () => [] } as never,
|
||||
{ list: () => [] } as never,
|
||||
);
|
||||
return { user, trip, visits, plans };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetTestDb(db);
|
||||
});
|
||||
|
||||
describe('a visit end time on the road trip', () => {
|
||||
it('is read from the visit, and from the place when the visit has none', () => {
|
||||
const { user, trip, visits, plans } = setup();
|
||||
db.prepare("UPDATE day_assignments SET assignment_end_time = '14:00' WHERE id = ?").run(visits[1].id);
|
||||
db.prepare(
|
||||
"UPDATE places SET end_time = '18:00' WHERE id = (SELECT place_id FROM day_assignments WHERE id = ?)",
|
||||
).run(visits[2].id);
|
||||
|
||||
const context = plans.context(trip.id, user.id);
|
||||
|
||||
expect(context.visits.map((v) => [v.name, v.end_time])).toEqual([
|
||||
['Hamburg', null],
|
||||
['Lueneburg', '14:00'],
|
||||
['Celle', '18:00'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('is when the drive leaves the stop, in place of its stay', async () => {
|
||||
const { user, trip, visits, plans } = setup();
|
||||
db.prepare("UPDATE day_assignments SET assignment_end_time = '14:00' WHERE id = ?").run(visits[1].id);
|
||||
|
||||
const { calculated } = await plans.calculate(trip.id, user.id);
|
||||
const day = calculated.days[0];
|
||||
|
||||
expect(day.stops[1]).toMatchObject({ name: 'Lueneburg', leaveAt: '14:00' });
|
||||
expect(day.schedule.entries.map((e) => [e.arrival, e.departure])).toEqual([
|
||||
['09:00', '09:00'],
|
||||
['10:00', '14:00'],
|
||||
['15:00', '15:30'],
|
||||
]);
|
||||
expect(day.schedule.warnings).toEqual([]);
|
||||
});
|
||||
|
||||
it('is reported when the drive gets there after it', async () => {
|
||||
const { user, trip, visits, plans } = setup();
|
||||
db.prepare("UPDATE day_assignments SET assignment_end_time = '09:30' WHERE id = ?").run(visits[1].id);
|
||||
|
||||
const { calculated } = await plans.calculate(trip.id, user.id);
|
||||
|
||||
expect(calculated.days[0].schedule.warnings).toEqual([{ index: 1, code: 'missedLeave', minutes: 30 }]);
|
||||
expect(calculated.days[0].schedule.entries[1]).toMatchObject({ arrival: '10:00', departure: '10:00' });
|
||||
});
|
||||
|
||||
it('leaves a stop without one to its stay', async () => {
|
||||
const { user, trip, plans } = setup();
|
||||
|
||||
const { calculated } = await plans.calculate(trip.id, user.id);
|
||||
|
||||
expect(calculated.days[0].schedule.entries[1]).toMatchObject({ arrival: '10:00', departure: '10:30' });
|
||||
expect(calculated.days[0].stops[1].leaveAt).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -176,6 +176,24 @@ describe('browser-independent roadtrip calculation', () => {
|
||||
const plan = await s.plans.calculate(10, 5);
|
||||
expect(plan.omittedVisits).toEqual([2]);
|
||||
});
|
||||
it('hands calculate_roadtrip the end time of a visit as the moment the drive leaves it', async () => {
|
||||
const s = setup();
|
||||
// Stop 1 is left at 07:30 and the road takes two hours, so stop 2 is reached half an
|
||||
// hour after the 09:00 it was meant to be left at. Stop 3 is left at noon, whatever
|
||||
// its stay says.
|
||||
Object.assign(s.visits[1], { end_time: '09:00' });
|
||||
Object.assign(s.visits[2], { end_time: '12:00' });
|
||||
const mcp = new RoadtripPlanningMcp(s.plans, {} as never, {} as never);
|
||||
const answer = await mcp.calculate(
|
||||
{ tripId: 10, includeGeometry: false, settings: { roadtrip_day_start: '', roadtrip_day_end: '' } },
|
||||
ctx,
|
||||
);
|
||||
const body = JSON.parse(answer.content[0].text as string);
|
||||
const [day] = body.days;
|
||||
expect(day.stops.map((stop: { leaveAt: string | null }) => stop.leaveAt)).toEqual([undefined, '09:00', '12:00']);
|
||||
expect(day.schedule.entries.map((e: { departure: string }) => e.departure)).toEqual(['07:30', '09:30', '12:00']);
|
||||
expect(day.schedule.warnings).toEqual([{ index: 1, code: 'missedLeave', minutes: 30 }]);
|
||||
});
|
||||
it('keeps explicit end-day visits and manual boundaries in the shared planning path', async () => {
|
||||
const s = setup();
|
||||
s.visits[0].end_day = 1;
|
||||
|
||||
Reference in New Issue
Block a user