go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/frontend/ui/src/testing_tools/mocks/test_variants_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 { 18 TestVariantIdentifier, 19 TestVariantFailureRateAnalysis, 20 QueryTestVariantFailureRateRequest, 21 QueryTestVariantFailureRateResponse, 22 QueryTestVariantStabilityRequest_TestVariantPosition, 23 QueryTestVariantStabilityRequest, 24 QueryTestVariantStabilityResponse, 25 TestVariantStabilityAnalysis, 26 TestStabilityCriteria, 27 } from '@/proto/go.chromium.org/luci/analysis/proto/v1/test_variants.pb'; 28 29 export const getMockTestVariantIdentifier = (id: string): TestVariantIdentifier => { 30 return TestVariantIdentifier.create({ 31 'testId': id, 32 }); 33 }; 34 35 export const getMockTestVariantFailureRateAnalysis = (id: string): TestVariantFailureRateAnalysis => { 36 return TestVariantFailureRateAnalysis.create({ 37 testId: id, 38 intervalStats: Object.freeze([{ 39 intervalAge: 1, 40 totalRunExpectedVerdicts: 101, 41 totalRunFlakyVerdicts: 102, 42 totalRunUnexpectedVerdicts: 103, 43 }, { 44 intervalAge: 2, 45 totalRunExpectedVerdicts: 0, 46 totalRunFlakyVerdicts: 0, 47 totalRunUnexpectedVerdicts: 0, 48 }, { 49 intervalAge: 3, 50 totalRunExpectedVerdicts: 0, 51 totalRunFlakyVerdicts: 0, 52 totalRunUnexpectedVerdicts: 0, 53 }, { 54 intervalAge: 4, 55 totalRunExpectedVerdicts: 0, 56 totalRunFlakyVerdicts: 0, 57 totalRunUnexpectedVerdicts: 0, 58 }, { 59 intervalAge: 5, 60 totalRunExpectedVerdicts: 0, 61 totalRunFlakyVerdicts: 0, 62 totalRunUnexpectedVerdicts: 0, 63 }]), 64 }); 65 }; 66 67 export const getMockTestVariantPosition = (id: string): QueryTestVariantStabilityRequest_TestVariantPosition => { 68 return QueryTestVariantStabilityRequest_TestVariantPosition.create({ 69 testId: id, 70 sources: { 71 gitilesCommit: { 72 host: 'myproject.googlesource.com', 73 project: 'myproject/src', 74 ref: 'refs/heads/mybranch', 75 commitHash: 'ff'.repeat(20), 76 position: '999999999999', 77 }, 78 }, 79 }); 80 }; 81 82 export const getMockTestVariantStabilityAnalysis = (id: string): TestVariantStabilityAnalysis => { 83 return TestVariantStabilityAnalysis.create({ 84 testId: id, 85 failureRate: { 86 isMet: true, 87 unexpectedTestRuns: 101, 88 consecutiveUnexpectedTestRuns: 102, 89 }, 90 flakeRate: { 91 isMet: false, 92 runFlakyVerdicts: 201, 93 totalVerdicts: 202, 94 startPosition: '301', 95 endPosition: '302', 96 }, 97 }); 98 }; 99 100 export const getMockTestStabilityCriteria = (): TestStabilityCriteria => { 101 return TestStabilityCriteria.create({ 102 failureRate: { 103 failureThreshold: 6, 104 consecutiveFailureThreshold: 3, 105 }, 106 flakeRate: { 107 minWindow: 100, 108 flakeRateThreshold: 0.01, 109 flakeThreshold: 2, 110 }, 111 }); 112 }; 113 114 115 export const mockQueryFailureRate = (request: QueryTestVariantFailureRateRequest, response: QueryTestVariantFailureRateResponse) => { 116 fetchMock.post({ 117 url: 'http://localhost/prpc/luci.analysis.v1.TestVariants/QueryFailureRate', 118 body: QueryTestVariantFailureRateRequest.toJSON(request) as object, 119 }, { 120 headers: { 121 'X-Prpc-Grpc-Code': '0', 122 }, 123 body: ')]}\'\n' + JSON.stringify(QueryTestVariantFailureRateResponse.toJSON(response)), 124 }, { overwriteRoutes: true }); 125 }; 126 127 export const mockQueryStability = (request: QueryTestVariantStabilityRequest, response: QueryTestVariantStabilityResponse) => { 128 fetchMock.post({ 129 url: 'http://localhost/prpc/luci.analysis.v1.TestVariants/QueryStability', 130 body: QueryTestVariantStabilityRequest.toJSON(request) as object, 131 }, { 132 headers: { 133 'X-Prpc-Grpc-Code': '0', 134 }, 135 body: ')]}\'\n' + JSON.stringify(QueryTestVariantStabilityResponse.toJSON(response)), 136 }, { overwriteRoutes: true }); 137 };