#!/usr/bin/env node // Allers-retours brefs retrouves dans l'historique HA (06/10/2026). // Usage : node tests/allers-retours/run.mjs import { transitionsDansSerie } from '../../tools/allers-retours.mjs'; let ok = 0, ko = 0; function verifie(nom, obtenu, attendu) { const a = JSON.stringify(obtenu), b = JSON.stringify(attendu); if (a === b) { ok++; console.log(`✅ ${nom}`); } else { ko++; console.log(`❌ ${nom}\n attendu ${b}\n obtenu ${a}`); } } const s = (state, last_changed) => ({ state, last_changed }); // Cas reel du 06/10 : echange VW -> JEEP en 20 s, entre deux cycles. // L'historique HA commence par l'etat a l'instant de debut (16:16:10). const reel = [ s('on', '2026-10-06T14:16:10.124Z'), s('off', '2026-10-06T17:52:36.000Z'), s('on', '2026-10-06T17:52:56.474Z'), ]; verifie('echange-20s-du-06-10', transitionsDansSerie(reel, 'on', '2026-10-06T14:16:10.124Z', '2026-10-06T17:52:56.474Z'), [{ avant: 'on', apres: 'off', a: '2026-10-06T17:52:36.000Z' }, { avant: 'off', apres: 'on', a: '2026-10-06T17:52:56.474Z' }]); // Un hoquet d'indisponibilite n'est pas un branchement. verifie('indisponible-ignore', transitionsDansSerie([s('on', '2026-10-06T14:16:10Z'), s('unavailable', '2026-10-06T15:00:00Z'), s('on', '2026-10-06T15:00:05Z')], 'on', '2026-10-06T14:16:10Z', '2026-10-06T15:00:05Z'), []); // L'entree de depart (= dernier changement deja vu) n'est pas rejouee. verifie('point-de-depart-exclu', transitionsDansSerie([s('on', '2026-10-06T14:16:10Z')], 'on', '2026-10-06T14:16:10Z', '2026-10-06T14:16:10Z'), []); // Deux allers-retours dans le meme intervalle : les quatre transitions, dans l'ordre. verifie('deux-allers-retours', transitionsDansSerie([s('on', '2026-10-06T10:00:00Z'), s('off', '2026-10-06T10:00:10Z'), s('on', '2026-10-06T10:00:20Z'), s('off', '2026-10-06T10:00:40Z'), s('on', '2026-10-06T10:00:50Z')], 'on', '2026-10-06T10:00:00Z', '2026-10-06T10:00:50Z').length, 4); console.log(ko ? `\n${ko} ECHEC(S)` : `\nALLERS-RETOURS OK — ${ok}/${ok}`); process.exit(ko ? 1 : 0);