All files / server/services simulation-state-machine.ts

100% Statements 5/5
100% Branches 4/4
100% Functions 2/2
100% Lines 4/4

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    14x                   38x 37x       2x    
export type SimulationState = "stopped" | "starting" | "running" | "paused" | "error";
 
const transitions: Record<SimulationState, readonly SimulationState[]> = {
  stopped: ["starting"],
  starting: ["stopped", "running", "error"],
  running: ["paused", "stopped", "error"],
  paused: ["running", "stopped", "error"],
  error: [],
};
 
/** Pure lifecycle transition used by all execution controllers. */
export function canTransition(from: SimulationState, to: SimulationState): boolean {
  if (from === "error") return false;
  return transitions[from].includes(to);
}
 
export function transition(from: SimulationState, to: SimulationState): SimulationState {
  return canTransition(from, to) ? to : from;
}