v1.12.0 — takeoff & landing phases on the marker
CI / build + unit + e2e (pull_request) Failing after 7m29s

Every leg gains a 10-min takeoff phase at the origin and a 10-min
landing phase at the destination, each weather-scaled up to 2x via a
monotone condition rule (Clear < Cloudy < Rain < Fog). Single
composition in lib/sim/movement.ts: BASE_PHASE_MS, phaseFactor,
MAX_PHASE_FACTOR, legPhases; arrivalTimeFor and progressAt route
through it (progress is now piecewise — hold at origin / linear cruise
/ hold at destination), so engine arrival gate, client position/fuel
recompute and the job expiry floor all agree. Phases burn no fuel.

Marker shows the current phase next to the status in the popup
(mapData derives it from departureSimTime + clockMs; new
data-testid=plane-phase sibling). plane-status keeps the raw status.

Tests: movement.test.ts (phaseFactor bounds/monotonicity, origin-vs-dest
composition, piecewise progressAt), engine.test.ts (deleted local travelMs
mirror — all ~18 fixture clocks use the real composition), seed.test.ts
(phase-inclusive expiry floor mirrors). New e2e/phases.spec.ts asserts
'taking off' at the origin gate and 'landing' at the destination gate on
EDDF→LEMD using the same legPhases boundaries as the engine.
This commit is contained in:
2026-09-28 07:09:19 +00:00
parent db5ad639fb
commit 82e4c64206
12 changed files with 451 additions and 67 deletions
+94 -24
View File
@@ -1,13 +1,19 @@
import { describe, expect, it } from 'vitest';
import { haversineKm } from '@/lib/geo';
import { weatherFor, type AirportWeather, type WeatherCondition } from '@/lib/weather';
import {
arrivalTimeFor,
BASE_PHASE_MS,
burnRateConstant,
FUEL_DENSITY_KG_PER_L,
fuelAtWeighted,
guaranteedRangeKm,
legPhases,
legPhaseAt,
MAX_PHASE_FACTOR,
MAX_WIND_BURN_FACTOR,
maxRangeKm,
phaseFactor,
positionAt,
progressAt,
requiredFuelFor,
@@ -38,36 +44,98 @@ describe('travelDurationMs / arrivalTimeFor', () => {
expect(() => travelDurationMs(100, 0)).toThrow();
});
it('arrival is departure + duration', () => {
const arrival = arrivalTimeFor(DEP, KM, SPEED);
expect(arrival).toBeCloseTo(DEP + (KM / SPEED) * 3_600_000, 9);
expect(arrival).toBeGreaterThan(DEP);
it('arrival is departure plus all three segments (v1.12.0)', () => {
const ph = legPhases(KM, SPEED, 'EDDF', 'LEMD');
expect(arrivalTimeFor(DEP, KM, SPEED, 'EDDF', 'LEMD')).toBe(DEP + ph.totalMs);
// The cruise part is the old pure duration — phases only add on top.
expect(ph.cruiseMs).toBeCloseTo((KM / SPEED) * 3_600_000, 9);
expect(arrivalTimeFor(DEP, KM, SPEED, 'EDDF', 'LEMD')).toBeGreaterThan(DEP + ph.cruiseMs);
});
});
describe('progressAt', () => {
const duration = travelDurationMs(KM, SPEED);
describe('phaseFactor / legPhases (v1.12.0)', () => {
const mk = (condition: WeatherCondition): AirportWeather => ({ condition, windDirDeg: 90, windKt: 15, visibilityM: 8_000 });
it('is 0 before departure and at departure', () => {
expect(progressAt(DEP, KM, SPEED, DEP - 1)).toBe(0);
expect(progressAt(DEP, KM, SPEED, DEP)).toBe(0);
it('MAX_PHASE_FACTOR is phaseFactor\'s actual maximum over the weather domain', () => {
// Fog is the worst condition — the constant must track the factor function, not a copy.
expect(MAX_PHASE_FACTOR).toBe(phaseFactor(mk('Fog')));
});
it('is linear in the middle of the flight', () => {
it('stays within [1, MAX_PHASE_FACTOR] and is monotone in condition severity', () => {
for (const c of ['Clear', 'Cloudy', 'Rain', 'Fog'] as const) {
const f = phaseFactor(mk(c));
expect(f).toBeGreaterThanOrEqual(1);
expect(f).toBeLessThanOrEqual(MAX_PHASE_FACTOR);
}
expect(phaseFactor(mk('Clear'))).toBe(1); // clear skies stay at 1×
expect(phaseFactor(mk('Cloudy'))).toBeGreaterThan(phaseFactor(mk('Clear')));
expect(phaseFactor(mk('Rain'))).toBeGreaterThan(phaseFactor(mk('Cloudy')));
expect(phaseFactor(mk('Fog'))).toBe(MAX_PHASE_FACTOR); // worst condition is exactly 2×
});
it('is a deterministic function of the ICAO via weatherFor', () => {
for (const icao of ['EDDF', 'LEMD', 'EHAM', 'KSMF']) {
expect(phaseFactor(weatherFor(icao))).toBe(phaseFactor(weatherFor(icao)));
}
// KSMF is Clear in the deterministic generator — a real ICAO pinned at exactly 1×.
expect(phaseFactor(weatherFor('KSMF'))).toBe(1);
});
it('takeoff scales on ORIGIN weather, landing on DESTINATION weather', () => {
const ph = legPhases(KM, SPEED, 'EDDF', 'LEMD');
expect(ph.takeoffMs).toBe(BASE_PHASE_MS * phaseFactor(weatherFor('EDDF')));
expect(ph.landingMs).toBe(BASE_PHASE_MS * phaseFactor(weatherFor('LEMD')));
// Non-vacuous: the two airports really do differ (Rain vs Cloudy), so a swapped
// origin/dest would fail these pins.
expect(phaseFactor(weatherFor('EDDF'))).not.toBe(phaseFactor(weatherFor('LEMD')));
});
it('total is the sum of the three segments and exceeds the old pure duration', () => {
const ph = legPhases(KM, SPEED, 'EDDF', 'LEMD');
expect(ph.totalMs).toBe(ph.takeoffMs + ph.cruiseMs + ph.landingMs);
expect(ph.totalMs).toBeGreaterThan(travelDurationMs(KM, SPEED));
});
it('legPhaseAt walks takeoff → cruise → landing over the leg', () => {
const ph = legPhases(KM, SPEED, 'EDDF', 'LEMD');
expect(legPhaseAt(DEP, KM, SPEED, 'EDDF', 'LEMD', DEP)).toBe('takeoff');
expect(legPhaseAt(DEP, KM, SPEED, 'EDDF', 'LEMD', DEP + ph.takeoffMs - 1)).toBe('takeoff');
expect(legPhaseAt(DEP, KM, SPEED, 'EDDF', 'LEMD', DEP + ph.takeoffMs)).toBe('cruise');
expect(legPhaseAt(DEP, KM, SPEED, 'EDDF', 'LEMD', DEP + ph.takeoffMs + ph.cruiseMs - 1)).toBe('cruise');
expect(legPhaseAt(DEP, KM, SPEED, 'EDDF', 'LEMD', DEP + ph.totalMs - 1)).toBe('landing');
});
});
describe('progressAt (piecewise, v1.12.0)', () => {
const ORIGIN = 'EDDF';
const DEST = 'LEMD';
const ph = legPhases(KM, SPEED, ORIGIN, DEST);
it('is 0 before departure and throughout the takeoff hold at the origin', () => {
expect(progressAt(DEP, KM, SPEED, ORIGIN, DEST, DEP - 1)).toBe(0);
expect(progressAt(DEP, KM, SPEED, ORIGIN, DEST, DEP + ph.takeoffMs / 2)).toBe(0);
const p = positionAt(FROM.lat, FROM.lon, TO.lat, TO.lon, progressAt(DEP, KM, SPEED, ORIGIN, DEST, DEP + ph.takeoffMs - 1));
expect(p).toEqual({ lat: FROM.lat, lon: FROM.lon }); // still at the gate — no crawl along the route
});
it('is linear over the cruise window', () => {
// Precision 9: clock values sit at ~1.75e12 ms, so (clock - DEP) loses a few
// ulps to cancellation — 1e-9 progress is still sub-millisecond on this leg.
expect(progressAt(DEP, KM, SPEED, DEP + duration / 2)).toBeCloseTo(0.5, 9);
expect(progressAt(DEP, KM, SPEED, DEP + duration * 0.25)).toBeCloseTo(0.25, 9);
expect(progressAt(DEP, KM, SPEED, ORIGIN, DEST, DEP + ph.takeoffMs + ph.cruiseMs / 2)).toBeCloseTo(0.5, 9);
expect(progressAt(DEP, KM, SPEED, ORIGIN, DEST, DEP + ph.takeoffMs + ph.cruiseMs * 0.25)).toBeCloseTo(0.25, 9);
});
it('is exactly 1 at arrival and stays 1 after', () => {
const arrival = DEP + duration;
expect(progressAt(DEP, KM, SPEED, arrival)).toBe(1);
expect(progressAt(DEP, KM, SPEED, arrival + 10_000)).toBe(1);
it('is exactly 1 at the start of the landing hold and stays 1 through arrival', () => {
const p = positionAt(FROM.lat, FROM.lon, TO.lat, TO.lon, progressAt(DEP, KM, SPEED, ORIGIN, DEST, DEP + ph.takeoffMs + ph.cruiseMs));
expect(p).toEqual({ lat: TO.lat, lon: TO.lon }); // parked at the destination gate during landing
const arrival = DEP + ph.totalMs;
expect(progressAt(DEP, KM, SPEED, ORIGIN, DEST, arrival)).toBe(1);
expect(progressAt(DEP, KM, SPEED, ORIGIN, DEST, arrival + 10_000)).toBe(1);
});
it('a zero-distance leg is complete immediately', () => {
expect(progressAt(DEP, 0, SPEED, DEP - 5)).toBe(1);
it('a zero-distance leg has no cruise to progress through', () => {
expect(progressAt(DEP, 0, SPEED, 'EDDF', 'EDDF', DEP - 5)).toBe(0); // before departure
expect(progressAt(DEP, 0, SPEED, 'EDDF', 'EDDF', DEP + 1)).toBe(1); // complete once departed
});
});
@@ -160,12 +228,14 @@ describe('weighted burn model (v0.16.0)', () => {
expect(fuelAtWeighted(TANK, BURN, WE, TANK, 0, 1)).toBe(TANK);
});
it('composes with progressAt for a just-before-arrival snapshot', () => {
const duration = travelDurationMs(KM, SPEED);
const clock = DEP + duration - 1; // one ms before arrival
const p = positionAt(FROM.lat, FROM.lon, TO.lat, TO.lon, progressAt(DEP, KM, SPEED, clock));
expect(haversineKm(p.lat, p.lon, TO.lat, TO.lon)).toBeGreaterThan(0); // not yet at the gate
expect(fuelAtWeighted(TANK, BURN, WE, TANK, KM, progressAt(DEP, KM, SPEED, clock))).toBeGreaterThan(0);
it('composes with progressAt for a just-before-arrival snapshot (v1.12.0)', () => {
const ph = legPhases(KM, SPEED, 'EDDF', 'LEMD');
const clock = DEP + ph.totalMs - 1; // one ms before arrival — inside the landing hold
const progress = progressAt(DEP, KM, SPEED, 'EDDF', 'LEMD', clock);
expect(progress).toBe(1); // cruise is done: the plane holds at the destination gate…
const p = positionAt(FROM.lat, FROM.lon, TO.lat, TO.lon, progress);
expect(p).toEqual({ lat: TO.lat, lon: TO.lon }); // …so it IS at the gate (v1.12.0 piecewise)
expect(fuelAtWeighted(TANK, BURN, WE, TANK, KM, progress)).toBeGreaterThan(0);
});
});