From b1729d8ddc92451b9d9807b2937ac07ef9375710 Mon Sep 17 00:00:00 2001 From: simoleo89 Date: Mon, 11 May 2026 20:37:41 +0200 Subject: [PATCH] Vitest: cover dedupeBadges with 6 cases The badge-deduplication helper was extracted from InfoStandWidgetUserView in the prior commit; it's a pure (badges[]) => badges[] function that keeps slot indices stable by replacing duplicate codes with empty strings. Coverage: - empty input - unique-only passthrough - duplicate-replaced-with-empty - falsy entries (null / undefined / '') normalized to '' - first-occurrence-wins semantics - order sensitivity (same multiset, different order -> different output) --- tests/dedupeBadges.test.ts | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/dedupeBadges.test.ts diff --git a/tests/dedupeBadges.test.ts b/tests/dedupeBadges.test.ts new file mode 100644 index 0000000..103db9d --- /dev/null +++ b/tests/dedupeBadges.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest'; +import { dedupeBadges } from '../src/api/avatar/dedupeBadges'; + +describe('dedupeBadges', () => +{ + it('returns an empty array for an empty input', () => + { + expect(dedupeBadges([])).toEqual([]); + }); + + it('preserves unique badges in slot order', () => + { + expect(dedupeBadges([ 'a', 'b', 'c' ])).toEqual([ 'a', 'b', 'c' ]); + }); + + it('replaces duplicate slots with empty strings to preserve slot indices', () => + { + expect(dedupeBadges([ 'a', 'b', 'a', 'c' ])).toEqual([ 'a', 'b', '', 'c' ]); + }); + + it('normalizes falsy entries (null, undefined, "") to empty string', () => + { + // server sometimes returns null/undefined for unused slots + const input = [ 'a', null as unknown as string, '', undefined as unknown as string, 'b' ]; + + expect(dedupeBadges(input)).toEqual([ 'a', '', '', '', 'b' ]); + }); + + it('only keeps the FIRST occurrence of each unique code', () => + { + expect(dedupeBadges([ 'a', 'a', 'a' ])).toEqual([ 'a', '', '' ]); + }); + + it('is order-sensitive: identical multisets but different orderings yield different outputs', () => + { + expect(dedupeBadges([ 'a', 'b', 'a' ])).toEqual([ 'a', 'b', '' ]); + expect(dedupeBadges([ 'b', 'a', 'a' ])).toEqual([ 'b', 'a', '' ]); + }); +});