import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";

const read = (path) => readFile(new URL(`../${path}`, import.meta.url), "utf8");

test("TravelerLayout always mounts mobile and desktop navigation", async () => {
  const [layout, nav] = await Promise.all([
    read("src/components/layout/TravelerLayout.tsx"),
    read("src/components/layout/TravelerBottomNav.tsx"),
  ]);
  assert.match(layout, /<TravelerBottomNav variant="desktop" \/>/);
  assert.match(layout, /<TravelerBottomNav variant="mobile" \/>/);
  assert.match(nav, /Navigation Voyageur principale/);
  assert.match(nav, /Navigation Voyageur/);
  assert.match(nav, /lg:flex/);
  assert.match(nav, /lg:hidden/);
  for (const label of ["Aujourd’hui", "Voyage", "Documents", "Plus"]) assert.match(nav, new RegExp(label));
});

test("traveler navigation preserves an authorized contextual trip", async () => {
  const nav = await read("src/components/layout/TravelerBottomNav.tsx");
  assert.match(nav, /routeTripId/);
  assert.match(nav, /sessionStorage\.setItem\(LAST_TRIP_KEY/);
  assert.match(nav, /trips\.find\(\(trip\) => trip\.id === rememberedTripId\)/);
  assert.match(nav, /`\/voyageur\/trips\/\$\{tripId\}`/);
  assert.match(nav, /`\/voyageur\/documents\/\$\{tripId\}`/);
});

test("Documents provides a direct return to the same trip", async () => {
  const page = await read("src/pages/traveler/DocumentsPage.tsx");
  assert.match(page, /Retour au voyage/);
  assert.match(page, /`\/voyageur\/trips\/\$\{contextTrip\.id\}`/);
});

test("all trip days remain scrollable and the active day is kept visible", async () => {
  const [page, css] = await Promise.all([
    read("src/pages/traveler/TripDetailPage.tsx"),
    read("src/index.css"),
  ]);
  assert.match(page, /Array\.from\(\{ length: dayCount \}/);
  assert.match(page, /overflow-x-auto whitespace-nowrap/);
  assert.match(page, /min-w-max shrink-0/);
  assert.match(page, /aria-current=\{selectedDay === day \? "date"/);
  assert.match(page, /scrollIntoView\(\{ behavior: "smooth", block: "nearest", inline: "center" \}\)/);
  assert.match(page, /next\.set\("day", String\(day\)\)/);
  assert.match(css, /scrollbar-width: thin/);
});

test("Plus exposes dashboard, public home and the existing logout", async () => {
  const page = await read("src/pages/traveler/MorePage.tsx");
  assert.match(page, /to="\/voyageur\/dashboard"/);
  assert.match(page, /to="\/"/);
  assert.match(page, /Se déconnecter/);
  assert.match(page, /void logout\(\)/);
});
