-
-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement warning messages for invalid characters in names (#1009)
- Loading branch information
1 parent
5122684
commit 63ef5c2
Showing
6 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { checkName } from "./checkName"; | ||
|
||
describe("#checkName()", () => { | ||
let consoleLogSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
consoleLogSpy = jest.spyOn(console, "warn"); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleLogSpy.mockRestore(); | ||
}); | ||
|
||
test("Accessory Name ending with !", async () => { | ||
checkName("displayName", "name", "bad 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!"); | ||
}); | ||
|
||
test("Accessory Name begining with !", async () => { | ||
checkName("displayName", "name", "!bad 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!"); | ||
}); | ||
|
||
test("Accessory Name containing !", async () => { | ||
checkName("displayName", "name", "bad ! 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!"); | ||
}); | ||
|
||
test("Accessory Name begining with '", async () => { | ||
checkName("displayName", "name", "'bad 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!"); | ||
}); | ||
|
||
test("Accessory Name containing '", async () => { | ||
checkName("displayName", "name", "bad ' name"); | ||
|
||
expect(consoleLogSpy).toBeCalledTimes(0); | ||
// 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!"); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Checks that supplied field meets Apple HomeKit naming rules | ||
* https://developer.apple.com/design/human-interface-guidelines/homekit#Help-people-choose-useful-names | ||
* @private Private API | ||
*/ | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
export function checkName(displayName: string, name: string, value: any): void { | ||
const validHK = /^[a-zA-Z0-9\s'-.]+$/; // Ensure only letter, numbers, apostrophe, or dash | ||
const startWith = /^[a-zA-Z0-9]/; // Ensure only letters or numbers are at the beginning of string | ||
const endWith = /[a-zA-Z0-9]$/; // Ensure only letters or numbers are at the end of string | ||
|
||
if (!validHK.test(value) || !startWith.test(value) || !endWith.test(value)) { | ||
console.warn("HAP-NodeJS WARNING: The accessory '" + displayName + "' is getting published with the characteristic '" + | ||
name + "'" + " not following HomeKit naming rules ('" + value + "'). " + | ||
"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!"); | ||
} | ||
} |