Disabled sonos should return false for all mutations

This commit is contained in:
simojenki
2021-09-21 11:15:51 +10:00
parent a0043668d2
commit 0bc2d39a37
7 changed files with 60 additions and 41 deletions

View File

@@ -37,7 +37,7 @@ export default function () {
sonos: {
serviceName: process.env["BONOB_SONOS_SERVICE_NAME"] || "bonob",
discovery: {
auto:
enabled:
(process.env["BONOB_SONOS_DEVICE_DISCOVERY"] || "true") == "true",
seedHost: process.env["BONOB_SONOS_SEED_HOST"],
},

View File

@@ -13,7 +13,7 @@ const bonobUrl = new URLBuilder(params[0]!);
const config = readConfig();
registrar(bonobUrl, config.sonos.discovery)()
registrar(bonobUrl, config.sonos.discovery.seedHost)()
.then((success) => {
if (success) {
console.log(`Successfully registered bonob @ ${bonobUrl} with sonos`);

View File

@@ -1,15 +1,12 @@
import axios from "axios";
import _ from "underscore";
import logger from "./logger";
import sonos, { bonobService, Discovery } from "./sonos";
import sonos, { bonobService } from "./sonos";
import { URLBuilder } from "./url_builder";
export default (
bonobUrl: URLBuilder,
sonosDiscovery: Discovery = {
auto: true,
seedHost: undefined,
}
seedHost?: string
) =>
async () => {
const about = bonobUrl.append({ pathname: "/about" });
@@ -34,5 +31,5 @@ export default (
.then(({ name, sid }: { name: string; sid: number }) =>
bonobService(name, sid, bonobUrl)
)
.then((service) => sonos(sonosDiscovery).register(service));
.then((service) => sonos({ enabled: true, seedHost }).register(service));
};

View File

@@ -101,8 +101,8 @@ export interface Sonos {
export const SONOS_DISABLED: Sonos = {
devices: () => Promise.resolve([]),
services: () => Promise.resolve([]),
remove: (_: number) => Promise.resolve(true),
register: (_: Service) => Promise.resolve(true),
remove: (_: number) => Promise.resolve(false),
register: (_: Service) => Promise.resolve(false),
};
export const asService = (musicService: MusicService): Service => ({
@@ -243,13 +243,13 @@ export function autoDiscoverySonos(sonosSeedHost?: string): Sonos {
}
export type Discovery = {
auto: boolean;
enabled: boolean;
seedHost?: string;
};
export default (
sonosDiscovery: Discovery = { auto: true }
sonosDiscovery: Discovery = { enabled: true }
): Sonos =>
sonosDiscovery.auto
sonosDiscovery.enabled
? autoDiscoverySonos(sonosDiscovery.seedHost)
: SONOS_DISABLED;

View File

@@ -198,7 +198,7 @@ describe("config", () => {
"deviceDiscovery",
"BONOB_SONOS_DEVICE_DISCOVERY",
true,
(config) => config.sonos.discovery.auto
(config) => config.sonos.discovery.enabled
);
describe("seedHost", () => {

View File

@@ -75,12 +75,13 @@ describe("registrar", () => {
(sonos as jest.Mock).mockReturnValue(fakeSonos);
});
describe("when registration succeeds", () => {
it("should fetch the service details and register", async () => {
describe("seedHost", () => {
describe("is specified", () => {
it("should register using the seed host", async () => {
fakeSonos.register.mockResolvedValue(true);
const sonosDiscovery = { auto: true };
const seedHost = "127.0.0.11";
expect(await registrar(bonobUrl, sonosDiscovery)()).toEqual(
expect(await registrar(bonobUrl, seedHost)()).toEqual(
true
);
@@ -89,18 +90,17 @@ describe("registrar", () => {
serviceDetails.sid,
bonobUrl
);
expect(sonos).toHaveBeenCalledWith(sonosDiscovery);
expect(sonos).toHaveBeenCalledWith({ enabled: true, seedHost });
expect(fakeSonos.register).toHaveBeenCalledWith(service);
});
});
describe("when registration fails", () => {
it("should fetch the service details and register", async () => {
fakeSonos.register.mockResolvedValue(false);
const sonosDiscovery = { auto: false, seedHost: "192.168.1.163" };
describe("is not specified", () => {
it("should register without using the seed host", async () => {
fakeSonos.register.mockResolvedValue(true);
expect(await registrar(bonobUrl, sonosDiscovery)()).toEqual(
false
expect(await registrar(bonobUrl)()).toEqual(
true
);
expect(bonobService).toHaveBeenCalledWith(
@@ -108,9 +108,30 @@ describe("registrar", () => {
serviceDetails.sid,
bonobUrl
);
expect(sonos).toHaveBeenCalledWith(sonosDiscovery);
expect(sonos).toHaveBeenCalledWith({ enabled: true });
expect(fakeSonos.register).toHaveBeenCalledWith(service);
});
});
});
describe("when registration succeeds", () => {
it("should fetch the service details and register", async () => {
fakeSonos.register.mockResolvedValue(true);
expect(await registrar(bonobUrl)()).toEqual(
true
);
});
});
describe("when registration fails", () => {
it("should fetch the service details and register", async () => {
fakeSonos.register.mockResolvedValue(false);
expect(await registrar(bonobUrl)()).toEqual(
false
);
});
});
});
});

View File

@@ -274,12 +274,13 @@ describe("sonos", () => {
describe("when is disabled", () => {
it("should return a disabled client", async () => {
const disabled = sonos({ auto: false });
const disabled = sonos({ enabled: false });
expect(disabled).toEqual(SONOS_DISABLED);
expect(await disabled.devices()).toEqual([]);
expect(await disabled.services()).toEqual([]);
expect(await disabled.register(aService())).toEqual(true);
expect(await disabled.register(aService())).toEqual(false);
expect(await disabled.remove(123)).toEqual(false);
});
});
@@ -310,7 +311,7 @@ describe("sonos", () => {
);
sonosManager.InitializeWithDiscovery.mockResolvedValue(true);
const actualDevices = await sonos({ auto: true }).devices();
const actualDevices = await sonos({ enabled: true }).devices();
expect(SonosManager).toHaveBeenCalledTimes(1);
expect(sonosManager.InitializeWithDiscovery).toHaveBeenCalledWith(10);
@@ -331,7 +332,7 @@ describe("sonos", () => {
);
sonosManager.InitializeWithDiscovery.mockResolvedValue(true);
const actualDevices = await sonos({ auto: true, seedHost: "" }).devices();
const actualDevices = await sonos({ enabled: true, seedHost: "" }).devices();
expect(SonosManager).toHaveBeenCalledTimes(1);
expect(sonosManager.InitializeWithDiscovery).toHaveBeenCalledWith(10);
@@ -354,7 +355,7 @@ describe("sonos", () => {
);
sonosManager.InitializeFromDevice.mockResolvedValue(true);
const actualDevices = await sonos({ auto: true, seedHost }).devices();
const actualDevices = await sonos({ enabled: true, seedHost }).devices();
expect(SonosManager).toHaveBeenCalledTimes(1);
expect(sonosManager.InitializeFromDevice).toHaveBeenCalledWith(
@@ -377,7 +378,7 @@ describe("sonos", () => {
);
sonosManager.InitializeWithDiscovery.mockResolvedValue(true);
const actualDevices = await sonos({ auto: true, seedHost: undefined }).devices();
const actualDevices = await sonos({ enabled: true, seedHost: undefined }).devices();
expect(actualDevices).toEqual([
{
@@ -408,7 +409,7 @@ describe("sonos", () => {
);
sonosManager.InitializeWithDiscovery.mockResolvedValue(false);
expect(await sonos({ auto: true, seedHost: "" }).devices()).toEqual([]);
expect(await sonos({ enabled: true, seedHost: "" }).devices()).toEqual([]);
});
});
});