From f0458675544b0066a099eedd8019ab4c2fd321fb Mon Sep 17 00:00:00 2001 From: Simon J Date: Tue, 31 Aug 2021 22:03:44 +1000 Subject: [PATCH] Remove register button when there are no sonos devices (#39) --- src/i8n.ts | 5 +- tests/scenarios.test.ts | 20 ++- tests/server.test.ts | 336 ++++++++++++++++++++++++---------------- web/views/index.eta | 5 + 4 files changed, 226 insertions(+), 140 deletions(-) diff --git a/src/i8n.ts b/src/i8n.ts index 96168c4..32ee1b3 100644 --- a/src/i8n.ts +++ b/src/i8n.ts @@ -36,7 +36,8 @@ export type KEY = | "failedToRemoveRegistration" | "invalidLinkCode" | "loginSuccessful" - | "loginFailed"; + | "loginFailed" + | "noSonosDevices"; const translations: Record> = { "en-US": { @@ -71,6 +72,7 @@ const translations: Record> = { invalidLinkCode: "Invalid linkCode!", loginSuccessful: "Login successful!", loginFailed: "Login failed!", + noSonosDevices: "No sonos devices", }, "nl-NL": { AppLinkMessage: "Sonos koppelen aan $BONOB_SONOS_SERVICE_NAME", @@ -104,6 +106,7 @@ const translations: Record> = { invalidLinkCode: "Ongeldige linkcode!", loginSuccessful: "Inloggen gelukt!", loginFailed: "Inloggen mislukt!", + noSonosDevices: "Geen Sonos-apparaten", }, }; diff --git a/tests/scenarios.test.ts b/tests/scenarios.test.ts index 3978df8..fdde064 100644 --- a/tests/scenarios.test.ts +++ b/tests/scenarios.test.ts @@ -9,6 +9,7 @@ import { GetMetadataResponse, } from "../src/smapi"; import { + aDevice, BLONDIE, BOB_MARLEY, getAppLinkMessage, @@ -19,7 +20,7 @@ import { InMemoryMusicService } from "./in_memory_music_service"; import { InMemoryLinkCodes } from "../src/link_codes"; import { Credentials } from "../src/music_service"; import makeServer from "../src/server"; -import { Service, bonobService, SONOS_DISABLED } from "../src/sonos"; +import { Service, bonobService, Sonos } from "../src/sonos"; import supersoap from "./supersoap"; import url, { URLBuilder } from "../src/url_builder"; @@ -171,6 +172,17 @@ describe("scenarios", () => { ); const linkCodes = new InMemoryLinkCodes(); + const fakeSonos: Sonos = { + devices: () => Promise.resolve([aDevice({ + name: "device1", + ip: "172.0.0.1", + port: 4301, + })]), + services: () => Promise.resolve([]), + remove: () => Promise.resolve(true), + register: () => Promise.resolve(true), + }; + beforeEach(() => { musicService.clear(); linkCodes.clear(); @@ -255,7 +267,7 @@ describe("scenarios", () => { const bonobUrl = url("http://localhost:1234"); const bonob = bonobService("bonob", 123, bonobUrl); const server = makeServer( - SONOS_DISABLED, + fakeSonos, bonob, bonobUrl, musicService, @@ -273,7 +285,7 @@ describe("scenarios", () => { const bonobUrl = url("http://localhost:1234/"); const bonob = bonobService("bonob", 123, bonobUrl); const server = makeServer( - SONOS_DISABLED, + fakeSonos, bonob, bonobUrl, musicService, @@ -291,7 +303,7 @@ describe("scenarios", () => { const bonobUrl = url("http://localhost:1234/context-for-bonob"); const bonob = bonobService("bonob", 123, bonobUrl); const server = makeServer( - SONOS_DISABLED, + fakeSonos, bonob, bonobUrl, musicService, diff --git a/tests/server.test.ts b/tests/server.test.ts index 721be91..72fb4ac 100644 --- a/tests/server.test.ts +++ b/tests/server.test.ts @@ -193,6 +193,7 @@ describe("server", () => { it("should display it", async () => { const res = await request(server) .get(bonobUrl.append({ pathname: "/" }).pathname()) + .set("accept-language", acceptLanguage) .send(); expect(res.status).toEqual(200); @@ -212,155 +213,220 @@ describe("server", () => { it("should be empty", async () => { const res = await request(server) .get(bonobUrl.append({ pathname: "/" }).pathname()) + .set("accept-language", acceptLanguage) .send(); expect(res.status).toEqual(200); + expect(res.text).toMatch(`

${lang("devices")} \(0\)

`); expect(res.text).not.toMatch(/class=device/); + expect(res.text).toContain(lang("noSonosDevices")); }); }); }); - describe("when there are 2 devices and bonob is not registered", () => { - const service1 = aService({ - name: "s1", - sid: 1, - }); - const service2 = aService({ - name: "s2", - sid: 2, - }); - const service3 = aService({ - name: "s3", - sid: 3, - }); - const service4 = aService({ - name: "s4", - sid: 4, - }); - const missingBonobService = aService({ - name: "bonobMissing", - sid: 88, - }); - - const device1: Device = aDevice({ - name: "device1", - ip: "172.0.0.1", - port: 4301, - }); - - const device2: Device = aDevice({ - name: "device2", - ip: "172.0.0.2", - port: 4302, - }); - - const fakeSonos: Sonos = { - devices: () => Promise.resolve([device1, device2]), - services: () => - Promise.resolve([service1, service2, service3, service4]), - remove: () => Promise.resolve(false), - register: () => Promise.resolve(false), - }; - - const server = makeServer( - fakeSonos, - missingBonobService, - bonobUrl, - new InMemoryMusicService() - ); - - describe("devices list", () => { - it("should contain the devices returned from sonos", async () => { - const res = await request(server) - .get(bonobUrl.append({ pathname: "/" }).path()) - .set("accept-language", acceptLanguage) - .send(); - - expect(res.status).toEqual(200); - expect(res.text).toMatch(`

${lang("devices")} \(2\)

`); - expect(res.text).toMatch(/device1\s+\(172.0.0.1:4301\)/); - expect(res.text).toMatch(/device2\s+\(172.0.0.2:4302\)/); + describe("when sonos integration is enabled", () => { + describe("there are no devices and bonob is not registered", () => { + const missingBonobService = aService({ + name: "bonobMissing", + sid: 88, + }); + + const fakeSonos: Sonos = { + devices: () => Promise.resolve([]), + services: () => + Promise.resolve([]), + remove: () => Promise.resolve(false), + register: () => Promise.resolve(false), + }; + + const server = makeServer( + fakeSonos, + missingBonobService, + bonobUrl, + new InMemoryMusicService() + ); + + describe("devices list", () => { + it("should be empty", async () => { + const res = await request(server) + .get(bonobUrl.append({ pathname: "/" }).path()) + .set("accept-language", acceptLanguage) + .send(); + + expect(res.status).toEqual(200); + expect(res.text).toMatch(`

${lang("devices")} \(0\)

`); + expect(res.text).not.toMatch(/class=device/); + expect(res.text).toContain(lang("noSonosDevices")); + }); + }); + + describe("services", () => { + it("should be empty", async () => { + const res = await request(server) + .get(bonobUrl.append({ pathname: "/" }).path()) + .set("accept-language", acceptLanguage) + .send(); + + expect(res.status).toEqual(200); + expect(res.text).toMatch(`

${lang("services")} \(0\)

`); + }); }); }); - - describe("services", () => { - it("should contain a list of services returned from sonos", async () => { - const res = await request(server) - .get(bonobUrl.append({ pathname: "/" }).path()) - .set("accept-language", acceptLanguage) - .send(); - - expect(res.status).toEqual(200); - expect(res.text).toMatch(`

${lang("services")} \(4\)

`); - expect(res.text).toMatch(/s1\s+\(1\)/); - expect(res.text).toMatch(/s2\s+\(2\)/); - expect(res.text).toMatch(/s3\s+\(3\)/); - expect(res.text).toMatch(/s4\s+\(4\)/); + + describe("there are 2 devices and bonob is not registered", () => { + const service1 = aService({ + name: "s1", + sid: 1, + }); + const service2 = aService({ + name: "s2", + sid: 2, + }); + const service3 = aService({ + name: "s3", + sid: 3, + }); + const service4 = aService({ + name: "s4", + sid: 4, + }); + const missingBonobService = aService({ + name: "bonobMissing", + sid: 88, + }); + + const device1: Device = aDevice({ + name: "device1", + ip: "172.0.0.1", + port: 4301, + }); + + const device2: Device = aDevice({ + name: "device2", + ip: "172.0.0.2", + port: 4302, + }); + + const fakeSonos: Sonos = { + devices: () => Promise.resolve([device1, device2]), + services: () => + Promise.resolve([service1, service2, service3, service4]), + remove: () => Promise.resolve(false), + register: () => Promise.resolve(false), + }; + + const server = makeServer( + fakeSonos, + missingBonobService, + bonobUrl, + new InMemoryMusicService() + ); + + describe("devices list", () => { + it("should contain the devices returned from sonos", async () => { + const res = await request(server) + .get(bonobUrl.append({ pathname: "/" }).path()) + .set("accept-language", acceptLanguage) + .send(); + + expect(res.status).toEqual(200); + expect(res.text).toMatch(`

${lang("devices")} \(2\)

`); + expect(res.text).toMatch(/device1\s+\(172.0.0.1:4301\)/); + expect(res.text).toMatch(/device2\s+\(172.0.0.2:4302\)/); + }); + }); + + describe("services", () => { + it("should contain a list of services returned from sonos", async () => { + const res = await request(server) + .get(bonobUrl.append({ pathname: "/" }).path()) + .set("accept-language", acceptLanguage) + .send(); + + expect(res.status).toEqual(200); + expect(res.text).toMatch(`

${lang("services")} \(4\)

`); + expect(res.text).toMatch(/s1\s+\(1\)/); + expect(res.text).toMatch(/s2\s+\(2\)/); + expect(res.text).toMatch(/s3\s+\(3\)/); + expect(res.text).toMatch(/s4\s+\(4\)/); + }); + }); + + describe("registration status", () => { + it("should be not-registered", async () => { + const res = await request(server) + .get(bonobUrl.append({ pathname: "/" }).path()) + .set("accept-language", acceptLanguage) + .send(); + expect(res.status).toEqual(200); + expect(res.text).toMatch( + `` + ); + expect(res.text).toMatch(`

${lang("expectedConfig")}

`); + expect(res.text).toMatch( + `

${lang("noExistingServiceRegistration")}

` + ); + expect(res.text).not.toMatch( + `` + ); + }); }); }); + + describe("there are 2 devices and bonob is registered", () => { + const service1 = aService(); + + const service2 = aService(); - describe("registration status", () => { - it("should be not-registered", async () => { - const res = await request(server) - .get(bonobUrl.append({ pathname: "/" }).path()) - .set("accept-language", acceptLanguage) - .send(); - expect(res.status).toEqual(200); - expect(res.text).toMatch( - `` - ); - expect(res.text).toMatch(`

${lang("expectedConfig")}

`); - expect(res.text).toMatch( - `

${lang("noExistingServiceRegistration")}

` - ); - expect(res.text).not.toMatch( - `` - ); + const device1: Device = aDevice({ + name: "device1", + ip: "172.0.0.1", + port: 4301, }); - }); - }); - - describe("when there are 2 devices and bonob is registered", () => { - const service1 = aService(); - - const service2 = aService(); - - const bonobService = aService({ - name: "bonobNotMissing", - sid: 99, - }); - - const fakeSonos: Sonos = { - devices: () => Promise.resolve([]), - services: () => Promise.resolve([service1, service2, bonobService]), - remove: () => Promise.resolve(false), - register: () => Promise.resolve(false), - }; - - const server = makeServer( - fakeSonos, - bonobService, - bonobUrl, - new InMemoryMusicService() - ); - - describe("registration status", () => { - it("should be registered", async () => { - const res = await request(server) - .get(bonobUrl.append({ pathname: "/" }).path()) - .set("accept-language", acceptLanguage) - .send(); - expect(res.status).toEqual(200); - expect(res.text).toMatch( - `` - ); - expect(res.text).toMatch(`

${lang("expectedConfig")}

`); - expect(res.text).toMatch( - `

${lang("existingServiceConfig")}

` - ); - expect(res.text).toMatch( - `` - ); + + const device2: Device = aDevice({ + name: "device2", + ip: "172.0.0.2", + port: 4302, + }); + + const bonobService = aService({ + name: "bonobNotMissing", + sid: 99, + }); + + const fakeSonos: Sonos = { + devices: () => Promise.resolve([device1, device2]), + services: () => Promise.resolve([service1, service2, bonobService]), + remove: () => Promise.resolve(false), + register: () => Promise.resolve(false), + }; + + const server = makeServer( + fakeSonos, + bonobService, + bonobUrl, + new InMemoryMusicService() + ); + + describe("registration status", () => { + it("should be registered", async () => { + const res = await request(server) + .get(bonobUrl.append({ pathname: "/" }).path()) + .set("accept-language", acceptLanguage) + .send(); + expect(res.status).toEqual(200); + expect(res.text).toMatch( + `` + ); + expect(res.text).toMatch(`

${lang("expectedConfig")}

`); + expect(res.text).toMatch( + `

${lang("existingServiceConfig")}

` + ); + expect(res.text).toMatch( + `` + ); + }); }); }); }); diff --git a/web/views/index.eta b/web/views/index.eta index 97e0f1e..6568312 100644 --- a/web/views/index.eta +++ b/web/views/index.eta @@ -6,10 +6,15 @@

<%= it.lang("expectedConfig") %>

<%= JSON.stringify(it.bonobService) %>

+ <% if(it.devices.length > 0) { %>
">

+ <% } else { %> +

<%= it.lang("noSonosDevices") %>

+
+ <% } %> <% if(it.registeredBonobService) { %>

<%= it.lang("existingServiceConfig") %>