All files / modules/70-pipeline/pages/execution/ExecutionTestView TestsUtils.ts

82.52% Statements 85/103
71.67% Branches 86/120
75% Functions 9/12
82.29% Lines 79/96

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                    1x       1x 164x 164x   164x 164x                       1x 1x 1x 1x 1x 1x 1x     1x             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x     1x                       1x 1x 1x   1x   1x         1x                   1x             96x   96x 89x     96x 89x   96x     1x                                     7x 7x 7x 7x 7x       7x 7x 7x           7x         7x 7x                 7x   7x 7x           7x       7x     7x 7x       7x     7x 7x       1x                     14x 2x 12x 2x 10x 8x 2x     2x     1x                                 14x          
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import type { SetStateAction, Dispatch } from 'react'
import type { GetDataError } from 'restful-react'
import type { SelectOption } from '@wings-software/uicore'
import { uniqWith, isEqual, orderBy } from 'lodash-es'
import type { StepInfo, Error } from 'services/ti-service'
import type { GraphLayoutNode } from 'services/pipeline-ng'
 
export const renderFailureRate = (failureRate: number): number => {
  let scale = 1
  let value = failureRate
 
  Eif (failureRate === 0 || Math.round(failureRate * 100) > 0) {
    return Math.round(failureRate * 100)
  }
 
  while (value < 10) {
    scale *= 10
    value *= 10
  }
  const valueScaled = Math.round(value) / scale
  const exceeds4DecimalPlaces = valueScaled.toString().split('.')?.[1]?.length > 4
  return exceeds4DecimalPlaces ? Number(valueScaled.toFixed(4)) : valueScaled
}
 
export enum SortByKey {
  FAILURE_RATE = 'fail_pct',
  FAILED_TESTS = 'failed_tests',
  PASSED_TESTS = 'passed_tests',
  SKIPPED_TESTS = 'skipped_tests',
  DURATION_MS = 'duration_ms',
  TOTAL_TESTS = 'total_tests'
}
 
export const TestStatus = {
  PASSED: 'passed',
  SKIPPED: 'skipped',
  ERROR: 'error',
  FAILED: 'failed'
}
 
enum ExecutionStatus {
  RUNNING = 'running',
  FAILED = 'failed',
  NOTSTARTED = 'notstarted',
  EXPIRED = 'expired',
  ABORTED = 'aborted',
  QUEUED = 'queued',
  PAUSED = 'paused',
  WAITING = 'waiting',
  SUCCESS = 'success',
  SUSPENDED = 'suspended',
  SKIPPED = 'skipped'
}
 
/* eslint-disable @typescript-eslint/no-shadow */
export enum UI {
  TIAndReports,
  TI,
  Reports,
  ZeroState,
  LoadingState
}
 
export const isExecutionComplete = (status: string) => {
  const _status = (status || '').toLowerCase()
 
  return (
    _status === ExecutionStatus.SUCCESS ||
    _status === ExecutionStatus.FAILED ||
    _status === ExecutionStatus.EXPIRED ||
    _status === ExecutionStatus.ABORTED ||
    _status === ExecutionStatus.SKIPPED
  )
}
 
export const CALL_GRAPH_WIDTH = 360
export const CALL_GRAPH_HEIGHT = 360
export const CALL_GRAPH_API_LIMIT = 75
 
export const AllOption = { label: 'All', value: 'AGGREGATE_ALL_TEST_REPORTS' }
 
export const AllStagesOption = {
  label: `${AllOption.label} Stages`,
  value: AllOption.value
}
 
export const AllStepsOption = {
  label: `${AllOption.label} Steps`,
  value: AllOption.value
}
 
interface OptionalQueryParamKeys {
  stageId?: string
  stepId?: string
}
 
export const getOptionalQueryParamKeys = ({
  stageId,
  stepId
}: {
  stageId?: string
  stepId?: string
}): OptionalQueryParamKeys => {
  const optionalKeys: OptionalQueryParamKeys = {}
 
  if (stageId !== AllOption.value && stageId) {
    optionalKeys.stageId = stageId
  }
 
  if (stepId !== AllOption.value && stepId) {
    optionalKeys.stepId = stepId
  }
  return optionalKeys
}
 
export const setInitialStageAndSteps = ({
  reportInfoData,
  testInfoData,
  context,
  setStepIdOptionsFromStageKeyMap,
  setSelectedStageId,
  setSelectedStepId,
  setStageIdOptions,
  setStepIdOptions
}: {
  reportInfoData: StepInfo[]
  testInfoData: StepInfo[]
  context?: any
  setStepIdOptionsFromStageKeyMap: Dispatch<SetStateAction<{ [key: string]: SelectOption[] }>>
  setSelectedStageId: Dispatch<SetStateAction<SelectOption | undefined>>
  setSelectedStepId: Dispatch<SetStateAction<SelectOption | undefined>>
  setStageIdOptions: Dispatch<SetStateAction<SelectOption[]>>
  setStepIdOptions: Dispatch<SetStateAction<SelectOption[]>>
}): void => {
  const uniqItems = uniqWith([...reportInfoData, ...testInfoData], isEqual)
  let uniqueStageIdOptions: SelectOption[] | any = [] // any includes additionally index for ordering below
  const uniqueStepIdOptionsFromStageKeyMap: { [key: string]: SelectOption[] | any } = {}
  const pipelineOrderedStagesMap: { [key: string]: number } = {}
  Array.from(context?.pipelineStagesMap?.values() || {})?.forEach(
    (stage, index) => (pipelineOrderedStagesMap[`${(stage as GraphLayoutNode).nodeIdentifier}`] = index)
  )
 
  uniqItems.forEach(({ stage, step }) => {
    Eif (stage && !uniqueStageIdOptions.some((option: { value: string }) => option.value === stage)) {
      uniqueStageIdOptions.push({
        label: `Stage: ${stage}`,
        value: stage,
        index: typeof stage === 'string' && pipelineOrderedStagesMap[stage]
      })
    }
    Iif (stage && Array.isArray(uniqueStepIdOptionsFromStageKeyMap?.[stage])) {
      uniqueStepIdOptionsFromStageKeyMap[stage].push({
        label: `Step: ${step}`,
        value: step
      })
    } else Eif (stage && step) {
      uniqueStepIdOptionsFromStageKeyMap[stage] = [
        {
          label: `Step: ${step}`,
          value: step
        }
      ]
    }
  })
 
  setStepIdOptionsFromStageKeyMap(uniqueStepIdOptionsFromStageKeyMap)
 
  let selectedStageIndex = 0
  Iif (uniqueStageIdOptions.length > 1) {
    uniqueStageIdOptions = orderBy(uniqueStageIdOptions, 'index')
    uniqueStageIdOptions.unshift(AllStagesOption)
    selectedStageIndex = 1
    setSelectedStageId(uniqueStageIdOptions[1])
  } else {
    setSelectedStageId(uniqueStageIdOptions[0])
  }
 
  const selectedStepOptions =
    typeof selectedStageIndex !== 'undefined' &&
    uniqueStepIdOptionsFromStageKeyMap[uniqueStageIdOptions[selectedStageIndex]?.value as string]
 
  Eif (selectedStepOptions?.length) {
    Iif (selectedStepOptions?.length > 1) {
      selectedStepOptions.unshift(AllStepsOption)
      setSelectedStepId(selectedStepOptions[1])
    } else {
      setSelectedStepId(selectedStepOptions[0])
    }
 
    setStageIdOptions(uniqueStageIdOptions)
    setStepIdOptions(selectedStepOptions)
  }
}
 
export const getUIType = ({
  reportSummaryHasTests,
  testOverviewHasTests,
  reportInfoLoading,
  testInfoLoading
}: {
  reportSummaryHasTests: boolean
  testOverviewHasTests: boolean
  reportInfoLoading: boolean
  testInfoLoading: boolean
}): UI => {
  if (reportSummaryHasTests && testOverviewHasTests) {
    return UI.TIAndReports
  } else if (!reportSummaryHasTests && testOverviewHasTests) {
    return UI.TI
  } else if (reportSummaryHasTests && !testOverviewHasTests) {
    return UI.Reports
  } else Iif (reportInfoLoading || testInfoLoading) {
    return UI.LoadingState
  }
  return UI.ZeroState
}
 
export const getError = ({
  reportInfoData,
  reportSummaryError,
  serviceTokenError,
  testInfoData,
  testOverviewError,
  reportInfoError,
  testInfoError
}: {
  reportInfoData?: StepInfo[] | null
  reportSummaryError: GetDataError<Error> | null
  serviceTokenError: GetDataError<Error> | null
  testInfoData?: StepInfo[] | null
  testOverviewError: GetDataError<Error> | null
  reportInfoError: GetDataError<Error> | null
  testInfoError: GetDataError<Error> | null
}): GetDataError<Error> | null =>
  (reportInfoData && reportInfoData?.length > 0 && reportSummaryError) ||
  serviceTokenError ||
  (testInfoData && testInfoData?.length > 0 && testOverviewError) ||
  reportInfoError ||
  testInfoError