From d07bcb6d11c93249bf66e8c45c1a0710a796d92e Mon Sep 17 00:00:00 2001 From: Ben <43026681+bwp91@users.noreply.github.com> Date: Sat, 22 Mar 2025 22:39:41 +0000 Subject: [PATCH] fix OOC errors from `validateUserInput` on steps --- src/lib/Characteristic.spec.ts | 58 +++++++++++++++++++++++++++++++++- src/lib/Characteristic.ts | 5 ++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/lib/Characteristic.spec.ts b/src/lib/Characteristic.spec.ts index 5bf0e5994..b1c2d67d3 100644 --- a/src/lib/Characteristic.spec.ts +++ b/src/lib/Characteristic.spec.ts @@ -1268,6 +1268,62 @@ describe("Characteristic", () => { }, ); + it("should adjust maxValue based on minStep", () => { + const characteristic = createCharacteristicWithProps({ + format: Formats.INT, + minValue: 0, + maxValue: 25, + minStep: 2, + perms: [Perms.PAIRED_READ, Perms.PAIRED_WRITE], + }); + + // @ts-expect-error: private access + const adjustedMaxValue = characteristic.validateUserInput(25); + expect(adjustedMaxValue).toEqual(24); // maxValue should be adjusted to 24 + }); + + it("should quantize values based on minStep", () => { + const characteristic = createCharacteristicWithProps({ + format: Formats.INT, + minValue: 0, + maxValue: 25, + minStep: 2, + perms: [Perms.PAIRED_READ, Perms.PAIRED_WRITE], + }); + + // @ts-expect-error: private access + const quantizedValue = characteristic.validateUserInput(23); + expect(quantizedValue).toEqual(24); // value should be quantized to 24 + }); + + it("should truncate values to minValue", () => { + const characteristic = createCharacteristicWithProps({ + format: Formats.INT, + minValue: 10, + maxValue: 25, + minStep: 2, + perms: [Perms.PAIRED_READ, Perms.PAIRED_WRITE], + }); + + // @ts-expect-error: private access + const truncatedValue = characteristic.validateUserInput(5); + expect(truncatedValue).toEqual(10); // value should be truncated to minValue 10 + }); + + it("should truncate values to maxValue", () => { + const characteristic = createCharacteristicWithProps({ + format: Formats.INT, + minValue: 0, + maxValue: 25, + minStep: 2, + perms: [Perms.PAIRED_READ, Perms.PAIRED_WRITE], + }); + + // @ts-expect-error: private access + const truncatedValue = characteristic.validateUserInput(30); + expect(truncatedValue).toEqual(24); // value should be truncated to adjusted maxValue 24 + }); + it("should not round floats for Formats.FLOAT", () => { const characteristic = createCharacteristicWithProps({ format: Formats.FLOAT, @@ -1474,7 +1530,7 @@ describe("Characteristic", () => { expect(mock).toBeCalledTimes(1); }); - it("should validate data type intputs", () => { + it("should validate data type inputs", () => { const characteristic = createCharacteristicWithProps({ format: Formats.DATA, perms: [Perms.PAIRED_READ, Perms.PAIRED_WRITE], diff --git a/src/lib/Characteristic.ts b/src/lib/Characteristic.ts index b33f61426..10b291540 100644 --- a/src/lib/Characteristic.ts +++ b/src/lib/Characteristic.ts @@ -2700,7 +2700,7 @@ export class Characteristic extends EventEmitter { } const numericMin = maxWithUndefined(this.props.minValue, numericLowerBound(this.props.format)); - const numericMax = minWithUndefined(this.props.maxValue, numericUpperBound(this.props.format)); + let numericMax = minWithUndefined(this.props.maxValue, numericUpperBound(this.props.format)); let stepValue: number | undefined = undefined; if (this.props.format === Formats.FLOAT) { @@ -2711,6 +2711,9 @@ export class Characteristic extends EventEmitter { if (stepValue != null && stepValue > 0) { const minValue = this.props.minValue != null ? this.props.minValue : 0; + if (numericMax != null) { + numericMax = stepValue * Math.floor((numericMax - minValue) / stepValue) + minValue; + } value = stepValue * Math.round((value - minValue) / stepValue) + minValue; }