Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/lib/Characteristic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1474,7 +1530,7 @@ describe("Characteristic", () => {
expect(mock).toHaveBeenCalledTimes(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],
Expand Down
5 changes: 4 additions & 1 deletion src/lib/Characteristic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2717,7 +2717,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) {
Expand All @@ -2728,6 +2728,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;
}

Expand Down
Loading