go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/frontend/ui/src/testing_tools/mocks/metrics_mock.ts (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import fetchMock from 'fetch-mock-jest';
    16  
    17  import { ListProjectMetricsRequest, ListProjectMetricsResponse, ProjectMetric } from '@/proto/go.chromium.org/luci/analysis/proto/v1/metrics.pb';
    18  
    19  export const getMockMetricsList = (project: string): ProjectMetric[] => {
    20    const humanClsFailedPresubmitMetric : ProjectMetric = {
    21      name: 'projects/' + project + '/metrics/human-cls-failed-presubmit',
    22      metricId: 'human-cls-failed-presubmit',
    23      humanReadableName: 'User Cls Failed Presubmit',
    24      description: 'User Cls Failed Presubmit Description',
    25      isDefault: true,
    26      sortPriority: 30,
    27    };
    28    const criticalFailuresExonerated : ProjectMetric = {
    29      name: 'projects/' + project + '/metrics/critical-failures-exonerated',
    30      metricId: 'critical-failures-exonerated',
    31      humanReadableName: 'Presubmit-blocking Failures Exonerated',
    32      description: 'Critical Failures Exonerated Description',
    33      isDefault: true,
    34      sortPriority: 40,
    35    };
    36    const testRunsFailed : ProjectMetric = {
    37      name: 'projects/' + project + '/metrics/test-runs-failed',
    38      metricId: 'test-runs-failed',
    39      humanReadableName: 'Test Runs Failed',
    40      description: 'Test Runs Failed Description',
    41      isDefault: false,
    42      sortPriority: 20,
    43    };
    44    const failures : ProjectMetric = {
    45      name: 'projects/' + project + '/metrics/failures',
    46      metricId: 'failures',
    47      humanReadableName: 'Total Failures',
    48      description: 'Test Results Failed Description',
    49      isDefault: true,
    50      sortPriority: 10,
    51    };
    52    return [humanClsFailedPresubmitMetric, criticalFailuresExonerated, testRunsFailed, failures];
    53  };
    54  
    55  export const mockFetchMetrics = (project?: string, metrics?: ProjectMetric[]) => {
    56    if (project === undefined) {
    57      project = 'testproject';
    58    }
    59    if (metrics === undefined) {
    60      metrics = getMockMetricsList(project);
    61    }
    62    const request: ListProjectMetricsRequest = {
    63      parent: 'projects/' + project,
    64    };
    65    const response: ListProjectMetricsResponse = {
    66      metrics: metrics,
    67    };
    68  
    69    fetchMock.post({
    70      url: 'http://localhost/prpc/luci.analysis.v1.Metrics/ListForProject',
    71      body: ListProjectMetricsRequest.toJSON(request) as object,
    72    }, {
    73      headers: {
    74        'X-Prpc-Grpc-Code': '0',
    75      },
    76      body: ')]}\'\n' + JSON.stringify(ListProjectMetricsResponse.toJSON(response)),
    77    });
    78  };