mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
feat(assistant): hike, walk and route cards with their lines on the map, and "Plan this"
This commit is contained in:
@@ -1,5 +1,37 @@
|
||||
import { idSchema } from '../../common/primitives.schema';
|
||||
import { tourMaxHikingDifficultySchema, tourTypeKeySchema } from '../../tours/tours.schema';
|
||||
import { cardText } from './card-parts';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/** The tours of a trip or of one of its days. Only its kind so far; the tours cards give it its fields. */
|
||||
export const tourListCardSchema = z.object({ kind: z.literal('tourList') });
|
||||
/**
|
||||
* The saved tours of a trip, or of one of its days, as rows like the Tours
|
||||
* sidebar shows them: the activity, the name, time, distance and climb, the
|
||||
* stages of a tour over several days, and the days it is on.
|
||||
*/
|
||||
|
||||
/** The most rows one card lists; `more` counts the rest. */
|
||||
export const TOUR_LIST_ROWS = 12;
|
||||
|
||||
export const tourListRowSchema = z.object({
|
||||
placeId: idSchema,
|
||||
name: cardText(120),
|
||||
activity: tourTypeKeySchema,
|
||||
limit: tourMaxHikingDifficultySchema.nullable(),
|
||||
distanceM: z.number().int().min(0).max(5_000_000).nullable(),
|
||||
ascentM: z.number().int().min(0).max(100_000).nullable(),
|
||||
minutes: z.number().int().min(0).max(86_400).nullable(),
|
||||
stages: z.number().int().min(2).max(60).nullable(),
|
||||
/** The numbers of the trip days it is on, in day order. */
|
||||
days: z.array(z.number().int().min(1).max(999)).max(10),
|
||||
});
|
||||
export type TourListRow = z.infer<typeof tourListRowSchema>;
|
||||
|
||||
export const tourListCardSchema = z.object({
|
||||
kind: z.literal('tourList'),
|
||||
/** The day the list was asked for; null for the whole trip. */
|
||||
dayId: idSchema.nullable(),
|
||||
more: z.number().int().min(0).max(10_000),
|
||||
rows: z.array(tourListRowSchema).min(1).max(TOUR_LIST_ROWS),
|
||||
});
|
||||
export type TourListCard = z.infer<typeof tourListCardSchema>;
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
import { storedTrekCardSchema, trekCardSchema } from './index';
|
||||
import { TOUR_LIST_ROWS, tourListCardSchema } from './tour-list.card';
|
||||
import { TOUR_ROUTE_LIMITS, tourRouteCardSchema, type TourRouteCard, type TourRouteStop } from './tour-route.card';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
function stop(atM: number, extra: Partial<TourRouteStop> = {}): TourRouteStop {
|
||||
return {
|
||||
role: 'via',
|
||||
kind: null,
|
||||
name: null,
|
||||
lat: 47.6,
|
||||
lng: 11.3,
|
||||
atM,
|
||||
offM: null,
|
||||
minutes: null,
|
||||
clock: null,
|
||||
stayMinutes: null,
|
||||
stageEnd: false,
|
||||
flag: null,
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
/** A routed hike with every part filled once. */
|
||||
function hike(extra: Partial<TourRouteCard> = {}): TourRouteCard {
|
||||
return {
|
||||
kind: 'tourRoute',
|
||||
source: 'proposal',
|
||||
activity: 'hike',
|
||||
placeId: null,
|
||||
name: 'Hochfelln from Bergen',
|
||||
label: 'short and steep',
|
||||
distanceM: 8200,
|
||||
ascentM: 640,
|
||||
descentM: 640,
|
||||
minEleM: 560,
|
||||
maxEleM: 1670,
|
||||
minutes: 200,
|
||||
minutesBasis: 'din33466',
|
||||
limit: 3,
|
||||
grades: { highest: 3, unknownShare: 0.2 },
|
||||
routed: true,
|
||||
problem: null,
|
||||
line: [
|
||||
[47.6, 11.3, 560],
|
||||
[47.61, 11.31, 1670],
|
||||
],
|
||||
stretches: [{ facet: 'sac', fromM: 0, toM: 4000, step: 3 }],
|
||||
stops: [
|
||||
stop(0, { role: 'start', clock: '08:00' }),
|
||||
stop(4100, { role: 'end', kind: 'peak', name: 'Hochfelln' }),
|
||||
],
|
||||
stages: null,
|
||||
skiScheme: null,
|
||||
schedule: {
|
||||
date: '2026-10-14',
|
||||
dayId: 31,
|
||||
dayNumber: 2,
|
||||
start: '08:00',
|
||||
arrive: '11:20',
|
||||
nextDay: false,
|
||||
sunrise: '06:58',
|
||||
sunset: '18:41',
|
||||
latestStart: null,
|
||||
weather: 'forecast',
|
||||
hours: [{ hour: 8, main: 'Clear', tempC: 12, rainPct: null, windKmh: 10 }],
|
||||
},
|
||||
warnings: [{ kind: 'thunder', at: '11:00', count: null }],
|
||||
previous: null,
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
describe('tourRouteCardSchema', () => {
|
||||
it('TOURCARD-001: a routed hike parses and joins the card union', () => {
|
||||
expect(tourRouteCardSchema.safeParse(hike()).success).toBe(true);
|
||||
expect(trekCardSchema.safeParse(hike()).success).toBe(true);
|
||||
});
|
||||
|
||||
it('TOURCARD-002: refuses a line, stops or stretches past their bounds', () => {
|
||||
const point: [number, number, number | null] = [47.6, 11.3, null];
|
||||
const line = Array.from({ length: TOUR_ROUTE_LIMITS.line + 1 }, () => point);
|
||||
expect(tourRouteCardSchema.safeParse(hike({ line })).success).toBe(false);
|
||||
const stops = Array.from({ length: TOUR_ROUTE_LIMITS.stops + TOUR_ROUTE_LIMITS.along + 1 }, (_, i) => stop(i));
|
||||
expect(tourRouteCardSchema.safeParse(hike({ stops })).success).toBe(false);
|
||||
const stretches = Array.from({ length: TOUR_ROUTE_LIMITS.stretches + 1 }, (_, i) => ({
|
||||
facet: 'sac' as const,
|
||||
fromM: i,
|
||||
toM: i + 1,
|
||||
step: 2,
|
||||
}));
|
||||
expect(tourRouteCardSchema.safeParse(hike({ stretches })).success).toBe(false);
|
||||
// At the bounds it still parses.
|
||||
expect(tourRouteCardSchema.safeParse(hike({ line: line.slice(1), stops: stops.slice(1) })).success).toBe(true);
|
||||
});
|
||||
|
||||
it('TOURCARD-003: an unknown facet, problem or stop kind fails, a height with decimals too', () => {
|
||||
const odd = { facet: 'colour', fromM: 0, toM: 1 } as unknown as TourRouteCard['stretches'][number];
|
||||
expect(tourRouteCardSchema.safeParse(hike({ stretches: [odd] })).success).toBe(false);
|
||||
const why = { why: 'weird', position: null, snappedM: null } as unknown as TourRouteCard['problem'];
|
||||
expect(tourRouteCardSchema.safeParse(hike({ problem: why })).success).toBe(false);
|
||||
const kind = stop(0, { kind: 'castle' as unknown as TourRouteStop['kind'] });
|
||||
expect(tourRouteCardSchema.safeParse(hike({ stops: [kind] })).success).toBe(false);
|
||||
expect(tourRouteCardSchema.safeParse(hike({ line: [[47.6, 11.3, 560.5]] })).success).toBe(false);
|
||||
});
|
||||
|
||||
it('TOURCARD-004: a straight card without figures parses, as a held change planned on apply', () => {
|
||||
const straight = hike({
|
||||
source: 'pending',
|
||||
label: null,
|
||||
distanceM: null,
|
||||
ascentM: null,
|
||||
descentM: null,
|
||||
minEleM: null,
|
||||
maxEleM: null,
|
||||
minutes: null,
|
||||
minutesBasis: null,
|
||||
grades: null,
|
||||
routed: false,
|
||||
problem: { why: 'not_planned', position: null, snappedM: null },
|
||||
line: [
|
||||
[47.6, 11.3, null],
|
||||
[47.61, 11.31, null],
|
||||
],
|
||||
stretches: [],
|
||||
schedule: null,
|
||||
warnings: [],
|
||||
});
|
||||
expect(tourRouteCardSchema.safeParse(straight).success).toBe(true);
|
||||
});
|
||||
|
||||
it('TOURCARD-005: ski stretches, marks, a failed-legs warning and the line before a change parse', () => {
|
||||
const ski = hike({
|
||||
activity: 'ski',
|
||||
source: 'plan',
|
||||
skiScheme: 'north_america',
|
||||
stretches: [
|
||||
{ facet: 'ski', fromM: 0, toM: 900, kind: 'run', difficulty: 'intermediate' },
|
||||
{ facet: 'mark', fromM: 900, toM: 1500, kind: 'lift' },
|
||||
],
|
||||
warnings: [{ kind: 'legs_failed', at: null, count: 2 }],
|
||||
previous: {
|
||||
line: [
|
||||
[47.6, 11.3],
|
||||
[47.62, 11.32],
|
||||
],
|
||||
distanceM: 9000,
|
||||
ascentM: null,
|
||||
minutes: 210,
|
||||
},
|
||||
});
|
||||
expect(tourRouteCardSchema.safeParse(ski).success).toBe(true);
|
||||
});
|
||||
|
||||
it('TOURCARD-006: extra keys are dropped on read, a broken card reads as null', () => {
|
||||
const parsed = storedTrekCardSchema.parse({ ...hike(), later: 'field' });
|
||||
expect(parsed).not.toHaveProperty('later');
|
||||
expect(storedTrekCardSchema.parse({ ...hike(), routed: 'yes' })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('tourListCardSchema', () => {
|
||||
const row = {
|
||||
placeId: 61,
|
||||
name: 'Herzogstand',
|
||||
activity: 'hike' as const,
|
||||
limit: 2,
|
||||
distanceM: 9800,
|
||||
ascentM: 520,
|
||||
minutes: 225,
|
||||
stages: null,
|
||||
days: [2],
|
||||
};
|
||||
|
||||
it('TOURCARD-007: a list parses with one to twelve rows, not none and not more', () => {
|
||||
expect(tourListCardSchema.safeParse({ kind: 'tourList', dayId: null, more: 0, rows: [row] }).success).toBe(true);
|
||||
expect(tourListCardSchema.safeParse({ kind: 'tourList', dayId: 31, more: 0, rows: [] }).success).toBe(false);
|
||||
const rows = Array.from({ length: TOUR_LIST_ROWS + 1 }, () => row);
|
||||
expect(tourListCardSchema.safeParse({ kind: 'tourList', dayId: null, more: 0, rows }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,174 @@
|
||||
import { idSchema } from '../../common/primitives.schema';
|
||||
import { skiColourSchemeSchema, skiRunDifficultySchema, skiSegmentKindSchema } from '../../ski/ski-route.schema';
|
||||
import { HIKE_UNROUTED_REASONS } from '../../tours/hike-plan.schema';
|
||||
import { HIKE_WARNING_KINDS } from '../../tours/hike-schedule';
|
||||
import { tourPoiCategorySchema } from '../../tours/tour-poi-category';
|
||||
import { tourMaxHikingDifficultySchema, tourTypeKeySchema } from '../../tours/tours.schema';
|
||||
import { cardClockSchema, cardDateSchema, cardLatSchema, cardLngSchema, cardText } from './card-parts';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* A tour's route: a hike, a city walk, a paddle or a ski plan the assistant proposed, or a saved tour
|
||||
* it read. Only its kind so far; the tours cards give it its fields.
|
||||
* A tour's route as the chat draws it: a hike, a city walk, a paddle or a ski
|
||||
* plan the assistant proposed, a held change that will create or change one,
|
||||
* or a saved tour it read. One kind for every line, so a sketch, a height
|
||||
* profile, the stops by how far along they lie, the day with its sun and
|
||||
* weather and what to look out for read the same whatever the activity.
|
||||
* Positions are metres along the whole route; the line is thinned for
|
||||
* drawing and every height is whole metres.
|
||||
*/
|
||||
export const tourRouteCardSchema = z.object({ kind: z.literal('tourRoute') });
|
||||
|
||||
/** How much one card carries at most. */
|
||||
export const TOUR_ROUTE_LIMITS = {
|
||||
line: 300,
|
||||
stops: 30,
|
||||
along: 12,
|
||||
stretches: 80,
|
||||
hours: 18,
|
||||
warnings: 6,
|
||||
previous: 200,
|
||||
} as const;
|
||||
|
||||
const L = TOUR_ROUTE_LIMITS;
|
||||
const metres = z.number().int().min(0).max(5_000_000);
|
||||
const climb = z.number().int().min(0).max(100_000);
|
||||
const height = z.number().int().min(-500).max(9_000);
|
||||
const minutes = z.number().int().min(0).max(86_400);
|
||||
const sacStep = z.number().int().min(1).max(6);
|
||||
|
||||
/** A point of the line, `[lat, lng, height]` at five decimals; every height is null when one is missing. */
|
||||
export const tourRoutePointSchema = z.tuple([cardLatSchema, cardLngSchema, height.nullable()]);
|
||||
|
||||
/** What a stop is beyond its role: a kind of place along a hike, its highest point, a city walk's station. */
|
||||
export const TOUR_ROUTE_STOP_KINDS = ['highest', 'station'] as const;
|
||||
|
||||
/** Why a city walk's station is flagged: closed when it is reached, late for an appointment, past the time frame. */
|
||||
export const TOUR_ROUTE_STOP_FLAGS = ['closed', 'late', 'after_frame'] as const;
|
||||
|
||||
export const tourRouteStopSchema = z.object({
|
||||
role: z.enum(['start', 'via', 'end', 'along']),
|
||||
kind: z.union([tourPoiCategorySchema, z.enum(TOUR_ROUTE_STOP_KINDS)]).nullable(),
|
||||
name: cardText(80).nullable(),
|
||||
lat: cardLatSchema,
|
||||
lng: cardLngSchema,
|
||||
atM: metres,
|
||||
/** A place along the way: how far off it, in metres. */
|
||||
offM: z.number().int().min(0).max(2_000).nullable(),
|
||||
/** The walking minutes from the start to it. */
|
||||
minutes: minutes.nullable(),
|
||||
/** When it is reached on the day's schedule. */
|
||||
clock: cardClockSchema.nullable(),
|
||||
stayMinutes: minutes.nullable(),
|
||||
stageEnd: z.boolean(),
|
||||
flag: z.enum(TOUR_ROUTE_STOP_FLAGS).nullable(),
|
||||
});
|
||||
export type TourRouteStop = z.infer<typeof tourRouteStopSchema>;
|
||||
|
||||
/** A stretch drawn dotted: carried, white water, a gap in the data, a ferry, a lift, a via ferrata, drawn freely. */
|
||||
export const TOUR_ROUTE_MARKS = ['portage', 'rapids', 'gap', 'ferry', 'lift', 'ferrata', 'free'] as const;
|
||||
|
||||
/** A stretch of the route with something to tell: its SAC grade, a ski run or trail, or a mark. */
|
||||
export const tourRouteStretchSchema = z.discriminatedUnion('facet', [
|
||||
z.object({ facet: z.literal('sac'), fromM: metres, toM: metres, step: sacStep }),
|
||||
z.object({
|
||||
facet: z.literal('ski'),
|
||||
fromM: metres,
|
||||
toM: metres,
|
||||
kind: skiSegmentKindSchema,
|
||||
difficulty: skiRunDifficultySchema.nullable(),
|
||||
}),
|
||||
z.object({ facet: z.literal('mark'), fromM: metres, toM: metres, kind: z.enum(TOUR_ROUTE_MARKS) }),
|
||||
]);
|
||||
export type TourRouteStretch = z.infer<typeof tourRouteStretchSchema>;
|
||||
|
||||
/** What to look out for: the day's weather and light, and legs of a plan that could not be planned. */
|
||||
export const TOUR_WARNING_KINDS = [...HIKE_WARNING_KINDS, 'legs_failed'] as const;
|
||||
|
||||
/**
|
||||
* Why a route is drawn straight between its stops: the hike planner's
|
||||
* reasons, a city walk whose only way runs by boat, or a held change whose
|
||||
* way is planned only once it is applied.
|
||||
*/
|
||||
export const TOUR_ROUTE_PROBLEMS = [...HIKE_UNROUTED_REASONS, 'ferry_only', 'not_planned'] as const;
|
||||
|
||||
export const tourRouteScheduleSchema = z.object({
|
||||
date: cardDateSchema,
|
||||
dayId: idSchema.nullable(),
|
||||
dayNumber: z.number().int().min(1).max(999).nullable(),
|
||||
start: cardClockSchema.nullable(),
|
||||
arrive: cardClockSchema.nullable(),
|
||||
nextDay: z.boolean(),
|
||||
sunrise: cardClockSchema.nullable(),
|
||||
sunset: cardClockSchema.nullable(),
|
||||
latestStart: cardClockSchema.nullable(),
|
||||
weather: z.enum(['forecast', 'climate']).nullable(),
|
||||
hours: z
|
||||
.array(
|
||||
z.object({
|
||||
hour: z.number().int().min(0).max(23),
|
||||
main: cardText(20),
|
||||
tempC: z.number().int().min(-80).max(60),
|
||||
/** Null where the weather knows no likelihood (last year's day, a past day): never read as 0 %. */
|
||||
rainPct: z.number().int().min(0).max(100).nullable(),
|
||||
windKmh: z.number().int().min(0).max(400).nullable(),
|
||||
}),
|
||||
)
|
||||
.max(L.hours),
|
||||
});
|
||||
export type TourRouteSchedule = z.infer<typeof tourRouteScheduleSchema>;
|
||||
|
||||
export const tourRouteCardSchema = z.object({
|
||||
kind: z.literal('tourRoute'),
|
||||
/** proposal: a way proposed; plan: a paddle or ski plan; pending: a held change; saved: a tour of the trip. */
|
||||
source: z.enum(['proposal', 'plan', 'pending', 'saved']),
|
||||
activity: tourTypeKeySchema,
|
||||
placeId: idSchema.nullable(),
|
||||
name: cardText(120).nullable(),
|
||||
/** A way's own words when several are compared ("short and steep"). */
|
||||
label: cardText(40).nullable(),
|
||||
distanceM: metres.nullable(),
|
||||
ascentM: climb.nullable(),
|
||||
descentM: climb.nullable(),
|
||||
minEleM: height.nullable(),
|
||||
maxEleM: height.nullable(),
|
||||
minutes: minutes.nullable(),
|
||||
minutesBasis: z.enum(['din33466', 'router', 'planned', 'stations', 'piste']).nullable(),
|
||||
/** The hiking limit the way was planned under, a setting rather than a grade of the path. */
|
||||
limit: tourMaxHikingDifficultySchema.nullable(),
|
||||
grades: z.object({ highest: sacStep.nullable(), unknownShare: z.number().min(0).max(1) }).nullable(),
|
||||
/** False: the stops joined straight, drawn dashed, without a profile. */
|
||||
routed: z.boolean(),
|
||||
problem: z
|
||||
.object({
|
||||
why: z.enum(TOUR_ROUTE_PROBLEMS),
|
||||
position: z.number().int().positive().nullable(),
|
||||
snappedM: z.number().int().min(0).max(1_000_000).nullable(),
|
||||
})
|
||||
.nullable(),
|
||||
line: z.array(tourRoutePointSchema).max(L.line),
|
||||
stretches: z.array(tourRouteStretchSchema).max(L.stretches),
|
||||
/** In the order they lie along the way. */
|
||||
stops: z.array(tourRouteStopSchema).max(L.stops + L.along),
|
||||
stages: z.number().int().min(2).max(60).nullable(),
|
||||
skiScheme: skiColourSchemeSchema.nullable(),
|
||||
schedule: tourRouteScheduleSchema.nullable(),
|
||||
warnings: z
|
||||
.array(
|
||||
z.object({
|
||||
kind: z.enum(TOUR_WARNING_KINDS),
|
||||
at: cardClockSchema.nullable(),
|
||||
count: z.number().int().min(1).max(50).nullable(),
|
||||
}),
|
||||
)
|
||||
.max(L.warnings),
|
||||
/** A held change of a saved tour's way: the way it has now, to draw faintly beside the new one. */
|
||||
previous: z
|
||||
.object({
|
||||
line: z.array(z.tuple([cardLatSchema, cardLngSchema])).min(2).max(L.previous),
|
||||
distanceM: metres,
|
||||
ascentM: climb.nullable(),
|
||||
minutes: minutes.nullable(),
|
||||
})
|
||||
.nullable(),
|
||||
});
|
||||
export type TourRouteCard = z.infer<typeof tourRouteCardSchema>;
|
||||
|
||||
Reference in New Issue
Block a user