All files / src/app/group-monitor/test-session test-session.util.ts

73.21% Statements 41/56
78.57% Branches 33/42
100% Functions 11/11
72.72% Lines 40/55

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                            1160x       147x       147x       130x     130x                     3x 6x 6x         130x   130x 26x   104x     104x     104x     104x     104x     104x 26x   78x     78x 78x                       78x       260x 260x                                     602x                 602x 1046x 1046x 559x 559x 559x   559x 88x 88x     487x               487x 168x   319x 319x       346x      
import { TestSessionChange } from 'testcenter-common/interfaces/test-session-change.interface';
import {
  Booklet,
  BookletError, isBooklet,
  isUnit,
  Testlet,
  TestSession,
  TestSessionData,
  TestSessionSuperState,
  UnitContext
} from '../group-monitor.interfaces';
 
export class TestSessionUtil {
  static hasState(state: Record<string, unknown>, key: string, value: string | null = null): boolean {
    return ((typeof state[key] !== 'undefined') && ((value !== null) ? (state[key] === value) : true));
  }
 
  static isPaused(session: TestSession): boolean {
    return TestSessionUtil.hasState(session.data.testState, 'CONTROLLER', 'PAUSED');
  }
 
  static isLocked(session: TestSession): boolean {
    return TestSessionUtil.hasState(session.data.testState, 'status', 'locked');
  }
 
  static analyzeTestSession(session: TestSessionChange, booklet: Booklet | BookletError): TestSession {
    const current = (isBooklet(booklet) && session.unitName) ?
      TestSessionUtil.getCurrent(booklet.units, session.unitName) :
      null;
    return {
      data: session,
      state: TestSessionUtil.getSuperState(session),
      current: current && current.unit ? current : null,
      booklet,
      timeLeft: TestSessionUtil.parseJsonState(session.testState, 'TESTLETS_TIMELEFT'),
      clearedCodes: TestSessionUtil.parseJsonState(session.testState, 'TESTLETS_CLEARED_CODE')
    };
  }
 
  static stateString(state: Record<string, string>, keys: string[], glue = ''): string {
    return keys
      .map((key: string) => (TestSessionUtil.hasState(state, key) ? state[key] : null))
      .filter(value => value !== null)
      .join(glue);
  }
 
  private static getSuperState(session: TestSessionData): TestSessionSuperState {
    const state = session.testState;
 
    if (this.hasState(state, 'status', 'pending')) {
      return 'pending';
    }
    Iif (this.hasState(state, 'status', 'locked')) {
      return 'locked';
    }
    Iif (this.hasState(state, 'CONTROLLER', 'ERROR')) {
      return 'error';
    }
    Iif (this.hasState(state, 'CONTROLLER', 'TERMINATED')) {
      return 'controller_terminated';
    }
    Iif (this.hasState(state, 'CONTROLLER', 'TERMINATED_PAUSED')) {
      return 'controller_terminated';
    }
    Iif (this.hasState(state, 'CONNECTION', 'LOST')) {
      return 'connection_lost';
    }
    if (this.hasState(state, 'CONTROLLER', 'PAUSED')) {
      return 'paused';
    }
    Iif (this.hasState(state, 'FOCUS', 'HAS_NOT')) {
      return 'focus_lost';
    }
    if (TestSessionUtil.idleSinceMinutes(session) > 5) {
      return 'idle';
    }
    Iif (this.hasState(state, 'CONNECTION', 'WEBSOCKET')) {
      return 'connection_websocket';
    }
    Iif (this.hasState(state, 'CONNECTION', 'POLLING')) {
      return 'connection_polling';
    }
    return 'ok';
  }
 
  private static idleSinceMinutes(testSession: TestSessionData): number {
    return (Date.now() - testSession.timestamp * 1000) / (1000 * 60);
  }
 
  private static parseJsonState(testStateObject: Record<string, string>, key: string): Record<string, string> | null {
    if (typeof testStateObject[key] === 'undefined') {
      return null;
    }
 
    const stateValueString = testStateObject[key];
 
    try {
      return JSON.parse(stateValueString);
    } catch (error) {
      // console.warn(`state ${key} is no valid JSON`, stateValueString, error);
      return null;
    }
  }
 
  private static getCurrent(
    testlet: Testlet,
    searchUnitId: string,
    level = 0,
    context: UnitContext | null = null
  ): UnitContext {
    const result: UnitContext = context ?? {
      unit: undefined,
      parent: testlet,
      ancestor: testlet,
      indexGlobal: -1,
      indexLocal: -1,
      indexAncestor: -1
    };
 
    for (let i = 0; i < testlet.children.length; i++) {
      const child = testlet.children[i];
      if (isUnit(child)) {
        result.indexLocal += 1;
        result.indexAncestor += 1;
        result.indexGlobal += 1;
 
        if (child.id === searchUnitId) {
          result.unit = child;
          return result;
        }
      } else {
        const subResult = TestSessionUtil.getCurrent(child, searchUnitId, level + 1, {
          unit: undefined,
          parent: child,
          ancestor: level < 1 ? child : result.ancestor,
          indexGlobal: result.indexGlobal,
          indexLocal: -1,
          indexAncestor: level < 1 ? -1 : result.indexAncestor
        });
        if (subResult.unit) {
          return subResult;
        }
        result.indexGlobal = subResult.indexGlobal;
        result.indexAncestor = level < 1 ? result.indexAncestor : subResult.indexAncestor;
      }
    }
 
    return result;
  }
}