diff --git a/processor/src/adapters/badge-context.ts b/processor/src/adapters/badge-context.ts index af68b46..b393b3b 100644 --- a/processor/src/adapters/badge-context.ts +++ b/processor/src/adapters/badge-context.ts @@ -56,7 +56,7 @@ export async function createBadgeContext({ return (attempt: number): Promise => { if (attempt > 1 && catalystServers.length > 0) { - const [catalystServerUrl] = catalystServers.splice(attempt % catalystServers.length, 1) || [] + const [catalystServerUrl] = catalystServers.splice(attempt % catalystServers.length, 1) contentClientToUse = getContentClientOrDefault(catalystServerUrl) } diff --git a/processor/test/unit/utils/array.spec.ts b/processor/test/unit/utils/array.spec.ts new file mode 100644 index 0000000..62f306a --- /dev/null +++ b/processor/test/unit/utils/array.spec.ts @@ -0,0 +1,54 @@ +import { shuffleArray } from '../../../src/utils/array' + +describe('shuffleArray', () => { + it('should return an array with the same elements in a different order', () => { + const originalArray = [1, 2, 3, 4, 5] + const shuffledArray = shuffleArray([...originalArray]) + + expect(shuffledArray).toHaveLength(originalArray.length) + expect(shuffledArray).not.toEqual(originalArray) + expect(shuffledArray.sort()).toEqual(originalArray.sort()) + }) + + it('should shuffle elements randomly (mocked Math.random)', () => { + const originalArray = [1, 2, 3, 4, 5] + + // to control the shuffle + jest + .spyOn(global.Math, 'random') + .mockReturnValueOnce(0.1) + .mockReturnValueOnce(0.3) + .mockReturnValueOnce(0.7) + .mockReturnValueOnce(0.9) + .mockReturnValueOnce(0.5) + + const shuffledArray = shuffleArray([...originalArray]) + + expect(shuffledArray).not.toEqual(originalArray) + + jest.restoreAllMocks() + }) + + it('should not modify the original array', () => { + const originalArray = [1, 2, 3, 4, 5] + const arrayCopy = [...originalArray] + + shuffleArray(arrayCopy) + + expect(originalArray).toEqual([1, 2, 3, 4, 5]) + }) + + it('should handle an empty array gracefully', () => { + const emptyArray: number[] = [] + const shuffledArray = shuffleArray(emptyArray) + + expect(shuffledArray).toEqual([]) + }) + + it('should handle an array with one element gracefully', () => { + const singleElementArray = [1] + const shuffledArray = shuffleArray(singleElementArray) + + expect(shuffledArray).toEqual([1]) + }) +})