All files / testee testee.service.ts

94.11% Statements 32/34
80% Branches 4/5
92.3% Functions 12/13
93.75% Lines 30/32

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 803x 3x   3x       3x   10x 10x   10x           10x   10x     16x       1x   1x 1x     1x       1x       3x 1x   2x 2x   2x 2x 2x   2x     1x     1x             1x 4x   8x 1x               1x      
import { Injectable, Logger } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { Testee } from './testee.interface';
import { WebsocketGateway } from '../common/websocket.gateway';
import { Command } from '../command/command.interface';
 
@Injectable()
export class TesteeService {
  constructor(
    private readonly websocketGateway: WebsocketGateway,
    private readonly http: HttpService,
  ) {
    this.websocketGateway.getDisconnectionObservable().subscribe((disconnected: string) => {
      this.notifyDisconnection(disconnected);
      this.removeTestee(disconnected);
    });
  }
 
  private readonly logger = new Logger(TesteeService.name);
 
  private testees: { [token: string]: Testee } = {};
 
  addTestee(testee: Testee): void {
    this.testees[testee.token] = testee;
  }
 
  removeTestee(testeeToken: string): void {
    this.logger.log(`remove testee: ${testeeToken}`);
 
    if (typeof this.testees[testeeToken] !== 'undefined') {
      delete this.testees[testeeToken];
    }
 
    this.websocketGateway.disconnectClient(testeeToken);
  }
 
  getTestees(): Testee[] {
    return Object.values(this.testees);
  }
 
  notifyDisconnection(testeeToken: string): void {
    if (typeof this.testees[testeeToken] === 'undefined') {
      return;
    }
    if (this.testees[testeeToken].disconnectNotificationUri) {
      const uri = new URL(this.testees[testeeToken].disconnectNotificationUri);
 
      const disconnectNotificationUri = this.testees[testeeToken].disconnectNotificationUri.replace(uri.search, '');
      const testMode = uri.searchParams.get('testMode');
      const config = testMode ? { headers: { testMode } } : {};
 
      this.http.post(this.testees[testeeToken].disconnectNotificationUri, {}, config)
        .subscribe(
          () => {
            this.logger.log(`sent connection-lost signal to ${disconnectNotificationUri}`);
          },
          error => {
            this.logger.warn(`could not send connection-lost signal to ${disconnectNotificationUri}: ${error.message}`);
          }
        );
    }
  }
 
  broadcastCommandToTestees(command: Command, testIds: number[]) : void {
    testIds.forEach((testId => {
      this.websocketGateway.broadcastToRegistered(
        Object.values(this.testees)
          .filter(testee => testee.testId === testId)
          .map(testee => testee.token),
        'commands',
        [command]
      );
    }));
  }
 
  clean(): void {
    this.testees = {};
  }
}