Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update characteristic naming convention warning message text and associated tests. #1054

Merged
merged 2 commits into from
Jul 4, 2024
Merged
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
72 changes: 54 additions & 18 deletions src/lib/Accessory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,14 @@ describe("Accessory", () => {
});

describe("Accessory and Service naming checks", () => {
let consoleLogSpy: jest.SpyInstance;
let consoleWarnSpy: jest.SpyInstance;

beforeEach(() => {
consoleLogSpy = jest.spyOn(console, "warn");
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
});

afterEach(() => {
consoleLogSpy.mockRestore();
consoleWarnSpy.mockRestore();
});

test("Accessory Name ending with !", async () => {
Expand All @@ -431,7 +431,7 @@ describe("Accessory", () => {

await accessoryBadName.publish(publishInfo);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'Bad Name! 7430' is getting published with the characteristic 'Name' not following HomeKit naming rules ('Bad Name! 7430'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'Bad Name!' has an invalid 'Name' characteristic ('Bad Name!'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -450,7 +450,7 @@ describe("Accessory", () => {

await accessoryBadName.publish(publishInfo);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'Bad ! Name 7430' is getting published with the characteristic 'Name' not following HomeKit naming rules ('Bad ! Name 7430'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'Bad ! Name' has an invalid 'Name' characteristic ('Bad ! Name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -468,7 +468,7 @@ describe("Accessory", () => {
};

await accessoryBadName.publish(publishInfo);
expect(consoleLogSpy).toBeCalledTimes(0);
expect(consoleWarnSpy).toBeCalledTimes(0);

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -487,9 +487,9 @@ describe("Accessory", () => {

await accessoryBadName.publish(publishInfo);
expect(accessoryBadName.displayName.startsWith(TEST_DISPLAY_NAME));
expect(consoleLogSpy).toBeCalledTimes(2);
expect(consoleWarnSpy).toBeCalledTimes(2);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory ''Bad Name 7430' is getting published with the characteristic 'Name' not following HomeKit naming rules (''Bad Name 7430'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory ''Bad Name' has an invalid 'Name' characteristic (''Bad Name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -510,7 +510,7 @@ describe("Accessory", () => {

await accessoryBadName.publish(publishInfo);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'My Bad ! Switch' is getting published with the characteristic 'Name' not following HomeKit naming rules ('My Bad ! Switch'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'My Bad ! Switch' has an invalid 'Name' characteristic ('My Bad ! Switch'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -530,9 +530,9 @@ describe("Accessory", () => {
};

await accessoryBadName.publish(publishInfo);
expect(consoleLogSpy).toBeCalledTimes(1);
expect(consoleWarnSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'My Bad Switch!' is getting published with the characteristic 'Name' not following HomeKit naming rules ('My Bad Switch!'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'My Bad Switch!' has an invalid 'Name' characteristic ('My Bad Switch!'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -552,7 +552,7 @@ describe("Accessory", () => {
};

await accessoryBadName.publish(publishInfo);
expect(consoleLogSpy).toBeCalledTimes(0);
expect(consoleWarnSpy).toBeCalledTimes(0);

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -572,9 +572,9 @@ describe("Accessory", () => {
};

await accessoryBadName.publish(publishInfo);
expect(consoleLogSpy).toBeCalledTimes(1);
expect(consoleWarnSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'My Bad Switch'' is getting published with the characteristic 'Name' not following HomeKit naming rules ('My Bad Switch''). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'My Bad Switch'' has an invalid 'Name' characteristic ('My Bad Switch''). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -594,9 +594,9 @@ describe("Accessory", () => {
};

await accessoryBadName.publish(publishInfo);
expect(consoleLogSpy).toBeCalledTimes(1);
expect(consoleWarnSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory ''My Bad Switch' is getting published with the characteristic 'Name' not following HomeKit naming rules (''My Bad Switch'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory ''My Bad Switch' has an invalid 'Name' characteristic (''My Bad Switch'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand All @@ -620,9 +620,9 @@ describe("Accessory", () => {

switchService.getCharacteristic(Characteristic.ConfiguredName).updateValue("'Bad Name");

expect(consoleLogSpy).toBeCalledTimes(1);
expect(consoleWarnSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'unknown' is getting published with the characteristic 'Configured Name' not following HomeKit naming rules (''Bad Name'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'Configured Name' has an invalid 'ConfiguredName' characteristic (''Bad Name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");

await awaitEventOnce(accessoryBadName, AccessoryEventTypes.ADVERTISED);
await accessoryBadName?.unpublish();
Expand Down Expand Up @@ -1228,6 +1228,18 @@ describe("Accessory", () => {
});

describe("handleSetCharacteristic", () => {
let consoleWarnSpy: jest.SpyInstance;

beforeEach(() => {
// Mock console.warn before each test that needs it
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
});

afterEach(() => {
// Restore console.warn after each test
consoleWarnSpy.mockRestore();
});

const testRequestResponse = async (
request: Partial<CharacteristicsWriteRequest>,
...expectedReadData: CharacteristicWriteData[]
Expand Down Expand Up @@ -1948,6 +1960,18 @@ describe("Accessory", () => {
});

describe("characteristicWarning", () => {
let consoleWarnSpy: jest.SpyInstance;

beforeEach(() => {
// Mock console.warn before each test that needs it
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
});

afterEach(() => {
// Restore console.warn after each test
consoleWarnSpy.mockRestore();
});

test("emit characteristic warning", () => {
accessory.on(AccessoryEventTypes.CHARACTERISTIC_WARNING, callback);

Expand Down Expand Up @@ -2006,6 +2030,18 @@ describe("Accessory", () => {
});

describe("deserialize", () => {
let consoleWarnSpy: jest.SpyInstance;

beforeEach(() => {
// Mock console.warn before each test that needs it
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
});

afterEach(() => {
// Restore console.warn after each test
consoleWarnSpy.mockRestore();
});

test("deserialize legacy json from homebridge", () => {
const json = JSON.parse("{\"plugin\":\"homebridge-samplePlatform\",\"platform\":\"SamplePlatform\"," +
"\"displayName\":\"2020-01-17T18:45:41.049Z\",\"UUID\":\"dc3951d8-662e-46f7-b6fe-d1b5b5e1a995\",\"category\":1," +
Expand Down
18 changes: 6 additions & 12 deletions src/lib/Accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,18 +1051,12 @@ export class Accessory extends EventEmitter {
}
};

const model = service.getCharacteristic(Characteristic.Model).value;
const serialNumber = service.getCharacteristic(Characteristic.SerialNumber).value;
const firmwareRevision = service.getCharacteristic(Characteristic.FirmwareRevision).value;
const name = service.getCharacteristic(Characteristic.Name).value;
const manufacturer = service.getCharacteristic(Characteristic.Manufacturer).value;

checkValue("Model", model);
checkValue("SerialNumber", serialNumber);
checkValue("FirmwareRevision", firmwareRevision);
checkValue("Name", name);
checkName(this.displayName, "Name", name);
checkValue("Manufacturer", manufacturer);
checkName(this.displayName, "Name", service.getCharacteristic(Characteristic.Name).value);
checkValue("FirmwareRevision", service.getCharacteristic(Characteristic.FirmwareRevision).value);
checkValue("Manufacturer", service.getCharacteristic(Characteristic.Manufacturer).value);
checkValue("Model", service.getCharacteristic(Characteristic.Model).value);
checkValue("Name", service.getCharacteristic(Characteristic.Name).value);
checkValue("SerialNumber", service.getCharacteristic(Characteristic.SerialNumber).value);
}

if (mainAccessory) {
Expand Down
39 changes: 18 additions & 21 deletions src/lib/util/checkName.spec.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
import { checkName } from "./checkName";

describe("#checkName()", () => {
let consoleLogSpy: jest.SpyInstance;
let consoleWarnSpy: jest.SpyInstance;

beforeEach(() => {
consoleLogSpy = jest.spyOn(console, "warn");
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
});

afterEach(() => {
consoleLogSpy.mockRestore();
consoleWarnSpy.mockRestore();
});

test("Accessory Name ending with !", async () => {
checkName("displayName", "Name", "bad name!");

expect(consoleLogSpy).toBeCalledTimes(1);
expect(consoleWarnSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' is getting published with the characteristic 'Name' not following HomeKit naming rules ('bad name!'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic ('bad name!'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");
});

test("Accessory Name begining with !", async () => {
test("Accessory Name beginning with !", async () => {
checkName("displayName", "Name", "!bad name");

expect(consoleLogSpy).toBeCalledTimes(1);
expect(consoleWarnSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' is getting published with the characteristic 'Name' not following HomeKit naming rules ('!bad name'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic ('!bad name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");
});

test("Accessory Name containing !", async () => {
checkName("displayName", "Name", "bad ! name");

expect(consoleLogSpy).toBeCalledTimes(1);
expect(consoleWarnSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' is getting published with the characteristic 'Name' not following HomeKit naming rules ('bad ! name'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic ('bad ! name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");
});

test("Accessory Name begining with '", async () => {
checkName("displayName", "Name", "'bad name");
test("Accessory Name beginning with '", async () => {
checkName("displayName", "Name", " 'bad name");

expect(consoleLogSpy).toBeCalledTimes(1);
expect(consoleWarnSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' is getting published with the characteristic 'Name' not following HomeKit naming rules (''bad name'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic (' 'bad name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");
});

test("Accessory Name ends with !", async () => {
checkName("displayName", "Name", "bad name!");
test("Accessory Name containing '", async () => {
checkName("displayName", "Name", "good ' name");

expect(consoleLogSpy).toBeCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleLogSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' is getting published with the characteristic 'Name' not following HomeKit naming rules ('bad name!'). Use only alphanumeric, space, and apostrophe characters, start and end with an alphabetic or numeric character, and don't include emojis. This might prevent the accessory from being added to the Home App or leading to the accessory being unresponsive!");
expect(consoleWarnSpy).toBeCalledTimes(0);
});

});
});
Loading
Loading