diff --git a/src/config.ts b/src/config.ts index 1ed1482..3d40769 100644 --- a/src/config.ts +++ b/src/config.ts @@ -22,7 +22,7 @@ export default function () { deviceDiscovery: (process.env["BONOB_SONOS_DEVICE_DISCOVERY"] || "true") == "true", seedHost: process.env["BONOB_SONOS_SEED_HOST"], - autoRegister: process.env["BONOB_SONOS_AUTO_REGISTER"] == "true", + autoRegister: (process.env["BONOB_SONOS_AUTO_REGISTER"] || "false") == "true", sid: Number(process.env["BONOB_SONOS_SERVICE_ID"] || "246"), }, navidrome: { diff --git a/tests/config.test.ts b/tests/config.test.ts index b807893..dd8cfb5 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -1,3 +1,4 @@ +import { hostname } from "os"; import config from "../src/config"; describe("config", () => { @@ -12,7 +13,7 @@ describe("config", () => { process.env = OLD_ENV; }); - function describeBooleanConfigValue(name : string, envVar: string) { + function describeBooleanConfigValue(name: string, envVar: string, expectedDefault: boolean, propertyGetter: (config: any) => any) { describe(name, () => { function expecting({ value, @@ -21,21 +22,95 @@ describe("config", () => { value: string; expected: boolean; }) { - describe(`when value is ${value}`, () => { + describe(`when value is '${value}'`, () => { it(`should be ${expected}`, () => { process.env[envVar] = value; - expect((config() as any)[name]).toEqual(expected); + expect(propertyGetter(config())).toEqual(expected); }); }); } - - expecting({ value: "", expected: true }); + + expecting({ value: "", expected: expectedDefault }); expecting({ value: "true", expected: true }); expecting({ value: "false", expected: false }); expecting({ value: "foo", expected: false }); }); }; - - describeBooleanConfigValue("scrobbleTracks", "BONOB_SCROBBLE_TRACKS"); - describeBooleanConfigValue("reportNowPlaying", "BONOB_REPORT_NOW_PLAYING"); + + describe("secret", () => { + it("should default to bonob", () => { + expect(config().secret).toEqual("bonob"); + }); + + it("should be overridable", () => { + process.env["BONOB_SECRET"] = "new secret"; + expect(config().secret).toEqual("new secret"); + }); + }); + + describe("sonos", () => { + describe("serviceName", () => { + it("should default to bonob", () => { + expect(config().sonos.serviceName).toEqual("bonob"); + }); + + it("should be overridable", () => { + process.env["BONOB_SONOS_SERVICE_NAME"] = "foobar1000"; + expect(config().sonos.serviceName).toEqual("foobar1000"); + }); + }); + + describeBooleanConfigValue("deviceDiscovery", "BONOB_SONOS_DEVICE_DISCOVERY", true, config => config.sonos.deviceDiscovery); + + describe("seedHost", () => { + it("should default to undefined", () => { + expect(config().sonos.seedHost).toBeUndefined(); + }); + + it("should be overridable", () => { + process.env["BONOB_SONOS_SEED_HOST"] = "123.456.789.0"; + expect(config().sonos.seedHost).toEqual("123.456.789.0"); + }); + }); + + describeBooleanConfigValue("autoRegister", "BONOB_SONOS_AUTO_REGISTER", false, config => config.sonos.autoRegister); + + describe("sid", () => { + it("should default to 246", () => { + expect(config().sonos.sid).toEqual(246); + }); + + it("should be overridable", () => { + process.env["BONOB_SONOS_SERVICE_ID"] = "786"; + expect(config().sonos.sid).toEqual(786); + }); + }); + }); + + describe("navidrome", () => { + describe("url", () => { + it("should default to http://${hostname()}:4533", () => { + expect(config().navidrome.url).toEqual(`http://${hostname()}:4533`); + }); + + it("should be overridable", () => { + process.env["BONOB_NAVIDROME_URL"] = "http://farfaraway.com"; + expect(config().navidrome.url).toEqual("http://farfaraway.com"); + }); + }); + + describe("customClientsFor", () => { + it("should default to undefined", () => { + expect(config().navidrome.customClientsFor).toBeUndefined(); + }); + + it("should be overridable", () => { + process.env["BONOB_NAVIDROME_CUSTOM_CLIENTS"] = "whoop/whoop"; + expect(config().navidrome.customClientsFor).toEqual("whoop/whoop"); + }); + }); + }); + + describeBooleanConfigValue("scrobbleTracks", "BONOB_SCROBBLE_TRACKS", true, config => config.scrobbleTracks); + describeBooleanConfigValue("reportNowPlaying", "BONOB_REPORT_NOW_PLAYING", true, config => config.reportNowPlaying); });