diff --git a/.changeset/bigint-convert.md b/.changeset/bigint-convert.md new file mode 100644 index 0000000000..7b07297ad3 --- /dev/null +++ b/.changeset/bigint-convert.md @@ -0,0 +1,5 @@ +--- +'@backstage/kubernetes-react': patch +--- + +Fixed bug in string-to-integer conversion to properly handle decimal values with BigInt. diff --git a/plugins/kubernetes-react/src/utils/pod.test.tsx b/plugins/kubernetes-react/src/utils/pod.test.tsx index c48e40cd42..b27df01519 100644 --- a/plugins/kubernetes-react/src/utils/pod.test.tsx +++ b/plugins/kubernetes-react/src/utils/pod.test.tsx @@ -33,6 +33,7 @@ describe('pod', () => { [10.0, '100'], [10.1, '100'], ['10', 100.1], + ['10.0', 100.1], ])('%p out of %p gives 10%%', (current, resource) => expect(currentToDeclaredResourceToPerc(current, resource)).toBe('10%'), ); diff --git a/plugins/kubernetes-react/src/utils/pod.tsx b/plugins/kubernetes-react/src/utils/pod.tsx index 498d5c9802..bd7d8342fd 100644 --- a/plugins/kubernetes-react/src/utils/pod.tsx +++ b/plugins/kubernetes-react/src/utils/pod.tsx @@ -127,10 +127,10 @@ export const currentToDeclaredResourceToPerc = ( } const numerator: bigint = BigInt( - typeof current === 'number' ? Math.round(current) : current, + typeof current === 'number' ? Math.round(current) : Number(current), ); const denominator: bigint = BigInt( - typeof resource === 'number' ? Math.round(resource) : resource, + typeof resource === 'number' ? Math.round(resource) : Number(resource), ); return `${(numerator * BigInt(100)) / denominator}%`; diff --git a/plugins/kubernetes-react/src/utils/resource.test.ts b/plugins/kubernetes-react/src/utils/resource.test.ts new file mode 100644 index 0000000000..6f45e8b617 --- /dev/null +++ b/plugins/kubernetes-react/src/utils/resource.test.ts @@ -0,0 +1,32 @@ +import { currentToDeclaredResourceToPerc } from './resources'; + +/* + * Copyright 2025 The Backstage Authors + * + * 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. + */ +describe('currentToDeclaredResourceToPerc', () => { + it.each([ + [10, 100], + [10, '100'], + ['10', 100], + ['10', '100'], + ['10', 100.0], + [10.0, '100'], + [10.1, '100'], + ['10', 100.1], + ['10.0', 100.1], + ])('should handle with decimal values', (current, resource) => + expect(currentToDeclaredResourceToPerc(current, resource)).toBe(10), + ); +}); diff --git a/plugins/kubernetes-react/src/utils/resources.ts b/plugins/kubernetes-react/src/utils/resources.ts index 916aa3463c..d11c873c93 100644 --- a/plugins/kubernetes-react/src/utils/resources.ts +++ b/plugins/kubernetes-react/src/utils/resources.ts @@ -23,8 +23,12 @@ export const currentToDeclaredResourceToPerc = ( return Math.round((current / resource) * 100); } - const numerator: bigint = BigInt(current); - const denominator: bigint = BigInt(resource); + const numerator: bigint = BigInt( + typeof current === 'number' ? Math.round(current) : Number(current), + ); + const denominator: bigint = BigInt( + typeof resource === 'number' ? Math.round(resource) : Number(resource), + ); return Number((numerator * BigInt(100)) / denominator); };