All files / src/utils eventIndoorNavigation.ts

95.47% Statements 232/243
84.97% Branches 147/173
100% Functions 28/28
99.03% Lines 205/207

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472                                                        2x 2x 2x 2x   2x   2x   2x   2x 2x 2x 2x 2x 2x 2x   2x 164x       2x 750x           2x 208x   2x 208x 208x 208x     2x       33x   33x 33x   20x 20x   20x 20x   11x 11x 10x 10x 2x     1x     2x 104x 104x 104x 104x   104x 104x   104x 104x   104x 104x   104x 104x   104x     2x           52x 52x 52x 52x   52x   52x   52x 5x     52x 5x     52x       52x 52x 13x     52x       17x     52x 52x 52x 52x 113x 34x     52x 18x     52x 34x     18x       18x 18x     18x     2x 93x 93x     93x 93x       2x 96x 96x 96x 174x 174x 78x 78x           2x 17x 17x 17x   15x 15x   15x 15x 15x 15x     2x       17x 17x 17x 17x   17x 17x 15x 15x 15x 15x 15x     17x 17x 17x   17x     2x         17x   15x   15x 25x 37x 52x           52x 12x         15x         2x         17x     17x   15x 15x 15x     1x     2x 14x 14x 12x 6x 6x 6x 6x 5x 5x     2x       15x   13x 20x   13x   12x 14x 2x   12x     2x         19x 14x     5x 13x   2x 2x 2x 1x     5x     2x           14x 14x 7x       2x           19x     2x       19x           19x   18x 18x 18x 1x                   17x         17x   29x 29x 28x         1x         17x 17x           17x   17x   17x 3x                     22x 14x 14x           14x   14x 14x                 14x 5x 5x               14x 3x     14x                  
import { getAvailableRooms } from "../api/indoorDirectionsApi";
import { BuildingId } from "../data/buildings";
import { getAvailableFloors } from "./buildingIndoorMaps";
import { findBuildingFromLocationText } from "./eventLocationBuildingMatcher";
 
export type EventDirectionsRequest = {
  locationText: string;
  detailsText: string;
};
 
export type EventIndoorTarget = {
  buildingId: BuildingId;
  floor: string | null;
  startFloor: string | null;
  floorSupported: boolean;
  destinationRoom: string | null;
  startRoom: string | null;
  globalOriginRoom?: string | null;
  globalOriginBuildingId?: BuildingId | null;
  globalDestinationRoom?: string | null;
  globalDestinationBuildingId?: BuildingId | null;
};
 
type RoomIndex = {
  floor: string;
  rooms: string[];
};
 
const MAX_REGEX_INPUT_LENGTH = 2048;
const MAX_REGEX_MATCHES = 200;
const MAX_REGEX_CAPTURE_LENGTH = 48;
const CLASSROOM_KEYWORD_REGEX = /\b(?:classroom|room|rm)\b/i;
const CLASSROOM_VALUE_REGEX =
  /^[ \t]{0,6}[:#-]?[ \t]{0,6}([A-Z0-9][A-Z0-9 ./-]{0,119})/i;
const FLOOR_LINE_REGEX =
  /\b(?:floor|flr|niveau)\b[ \t]{0,6}[:#-]?[ \t]{0,6}(S?\d{1,2})\b/i;
const ROOM_MARKER_REGEX =
  /\b(?:RM|ROOM)\b[ \t]{0,6}[:#-]?[ \t]{0,6}([A-Z0-9][A-Z0-9.\-/]{0,23})\b/gi;
const PREFIXED_ROOM_REGEX =
  /\b([A-Z]{1,3}[ \t]{0,3}-?[ \t]{0,3}S?\d[A-Z0-9.\-/]{0,15})\b/gi;
const ROOM_NUMBER_REGEX = /\b(S\d[-.]?\d{2,4}|\d{3,4})\b/gi;
const SUB_FLOOR_REGEX = /S(\d)/;
const DOTTED_FLOOR_REGEX = /\b(\d)\.\d{2,4}\b/;
const EXPLICIT_FLOOR_REGEX = /^[A-Z]{1,3}-?(\d)-/;
const ROOM_DIGITS_REGEX = /(\d{3,4})/;
const ROOM_AFTER_SUB_FLOOR_REGEX = /S\d[-.]?(\d{2,4})/;
 
const safeRegexInput = (value: string): string =>
  value.length > MAX_REGEX_INPUT_LENGTH
    ? value.slice(0, MAX_REGEX_INPUT_LENGTH)
    : value;
 
const normalizeRoom = (value: string): string =>
  value
    .toUpperCase()
    .replaceAll(/[\u2013\u2014]/g, "-")
    .replaceAll(/\s+/g, "")
    .trim();
 
const compactRoom = (value: string): string =>
  normalizeRoom(value).replaceAll(/[^A-Z0-9]/g, "");
 
const getRoomSuffix = (value: string): string => {
  const normalized = normalizeRoom(value);
  const lastSegment = normalized.split("-").at(-1) ?? normalized;
  return lastSegment.replaceAll(/[^A-Z0-9.]/g, "");
};
 
const inferFloorFromCandidate = (
  candidate: string,
  buildingId: BuildingId,
): string | null => {
  const normalized = normalizeRoom(candidate);
 
  const subFloor = SUB_FLOOR_REGEX.exec(normalized);
  if (subFloor?.[1]) return `S${subFloor[1]}`;
 
  const dottedFloor = DOTTED_FLOOR_REGEX.exec(normalized);
  Iif (dottedFloor?.[1]) return dottedFloor[1];
 
  const explicit = EXPLICIT_FLOOR_REGEX.exec(normalized);
  if (explicit?.[1]) return explicit[1];
 
  const digits = ROOM_DIGITS_REGEX.exec(normalized);
  if (digits?.[1]) {
    const first = digits[1][0];
    if (buildingId === "H") return first;
    Eif (["LB", "VL", "VE", "CC", "MB"].includes(buildingId)) return first;
  }
 
  return null;
};
 
const tokenizeForMatch = (value: string): Set<string> => {
  const normalized = normalizeRoom(value);
  const compact = compactRoom(normalized);
  const suffix = getRoomSuffix(normalized);
  const tokens = new Set<string>();
 
  Eif (compact.length >= 2) tokens.add(compact);
  if (suffix.length >= 2) tokens.add(suffix.replaceAll(".", ""));
 
  const digitsOnly = compact.replaceAll(/\D/g, "");
  if (digitsOnly.length >= 3) tokens.add(digitsOnly);
 
  const numbered = ROOM_DIGITS_REGEX.exec(normalized);
  if (numbered?.[1]) tokens.add(numbered[1]);
 
  const roomAfterSubFloor = ROOM_AFTER_SUB_FLOOR_REGEX.exec(normalized);
  if (roomAfterSubFloor?.[1]) tokens.add(roomAfterSubFloor[1]);
 
  return tokens;
};
 
const scoreRoomMatch = (
  candidate: string,
  room: string,
  roomFloor: string,
  buildingId: BuildingId,
): number => {
  const normalizedCandidate = normalizeRoom(candidate);
  const normalizedRoom = normalizeRoom(room);
  const compactCandidate = compactRoom(normalizedCandidate);
  const compactRoomId = compactRoom(normalizedRoom);
 
  Iif (!compactCandidate || !compactRoomId) return Number.NEGATIVE_INFINITY;
 
  let score = Number.NEGATIVE_INFINITY;
 
  if (normalizedRoom === normalizedCandidate) {
    score = 1200;
  }
 
  if (compactRoomId === compactCandidate) {
    score = Math.max(score, 1150);
  }
 
  const suffixCandidate = getRoomSuffix(normalizedCandidate).replaceAll(
    ".",
    "",
  );
  const suffixRoom = getRoomSuffix(normalizedRoom).replaceAll(".", "");
  if (suffixCandidate.length >= 2 && suffixCandidate === suffixRoom) {
    score = Math.max(score, 1050);
  }
 
  if (
    compactCandidate.length >= 3 &&
    compactRoomId.endsWith(compactCandidate)
  ) {
    score = Math.max(score, 980);
  }
 
  const candidateTokens = tokenizeForMatch(normalizedCandidate);
  const roomTokens = tokenizeForMatch(normalizedRoom);
  let bestTokenLength = 0;
  candidateTokens.forEach((token) => {
    if (token.length >= 3 && roomTokens.has(token)) {
      bestTokenLength = Math.max(bestTokenLength, token.length);
    }
  });
  if (bestTokenLength > 0) {
    score = Math.max(score, 820 + bestTokenLength);
  }
 
  if (!Number.isFinite(score)) {
    return Number.NEGATIVE_INFINITY;
  }
 
  const inferredFloor = inferFloorFromCandidate(
    normalizedCandidate,
    buildingId,
  );
  Eif (inferredFloor) {
    score += inferredFloor === roomFloor ? 120 : -80;
  }
 
  return score;
};
 
const pushCandidate = (set: Set<string>, value: string | undefined | null) => {
  Iif (!value) return;
  const normalized = normalizeRoom(value)
    .replaceAll(/^RM[-:]?/g, "")
    .replaceAll(/^ROOM[-:]?/g, "");
  Eif (normalized.length >= 2) {
    set.add(normalized);
  }
};
 
const collectRegexMatches = (set: Set<string>, text: string, regex: RegExp) => {
  const safeText = safeRegexInput(text);
  regex.lastIndex = 0;
  for (let i = 0; i < MAX_REGEX_MATCHES; i += 1) {
    const match = regex.exec(safeText);
    if (!match?.[1]) break;
    pushCandidate(set, match[1].slice(0, MAX_REGEX_CAPTURE_LENGTH));
    Iif (match[0].length === 0) {
      regex.lastIndex += 1;
    }
  }
};
 
const extractClassroomLine = (detailsText: string): string | null => {
  const safeDetailsText = safeRegexInput(detailsText);
  const keywordMatch = CLASSROOM_KEYWORD_REGEX.exec(safeDetailsText);
  if (!keywordMatch) return null;
 
  const startIndex = keywordMatch.index + keywordMatch[0].length;
  Iif (startIndex >= safeDetailsText.length) return null;
 
  const tail = safeDetailsText.slice(startIndex);
  const valueMatch = CLASSROOM_VALUE_REGEX.exec(tail);
  const rawValue = valueMatch?.[1]?.trim();
  return rawValue && rawValue.length > 0 ? rawValue : null;
};
 
const parseRoomCandidates = (
  detailsText: string,
  locationText: string,
): string[] => {
  const candidates = new Set<string>();
  const safeLocationText = safeRegexInput(locationText);
  const safeDetailsText = safeRegexInput(detailsText);
  const mergedText = `${safeLocationText}\n${safeDetailsText}`;
 
  const classroomLine = extractClassroomLine(safeDetailsText);
  if (classroomLine) {
    collectRegexMatches(candidates, classroomLine, ROOM_MARKER_REGEX);
    collectRegexMatches(candidates, classroomLine, PREFIXED_ROOM_REGEX);
    collectRegexMatches(candidates, classroomLine, ROOM_NUMBER_REGEX);
    const firstToken = classroomLine.trim().split(" ")[0];
    pushCandidate(candidates, firstToken);
  }
 
  collectRegexMatches(candidates, mergedText, ROOM_MARKER_REGEX);
  collectRegexMatches(candidates, mergedText, PREFIXED_ROOM_REGEX);
  collectRegexMatches(candidates, mergedText, ROOM_NUMBER_REGEX);
 
  return Array.from(candidates);
};
 
const resolveBestRoomMatch = (
  roomCandidates: string[],
  roomIndex: RoomIndex[],
  buildingId: BuildingId,
): { room: string; floor: string } | null => {
  if (!roomCandidates.length) return null;
 
  let best: { room: string; floor: string; score: number } | null = null;
 
  for (const floorEntry of roomIndex) {
    for (const room of floorEntry.rooms) {
      for (const candidate of roomCandidates) {
        const score = scoreRoomMatch(
          candidate,
          room,
          floorEntry.floor,
          buildingId,
        );
        if (!Number.isFinite(score) || (best && score <= best.score)) continue;
        best = { room, floor: floorEntry.floor, score };
      }
    }
  }
 
  return best && best.score >= 820
    ? { room: best.room, floor: best.floor }
    : null;
};
 
const parseFloorCandidate = (
  detailsText: string,
  roomCandidates: string[],
  buildingId: BuildingId,
): string | null => {
  const floorFromDetails = FLOOR_LINE_REGEX.exec(
    safeRegexInput(detailsText),
  )?.[1]?.toUpperCase();
  if (floorFromDetails) return floorFromDetails;
 
  for (const candidate of roomCandidates) {
    const inferred = inferFloorFromCandidate(candidate, buildingId);
    if (inferred) return inferred;
  }
 
  return null;
};
 
const scoreStartRoom = (room: string): number => {
  const lower = room.toLowerCase();
  if (/(entrance|entry)/.test(lower)) return 100;
  if (/elevator/.test(lower) && /main/.test(lower)) return 90;
  Iif (/stairs?/.test(lower) && /main/.test(lower)) return 80;
  Iif (/elevator/.test(lower)) return 70;
  Iif (/stairs?/.test(lower)) return 60;
  if (/exit/.test(lower) && !/emergency/.test(lower)) return 50;
  Iif (/exit/.test(lower)) return 30;
  return 0;
};
 
const resolveStartRoom = (
  availableRooms: string[],
  destinationRoom: string | null,
): string | null => {
  if (availableRooms.length === 0) return null;
 
  const pool = availableRooms.filter(
    (room) => room.toUpperCase() !== destinationRoom?.toUpperCase(),
  );
  if (pool.length === 0) return null;
 
  const scored = pool
    .map((room) => ({ room, score: scoreStartRoom(room) }))
    .sort((a, b) => b.score - a.score || a.room.localeCompare(b.room));
 
  return scored[0].score > 0 ? scored[0].room : null;
};
 
const resolveSpecialStartRoom = (
  availableRooms: string[],
  buildingId: BuildingId,
  floor: string,
): string | null => {
  if (buildingId !== "MB" || !/^S\d+$/i.test(floor)) {
    return null;
  }
 
  const elevators = availableRooms
    .filter((room) => /elevator/i.test(room))
    .sort((a, b) => {
      const aMain = /main/i.test(a) ? 1 : 0;
      const bMain = /main/i.test(b) ? 1 : 0;
      if (aMain !== bMain) return bMain - aMain;
      return a.localeCompare(b);
    });
 
  return elevators[0] ?? null;
};
 
const getPreferredStartFloor = (
  buildingId: BuildingId,
  availableFloors: string[],
  destinationFloor: string,
): string => {
  // Mirror backend entrance-floor behavior where possible.
  Iif (buildingId === "LB" && availableFloors.includes("2")) return "2";
  if (availableFloors.includes("1")) return "1";
  Eif (availableFloors.includes(destinationFloor)) return destinationFloor;
  return availableFloors[0] ?? destinationFloor;
};
 
const resolveStartRoomForFloor = (
  rooms: string[],
  buildingId: BuildingId,
  floor: string,
  destinationRoom: string | null,
): string | null =>
  resolveSpecialStartRoom(rooms, buildingId, floor) ??
  resolveStartRoom(rooms, destinationRoom);
 
export const buildEventIndoorTarget = async (
  request: EventDirectionsRequest,
): Promise<EventIndoorTarget | null> => {
  const building =
    findBuildingFromLocationText(request.locationText) ||
    findBuildingFromLocationText(request.detailsText) ||
    findBuildingFromLocationText(
      `${request.locationText} ${request.detailsText}`,
    );
 
  if (!building) return null;
 
  const buildingId = building.id;
  const availableFloors = getAvailableFloors(buildingId);
  if (!availableFloors.length) {
    return {
      buildingId,
      floor: null,
      startFloor: null,
      floorSupported: false,
      destinationRoom: null,
      startRoom: null,
    };
  }
 
  const roomCandidates = parseRoomCandidates(
    request.detailsText,
    request.locationText,
  );
 
  const roomIndex = await Promise.all(
    availableFloors.map(async (floor): Promise<RoomIndex> => {
      try {
        const fetched = await getAvailableRooms(buildingId, floor);
        return {
          floor,
          rooms: Array.isArray(fetched) ? fetched : [],
        };
      } catch {
        return { floor, rooms: [] };
      }
    }),
  );
 
  const bestMatch = resolveBestRoomMatch(roomCandidates, roomIndex, buildingId);
  const explicitFloor = parseFloorCandidate(
    request.detailsText,
    roomCandidates,
    buildingId,
  );
 
  const resolvedFloor = bestMatch?.floor ?? explicitFloor ?? null;
  const floorSupported =
    !!resolvedFloor && availableFloors.includes(resolvedFloor);
 
  if (!floorSupported || !resolvedFloor) {
    return {
      buildingId,
      floor: resolvedFloor,
      startFloor: null,
      floorSupported: false,
      destinationRoom: bestMatch?.room ?? roomCandidates[0] ?? null,
      startRoom: null,
    };
  }
 
  const floorRooms =
    roomIndex.find((entry) => entry.floor === resolvedFloor)?.rooms ?? [];
  const destinationRoom = bestMatch?.room ?? roomCandidates[0] ?? null;
  const preferredStartFloor = getPreferredStartFloor(
    buildingId,
    availableFloors,
    resolvedFloor,
  );
  const preferredStartFloorRooms =
    roomIndex.find((entry) => entry.floor === preferredStartFloor)?.rooms ?? [];
 
  let startFloor = preferredStartFloor;
  let startRoom = resolveStartRoomForFloor(
    preferredStartFloorRooms,
    buildingId,
    preferredStartFloor,
    destinationRoom,
  );
 
  // If we cannot resolve a meaningful start room on the preferred entrance floor,
  // fall back to destination-floor start behavior.
  if (!startRoom && preferredStartFloor !== resolvedFloor) {
    startFloor = resolvedFloor;
    startRoom = resolveStartRoomForFloor(
      floorRooms,
      buildingId,
      resolvedFloor,
      destinationRoom,
    );
  }
 
  if (!startRoom) {
    startFloor = resolvedFloor;
  }
 
  return {
    buildingId,
    floor: resolvedFloor,
    startFloor,
    floorSupported: true,
    destinationRoom,
    startRoom,
  };
};