diff --git a/control/src/backend/cell-info.test.ts b/control/src/backend/cell-info.test.ts index d09fbe9..74d879b 100644 --- a/control/src/backend/cell-info.test.ts +++ b/control/src/backend/cell-info.test.ts @@ -179,3 +179,28 @@ describe('selectServingCell — permutation invariance (MANDATORY)', () => { } }); }); + +describe('non-finite measurements are no measurement', () => { + const NON_FINITE = [Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]; + + test('NaN and infinities never reach a reading', () => { + for (const bad of NON_FINITE) { + const reading = read({ 'physical-ci': bad, rsrp: bad, rsrq: bad, sinr: bad }); + expect(reading.pci).toBeUndefined(); + expect(reading.rsrp).toBeUndefined(); + expect(reading.rsrq).toBeUndefined(); + expect(reading.sinr).toBeUndefined(); + } + }); + + test('the serving cell does not depend on input order when one rsrp is NaN', () => { + const cells = [ + read({ ci: 'a', rsrp: Number.NaN }), + read({ ci: 'b', rsrp: -90 }), + read({ ci: 'c', rsrp: -80 }), + ]; + for (const order of permutations(cells)) { + expect(selectServingCell(order)?.cellId).toBe('c'); + } + }); +}); diff --git a/control/src/backend/cell-info.ts b/control/src/backend/cell-info.ts index 7b76d0b..b4600e8 100644 --- a/control/src/backend/cell-info.ts +++ b/control/src/backend/cell-info.ts @@ -70,6 +70,16 @@ function readServing(cell: DecodedProps): boolean { return cellType?.toLowerCase().includes('serving') ?? false; } +/** + * A numeric property, but only a real measurement. A D-Bus `d` can carry NaN or an + * infinity, and either one makes the serving-cell comparison below return NaN, which + * breaks its total order; a reading that is not a number is the same as no reading. + */ +function measurementProp(cell: DecodedProps, key: string): number | undefined { + const value = numberProp(cell, key); + return value !== undefined && Number.isFinite(value) ? value : undefined; +} + /** Normalize ONE cell's `a{sv}` dict into a `CellReading`, pinning the known keys. */ export function normalizeCellReading( cell: DecodedProps, @@ -77,11 +87,11 @@ export function normalizeCellReading( ): CellReading { const cellId = stringProp(cell, 'ci') ?? stringProp(cell, 'cell-id'); const tac = stringProp(cell, 'tac'); - const pci = numberProp(cell, 'physical-ci'); - const rsrp = numberProp(cell, 'rsrp'); - const rsrq = numberProp(cell, 'rsrq'); + const pci = measurementProp(cell, 'physical-ci'); + const rsrp = measurementProp(cell, 'rsrp'); + const rsrq = measurementProp(cell, 'rsrq'); // `sinr` ONLY — a dict carrying `snr` must never populate this field. - const sinr = numberProp(cell, 'sinr'); + const sinr = measurementProp(cell, 'sinr'); // `band` ONLY when directly supplied; never inferred. const band = stringProp(cell, 'band'); return { diff --git a/control/src/fuzz/cell-info.property.test.ts b/control/src/fuzz/cell-info.property.test.ts index 1426805..01a50e8 100644 --- a/control/src/fuzz/cell-info.property.test.ts +++ b/control/src/fuzz/cell-info.property.test.ts @@ -29,8 +29,8 @@ const KEYS = [ const variantValue = fc.oneof( fc.string({ maxLength: 12 }), fc.integer(), - // Non-finite readings are covered next to the fix that handles them. - fc.double({ noNaN: true, noDefaultInfinity: true }), + fc.double(), + fc.constantFrom(Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY), fc.boolean(), fc.array(fc.integer(), { maxLength: 3 }), ); @@ -56,7 +56,7 @@ describe('cell-info normalization', () => { expect(typeof reading.serving).toBe('boolean'); for (const key of ['pci', 'rsrp', 'rsrq', 'sinr'] as const) { const value = reading[key]; - if (value !== undefined) expect(typeof value).toBe('number'); + if (value !== undefined) expect(Number.isFinite(value)).toBe(true); } } }),