From e34f5aa9fc928df25fb3867daa23666d398d1ba4 Mon Sep 17 00:00:00 2001 From: mark Date: Tue, 19 May 2026 09:44:35 +1000 Subject: [PATCH] Refactor warning messages in checkName tests --- src/lib/util/checkName.spec.ts | 56 ++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/lib/util/checkName.spec.ts b/src/lib/util/checkName.spec.ts index 5f842c268..e4538b0ef 100644 --- a/src/lib/util/checkName.spec.ts +++ b/src/lib/util/checkName.spec.ts @@ -1,5 +1,11 @@ import { checkName } from "./checkName"; +const warningMessage = (value: string): string => + "HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic ('" + value + + "'). Please ensure the name starts and ends with a letter or number. Only letters, numbers, spaces, apostrophes, " + + "and common punctuation are supported. Avoid emojis or unsupported symbols. This may prevent the accessory from being added " + + "in the Home App or cause unresponsiveness."; + describe("#checkName()", () => { let consoleWarnSpy: jest.SpyInstance; @@ -15,32 +21,27 @@ describe("#checkName()", () => { checkName("displayName", "Name", "bad name!"); expect(consoleWarnSpy).toHaveBeenCalledTimes(1); - // eslint-disable-next-line max-len - 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."); + expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage("bad name!")); }); test("Accessory Name beginning with !", async () => { checkName("displayName", "Name", "!bad name"); expect(consoleWarnSpy).toHaveBeenCalledTimes(1); - // eslint-disable-next-line max-len - 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."); + expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage("!bad name")); }); test("Accessory Name containing !", async () => { checkName("displayName", "Name", "bad ! name"); - expect(consoleWarnSpy).toHaveBeenCalledTimes(1); - // eslint-disable-next-line max-len - 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."); + expect(consoleWarnSpy).toHaveBeenCalledTimes(0); }); test("Accessory Name beginning with '", async () => { checkName("displayName", "Name", " 'bad name"); expect(consoleWarnSpy).toHaveBeenCalledTimes(1); - // eslint-disable-next-line max-len - 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."); + expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage(" 'bad name")); }); test("Accessory Name containing '", async () => { @@ -48,4 +49,41 @@ describe("#checkName()", () => { expect(consoleWarnSpy).toHaveBeenCalledTimes(0); }); + + test("Accessory Name containing common punctuation", async () => { + [ + "TV / Lounge", + "Heating & Cooling", + "Camera: Front Yard", + "Rear (Garage) Door", + "Fan_Light", + "Zone-2", + "Kitchen, Dining", + "Room; Sensor", + ].forEach(value => { + checkName("displayName", "Name", value); + + expect(consoleWarnSpy).toHaveBeenCalledTimes(0); + consoleWarnSpy.mockClear(); + }); + }); + + test("Accessory Name ending with punctuation", async () => { + [ + "TV /", + "Heating &", + "Camera:", + "Garage Door)", + "Fan_", + "Zone-", + "Kitchen,", + "Room;", + ].forEach(value => { + checkName("displayName", "Name", value); + + expect(consoleWarnSpy).toHaveBeenCalledTimes(1); + expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage(value)); + consoleWarnSpy.mockClear(); + }); + }); });