All files / client/src/components/features sim-cockpit.tsx

94.04% Statements 79/84
96.03% Branches 97/101
88.23% Functions 15/17
97.05% Lines 66/68

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                          3x                             25x 25x 20x 19x 16x 15x 13x       22x 2x 1x       6x 2x 11x         22x       22x 17x 15x       22x 13x 5x 2x 2x           22x 21x 19x       22x 21x 20x 19x         25x 22x                         66x                                     44x                                                     3x                           25x 25x   25x 25x 5x 5x   20x       25x 25x 20x 20x               25x 25x 25x   25x 25x 13x       12x 12x   25x 25x 13x 13x         25x     25x   22x               22x 22x             22x                                                                                                                             3x                       3x  
import React, { useState, useEffect, useRef } from "react";
import clsx from "clsx";
import { CircleCheck, CircleX } from "lucide-react";
import { ToolbarStatusIcon } from "@/components/ui/toolbar-status-icon";
import { StatusDot } from "@/components/ui/status-dot";
import { getStatusTextClass, type ApplicationStatus } from "@/lib/status-semantics";
import type { SimulationStatus, ClientState } from "@shared/types/arduino.types";
import type { CompilationStatus } from "@/types/compilation.types";
import type { ConnectionState } from "@/lib/websocket-manager";
 
// ── Constants ─────────────────────────────────────────────────────────────────
 
/** Minimum ms any state label stays visible before switching to a new one. */
const STATE_MIN_MS = 600;
 
// ── Pure helpers ──────────────────────────────────────────────────────────────
 
function deriveClientState(
  simulationStatus: SimulationStatus,
  compilationStatus: CompilationStatus,
  pendingExternalStart: boolean = false,
): ClientState {
  // pendingExternalStart means START_SIMULATION was received before the WS
  // connected (or before the backend was reachable).  The instance is waiting
  // for the compile + WS handshake phase — NOT for a simulation runner slot.
  // Without this check, simulationStatus === "queued" (set client-side by
  // handleExternalStartSimulation) would make the badge show QUEUED_FOR_SIMULATION
  // while the instance is actually queued for compilation.
  Iif (pendingExternalStart) return "QUEUED_FOR_COMPILING";
  if (compilationStatus === "compiling") return "COMPILING";
  if (simulationStatus === "queued") return "QUEUED_FOR_SIMULATION";
  if (simulationStatus === "running") return "RUNNING";
  if (simulationStatus === "paused") return "PAUSED";
  if (compilationStatus === "error") return "ERROR";
  return "IDLE";
}
 
function clientStateTone(state: ClientState): ApplicationStatus {
  switch (state) {
    case "RUNNING": return "success";
    case "PAUSED": return "warning";
    case "RUNNING_STARTING":
    case "COMPILING":
    case "QUEUED_FOR_COMPILING":
    case "QUEUED_FOR_SIMULATION": return "busy";
    case "ERROR": return "error";
    default: return "idle";
  }
}
 
function clientStateColor(state: ClientState): string {
  return getStatusTextClass(clientStateTone(state));
}
 
function compilationStatusTone(status: CompilationStatus): ApplicationStatus {
  if (status === "compiling") return "busy";
  if (status === "error") return "error";
  return "success";
}
 
function compilationStatusLabel(status: CompilationStatus): string {
  switch (status) {
    case "ready": return "HTTP ready";
    case "compiling": return "HTTP compiling";
    case "success": return "HTTP success";
    case "error": return "HTTP error";
  }
}
 
/** WS status is based on connection state only, not simulation telemetry. */
function wsStatusTone(wsState: ConnectionState, hasEverConnected: boolean): ApplicationStatus {
  if (wsState === "connected") return "success";
  if (wsState === "connecting" || wsState === "reconnecting") return "busy";
  return hasEverConnected ? "error" : "idle";
}
 
function wsStatusLabel(wsState: ConnectionState, hasEverConnected: boolean): string {
  if (wsState === "connected") return "WebSocket connected";
  if (wsState === "connecting") return "WebSocket connecting";
  if (wsState === "reconnecting") return "WebSocket reconnecting";
  return hasEverConnected ? "WebSocket disconnected" : "WebSocket not connected";
}
 
/** True when WS previously connected but is now disconnected/lost. */
function isWsError(wsState: ConnectionState, hasEverConnected: boolean): boolean {
  if (wsState === "connected" || wsState === "connecting" || wsState === "reconnecting") return false;
  return hasEverConnected;
}
 
// ── Sub-components ────────────────────────────────────────────────────────────
 
interface StatCellProps {
  readonly label: string;
  readonly value: React.ReactNode;
  readonly valueClass?: string;
}
 
/** A compact 2-row stat cell: dim label on top, bright value below. */
function StatCell({ label, value, valueClass }: StatCellProps) {
  return (
    <div className="flex flex-col items-start leading-tight">
      <span
        className="uppercase tracking-wider text-cyan-500/50 whitespace-nowrap"
        style={{ fontSize: "calc(9px * var(--ui-font-scale))" }}
      >
        {label}
      </span>
      <span
        className={clsx("font-bold font-mono whitespace-nowrap", valueClass ?? "text-white/50")}
        style={{ fontSize: "calc(11px * var(--ui-font-scale))" }}
      >
        {value}
      </span>
    </div>
  );
}
 
function ColSep() {
  return <div className="w-px h-5 bg-white/10 self-center mx-0.5 shrink-0" />;
}
 
// ── Component interface ───────────────────────────────────────────────────────
 
interface SimCockpitProps {
  batchStats?: unknown;
  simulationStatus?: SimulationStatus;
  compilationStatus?: CompilationStatus;
  workerIndex?: number;
  workerTotal?: number;
  backendReachable?: boolean;
  isConnected?: boolean;
  wsConnectionState?: ConnectionState;
  wsHasEverConnected?: boolean;
  baudRate?: number;
  debugMode?: boolean;
  /** When true, a START_SIMULATION arrived before the WS connected; the instance
   *  is waiting for compilation, not for a simulation runner slot. Without this
   *  flag deriveClientState incorrectly shows QUEUED_FOR_SIMULATION instead of
   *  QUEUED_FOR_COMPILING because simulationStatus is set to "queued" client-side
   *  by handleExternalStartSimulation. */
  pendingExternalStart?: boolean;
  /** @deprecated kept for prop compatibility; no longer used for WS dot logic */
  serverStatus?: unknown;
}
 
export const SimCockpit: React.FC<SimCockpitProps> = React.memo(({
  simulationStatus = "idle",
  compilationStatus = "ready",
  backendReachable = true,
  wsConnectionState = "disconnected",
  wsHasEverConnected = false,
  workerIndex,
  workerTotal,
  debugMode = false,
  pendingExternalStart = false,
}) => {
 
  // ── Compilation dot visual delay ───────────────────────────────────────
  // Keep the blue dot visible for at least STATE_MIN_MS even on fast compiles.
  const [visualCompStatus, setVisualCompStatus] = useState<CompilationStatus>(compilationStatus);
  const httpTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
 
  useEffect(() => {
    if (compilationStatus === "compiling") {
      Iif (httpTimerRef.current) clearTimeout(httpTimerRef.current);
      setVisualCompStatus("compiling");
    } else {
      httpTimerRef.current = setTimeout(() => {
        setVisualCompStatus(compilationStatus);
      }, STATE_MIN_MS);
    }
    return () => {
      if (httpTimerRef.current) {
        clearTimeout(httpTimerRef.current);
        httpTimerRef.current = null;
      }
    };
  }, [compilationStatus]);
 
  // ── Client state visual delay ──────────────────────────────────────────
  // Show active states immediately; delay the downgrade back to IDLE so it
  // stays readable for at least STATE_MIN_MS.
  const clientState = deriveClientState(simulationStatus, compilationStatus, pendingExternalStart);
  const [visualClientState, setVisualClientState] = useState<ClientState>(clientState);
  const clientStateTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
 
  useEffect(() => {
    if (clientState === "IDLE") {
      clientStateTimerRef.current = setTimeout(() => {
        setVisualClientState(clientState);
      }, STATE_MIN_MS);
    } else {
      Iif (clientStateTimerRef.current) clearTimeout(clientStateTimerRef.current);
      setVisualClientState(clientState);
    }
    return () => {
      if (clientStateTimerRef.current) {
        clearTimeout(clientStateTimerRef.current);
        clientStateTimerRef.current = null;
      }
    };
  }, [clientState]);
 
  const wsError = isWsError(wsConnectionState, wsHasEverConnected);
 
  // ── Debug mode: 3-group status row ───────────────────────────────────
  if (debugMode) {
    // Compile slot: only visible while a compilation is in progress.
    const compileSlotVal = visualCompStatus === "compiling"
      && !wsError
      && workerIndex !== undefined
      && workerTotal !== undefined
      ? `#${workerIndex + 1}/${workerTotal}`
      : null;
 
    // Simulation runner: only visible while simulation is active (running/paused/queued).
    const simActive = simulationStatus === "running" || simulationStatus === "paused" || simulationStatus === "queued";
    const simSlotVal = simActive
      && !wsError
      && workerIndex !== undefined
      && workerTotal !== undefined
      ? `#${workerIndex + 1}/${workerTotal}`
      : null;
 
    return (
      <div
        className="hidden lg:flex items-center gap-2 text-[10px] font-medium"
        data-testid="sim-cockpit-debug"
      >
        {/* GROUP 1: CLIENT state */}
        <StatCell
          label="CLIENT"
          value={<span data-testid="client-state-badge">{visualClientState}</span>}
          valueClass={clientStateColor(visualClientState)}
        />
 
        <ColSep />
 
        {/* GROUP 2: COMPILATION — HTTP dot + slot (slot only while compiling) */}
        <StatCell
          label="COMPILATION"
          value={(
            <span className="flex items-center gap-1">
              <span className="text-white/50">HTTP:</span>
              <StatusDot
                status={compilationStatusTone(visualCompStatus)}
                label={compilationStatusLabel(visualCompStatus)}
                pulse={visualCompStatus === "compiling"}
              />
              {compileSlotVal && (
                <>
                  <span className="text-white/30">|</span>
                  <span className="text-white/50">SLOT:</span>
                  <span className="text-white/50">{compileSlotVal}</span>
                </>
              )}
            </span>
          )}
        />
 
        <ColSep />
 
        {/* GROUP 3: SIMULATION — WS dot + mode + runner (mode/runner only while active) */}
        <StatCell
          label="SIMULATION"
          value={(
            <span className="flex items-center gap-1">
              <span className="text-white/50">WS:</span>
              <StatusDot
                status={wsStatusTone(wsConnectionState, wsHasEverConnected)}
                label={wsStatusLabel(wsConnectionState, wsHasEverConnected)}
                pulse={wsConnectionState === "connecting" || wsConnectionState === "reconnecting"}
              />
              {!wsError && simSlotVal && (
                <>
                  <span className="text-white/30">|</span>
                  <span className="text-cyan-300 font-bold font-mono whitespace-nowrap">{simSlotVal}</span>
                </>
              )}
            </span>
          )}
        />
      </div>
    );
  }
 
  // ── Normal mode: icon-only server status ─────────────────────────────
  return (
    <ToolbarStatusIcon
      className="hidden lg:flex"
      icon={backendReachable
        ? <CircleCheck aria-hidden="true" />
        : <CircleX aria-hidden="true" />}
      label={backendReachable ? "Server connected" : "Server offline"}
      status={backendReachable ? "success" : "error"}
    />
  );
});
 
SimCockpit.displayName = "SimCockpit";