From af58aa9c81cdab7adca1b1bca61f0a5c08f07674 Mon Sep 17 00:00:00 2001 From: fullsend-code <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Sun, 6 Sep 2026 06:43:44 +0000 Subject: [PATCH] test(#4594): add threshold validation tests for scorecard constants Add unit tests that call validateThresholdNumberIntervals for each built-in ThresholdConfig to assert their correctness at test time, preventing invalid threshold definitions from breaking the scorecard plugin at startup. Tests added for: - PERCENTAGE_THRESHOLDS (code-coverage) - DEPENDABOT_THRESHOLDS (dependabot) - OPENSSF_THRESHOLDS (openssf) - SONARQUBE_NUMBER_THRESHOLDS (sonarqube, all 11 metrics) - DEFAULT_NUMBER_THRESHOLDS (scorecard-common, in scorecard-node) Export PERCENTAGE_THRESHOLDS from CodeCoverageMetricProvider.ts so the test file can import it directly. This is not re-exported from the package public API. Closes #4594 --- .../CodeCoverageConfig.test.ts | 26 +++++++++++++++++++ .../CodeCoverageMetricProvider.ts | 2 +- .../metricProviders/DependabotConfig.test.ts | 26 +++++++++++++++++++ .../src/metricProviders/OpenSSFConfig.test.ts | 26 +++++++++++++++++++ .../metricProviders/SonarQubeConfig.test.ts | 13 ++++++++++ .../validateThresholdNumberIntervals.test.ts | 12 +++++++++ 6 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 workspaces/scorecard/plugins/scorecard-backend-module-code-coverage/src/metricProviders/CodeCoverageConfig.test.ts create mode 100644 workspaces/scorecard/plugins/scorecard-backend-module-dependabot/src/metricProviders/DependabotConfig.test.ts create mode 100644 workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/metricProviders/OpenSSFConfig.test.ts diff --git a/workspaces/scorecard/plugins/scorecard-backend-module-code-coverage/src/metricProviders/CodeCoverageConfig.test.ts b/workspaces/scorecard/plugins/scorecard-backend-module-code-coverage/src/metricProviders/CodeCoverageConfig.test.ts new file mode 100644 index 00000000000..4a085a34f85 --- /dev/null +++ b/workspaces/scorecard/plugins/scorecard-backend-module-code-coverage/src/metricProviders/CodeCoverageConfig.test.ts @@ -0,0 +1,26 @@ +/* + * Copyright Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { validateThresholdNumberIntervals } from '@red-hat-developer-hub/backstage-plugin-scorecard-node'; +import { PERCENTAGE_THRESHOLDS } from './CodeCoverageMetricProvider'; + +describe('CodeCoverageMetricProvider thresholds', () => { + it('PERCENTAGE_THRESHOLDS has valid number intervals', () => { + expect(() => + validateThresholdNumberIntervals(PERCENTAGE_THRESHOLDS.rules, 'number'), + ).not.toThrow(); + }); +}); diff --git a/workspaces/scorecard/plugins/scorecard-backend-module-code-coverage/src/metricProviders/CodeCoverageMetricProvider.ts b/workspaces/scorecard/plugins/scorecard-backend-module-code-coverage/src/metricProviders/CodeCoverageMetricProvider.ts index 977643140bb..ef3bd9ed6d6 100644 --- a/workspaces/scorecard/plugins/scorecard-backend-module-code-coverage/src/metricProviders/CodeCoverageMetricProvider.ts +++ b/workspaces/scorecard/plugins/scorecard-backend-module-code-coverage/src/metricProviders/CodeCoverageMetricProvider.ts @@ -105,7 +105,7 @@ export const CODE_COVERAGE_AGGREGATE_KEYS: Record< branchMissed: { section: 'branch', field: 'missed' }, }; -const PERCENTAGE_THRESHOLDS: ThresholdConfig = { +export const PERCENTAGE_THRESHOLDS: ThresholdConfig = { rules: [ { key: 'success', expression: '>80' }, { key: 'warning', expression: '50-80' }, diff --git a/workspaces/scorecard/plugins/scorecard-backend-module-dependabot/src/metricProviders/DependabotConfig.test.ts b/workspaces/scorecard/plugins/scorecard-backend-module-dependabot/src/metricProviders/DependabotConfig.test.ts new file mode 100644 index 00000000000..21c9519b615 --- /dev/null +++ b/workspaces/scorecard/plugins/scorecard-backend-module-dependabot/src/metricProviders/DependabotConfig.test.ts @@ -0,0 +1,26 @@ +/* + * Copyright Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { validateThresholdNumberIntervals } from '@red-hat-developer-hub/backstage-plugin-scorecard-node'; +import { DEPENDABOT_THRESHOLDS } from './DependabotConfig'; + +describe('DependabotConfig thresholds', () => { + it('DEPENDABOT_THRESHOLDS has valid number intervals', () => { + expect(() => + validateThresholdNumberIntervals(DEPENDABOT_THRESHOLDS.rules, 'number'), + ).not.toThrow(); + }); +}); diff --git a/workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/metricProviders/OpenSSFConfig.test.ts b/workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/metricProviders/OpenSSFConfig.test.ts new file mode 100644 index 00000000000..ef0c8c9264c --- /dev/null +++ b/workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/metricProviders/OpenSSFConfig.test.ts @@ -0,0 +1,26 @@ +/* + * Copyright Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { validateThresholdNumberIntervals } from '@red-hat-developer-hub/backstage-plugin-scorecard-node'; +import { OPENSSF_THRESHOLDS } from './OpenSSFConfig'; + +describe('OpenSSFConfig thresholds', () => { + it('OPENSSF_THRESHOLDS has valid number intervals', () => { + expect(() => + validateThresholdNumberIntervals(OPENSSF_THRESHOLDS.rules, 'number'), + ).not.toThrow(); + }); +}); diff --git a/workspaces/scorecard/plugins/scorecard-backend-module-sonarqube/src/metricProviders/SonarQubeConfig.test.ts b/workspaces/scorecard/plugins/scorecard-backend-module-sonarqube/src/metricProviders/SonarQubeConfig.test.ts index eb2aa79de94..4f36c2cae75 100644 --- a/workspaces/scorecard/plugins/scorecard-backend-module-sonarqube/src/metricProviders/SonarQubeConfig.test.ts +++ b/workspaces/scorecard/plugins/scorecard-backend-module-sonarqube/src/metricProviders/SonarQubeConfig.test.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { validateThresholdNumberIntervals } from '@red-hat-developer-hub/backstage-plugin-scorecard-node'; import { sonarqubeEntity } from '../../__fixtures__/sonarqubeEntity'; import { parseProjectKeyAnnotation, @@ -74,4 +75,16 @@ describe('SONARQUBE_NUMBER_THRESHOLDS', () => { ).toBeGreaterThan(0); }, ); + + it.each(SONARQUBE_NUMBER_METRICS)( + '%s thresholds have valid number intervals', + metricId => { + expect(() => + validateThresholdNumberIntervals( + SONARQUBE_NUMBER_THRESHOLDS[metricId].rules, + 'number', + ), + ).not.toThrow(); + }, + ); }); diff --git a/workspaces/scorecard/plugins/scorecard-node/src/utils/thresholds/intervals/validateThresholdNumberIntervals.test.ts b/workspaces/scorecard/plugins/scorecard-node/src/utils/thresholds/intervals/validateThresholdNumberIntervals.test.ts index 0ca481ef9f1..86b3c52c874 100644 --- a/workspaces/scorecard/plugins/scorecard-node/src/utils/thresholds/intervals/validateThresholdNumberIntervals.test.ts +++ b/workspaces/scorecard/plugins/scorecard-node/src/utils/thresholds/intervals/validateThresholdNumberIntervals.test.ts @@ -15,6 +15,7 @@ */ import type { ThresholdRule } from '@red-hat-developer-hub/backstage-plugin-scorecard-common'; +import { DEFAULT_NUMBER_THRESHOLDS } from '@red-hat-developer-hub/backstage-plugin-scorecard-common'; import { ThresholdConfigFormatError } from '../../../errors'; import { validateThresholdNumberIntervals } from './validateThresholdNumberIntervals'; @@ -166,6 +167,17 @@ describe('validateThresholdNumberIntervals', () => { }); }); + describe('built-in threshold constants', () => { + it('DEFAULT_NUMBER_THRESHOLDS has valid number intervals', () => { + expect(() => + validateThresholdNumberIntervals( + DEFAULT_NUMBER_THRESHOLDS.rules, + 'number', + ), + ).not.toThrow(); + }); + }); + describe('skips coverage check when applicable', () => { it('should not throw when there is only one number rule', () => { const rules: ThresholdRule[] = [{ key: 'success', expression: '<10' }];