Compare commits

...

11 Commits

26 changed files with 1569 additions and 949 deletions

View File

@@ -31,9 +31,9 @@ RUN apk add --no-cache --update --virtual .gyp \
FROM node:16.6-alpine FROM node:16.6-alpine
ENV BONOB_PORT=4534 ENV BNB_PORT=4534
EXPOSE $BONOB_PORT EXPOSE $BNB_PORT
WORKDIR /bonob WORKDIR /bonob

131
README.md
View File

@@ -2,13 +2,13 @@
A sonos SMAPI implementation to allow registering sources of music with sonos. A sonos SMAPI implementation to allow registering sources of music with sonos.
Currently only a single integration allowing Navidrome to be registered with sonos. In theory as Navidrome implements the subsonic API, it *may* work with other subsonic api clones. Support for Subsonic API clones (tested against Navidrome and Gonic).
![Build](https://github.com/simojenki/bonob/workflows/Build/badge.svg) ![Build](https://github.com/simojenki/bonob/workflows/Build/badge.svg)
## Features ## Features
- Integrates with Navidrome - Integrates with Subsonic API clones (Navidrome, Gonic)
- Browse by Artist, Albums, Genres, Playlist, Random Albums, Starred Albums, Recently Added Albums, Recently Played Albums, Most Played Albums - Browse by Artist, Albums, Genres, Playlist, Random Albums, Starred Albums, Recently Added Albums, Recently Played Albums, Most Played Albums
- Artist Art - Artist Art
- Album Art - Album Art
@@ -18,7 +18,7 @@ Currently only a single integration allowing Navidrome to be registered with son
- Discovery of sonos devices using seed IP address - Discovery of sonos devices using seed IP address
- Auto register bonob service with sonos system - Auto register bonob service with sonos system
- Multiple registrations within a single household. - Multiple registrations within a single household.
- Transcoding performed by Navidrome with specific player for bonob/sonos, customisable by mimeType - Transcoding support for flacs using a specific player for the flac mimeType bonob/sonos
- Ability to search by Album, Artist, Track - Ability to search by Album, Artist, Track
- Ability to play a playlist - Ability to play a playlist
- Ability to add/remove playlists - Ability to add/remove playlists
@@ -33,8 +33,8 @@ bonob is ditributed via docker and can be run in a number of ways
```bash ```bash
docker run \ docker run \
-e BONOB_SONOS_AUTO_REGISTER=true \ -e BNB_SONOS_AUTO_REGISTER=true \
-e BONOB_SONOS_DEVICE_DISCOVERY=true \ -e BNB_SONOS_DEVICE_DISCOVERY=true \
-p 4534:4534 \ -p 4534:4534 \
--network host \ --network host \
simojenki/bonob simojenki/bonob
@@ -46,10 +46,10 @@ Now open http://localhost:4534 in your browser, you should see sonos devices, an
```bash ```bash
docker run \ docker run \
-e BONOB_PORT=3000 \ -e BNB_PORT=3000 \
-e BONOB_SONOS_SEED_HOST=192.168.1.123 \ -e BNB_SONOS_SEED_HOST=192.168.1.123 \
-e BONOB_SONOS_AUTO_REGISTER=true \ -e BNB_SONOS_AUTO_REGISTER=true \
-e BONOB_SONOS_DEVICE_DISCOVERY=true \ -e BNB_SONOS_DEVICE_DISCOVERY=true \
-p 3000:3000 \ -p 3000:3000 \
simojenki/bonob simojenki/bonob
``` ```
@@ -66,13 +66,13 @@ Start bonob outside the LAN with sonos discovery & registration disabled as they
```bash ```bash
docker run \ docker run \
-e BONOB_PORT=4534 \ -e BNB_PORT=4534 \
-e BONOB_SONOS_SERVICE_NAME=MyAwesomeMusic \ -e BNB_SONOS_SERVICE_NAME=MyAwesomeMusic \
-e BONOB_SECRET=changeme \ -e BNB_SECRET=changeme \
-e BONOB_URL=https://my-server.example.com/bonob \ -e BNB_URL=https://my-server.example.com/bonob \
-e BONOB_SONOS_AUTO_REGISTER=false \ -e BNB_SONOS_AUTO_REGISTER=false \
-e BONOB_SONOS_DEVICE_DISCOVERY=false \ -e BNB_SONOS_DEVICE_DISCOVERY=false \
-e BONOB_NAVIDROME_URL=https://my-navidrome-service.com:4533 \ -e BNB_SUBSONIC_URL=https://my-navidrome-service.com:4533 \
-p 4534:4534 \ -p 4534:4534 \
simojenki/bonob simojenki/bonob
``` ```
@@ -93,11 +93,10 @@ docker run \
```bash ```bash
docker run \ docker run \
--rm \ --rm \
-e BONOB_SONOS_SEED_HOST=192.168.1.163 \ -e BNB_SONOS_SEED_HOST=192.168.1.163 \
simojenki/bonob register https://my-server.example.com/bonob simojenki/bonob register https://my-server.example.com/bonob
``` ```
### Running bonob and navidrome using docker-compose ### Running bonob and navidrome using docker-compose
```yaml ```yaml
@@ -125,72 +124,96 @@ services:
- "4534:4534" - "4534:4534"
restart: unless-stopped restart: unless-stopped
environment: environment:
BONOB_PORT: 4534 BNB_PORT: 4534
# ip address of your machine running bonob # ip address of your machine running bonob
BONOB_URL: http://192.168.1.111:4534 BNB_URL: http://192.168.1.111:4534
BONOB_SECRET: changeme BNB_SECRET: changeme
BONOB_SONOS_AUTO_REGISTER: true BNB_SONOS_AUTO_REGISTER: true
BONOB_SONOS_DEVICE_DISCOVERY: true BNB_SONOS_DEVICE_DISCOVERY: true
BONOB_SONOS_SERVICE_ID: 246 BNB_SONOS_SERVICE_ID: 246
# ip address of one of your sonos devices # ip address of one of your sonos devices
BONOB_SONOS_SEED_HOST: 192.168.1.121 BNB_SONOS_SEED_HOST: 192.168.1.121
BONOB_NAVIDROME_URL: http://navidrome:4533 BNB_SUBSONIC_URL: http://navidrome:4533
``` ```
## Configuration ## Configuration
item | default value | description item | default value | description
---- | ------------- | ----------- ---- | ------------- | -----------
BONOB_PORT | 4534 | Default http port for bonob to listen on BNB_PORT | 4534 | Default http port for bonob to listen on
BONOB_URL | http://$(hostname):4534 | URL (including path) for bonob so that sonos devices can communicate. **This must be either the public IP or DNS entry of the bonob instance so that the sonos devices can communicate with it.** BNB_URL | http://$(hostname):4534 | URL (including path) for bonob so that sonos devices can communicate. **This must be either the public IP or DNS entry of the bonob instance so that the sonos devices can communicate with it.**
BONOB_SECRET | bonob | secret used for encrypting credentials BNB_SECRET | bonob | secret used for encrypting credentials
BONOB_SONOS_AUTO_REGISTER | false | Whether or not to try and auto-register on startup BNB_SONOS_AUTO_REGISTER | false | Whether or not to try and auto-register on startup
BONOB_SONOS_DEVICE_DISCOVERY | true | whether or not sonos device discovery should be enabled BNB_SONOS_DEVICE_DISCOVERY | true | Enable/Disable sonos device discovery entirely. Setting this to 'false' will disable sonos device search, regardless of whether a seed host is specified.
BONOB_SONOS_SEED_HOST | undefined | sonos device seed host for discovery, or ommitted for for auto-discovery BNB_SONOS_SEED_HOST | undefined | sonos device seed host for discovery, or ommitted for for auto-discovery
BONOB_SONOS_SERVICE_NAME | bonob | service name for sonos BNB_SONOS_SERVICE_NAME | bonob | service name for sonos
BONOB_SONOS_SERVICE_ID | 246 | service id for sonos BNB_SONOS_SERVICE_ID | 246 | service id for sonos
BONOB_NAVIDROME_URL | http://$(hostname):4533 | URL for navidrome BNB_SUBSONIC_URL | http://$(hostname):4533 | URL for subsonic clone
BONOB_NAVIDROME_CUSTOM_CLIENTS | undefined | Comma delimeted mime types for custom navidrome clients when streaming. ie. "audio/flac,audio/ogg" would use client = 'bonob+audio/flac' for flacs, and 'bonob+audio/ogg' for oggs. BNB_SUBSONIC_CUSTOM_CLIENTS | undefined | Comma delimeted mime types for custom subsonic clients when streaming. ie. "audio/flac,audio/ogg" would use client = 'bonob+audio/flac' for flacs, and 'bonob+audio/ogg' for oggs.
BONOB_SCROBBLE_TRACKS | true | Whether to scrobble the playing of a track if it has been played for >30s BNB_SCROBBLE_TRACKS | true | Whether to scrobble the playing of a track if it has been played for >30s
BONOB_REPORT_NOW_PLAYING | true | Whether to report a track as now playing BNB_REPORT_NOW_PLAYING | true | Whether to report a track as now playing
BONOB_ICON_FOREGROUND_COLOR | undefined | Icon foreground color in sonos app, must be a valid [svg color](https://www.december.com/html/spec/colorsvg.html) BNB_ICON_FOREGROUND_COLOR | undefined | Icon foreground color in sonos app, must be a valid [svg color](https://www.december.com/html/spec/colorsvg.html)
BONOB_ICON_BACKGROUND_COLOR | undefined | Icon background color in sonos app, must be a valid [svg color](https://www.december.com/html/spec/colorsvg.html) BNB_ICON_BACKGROUND_COLOR | undefined | Icon background color in sonos app, must be a valid [svg color](https://www.december.com/html/spec/colorsvg.html)
## Initialising service within sonos app ## Initialising service within sonos app
- Configure bonob, make sure to set BONOB_URL. **bonob must be accessible from your sonos devices on BONOB_URL, otherwise it will fail to initialise within the sonos app, so make sure you test this in your browser by putting BONOB_URL in the address bar and seeing the bonob information page** - Configure bonob, make sure to set BNB_URL. **bonob must be accessible from your sonos devices on BNB_URL, otherwise it will fail to initialise within the sonos app, so make sure you test this in your browser by putting BNB_URL in the address bar and seeing the bonob information page**
- Start bonob, - Start bonob,
- Open sonos app on your device - Open sonos app on your device
- Settings -> Services & Voice -> + Add a Service - Settings -> Services & Voice -> + Add a Service
- Select your Music Service, default name is 'bonob', can be overriden with configuration BONOB_SONOS_SERVICE_NAME - Select your Music Service, default name is 'bonob', can be overriden with configuration BNB_SONOS_SERVICE_NAME
- Press 'Add to Sonos' -> 'Linking sonos with bonob' -> Authorize - Press 'Add to Sonos' -> 'Linking sonos with bonob' -> Authorize
- Your device should open a browser and you should now see a login screen, enter your navidrome credentials - Your device should open a browser and you should now see a login screen, enter your subsonic clone credentials
- You should get 'Login successful!' - You should get 'Login successful!'
- Go back into the sonos app and complete the process - Go back into the sonos app and complete the process
- You should now be able to play music from navidrome - You should now be able to play music on your sonos devices from you subsonic clone
- Within navidrome a new player will be created, 'bonob (username)', so you can configure transcoding specifically for sonos - Within the subsonic clone a new player will be created, 'bonob (username)', so you can configure transcoding specifically for sonos
## Implementing a different music source other than navidrome ## Implementing a different music source other than a subsonic clone
- Implement the MusicService/MusicLibrary interface - Implement the MusicService/MusicLibrary interface
- Startup bonob with your new implementation. - Startup bonob with your new implementation.
## Sample Icon colors ## A note on transcoding
tldr; Transcoding to mp3/m4a is not supported as sonos devices will not play the track. Transcoding to flac works however, use BNB_SUBSONIC_CUSTOM_CLIENTS=audio/flac if you want to transcode flac->flac ie. to downsample HD flacs (see below).
Sonos devices are very particular about how audio streams are presented to them, see [streaming basics](https://developer.sonos.com/build/content-service-add-features/streaming-basics/). When using transcoding both Navidrome and Gonic report no 'content-length', nor do they support range queries, this will cause the sonos device to fail to play the track.
## Cusomisation
### Audio File type specific transcoding options within Subsonic
In some situations you may wish to have different 'Players' within you Subsonic server so that you can configure different transcoding options depending on the file type. For example if you have flacs with a mixture of frequency formats where not all are supported by sonos [See issue #52](https://github.com/simojenki/bonob/issues/52) & [Sonos supported audio formats](https://developer.sonos.com/build/content-service-add-features/supported-audio-formats/)
In this case you could set;
```bash
BNB_SUBSONIC_CUSTOM_CLIENTS="audio/flac"
``` ```
-e BONOB_ICON_FOREGROUND_COLOR=white \
-e BONOB_ICON_BACKGROUND_COLOR=darkgrey This would result in 2 players in Navidrome, one called 'bonob', the other called 'bonob+audio/flac'. You could then configure a custom flac transcoder in Navidrome that re-samples the flacs to a sonos supported format, ie [Using something like this](https://stackoverflow.com/questions/41420391/ffmpeg-flac-24-bit-96khz-to-16-bit-48khz);
```bash
ffmpeg -i %s -af aresample=resampler=soxr:out_sample_fmt=s16:out_sample_rate=48000 -f flac -
``` ```
### Changing Icon colors
```bash
-e BNB_ICON_FOREGROUND_COLOR=white \
-e BNB_ICON_BACKGROUND_COLOR=darkgrey
```
![White & Dark Grey](https://github.com/simojenki/bonob/blob/master/docs/images/whiteDarkGrey.png?raw=true) ![White & Dark Grey](https://github.com/simojenki/bonob/blob/master/docs/images/whiteDarkGrey.png?raw=true)
```bash
-e BNB_ICON_FOREGROUND_COLOR=chartreuse \
-e BNB_ICON_BACKGROUND_COLOR=fuchsia
```
```
-e BONOB_ICON_FOREGROUND_COLOR=chartreuse \
-e BONOB_ICON_BACKGROUND_COLOR=fuchsia
```
![Chartreuse & Fuchsia](https://github.com/simojenki/bonob/blob/master/docs/images/chartreuseFuchsia.png?raw=true) ![Chartreuse & Fuchsia](https://github.com/simojenki/bonob/blob/master/docs/images/chartreuseFuchsia.png?raw=true)
## Credits ## Credits
- Icons courtesy of: [Navidrome](https://www.navidrome.org/), [Vectornator](https://www.vectornator.io/icons), and @jicho - Icons courtesy of: [Navidrome](https://www.navidrome.org/), [Vectornator](https://www.vectornator.io/icons), and @jicho

View File

@@ -22,13 +22,13 @@ services:
- "4534:4534" - "4534:4534"
restart: unless-stopped restart: unless-stopped
environment: environment:
BONOB_PORT: 4534 BNB_PORT: 4534
# ip address of your machine running bonob # ip address of your machine running bonob
BONOB_URL: http://192.168.1.111:4534 BNB_URL: http://192.168.1.111:4534
BONOB_SECRET: changeme BNB_SECRET: changeme
BONOB_SONOS_SERVICE_ID: 246 BNB_SONOS_SERVICE_ID: 246
BONOB_SONOS_AUTO_REGISTER: "true" BNB_SONOS_AUTO_REGISTER: "true"
BONOB_SONOS_DEVICE_DISCOVERY: "true" BNB_SONOS_DEVICE_DISCOVERY: "true"
# ip address of one of your sonos devices # ip address of one of your sonos devices
BONOB_SONOS_SEED_HOST: 192.168.1.121 BNB_SONOS_SEED_HOST: 192.168.1.121
BONOB_NAVIDROME_URL: http://navidrome:4533 BNB_SUBSONIC_URL: http://navidrome:4533

View File

@@ -50,8 +50,8 @@
"scripts": { "scripts": {
"clean": "rm -Rf build node_modules", "clean": "rm -Rf build node_modules",
"build": "tsc", "build": "tsc",
"dev": "BONOB_ICON_FOREGROUND_COLOR=white BONOB_ICON_BACKGROUND_COLOR=darkgrey BONOB_SONOS_SERVICE_NAME=bonobDev BONOB_SONOS_DEVICE_DISCOVERY=true nodemon -V ./src/app.ts", "dev": "BNB_ICON_FOREGROUND_COLOR=white BNB_ICON_BACKGROUND_COLOR=darkgrey BNB_SONOS_SERVICE_NAME=bonobDev BNB_SONOS_DEVICE_DISCOVERY=true nodemon -V ./src/app.ts",
"devr": "BONOB_ICON_FOREGROUND_COLOR=white BONOB_ICON_BACKGROUND_COLOR=darkgrey BONOB_SONOS_SERVICE_NAME=bonobDev BONOB_SONOS_DEVICE_DISCOVERY=true BONOB_SONOS_AUTO_REGISTER=true nodemon -V ./src/app.ts", "devr": "BNB_ICON_FOREGROUND_COLOR=white BNB_ICON_BACKGROUND_COLOR=darkgrey BNB_SONOS_SERVICE_NAME=bonobDev BNB_SONOS_DEVICE_DISCOVERY=true BNB_SONOS_AUTO_REGISTER=true nodemon -V ./src/app.ts",
"register-dev": "ts-node ./src/register.ts http://$(hostname):4534", "register-dev": "ts-node ./src/register.ts http://$(hostname):4534",
"test": "jest", "test": "jest",
"gitinfo": "git describe --tags > .gitinfo" "gitinfo": "git describe --tags > .gitinfo"

View File

@@ -5,6 +5,7 @@ import crypto from "crypto";
import { Encryption } from "./encryption"; import { Encryption } from "./encryption";
import logger from "./logger"; import logger from "./logger";
import { Clock, SystemClock } from "./clock"; import { Clock, SystemClock } from "./clock";
import { b64Encode, b64Decode } from "./b64";
type AccessToken = { type AccessToken = {
value: string; value: string;
@@ -60,14 +61,12 @@ export class EncryptedAccessTokens implements AccessTokens {
} }
mint = (authToken: string): string => mint = (authToken: string): string =>
Buffer.from(JSON.stringify(this.encryption.encrypt(authToken))).toString( b64Encode(JSON.stringify(this.encryption.encrypt(authToken)));
"base64"
);
authTokenFor(value: string): string | undefined { authTokenFor(value: string): string | undefined {
try { try {
return this.encryption.decrypt( return this.encryption.decrypt(
JSON.parse(Buffer.from(value, "base64").toString("ascii")) JSON.parse(b64Decode(value))
); );
} catch { } catch {
logger.warn("Failed to decrypt access token..."); logger.warn("Failed to decrypt access token...");

View File

@@ -2,7 +2,7 @@ import path from "path";
import fs from "fs"; import fs from "fs";
import server from "./server"; import server from "./server";
import logger from "./logger"; import logger from "./logger";
import { appendMimeTypeToClientFor, DEFAULT, Navidrome } from "./navidrome"; import { appendMimeTypeToClientFor, DEFAULT, Subsonic } from "./subsonic";
import encryption from "./encryption"; import encryption from "./encryption";
import { InMemoryAccessTokens, sha256 } from "./access_tokens"; import { InMemoryAccessTokens, sha256 } from "./access_tokens";
import { InMemoryLinkCodes } from "./link_codes"; import { InMemoryLinkCodes } from "./link_codes";
@@ -24,20 +24,20 @@ const bonob = bonobService(
const sonosSystem = sonos(config.sonos.discovery); const sonosSystem = sonos(config.sonos.discovery);
const streamUserAgent = config.navidrome.customClientsFor const streamUserAgent = config.subsonic.customClientsFor
? appendMimeTypeToClientFor(config.navidrome.customClientsFor.split(",")) ? appendMimeTypeToClientFor(config.subsonic.customClientsFor.split(","))
: DEFAULT; : DEFAULT;
const navidrome = new Navidrome( const subsonic = new Subsonic(
config.navidrome.url, config.subsonic.url,
encryption(config.secret), encryption(config.secret),
streamUserAgent streamUserAgent
); );
const featureFlagAwareMusicService: MusicService = { const featureFlagAwareMusicService: MusicService = {
generateToken: navidrome.generateToken, generateToken: subsonic.generateToken,
login: (authToken: string) => login: (authToken: string) =>
navidrome.login(authToken).then((library) => { subsonic.login(authToken).then((library) => {
return { return {
...library, ...library,
scrobble: (id: string) => { scrobble: (id: string) => {
@@ -90,7 +90,7 @@ if (config.sonos.autoRegister) {
); );
} }
}); });
} else if(config.sonos.discovery.auto) { } else if(config.sonos.discovery.enabled) {
sonosSystem.devices().then(devices => { sonosSystem.devices().then(devices => {
devices.forEach(d => { devices.forEach(d => {
logger.info(`Found device ${d.name}(${d.group}) @ ${d.ip}:${d.port}`) logger.info(`Found device ${d.name}(${d.group}) @ ${d.ip}:${d.port}`)

2
src/b64.ts Normal file
View File

@@ -0,0 +1,2 @@
export const b64Encode = (value: string) => Buffer.from(value).toString("base64");
export const b64Decode = (value: string) => Buffer.from(value, "base64").toString("ascii");

View File

@@ -2,56 +2,91 @@ import { hostname } from "os";
import logger from "./logger"; import logger from "./logger";
import url from "./url_builder"; import url from "./url_builder";
export const WORD = /^\w+$/;
type EnvVarOpts = {
default: string | undefined;
legacy: string[] | undefined;
validationPattern: RegExp | undefined;
};
export function envVar(
name: string,
opts: Partial<EnvVarOpts> = {
default: undefined,
legacy: undefined,
validationPattern: undefined,
}
) {
const result = [name, ...(opts.legacy || [])]
.map((it) => ({ key: it, value: process.env[it] }))
.find((it) => it.value);
if (
result &&
result.value &&
opts.validationPattern &&
!result.value.match(opts.validationPattern)
) {
throw `Invalid value specified for '${name}', must match ${opts.validationPattern}`;
}
if(result && result.value && result.key != name) {
logger.warn(`Configuration key '${result.key}' is deprecated, replace with '${name}'`)
}
return result?.value || opts.default;
}
export const bnbEnvVar = (key: string, opts: Partial<EnvVarOpts> = {}) =>
envVar(`BNB_${key}`, {
...opts,
legacy: [`BONOB_${key}`, ...(opts.legacy || [])],
});
export default function () { export default function () {
const port = +(process.env["BONOB_PORT"] || 4534); const port = +bnbEnvVar("PORT", { default: "4534" })!;
const bonobUrl = const bonobUrl = bnbEnvVar("URL", {
process.env["BONOB_URL"] || legacy: ["BONOB_WEB_ADDRESS"],
process.env["BONOB_WEB_ADDRESS"] || default: `http://${hostname()}:${port}`,
`http://${hostname()}:${port}`; })!;
if (bonobUrl.match("localhost")) { if (bonobUrl.match("localhost")) {
logger.error( logger.error(
"BONOB_URL containing localhost is almost certainly incorrect, sonos devices will not be able to communicate with bonob using localhost, please specify either public IP or DNS entry" "BNB_URL containing localhost is almost certainly incorrect, sonos devices will not be able to communicate with bonob using localhost, please specify either public IP or DNS entry"
); );
process.exit(1); process.exit(1);
} }
const wordFrom = (envVar: string) => {
const value = process.env[envVar];
if (value && value != "") {
if (value.match(/^\w+$/)) return value;
else throw `Invalid color specified for ${envVar}`;
} else {
return undefined;
}
};
return { return {
port, port,
bonobUrl: url(bonobUrl), bonobUrl: url(bonobUrl),
secret: process.env["BONOB_SECRET"] || "bonob", secret: bnbEnvVar("SECRET", { default: "bonob" })!,
icons: { icons: {
foregroundColor: wordFrom("BONOB_ICON_FOREGROUND_COLOR"), foregroundColor: bnbEnvVar("ICON_FOREGROUND_COLOR", {
backgroundColor: wordFrom("BONOB_ICON_BACKGROUND_COLOR"), validationPattern: WORD,
}),
backgroundColor: bnbEnvVar("ICON_BACKGROUND_COLOR", {
validationPattern: WORD,
}),
}, },
sonos: { sonos: {
serviceName: process.env["BONOB_SONOS_SERVICE_NAME"] || "bonob", serviceName: bnbEnvVar("SONOS_SERVICE_NAME", { default: "bonob" })!,
discovery: { discovery: {
auto: enabled:
(process.env["BONOB_SONOS_DEVICE_DISCOVERY"] || "true") == "true", bnbEnvVar("SONOS_DEVICE_DISCOVERY", { default: "true" }) == "true",
seedHost: process.env["BONOB_SONOS_SEED_HOST"], seedHost: bnbEnvVar("SONOS_SEED_HOST"),
}, },
autoRegister: autoRegister:
(process.env["BONOB_SONOS_AUTO_REGISTER"] || "false") == "true", bnbEnvVar("SONOS_AUTO_REGISTER", { default: "false" }) == "true",
sid: Number(process.env["BONOB_SONOS_SERVICE_ID"] || "246"), sid: Number(bnbEnvVar("SONOS_SERVICE_ID", { default: "246" })),
}, },
navidrome: { subsonic: {
url: process.env["BONOB_NAVIDROME_URL"] || `http://${hostname()}:4533`, url: bnbEnvVar("SUBSONIC_URL", { legacy: ["BONOB_NAVIDROME_URL"], default: `http://${hostname()}:4533` })!,
customClientsFor: customClientsFor: bnbEnvVar("SUBSONIC_CUSTOM_CLIENTS", { legacy: ["BONOB_NAVIDROME_CUSTOM_CLIENTS"] }),
process.env["BONOB_NAVIDROME_CUSTOM_CLIENTS"] || undefined,
}, },
scrobbleTracks: (process.env["BONOB_SCROBBLE_TRACKS"] || "true") == "true", scrobbleTracks: bnbEnvVar("SCROBBLE_TRACKS", { default: "true" }) == "true",
reportNowPlaying: reportNowPlaying:
(process.env["BONOB_REPORT_NOW_PLAYING"] || "true") == "true", bnbEnvVar("REPORT_NOW_PLAYING", { default: "true" }) == "true",
}; };
} }

View File

@@ -41,7 +41,7 @@ export type KEY =
const translations: Record<SUPPORTED_LANG, Record<KEY, string>> = { const translations: Record<SUPPORTED_LANG, Record<KEY, string>> = {
"en-US": { "en-US": {
AppLinkMessage: "Linking sonos with $BONOB_SONOS_SERVICE_NAME", AppLinkMessage: "Linking sonos with $BNB_SONOS_SERVICE_NAME",
artists: "Artists", artists: "Artists",
albums: "Albums", albums: "Albums",
tracks: "Tracks", tracks: "Tracks",
@@ -62,7 +62,7 @@ const translations: Record<SUPPORTED_LANG, Record<KEY, string>> = {
devices: "Devices", devices: "Devices",
services: "Services", services: "Services",
login: "Login", login: "Login",
logInToBonob: "Log in to $BONOB_SONOS_SERVICE_NAME", logInToBonob: "Log in to $BNB_SONOS_SERVICE_NAME",
username: "Username", username: "Username",
password: "Password", password: "Password",
successfullyRegistered: "Successfully registered", successfullyRegistered: "Successfully registered",
@@ -75,7 +75,7 @@ const translations: Record<SUPPORTED_LANG, Record<KEY, string>> = {
noSonosDevices: "No sonos devices", noSonosDevices: "No sonos devices",
}, },
"nl-NL": { "nl-NL": {
AppLinkMessage: "Sonos koppelen aan $BONOB_SONOS_SERVICE_NAME", AppLinkMessage: "Sonos koppelen aan $BNB_SONOS_SERVICE_NAME",
artists: "Artiesten", artists: "Artiesten",
albums: "Albums", albums: "Albums",
tracks: "Nummers", tracks: "Nummers",
@@ -96,7 +96,7 @@ const translations: Record<SUPPORTED_LANG, Record<KEY, string>> = {
devices: "Apparaten", devices: "Apparaten",
services: "Services", services: "Services",
login: "Inloggen", login: "Inloggen",
logInToBonob: "Login op $BONOB_SONOS_SERVICE_NAME", logInToBonob: "Login op $BNB_SONOS_SERVICE_NAME",
username: "Gebruikersnaam", username: "Gebruikersnaam",
password: "Wachtwoord", password: "Wachtwoord",
successfullyRegistered: "Registratie geslaagd", successfullyRegistered: "Registratie geslaagd",
@@ -151,7 +151,7 @@ export default (serviceName: string): I8N =>
translations["en-US"]; translations["en-US"];
return (key: KEY) => { return (key: KEY) => {
const value = langToUse[key]?.replace( const value = langToUse[key]?.replace(
"$BONOB_SONOS_SERVICE_NAME", "$BNB_SONOS_SERVICE_NAME",
serviceName serviceName
); );
if (value) return value; if (value) return value;

View File

@@ -52,6 +52,7 @@ export type AlbumSummary = {
name: string; name: string;
year: string | undefined; year: string | undefined;
genre: Genre | undefined; genre: Genre | undefined;
coverArt: string | undefined;
artistName: string; artistName: string;
artistId: string; artistId: string;
@@ -71,6 +72,7 @@ export type Track = {
duration: number; duration: number;
number: number | undefined; number: number | undefined;
genre: Genre | undefined; genre: Genre | undefined;
coverArt: string | undefined;
album: AlbumSummary; album: AlbumSummary;
artist: ArtistSummary; artist: ArtistSummary;
}; };
@@ -118,6 +120,7 @@ export const albumToAlbumSummary = (it: Album): AlbumSummary => ({
genre: it.genre, genre: it.genre,
artistName: it.artistName, artistName: it.artistName,
artistId: it.artistId, artistId: it.artistId,
coverArt: it.coverArt
}); });
export const playlistToPlaylistSummary = (it: Playlist): PlaylistSummary => ({ export const playlistToPlaylistSummary = (it: Playlist): PlaylistSummary => ({
@@ -174,7 +177,7 @@ export interface MusicLibrary {
trackId: string; trackId: string;
range: string | undefined; range: string | undefined;
}): Promise<TrackStream>; }): Promise<TrackStream>;
coverArt(id: string, type: "album" | "artist", size?: number): Promise<CoverArt | undefined>; coverArt(id: string, size?: number): Promise<CoverArt | undefined>;
nowPlaying(id: string): Promise<boolean> nowPlaying(id: string): Promise<boolean>
scrobble(id: string): Promise<boolean> scrobble(id: string): Promise<boolean>
searchArtists(query: string): Promise<ArtistSummary[]>; searchArtists(query: string): Promise<ArtistSummary[]>;

View File

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

View File

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

View File

@@ -3,6 +3,7 @@ import express, { Express, Request } from "express";
import * as Eta from "eta"; import * as Eta from "eta";
import path from "path"; import path from "path";
import sharp from "sharp"; import sharp from "sharp";
import { v4 as uuid } from "uuid";
import { PassThrough, Transform, TransformCallback } from "stream"; import { PassThrough, Transform, TransformCallback } from "stream";
@@ -15,6 +16,7 @@ import {
LOGIN_ROUTE, LOGIN_ROUTE,
CREATE_REGISTRATION_ROUTE, CREATE_REGISTRATION_ROUTE,
REMOVE_REGISTRATION_ROUTE, REMOVE_REGISTRATION_ROUTE,
sonosifyMimeType,
} from "./smapi"; } from "./smapi";
import { LinkCodes, InMemoryLinkCodes } from "./link_codes"; import { LinkCodes, InMemoryLinkCodes } from "./link_codes";
import { MusicService, isSuccess } from "./music_service"; import { MusicService, isSuccess } from "./music_service";
@@ -288,11 +290,15 @@ function server(
app.get("/stream/track/:id", async (req, res) => { app.get("/stream/track/:id", async (req, res) => {
const id = req.params["id"]!; const id = req.params["id"]!;
const trace = uuid();
logger.info( logger.info(
`-> /stream/track/${id}, headers=${JSON.stringify(req.headers)}` `${trace} bnb<- ${req.method} ${req.path}?${
JSON.stringify(req.query)
}, headers=${JSON.stringify(req.headers)}`
); );
const authToken = pipe( const authToken = pipe(
req.header(BONOB_ACCESS_TOKEN_HEADER), req.query[BONOB_ACCESS_TOKEN_HEADER] as string,
O.fromNullable, O.fromNullable,
O.map((accessToken) => accessTokens.authTokenFor(accessToken)), O.map((accessToken) => accessTokens.authTokenFor(accessToken)),
O.getOrElseW(() => undefined) O.getOrElseW(() => undefined)
@@ -310,42 +316,44 @@ function server(
}) })
.then((stream) => ({ musicLibrary: it, stream })) .then((stream) => ({ musicLibrary: it, stream }))
) )
.then(({ musicLibrary, stream }) => { .then(({ stream }) => {
logger.info( logger.info(
`stream response from music service for ${id}, status=${ `${trace} bnb<- stream response from music service for ${id}, status=${
stream.status stream.status
}, headers=(${JSON.stringify(stream.headers)})` }, headers=(${JSON.stringify(stream.headers)})`
); );
const sonosisfyContentType = (contentType: string) =>
contentType
.split(";")
.map((it) => it.trim())
.map((it) => sonosifyMimeType(it))
.join("; ");
const respondWith = ({ const respondWith = ({
status, status,
filter, filter,
headers, headers,
sendStream, sendStream,
nowPlaying,
}: { }: {
status: number; status: number;
filter: Transform; filter: Transform;
headers: Record<string, string | undefined>; headers: Record<string, string>;
sendStream: boolean; sendStream: boolean;
nowPlaying: boolean;
}) => { }) => {
logger.info( logger.info(
`<- /stream/track/${id}, status=${status}, headers=${JSON.stringify( `${trace} bnb-> ${
headers req.path
)}` }, status=${status}, headers=${JSON.stringify(headers)}`
); );
(nowPlaying
? musicLibrary.nowPlaying(id)
: Promise.resolve(true)
).then((_) => {
res.status(status); res.status(status);
Object.entries(stream.headers) Object.entries(headers)
.filter(([_, v]) => v !== undefined) .filter(([_, v]) => v !== undefined)
.forEach(([header, value]) => res.setHeader(header, value)); .forEach(([header, value]) => {
res.setHeader(header, value!);
});
if (sendStream) stream.stream.pipe(filter).pipe(res); if (sendStream) stream.stream.pipe(filter).pipe(res);
else res.send(); else res.send();
});
}; };
if (stream.status == 200) { if (stream.status == 200) {
@@ -353,25 +361,27 @@ function server(
status: 200, status: 200,
filter: new PassThrough(), filter: new PassThrough(),
headers: { headers: {
"content-type": stream.headers["content-type"], "content-type": sonosisfyContentType(
stream.headers["content-type"]
),
"content-length": stream.headers["content-length"], "content-length": stream.headers["content-length"],
"accept-ranges": stream.headers["accept-ranges"], "accept-ranges": stream.headers["accept-ranges"],
}, },
sendStream: req.method == "GET", sendStream: req.method == "GET",
nowPlaying: req.method == "GET",
}); });
} else if (stream.status == 206) { } else if (stream.status == 206) {
respondWith({ respondWith({
status: 206, status: 206,
filter: new PassThrough(), filter: new PassThrough(),
headers: { headers: {
"content-type": stream.headers["content-type"], "content-type": sonosisfyContentType(
stream.headers["content-type"]
),
"content-length": stream.headers["content-length"], "content-length": stream.headers["content-length"],
"content-range": stream.headers["content-range"], "content-range": stream.headers["content-range"],
"accept-ranges": stream.headers["accept-ranges"], "accept-ranges": stream.headers["accept-ranges"],
}, },
sendStream: req.method == "GET", sendStream: req.method == "GET",
nowPlaying: req.method == "GET",
}); });
} else { } else {
respondWith({ respondWith({
@@ -379,7 +389,6 @@ function server(
filter: new PassThrough(), filter: new PassThrough(),
headers: {}, headers: {},
sendStream: req.method == "GET", sendStream: req.method == "GET",
nowPlaying: false,
}); });
} }
}); });
@@ -457,25 +466,22 @@ function server(
"centre", "centre",
]; ];
app.get("/art/:type/:ids/size/:size", (req, res) => { app.get("/art/:ids/size/:size", (req, res) => {
const authToken = accessTokens.authTokenFor( const authToken = accessTokens.authTokenFor(
req.query[BONOB_ACCESS_TOKEN_HEADER] as string req.query[BONOB_ACCESS_TOKEN_HEADER] as string
); );
const type = req.params["type"]!;
const ids = req.params["ids"]!.split("&"); const ids = req.params["ids"]!.split("&");
const size = Number.parseInt(req.params["size"]!); const size = Number.parseInt(req.params["size"]!);
if (!authToken) { if (!authToken) {
return res.status(401).send(); return res.status(401).send();
} else if (type != "artist" && type != "album") {
return res.status(400).send();
} else if (!(size > 0)) { } else if (!(size > 0)) {
return res.status(400).send(); return res.status(400).send();
} }
return musicService return musicService
.login(authToken) .login(authToken)
.then((it) => Promise.all(ids.map((id) => it.coverArt(id, type, size)))) .then((it) => Promise.all(ids.map((id) => it.coverArt(id, size))))
.then((coverArts) => coverArts.filter((it) => it)) .then((coverArts) => coverArts.filter((it) => it))
.then(shuffle) .then(shuffle)
.then((coverArts) => { .then((coverArts) => {
@@ -513,12 +519,9 @@ function server(
} }
}) })
.catch((e: Error) => { .catch((e: Error) => {
logger.error( logger.error(`Failed fetching image ${ids.join("&")}/size/${size}`, {
`Failed fetching image ${type}/${ids.join("&")}/size/${size}`,
{
cause: e, cause: e,
} });
);
return res.status(500).send(); return res.status(500).send();
}); });
}); });

View File

@@ -18,7 +18,6 @@ import {
Track, Track,
} from "./music_service"; } from "./music_service";
import { AccessTokens } from "./access_tokens"; import { AccessTokens } from "./access_tokens";
import { BONOB_ACCESS_TOKEN_HEADER } from "./server";
import { Clock } from "./clock"; import { Clock } from "./clock";
import { URLBuilder } from "./url_builder"; import { URLBuilder } from "./url_builder";
import { asLANGs, I8N } from "./i8n"; import { asLANGs, I8N } from "./i8n";
@@ -215,10 +214,7 @@ const genre = (bonobUrl: URLBuilder, genre: Genre) => ({
itemType: "container", itemType: "container",
id: `genre:${genre.id}`, id: `genre:${genre.id}`,
title: genre.name, title: genre.name,
albumArtURI: iconArtURI( albumArtURI: iconArtURI(bonobUrl, iconForGenre(genre.name)).href(),
bonobUrl,
iconForGenre(genre.name)
).href(),
}); });
const playlist = (bonobUrl: URLBuilder, playlist: Playlist) => ({ const playlist = (bonobUrl: URLBuilder, playlist: Playlist) => ({
@@ -238,31 +234,38 @@ export const playlistAlbumArtURL = (
bonobUrl: URLBuilder, bonobUrl: URLBuilder,
playlist: Playlist playlist: Playlist
) => { ) => {
const ids = uniq(playlist.entries.map((it) => it.album?.id).filter((it) => it)); const ids = uniq(
playlist.entries.map((it) => it.coverArt).filter((it) => it)
);
if (ids.length == 0) { if (ids.length == 0) {
return iconArtURI(bonobUrl, "error"); return iconArtURI(bonobUrl, "error");
} else { } else {
return bonobUrl.append({ return bonobUrl.append({
pathname: `/art/album/${ids.slice(0, 9).join("&")}/size/180` pathname: `/art/${ids.slice(0, 9).join("&")}/size/180`,
}); });
} }
}; };
export const defaultAlbumArtURI = (bonobUrl: URLBuilder, album: AlbumSummary) => export const defaultAlbumArtURI = (
bonobUrl.append({ pathname: `/art/album/${album.id}/size/180` });
export const iconArtURI = (
bonobUrl: URLBuilder, bonobUrl: URLBuilder,
icon: ICON { coverArt }: { coverArt: string | undefined }
) => ) =>
coverArt
? bonobUrl.append({ pathname: `/art/${coverArt}/size/180` })
: iconArtURI(bonobUrl, "vinyl");
export const iconArtURI = (bonobUrl: URLBuilder, icon: ICON) =>
bonobUrl.append({ bonobUrl.append({
pathname: `/icon/${icon}/size/legacy` pathname: `/icon/${icon}/size/legacy`,
}); });
export const defaultArtistArtURI = ( export const defaultArtistArtURI = (
bonobUrl: URLBuilder, bonobUrl: URLBuilder,
artist: ArtistSummary artist: ArtistSummary
) => bonobUrl.append({ pathname: `/art/artist/${artist.id}/size/180` }); ) => bonobUrl.append({ pathname: `/art/artist:${artist.id}/size/180` });
export const sonosifyMimeType = (mimeType: string) =>
mimeType == "audio/x-flac" ? "audio/flac" : mimeType;
export const album = (bonobUrl: URLBuilder, album: AlbumSummary) => ({ export const album = (bonobUrl: URLBuilder, album: AlbumSummary) => ({
itemType: "album", itemType: "album",
@@ -281,17 +284,17 @@ export const album = (bonobUrl: URLBuilder, album: AlbumSummary) => ({
export const track = (bonobUrl: URLBuilder, track: Track) => ({ export const track = (bonobUrl: URLBuilder, track: Track) => ({
itemType: "track", itemType: "track",
id: `track:${track.id}`, id: `track:${track.id}`,
mimeType: track.mimeType, mimeType: sonosifyMimeType(track.mimeType),
title: track.name, title: track.name,
trackMetadata: { trackMetadata: {
album: track.album.name, album: track.album.name,
albumId: track.album.id, albumId: `album:${track.album.id}`,
albumArtist: track.artist.name, albumArtist: track.artist.name,
albumArtistId: track.artist.id, albumArtistId: `artist:${track.artist.id}`,
albumArtURI: defaultAlbumArtURI(bonobUrl, track.album).href(), albumArtURI: defaultAlbumArtURI(bonobUrl, track).href(),
artist: track.artist.name, artist: track.artist.name,
artistId: track.artist.id, artistId: `artist:${track.artist.id}`,
duration: track.duration, duration: track.duration,
genre: track.album.genre?.name, genre: track.album.genre?.name,
genreId: track.album.genre?.id, genreId: track.album.genre?.id,
@@ -368,7 +371,7 @@ function bindSmapiSoapServiceToExpress(
const urlWithToken = (accessToken: string) => const urlWithToken = (accessToken: string) =>
bonobUrl.append({ bonobUrl.append({
searchParams: { searchParams: {
"bat": accessToken, bat: accessToken,
}, },
}); });
@@ -400,14 +403,9 @@ function bindSmapiSoapServiceToExpress(
getMediaURIResult: bonobUrl getMediaURIResult: bonobUrl
.append({ .append({
pathname: `/stream/${type}/${typeId}`, pathname: `/stream/${type}/${typeId}`,
searchParams: { "bat": accessToken }
}) })
.href(), .href(),
httpHeaders: [
{
header: BONOB_ACCESS_TOKEN_HEADER,
value: accessToken,
},
],
})), })),
getMediaMetadata: async ( getMediaMetadata: async (
{ id }: { id: string }, { id }: { id: string },
@@ -506,23 +504,7 @@ function bindSmapiSoapServiceToExpress(
return musicLibrary.track(typeId).then((it) => ({ return musicLibrary.track(typeId).then((it) => ({
getExtendedMetadataResult: { getExtendedMetadataResult: {
mediaMetadata: { mediaMetadata: {
id: `track:${it.id}`, ...track(urlWithToken(accessToken), it),
itemType: "track",
title: it.name,
mimeType: it.mimeType,
trackMetadata: {
artistId: `artist:${it.artist.id}`,
artist: it.artist.name,
albumId: `album:${it.album.id}`,
album: it.album.name,
genre: it.genre?.name,
genreId: it.genre?.id,
duration: it.duration,
albumArtURI: defaultAlbumArtURI(
urlWithToken(accessToken),
it.album
).href(),
},
}, },
}, },
})); }));
@@ -900,18 +882,29 @@ function bindSmapiSoapServiceToExpress(
.then(({ musicLibrary, type, typeId }) => { .then(({ musicLibrary, type, typeId }) => {
switch (type) { switch (type) {
case "track": case "track":
musicLibrary.track(typeId).then(({ duration }) => { return musicLibrary
.track(typeId)
.then(({ duration }) => {
if ( if (
(duration < 30 && +seconds >= 10) || (duration < 30 && +seconds >= 10) ||
(duration >= 30 && +seconds >= 30) (duration >= 30 && +seconds >= 30)
) { ) {
musicLibrary.scrobble(typeId); return musicLibrary.scrobble(typeId);
} else {
return Promise.resolve(true);
}
})
.then(() => {
if (+seconds > 0) {
return musicLibrary.nowPlaying(typeId);
} else {
return Promise.resolve(true);
} }
}); });
break; break;
default: default:
logger.info("Unsupported scrobble", { id, seconds }); logger.info("Unsupported scrobble", { id, seconds });
break; return Promise.resolve(true);
} }
}) })
.then((_) => ({ .then((_) => ({

View File

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

View File

@@ -21,11 +21,13 @@ import {
} from "./music_service"; } from "./music_service";
import X2JS from "x2js"; import X2JS from "x2js";
import sharp from "sharp"; import sharp from "sharp";
import _, { pick } from "underscore"; import _ from "underscore";
import axios, { AxiosRequestConfig } from "axios"; import axios, { AxiosRequestConfig } from "axios";
import { Encryption } from "./encryption"; import { Encryption } from "./encryption";
import randomString from "./random_string"; import randomString from "./random_string";
import { b64Encode, b64Decode } from "./b64";
import logger from "./logger";
export const BROWSER_HEADERS = { export const BROWSER_HEADERS = {
accept: accept:
@@ -147,7 +149,7 @@ export type song = {
_artist: string; _artist: string;
_track: string | undefined; _track: string | undefined;
_genre: string; _genre: string;
_coverArt: string; _coverArt: string | undefined;
_created: "2004-11-08T23:36:11"; _created: "2004-11-08T23:36:11";
_duration: string | undefined; _duration: string | undefined;
_bitRate: "128"; _bitRate: "128";
@@ -178,6 +180,7 @@ export type entry = {
_track: string; _track: string;
_year: string; _year: string;
_genre: string; _genre: string;
_coverArt: string;
_contentType: string; _contentType: string;
_duration: string; _duration: string;
_albumId: string; _albumId: string;
@@ -222,6 +225,12 @@ export function isError(
return (subsonicResponse as SubsonicError).error !== undefined; return (subsonicResponse as SubsonicError).error !== undefined;
} }
export const splitCoverArtId = (coverArt: string): [string, string] => {
const parts = coverArt.split(":").filter((it) => it.length > 0);
if (parts.length < 2) throw `'${coverArt}' is an invalid coverArt id'`;
return [parts[0]!, parts.slice(1).join(":")];
};
export type IdName = { export type IdName = {
id: string; id: string;
name: string; name: string;
@@ -238,6 +247,9 @@ export type getAlbumListParams = {
export const MAX_ALBUM_LIST = 500; export const MAX_ALBUM_LIST = 500;
const maybeAsCoverArt = (coverArt: string | undefined) =>
coverArt ? `coverArt:${coverArt}` : undefined;
const asTrack = (album: Album, song: song) => ({ const asTrack = (album: Album, song: song) => ({
id: song._id, id: song._id,
name: song._title, name: song._title,
@@ -245,6 +257,7 @@ const asTrack = (album: Album, song: song) => ({
duration: parseInt(song._duration || "0"), duration: parseInt(song._duration || "0"),
number: parseInt(song._track || "0"), number: parseInt(song._track || "0"),
genre: maybeAsGenre(song._genre), genre: maybeAsGenre(song._genre),
coverArt: maybeAsCoverArt(song._coverArt),
album, album,
artist: { artist: {
id: song._artistId, id: song._artistId,
@@ -259,10 +272,11 @@ const asAlbum = (album: album) => ({
genre: maybeAsGenre(album._genre), genre: maybeAsGenre(album._genre),
artistId: album._artistId, artistId: album._artistId,
artistName: album._artist, artistName: album._artist,
coverArt: maybeAsCoverArt(album._coverArt),
}); });
export const asGenre = (genreName: string) => ({ export const asGenre = (genreName: string) => ({
id: genreName, id: b64Encode(genreName),
name: genreName, name: genreName,
}); });
@@ -297,7 +311,7 @@ export const asURLSearchParams = (q: any) => {
return urlSearchParams; return urlSearchParams;
}; };
export class Navidrome implements MusicService { export class Subsonic implements MusicService {
url: string; url: string;
encryption: Encryption; encryption: Encryption;
streamClientApplication: StreamClientApplication; streamClientApplication: StreamClientApplication;
@@ -334,7 +348,7 @@ export class Navidrome implements MusicService {
}) })
.then((response) => { .then((response) => {
if (response.status != 200 && response.status != 206) { if (response.status != 200 && response.status != 206) {
throw `Navidrome failed with a ${response.status || "no!"} status`; throw `Subsonic failed with a ${response.status || "no!"} status`;
} else return response; } else return response;
}); });
@@ -367,27 +381,23 @@ export class Navidrome implements MusicService {
) )
.then((json) => json["subsonic-response"]) .then((json) => json["subsonic-response"])
.then((json) => { .then((json) => {
if (isError(json)) throw `Navidrome error:${json.error._message}`; if (isError(json)) throw `Subsonic error:${json.error._message}`;
else return json as unknown as T; else return json as unknown as T;
}); });
generateToken = async (credentials: Credentials) => generateToken = async (credentials: Credentials) =>
this.getJSON(credentials, "/rest/ping.view") this.getJSON(credentials, "/rest/ping.view")
.then(() => ({ .then(() => ({
authToken: Buffer.from( authToken: b64Encode(
JSON.stringify(this.encryption.encrypt(JSON.stringify(credentials))) JSON.stringify(this.encryption.encrypt(JSON.stringify(credentials)))
).toString("base64"), ),
userId: credentials.username, userId: credentials.username,
nickname: credentials.username, nickname: credentials.username,
})) }))
.catch((e) => ({ message: `${e}` })); .catch((e) => ({ message: `${e}` }));
parseToken = (token: string): Credentials => parseToken = (token: string): Credentials =>
JSON.parse( JSON.parse(this.encryption.decrypt(JSON.parse(b64Decode(token))));
this.encryption.decrypt(
JSON.parse(Buffer.from(token, "base64").toString("ascii"))
)
);
getArtists = ( getArtists = (
credentials: Credentials credentials: Credentials
@@ -430,6 +440,7 @@ export class Navidrome implements MusicService {
genre: maybeAsGenre(album._genre), genre: maybeAsGenre(album._genre),
artistId: album._artistId, artistId: album._artistId,
artistName: album._artist, artistName: album._artist,
coverArt: maybeAsCoverArt(album._coverArt),
})); }));
getArtist = ( getArtist = (
@@ -443,14 +454,7 @@ export class Navidrome implements MusicService {
.then((it) => ({ .then((it) => ({
id: it._id, id: it._id,
name: it._name, name: it._name,
albums: (it.album || []).map((album) => ({ albums: this.toAlbumSummary(it.album || []),
id: album._id,
name: album._name,
year: album._year,
genre: maybeAsGenre(album._genre),
artistId: it._id,
artistName: it._name,
})),
})); }));
getArtistWithInfo = (credentials: Credentials, id: string) => getArtistWithInfo = (credentials: Credentials, id: string) =>
@@ -490,6 +494,7 @@ export class Navidrome implements MusicService {
genre: maybeAsGenre(album._genre), genre: maybeAsGenre(album._genre),
artistId: album._artistId, artistId: album._artistId,
artistName: album._artist, artistName: album._artist,
coverArt: maybeAsCoverArt(album._coverArt),
})); }));
search3 = (credentials: Credentials, q: any) => search3 = (credentials: Credentials, q: any) =>
@@ -505,12 +510,12 @@ export class Navidrome implements MusicService {
})); }));
async login(token: string) { async login(token: string) {
const navidrome = this; const subsonic = this;
const credentials: Credentials = this.parseToken(token); const credentials: Credentials = this.parseToken(token);
const musicLibrary: MusicLibrary = { const musicLibrary: MusicLibrary = {
artists: (q: ArtistQuery): Promise<Result<ArtistSummary>> => artists: (q: ArtistQuery): Promise<Result<ArtistSummary>> =>
navidrome subsonic
.getArtists(credentials) .getArtists(credentials)
.then(slice2(q)) .then(slice2(q))
.then(([page, total]) => ({ .then(([page, total]) => ({
@@ -518,34 +523,30 @@ export class Navidrome implements MusicService {
results: page.map((it) => ({ id: it.id, name: it.name })), results: page.map((it) => ({ id: it.id, name: it.name })),
})), })),
artist: async (id: string): Promise<Artist> => artist: async (id: string): Promise<Artist> =>
navidrome.getArtistWithInfo(credentials, id), subsonic.getArtistWithInfo(credentials, id),
albums: async (q: AlbumQuery): Promise<Result<AlbumSummary>> => { albums: async (q: AlbumQuery): Promise<Result<AlbumSummary>> =>
return Promise.all([ Promise.all([
navidrome subsonic
.getArtists(credentials) .getArtists(credentials)
.then((it) => .then((it) =>
_.inject(it, (total, artist) => total + artist.albumCount, 0) _.inject(it, (total, artist) => total + artist.albumCount, 0)
), ),
navidrome subsonic
.getJSON<GetAlbumListResponse>(credentials, "/rest/getAlbumList2", { .getJSON<GetAlbumListResponse>(credentials, "/rest/getAlbumList2", {
...pick(q, "type", "genre"), type: q.type,
...(q.genre ? { genre: b64Decode(q.genre) } : {}),
size: 500, size: 500,
offset: q._index, offset: q._index,
}) })
.then((response) => response.albumList2.album || []) .then((response) => response.albumList2.album || [])
.then(navidrome.toAlbumSummary), .then(subsonic.toAlbumSummary),
]).then(([total, albums]) => ({ ]).then(([total, albums]) => ({
results: albums.slice(0, q._count), results: albums.slice(0, q._count),
total: total: albums.length == 500 ? total : q._index + albums.length,
albums.length == 500 })),
? total album: (id: string): Promise<Album> => subsonic.getAlbum(credentials, id),
: q._index + albums.length,
}));
},
album: (id: string): Promise<Album> =>
navidrome.getAlbum(credentials, id),
genres: () => genres: () =>
navidrome subsonic
.getJSON<GetGenresResponse>(credentials, "/rest/getGenres") .getJSON<GetGenresResponse>(credentials, "/rest/getGenres")
.then((it) => .then((it) =>
pipe( pipe(
@@ -553,11 +554,11 @@ export class Navidrome implements MusicService {
A.filter((it) => Number.parseInt(it._albumCount) > 0), A.filter((it) => Number.parseInt(it._albumCount) > 0),
A.map((it) => it.__text), A.map((it) => it.__text),
A.sort(ordString), A.sort(ordString),
A.map((it) => ({ id: it, name: it })) A.map((it) => ({ id: b64Encode(it), name: it }))
) )
), ),
tracks: (albumId: string) => tracks: (albumId: string) =>
navidrome subsonic
.getJSON<GetAlbumResponse>(credentials, "/rest/getAlbum", { .getJSON<GetAlbumResponse>(credentials, "/rest/getAlbum", {
id: albumId, id: albumId,
}) })
@@ -565,7 +566,7 @@ export class Navidrome implements MusicService {
.then((album) => .then((album) =>
(album.song || []).map((song) => asTrack(asAlbum(album), song)) (album.song || []).map((song) => asTrack(asAlbum(album), song))
), ),
track: (trackId: string) => navidrome.getTrack(credentials, trackId), track: (trackId: string) => subsonic.getTrack(credentials, trackId),
stream: async ({ stream: async ({
trackId, trackId,
range, range,
@@ -573,8 +574,8 @@ export class Navidrome implements MusicService {
trackId: string; trackId: string;
range: string | undefined; range: string | undefined;
}) => }) =>
navidrome.getTrack(credentials, trackId).then((track) => subsonic.getTrack(credentials, trackId).then((track) =>
navidrome subsonic
.get( .get(
credentials, credentials,
`/rest/stream`, `/rest/stream`,
@@ -608,14 +609,26 @@ export class Navidrome implements MusicService {
stream: res.data, stream: res.data,
})) }))
), ),
coverArt: async (id: string, type: "album" | "artist", size?: number) => { coverArt: async (coverArt: string, size?: number) => {
if (type == "album") { const [type, id] = splitCoverArtId(coverArt);
return navidrome.getCoverArt(credentials, id, size).then((res) => ({ if (type == "coverArt") {
return subsonic
.getCoverArt(credentials, id, size)
.then((res) => ({
contentType: res.headers["content-type"], contentType: res.headers["content-type"],
data: Buffer.from(res.data, "binary"), data: Buffer.from(res.data, "binary"),
})); }))
.catch((e) => {
logger.error(`Failed getting coverArt ${coverArt}: ${e}`);
return undefined;
});
} else { } else {
return navidrome.getArtistWithInfo(credentials, id).then((artist) => { return subsonic
.getArtistWithInfo(credentials, id)
.then((artist) => {
const albumsWithCoverArt = artist.albums.filter(
(it) => it.coverArt
);
if (artist.image.large) { if (artist.image.large) {
return axios return axios
.get(artist.image.large!, { .get(artist.image.large!, {
@@ -639,9 +652,13 @@ export class Navidrome implements MusicService {
}; };
} }
}); });
} else if (artist.albums.length > 0) { } else if (albumsWithCoverArt.length > 0) {
return navidrome return subsonic
.getCoverArt(credentials, artist.albums[0]!.id, size) .getCoverArt(
credentials,
splitCoverArtId(albumsWithCoverArt[0]!.coverArt!)[1],
size
)
.then((res) => ({ .then((res) => ({
contentType: res.headers["content-type"], contentType: res.headers["content-type"],
data: Buffer.from(res.data, "binary"), data: Buffer.from(res.data, "binary"),
@@ -649,11 +666,15 @@ export class Navidrome implements MusicService {
} else { } else {
return undefined; return undefined;
} }
})
.catch((e) => {
logger.error(`Failed getting coverArt ${coverArt}: ${e}`);
return undefined;
}); });
} }
}, },
scrobble: async (id: string) => scrobble: async (id: string) =>
navidrome subsonic
.get(credentials, `/rest/scrobble`, { .get(credentials, `/rest/scrobble`, {
id, id,
submission: true, submission: true,
@@ -661,7 +682,7 @@ export class Navidrome implements MusicService {
.then((_) => true) .then((_) => true)
.catch(() => false), .catch(() => false),
nowPlaying: async (id: string) => nowPlaying: async (id: string) =>
navidrome subsonic
.get(credentials, `/rest/scrobble`, { .get(credentials, `/rest/scrobble`, {
id, id,
submission: false, submission: false,
@@ -669,7 +690,7 @@ export class Navidrome implements MusicService {
.then((_) => true) .then((_) => true)
.catch(() => false), .catch(() => false),
searchArtists: async (query: string) => searchArtists: async (query: string) =>
navidrome subsonic
.search3(credentials, { query, artistCount: 20 }) .search3(credentials, { query, artistCount: 20 })
.then(({ artists }) => .then(({ artists }) =>
artists.map((artist) => ({ artists.map((artist) => ({
@@ -678,26 +699,26 @@ export class Navidrome implements MusicService {
})) }))
), ),
searchAlbums: async (query: string) => searchAlbums: async (query: string) =>
navidrome subsonic
.search3(credentials, { query, albumCount: 20 }) .search3(credentials, { query, albumCount: 20 })
.then(({ albums }) => navidrome.toAlbumSummary(albums)), .then(({ albums }) => subsonic.toAlbumSummary(albums)),
searchTracks: async (query: string) => searchTracks: async (query: string) =>
navidrome subsonic
.search3(credentials, { query, songCount: 20 }) .search3(credentials, { query, songCount: 20 })
.then(({ songs }) => .then(({ songs }) =>
Promise.all( Promise.all(
songs.map((it) => navidrome.getTrack(credentials, it._id)) songs.map((it) => subsonic.getTrack(credentials, it._id))
) )
), ),
playlists: async () => playlists: async () =>
navidrome subsonic
.getJSON<GetPlaylistsResponse>(credentials, "/rest/getPlaylists") .getJSON<GetPlaylistsResponse>(credentials, "/rest/getPlaylists")
.then((it) => it.playlists.playlist || []) .then((it) => it.playlists.playlist || [])
.then((playlists) => .then((playlists) =>
playlists.map((it) => ({ id: it._id, name: it._name })) playlists.map((it) => ({ id: it._id, name: it._name }))
), ),
playlist: async (id: string) => playlist: async (id: string) =>
navidrome subsonic
.getJSON<GetPlaylistResponse>(credentials, "/rest/getPlaylist", { .getJSON<GetPlaylistResponse>(credentials, "/rest/getPlaylist", {
id, id,
}) })
@@ -714,6 +735,7 @@ export class Navidrome implements MusicService {
duration: parseInt(entry._duration || "0"), duration: parseInt(entry._duration || "0"),
number: trackNumber++, number: trackNumber++,
genre: maybeAsGenre(entry._genre), genre: maybeAsGenre(entry._genre),
coverArt: maybeAsCoverArt(entry._coverArt),
album: { album: {
id: entry._albumId, id: entry._albumId,
name: entry._album, name: entry._album,
@@ -721,6 +743,7 @@ export class Navidrome implements MusicService {
genre: maybeAsGenre(entry._genre), genre: maybeAsGenre(entry._genre),
artistName: entry._artist, artistName: entry._artist,
artistId: entry._artistId, artistId: entry._artistId,
coverArt: maybeAsCoverArt(entry._coverArt),
}, },
artist: { artist: {
id: entry._artistId, id: entry._artistId,
@@ -730,34 +753,34 @@ export class Navidrome implements MusicService {
}; };
}), }),
createPlaylist: async (name: string) => createPlaylist: async (name: string) =>
navidrome subsonic
.getJSON<GetPlaylistResponse>(credentials, "/rest/createPlaylist", { .getJSON<GetPlaylistResponse>(credentials, "/rest/createPlaylist", {
name, name,
}) })
.then((it) => it.playlist) .then((it) => it.playlist)
.then((it) => ({ id: it._id, name: it._name })), .then((it) => ({ id: it._id, name: it._name })),
deletePlaylist: async (id: string) => deletePlaylist: async (id: string) =>
navidrome subsonic
.getJSON<GetPlaylistResponse>(credentials, "/rest/deletePlaylist", { .getJSON<GetPlaylistResponse>(credentials, "/rest/deletePlaylist", {
id, id,
}) })
.then((_) => true), .then((_) => true),
addToPlaylist: async (playlistId: string, trackId: string) => addToPlaylist: async (playlistId: string, trackId: string) =>
navidrome subsonic
.getJSON<GetPlaylistResponse>(credentials, "/rest/updatePlaylist", { .getJSON<GetPlaylistResponse>(credentials, "/rest/updatePlaylist", {
playlistId, playlistId,
songIdToAdd: trackId, songIdToAdd: trackId,
}) })
.then((_) => true), .then((_) => true),
removeFromPlaylist: async (playlistId: string, indicies: number[]) => removeFromPlaylist: async (playlistId: string, indicies: number[]) =>
navidrome subsonic
.getJSON<GetPlaylistResponse>(credentials, "/rest/updatePlaylist", { .getJSON<GetPlaylistResponse>(credentials, "/rest/updatePlaylist", {
playlistId, playlistId,
songIndexToRemove: indicies, songIndexToRemove: indicies,
}) })
.then((_) => true), .then((_) => true),
similarSongs: async (id: string) => similarSongs: async (id: string) =>
navidrome subsonic
.getJSON<GetSimilarSongsResponse>( .getJSON<GetSimilarSongsResponse>(
credentials, credentials,
"/rest/getSimilarSongs2", "/rest/getSimilarSongs2",
@@ -767,15 +790,15 @@ export class Navidrome implements MusicService {
.then((songs) => .then((songs) =>
Promise.all( Promise.all(
songs.map((song) => songs.map((song) =>
navidrome subsonic
.getAlbum(credentials, song._albumId) .getAlbum(credentials, song._albumId)
.then((album) => asTrack(album, song)) .then((album) => asTrack(album, song))
) )
) )
), ),
topSongs: async (artistId: string) => topSongs: async (artistId: string) =>
navidrome.getArtist(credentials, artistId).then(({ name }) => subsonic.getArtist(credentials, artistId).then(({ name }) =>
navidrome subsonic
.getJSON<GetTopSongsResponse>(credentials, "/rest/getTopSongs", { .getJSON<GetTopSongsResponse>(credentials, "/rest/getTopSongs", {
artist: name, artist: name,
count: 50, count: 50,
@@ -784,7 +807,7 @@ export class Navidrome implements MusicService {
.then((songs) => .then((songs) =>
Promise.all( Promise.all(
songs.map((song) => songs.map((song) =>
navidrome subsonic
.getAlbum(credentials, song._albumId) .getAlbum(credentials, song._albumId)
.then((album) => asTrack(album, song)) .then((album) => asTrack(album, song))
) )

17
tests/b64.test.ts Normal file
View File

@@ -0,0 +1,17 @@
import { b64Encode, b64Decode } from "../src/b64";
describe("b64", () => {
const value = "foobar100";
const encoded = Buffer.from(value).toString("base64");
describe("encode", () => {
it("should encode", () => {
expect(b64Encode(value)).toEqual(encoded);
});
});
describe("decode", () => {
it("should decode", () => {
expect(b64Decode(encoded)).toEqual(value);
});
});
});

View File

@@ -5,6 +5,7 @@ import { Credentials } from "../src/smapi";
import { Service, Device } from "../src/sonos"; import { Service, Device } from "../src/sonos";
import { Album, Artist, Track, albumToAlbumSummary, artistToArtistSummary, PlaylistSummary, Playlist } from "../src/music_service"; import { Album, Artist, Track, albumToAlbumSummary, artistToArtistSummary, PlaylistSummary, Playlist } from "../src/music_service";
import randomString from "../src/random_string"; import randomString from "../src/random_string";
import { b64Encode } from "../src/b64";
const randomInt = (max: number) => Math.floor(Math.random() * Math.floor(max)); const randomInt = (max: number) => Math.floor(Math.random() * Math.floor(max));
const randomIpAddress = () => `127.0.${randomInt(255)}.${randomInt(255)}`; const randomIpAddress = () => `127.0.${randomInt(255)}.${randomInt(255)}`;
@@ -111,16 +112,18 @@ export function anArtist(fields: Partial<Artist> = {}): Artist {
return artist; return artist;
} }
export const HIP_HOP = { id: "genre_hip_hop", name: "Hip-Hop" }; export const aGenre = (name: string) => ({ id: b64Encode(name), name })
export const METAL = { id: "genre_metal", name: "Metal" };
export const NEW_WAVE = { id: "genre_new_wave", name: "New Wave" }; export const HIP_HOP = aGenre("Hip-Hop");
export const POP = { id: "genre_pop", name: "Pop" }; export const METAL = aGenre("Metal");
export const POP_ROCK = { id: "genre_pop_rock", name: "Pop Rock" }; export const NEW_WAVE = aGenre("New Wave");
export const REGGAE = { id: "genre_reggae", name: "Reggae" }; export const POP = aGenre("Pop");
export const ROCK = { id: "genre_rock", name: "Rock" }; export const POP_ROCK = aGenre("Pop Rock");
export const SKA = { id: "genre_ska", name: "Ska" }; export const REGGAE = aGenre("Reggae");
export const PUNK = { id: "genre_punk", name: "Punk" }; export const ROCK = aGenre("Rock");
export const TRIP_HOP = { id: "genre_trip_hop", name: "Trip Hop" }; export const SKA = aGenre("Ska");
export const PUNK = aGenre("Punk");
export const TRIP_HOP = aGenre("Trip Hop");
export const SAMPLE_GENRES = [HIP_HOP, METAL, NEW_WAVE, POP, POP_ROCK, REGGAE, ROCK, SKA]; export const SAMPLE_GENRES = [HIP_HOP, METAL, NEW_WAVE, POP, POP_ROCK, REGGAE, ROCK, SKA];
export const randomGenre = () => SAMPLE_GENRES[randomInt(SAMPLE_GENRES.length)]; export const randomGenre = () => SAMPLE_GENRES[randomInt(SAMPLE_GENRES.length)];
@@ -138,6 +141,7 @@ export function aTrack(fields: Partial<Track> = {}): Track {
genre, genre,
artist: artistToArtistSummary(artist), artist: artistToArtistSummary(artist),
album: albumToAlbumSummary(anAlbum({ artistId: artist.id, artistName: artist.name, genre })), album: albumToAlbumSummary(anAlbum({ artistId: artist.id, artistName: artist.name, genre })),
coverArt: `coverArt:${uuid()}`,
...fields, ...fields,
}; };
} }
@@ -151,6 +155,7 @@ export function anAlbum(fields: Partial<Album> = {}): Album {
year: `19${randomInt(99)}`, year: `19${randomInt(99)}`,
artistId: `Artist ${uuid()}`, artistId: `Artist ${uuid()}`,
artistName: `Artist ${randomString()}`, artistName: `Artist ${randomString()}`,
coverArt: `coverArt:${uuid()}`,
...fields, ...fields,
}; };
} }
@@ -167,7 +172,8 @@ export const BLONDIE: Artist = {
year: "1976", year: "1976",
genre: NEW_WAVE, genre: NEW_WAVE,
artistId: BLONDIE_ID, artistId: BLONDIE_ID,
artistName: BLONDIE_NAME artistName: BLONDIE_NAME,
coverArt: `coverArt:${uuid()}`
}, },
{ {
id: uuid(), id: uuid(),
@@ -175,7 +181,8 @@ export const BLONDIE: Artist = {
year: "1978", year: "1978",
genre: POP_ROCK, genre: POP_ROCK,
artistId: BLONDIE_ID, artistId: BLONDIE_ID,
artistName: BLONDIE_NAME artistName: BLONDIE_NAME,
coverArt: `coverArt:${uuid()}`
}, },
], ],
image: { image: {
@@ -192,9 +199,9 @@ export const BOB_MARLEY: Artist = {
id: BOB_MARLEY_ID, id: BOB_MARLEY_ID,
name: BOB_MARLEY_NAME, name: BOB_MARLEY_NAME,
albums: [ albums: [
{ id: uuid(), name: "Burin'", year: "1973", genre: REGGAE, artistId: BOB_MARLEY_ID, artistName: BOB_MARLEY_NAME }, { id: uuid(), name: "Burin'", year: "1973", genre: REGGAE, artistId: BOB_MARLEY_ID, artistName: BOB_MARLEY_NAME, coverArt: `coverArt:${uuid()}` },
{ id: uuid(), name: "Exodus", year: "1977", genre: REGGAE, artistId: BOB_MARLEY_ID, artistName: BOB_MARLEY_NAME }, { id: uuid(), name: "Exodus", year: "1977", genre: REGGAE, artistId: BOB_MARLEY_ID, artistName: BOB_MARLEY_NAME, coverArt: `coverArt:${uuid()}` },
{ id: uuid(), name: "Kaya", year: "1978", genre: SKA, artistId: BOB_MARLEY_ID, artistName: BOB_MARLEY_NAME }, { id: uuid(), name: "Kaya", year: "1978", genre: SKA, artistId: BOB_MARLEY_ID, artistName: BOB_MARLEY_NAME, coverArt: `coverArt:${uuid()}` },
], ],
image: { image: {
small: "http://localhost/BOB_MARLEY/sml", small: "http://localhost/BOB_MARLEY/sml",
@@ -231,6 +238,7 @@ export const METALLICA: Artist = {
genre: METAL, genre: METAL,
artistId: METALLICA_ID, artistId: METALLICA_ID,
artistName: METALLICA_NAME, artistName: METALLICA_NAME,
coverArt: `coverArt:${uuid()}`
}, },
{ {
id: uuid(), id: uuid(),
@@ -239,6 +247,7 @@ export const METALLICA: Artist = {
genre: METAL, genre: METAL,
artistId: METALLICA_ID, artistId: METALLICA_ID,
artistName: METALLICA_NAME, artistName: METALLICA_NAME,
coverArt: `coverArt:${uuid()}`
}, },
], ],
image: { image: {

View File

@@ -1,5 +1,79 @@
import { hostname } from "os"; import { hostname } from "os";
import config from "../src/config"; import config, { envVar, WORD } from "../src/config";
describe("envVar", () => {
const OLD_ENV = process.env;
beforeEach(() => {
jest.resetModules();
process.env = { ...OLD_ENV };
process.env["bnb-var"] = "bnb-var-value";
process.env["bnb-legacy2"] = "bnb-legacy2-value";
process.env["bnb-legacy3"] = "bnb-legacy3-value";
});
afterEach(() => {
process.env = OLD_ENV;
});
describe("when the env var exists", () => {
describe("and there are no legacy env vars that match", () => {
it("should return the env var", () => {
expect(envVar("bnb-var")).toEqual("bnb-var-value");
});
});
describe("and there are legacy env vars that match", () => {
it("should return the env var", () => {
expect(
envVar("bnb-var", {
default: "not valid",
legacy: ["bnb-legacy1", "bnb-legacy2", "bnb-legacy3"],
})
).toEqual("bnb-var-value");
});
});
});
describe("when the env var doesnt exist", () => {
describe("and there are no legacy env vars specified", () => {
describe("and there is no default value specified", () => {
it("should be undefined", () => {
expect(envVar("bnb-not-set")).toBeUndefined();
});
});
describe("and there is a default value specified", () => {
it("should return the default", () => {
expect(envVar("bnb-not-set", { default: "widget" })).toEqual(
"widget"
);
});
});
});
describe("when there are legacy env vars specified", () => {
it("should return the value from the first matched legacy env var", () => {
expect(
envVar("bnb-not-set", {
legacy: ["bnb-legacy1", "bnb-legacy2", "bnb-legacy3"],
})
).toEqual("bnb-legacy2-value");
});
});
});
describe("validationPattern", () => {
it("should fail when the value does not match the pattern", () => {
expect(
() => envVar("bnb-var", {
validationPattern: /^foobar$/,
})
).toThrowError(`Invalid value specified for 'bnb-var', must match ${/^foobar$/}`)
});
});
});
describe("config", () => { describe("config", () => {
const OLD_ENV = process.env; const OLD_ENV = process.env;
@@ -43,26 +117,22 @@ describe("config", () => {
} }
describe("bonobUrl", () => { describe("bonobUrl", () => {
describe("when BONOB_URL is specified", () => { ["BNB_URL", "BONOB_URL", "BONOB_WEB_ADDRESS"].forEach(key => {
describe(`when ${key} is specified`, () => {
it("should be used", () => { it("should be used", () => {
const url = "http://bonob1.example.com:8877/"; const url = "http://bonob1.example.com:8877/";
process.env["BONOB_URL"] = url;
expect(config().bonobUrl.href()).toEqual(url); process.env["BNB_URL"] = "";
});
});
describe("when BONOB_URL is not specified, however legacy BONOB_WEB_ADDRESS is specified", () => {
it("should be used", () => {
const url = "http://bonob2.example.com:9988/";
process.env["BONOB_URL"] = ""; process.env["BONOB_URL"] = "";
process.env["BONOB_WEB_ADDRESS"] = url; process.env["BONOB_WEB_ADDRESS"] = "";
process.env[key] = url;
expect(config().bonobUrl.href()).toEqual(url); expect(config().bonobUrl.href()).toEqual(url);
}); });
}); });
});
describe("when neither BONOB_URL nor BONOB_WEB_ADDRESS are specified", () => { describe("when none of BNB_URL, BONOB_URL, BONOB_WEB_ADDRESS are specified", () => {
describe("when BONOB_PORT is not specified", () => { describe("when BONOB_PORT is not specified", () => {
it(`should default to http://${hostname()}:4534`, () => { it(`should default to http://${hostname()}:4534`, () => {
expect(config().bonobUrl.href()).toEqual( expect(config().bonobUrl.href()).toEqual(
@@ -71,6 +141,15 @@ describe("config", () => {
}); });
}); });
describe("when BNB_PORT is specified as 3322", () => {
it(`should default to http://${hostname()}:3322`, () => {
process.env["BNB_PORT"] = "3322";
expect(config().bonobUrl.href()).toEqual(
`http://${hostname()}:3322/`
);
});
});
describe("when BONOB_PORT is specified as 3322", () => { describe("when BONOB_PORT is specified as 3322", () => {
it(`should default to http://${hostname()}:3322`, () => { it(`should default to http://${hostname()}:3322`, () => {
process.env["BONOB_PORT"] = "3322"; process.env["BONOB_PORT"] = "3322";
@@ -82,105 +161,86 @@ describe("config", () => {
}); });
}); });
describe("navidrome", () => {
describe("url", () => {
describe("when BONOB_NAVIDROME_URL is not specified", () => {
it(`should default to http://${hostname()}:4533`, () => {
expect(config().navidrome.url).toEqual(`http://${hostname()}:4533`);
});
});
describe("when BONOB_NAVIDROME_URL is ''", () => {
it(`should default to http://${hostname()}:4533`, () => {
process.env["BONOB_NAVIDROME_URL"] = "";
expect(config().navidrome.url).toEqual(`http://${hostname()}:4533`);
});
});
describe("when BONOB_NAVIDROME_URL is specified", () => {
it(`should use it`, () => {
const url = "http://navidrome.example.com:1234";
process.env["BONOB_NAVIDROME_URL"] = url;
expect(config().navidrome.url).toEqual(url);
});
});
});
});
describe("icons", () => { describe("icons", () => {
describe("foregroundColor", () => { describe("foregroundColor", () => {
describe("when BONOB_ICON_FOREGROUND_COLOR is not specified", () => { ["BNB_ICON_FOREGROUND_COLOR", "BONOB_ICON_FOREGROUND_COLOR"].forEach(k => {
describe(`when ${k} is not specified`, () => {
it(`should default to undefined`, () => { it(`should default to undefined`, () => {
expect(config().icons.foregroundColor).toEqual(undefined); expect(config().icons.foregroundColor).toEqual(undefined);
}); });
}); });
describe("when BONOB_ICON_FOREGROUND_COLOR is ''", () => { describe(`when ${k} is ''`, () => {
it(`should default to undefined`, () => { it(`should default to undefined`, () => {
process.env["BONOB_ICON_FOREGROUND_COLOR"] = ""; process.env[k] = "";
expect(config().icons.foregroundColor).toEqual(undefined); expect(config().icons.foregroundColor).toEqual(undefined);
}); });
}); });
describe("when BONOB_ICON_FOREGROUND_COLOR is specified", () => { describe(`when ${k} is specified`, () => {
it(`should use it`, () => { it(`should use it`, () => {
process.env["BONOB_ICON_FOREGROUND_COLOR"] = "pink"; process.env[k] = "pink";
expect(config().icons.foregroundColor).toEqual("pink"); expect(config().icons.foregroundColor).toEqual("pink");
}); });
}); });
describe("when BONOB_ICON_FOREGROUND_COLOR is an invalid string", () => { describe(`when ${k} is an invalid string`, () => {
it(`should blow up`, () => { it(`should blow up`, () => {
process.env["BONOB_ICON_FOREGROUND_COLOR"] = "#dfasd"; process.env[k] = "#dfasd";
expect(() => config()).toThrow( expect(() => config()).toThrow(
"Invalid color specified for BONOB_ICON_FOREGROUND_COLOR" `Invalid value specified for 'BNB_ICON_FOREGROUND_COLOR', must match ${WORD}`
); );
}); });
}); });
}); });
});
describe("backgroundColor", () => { describe("backgroundColor", () => {
describe("when BONOB_ICON_BACKGROUND_COLOR is not specified", () => { ["BNB_ICON_BACKGROUND_COLOR", "BONOB_ICON_BACKGROUND_COLOR"].forEach(k => {
describe(`when ${k} is not specified`, () => {
it(`should default to undefined`, () => { it(`should default to undefined`, () => {
expect(config().icons.backgroundColor).toEqual(undefined); expect(config().icons.backgroundColor).toEqual(undefined);
}); });
}); });
describe("when BONOB_ICON_BACKGROUND_COLOR is ''", () => { describe(`when ${k} is ''`, () => {
it(`should default to undefined`, () => { it(`should default to undefined`, () => {
process.env["BONOB_ICON_BACKGROUND_COLOR"] = ""; process.env[k] = "";
expect(config().icons.backgroundColor).toEqual(undefined); expect(config().icons.backgroundColor).toEqual(undefined);
}); });
}); });
describe("when BONOB_ICON_BACKGROUND_COLOR is specified", () => { describe(`when ${k} is specified`, () => {
it(`should use it`, () => { it(`should use it`, () => {
process.env["BONOB_ICON_BACKGROUND_COLOR"] = "blue"; process.env[k] = "blue";
expect(config().icons.backgroundColor).toEqual("blue"); expect(config().icons.backgroundColor).toEqual("blue");
}); });
}); });
describe("when BONOB_ICON_BACKGROUND_COLOR is an invalid string", () => { describe(`when ${k} is an invalid string`, () => {
it(`should blow up`, () => { it(`should blow up`, () => {
process.env["BONOB_ICON_BACKGROUND_COLOR"] = "#red"; process.env[k] = "#red";
expect(() => config()).toThrow( expect(() => config()).toThrow(
"Invalid color specified for BONOB_ICON_BACKGROUND_COLOR" `Invalid value specified for 'BNB_ICON_BACKGROUND_COLOR', must match ${WORD}`
); );
}); });
}); });
}); });
}); });
});
describe("secret", () => { describe("secret", () => {
it("should default to bonob", () => { it("should default to bonob", () => {
expect(config().secret).toEqual("bonob"); expect(config().secret).toEqual("bonob");
}); });
it("should be overridable", () => { ["BNB_SECRET", "BONOB_SECRET"].forEach(key => {
process.env["BONOB_SECRET"] = "new secret"; it(`should be overridable using ${key}`, () => {
process.env[key] = "new secret";
expect(config().secret).toEqual("new secret"); expect(config().secret).toEqual("new secret");
}); });
}); });
});
describe("sonos", () => { describe("sonos", () => {
describe("serviceName", () => { describe("serviceName", () => {
@@ -188,83 +248,116 @@ describe("config", () => {
expect(config().sonos.serviceName).toEqual("bonob"); expect(config().sonos.serviceName).toEqual("bonob");
}); });
["BNB_SONOS_SERVICE_NAME", "BONOB_SONOS_SERVICE_NAME"].forEach(k => {
it("should be overridable", () => { it("should be overridable", () => {
process.env["BONOB_SONOS_SERVICE_NAME"] = "foobar1000"; process.env[k] = "foobar1000";
expect(config().sonos.serviceName).toEqual("foobar1000"); expect(config().sonos.serviceName).toEqual("foobar1000");
}); });
}); });
});
["BNB_SONOS_DEVICE_DISCOVERY", "BONOB_SONOS_DEVICE_DISCOVERY"].forEach(k => {
describeBooleanConfigValue( describeBooleanConfigValue(
"deviceDiscovery", "deviceDiscovery",
"BONOB_SONOS_DEVICE_DISCOVERY", k,
true, true,
(config) => config.sonos.discovery.auto (config) => config.sonos.discovery.enabled
); );
});
describe("seedHost", () => { describe("seedHost", () => {
it("should default to undefined", () => { it("should default to undefined", () => {
expect(config().sonos.discovery.seedHost).toBeUndefined(); expect(config().sonos.discovery.seedHost).toBeUndefined();
}); });
["BNB_SONOS_SEED_HOST", "BONOB_SONOS_SEED_HOST"].forEach(k => {
it("should be overridable", () => { it("should be overridable", () => {
process.env["BONOB_SONOS_SEED_HOST"] = "123.456.789.0"; process.env[k] = "123.456.789.0";
expect(config().sonos.discovery.seedHost).toEqual("123.456.789.0"); expect(config().sonos.discovery.seedHost).toEqual("123.456.789.0");
}); });
}); });
});
["BNB_SONOS_AUTO_REGISTER", "BONOB_SONOS_AUTO_REGISTER"].forEach(k => {
describeBooleanConfigValue( describeBooleanConfigValue(
"autoRegister", "autoRegister",
"BONOB_SONOS_AUTO_REGISTER", k,
false, false,
(config) => config.sonos.autoRegister (config) => config.sonos.autoRegister
); );
});
describe("sid", () => { describe("sid", () => {
it("should default to 246", () => { it("should default to 246", () => {
expect(config().sonos.sid).toEqual(246); expect(config().sonos.sid).toEqual(246);
}); });
["BNB_SONOS_SERVICE_ID", "BONOB_SONOS_SERVICE_ID"].forEach(k => {
it("should be overridable", () => { it("should be overridable", () => {
process.env["BONOB_SONOS_SERVICE_ID"] = "786"; process.env[k] = "786";
expect(config().sonos.sid).toEqual(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", () => { describe("subsonic", () => {
process.env["BONOB_NAVIDROME_URL"] = "http://farfaraway.com"; describe("url", () => {
expect(config().navidrome.url).toEqual("http://farfaraway.com"); ["BNB_SUBSONIC_URL", "BONOB_SUBSONIC_URL", "BONOB_NAVIDROME_URL"].forEach(k => {
describe(`when ${k} is not specified`, () => {
it(`should default to http://${hostname()}:4533`, () => {
expect(config().subsonic.url).toEqual(`http://${hostname()}:4533`);
});
});
describe(`when ${k} is ''`, () => {
it(`should default to http://${hostname()}:4533`, () => {
process.env[k] = "";
expect(config().subsonic.url).toEqual(`http://${hostname()}:4533`);
});
});
describe(`when ${k} is specified`, () => {
it(`should use it for ${k}`, () => {
const url = "http://navidrome.example.com:1234";
process.env[k] = url;
expect(config().subsonic.url).toEqual(url);
});
});
}); });
}); });
describe("customClientsFor", () => { describe("customClientsFor", () => {
it("should default to undefined", () => { it("should default to undefined", () => {
expect(config().navidrome.customClientsFor).toBeUndefined(); expect(config().subsonic.customClientsFor).toBeUndefined();
}); });
it("should be overridable", () => { ["BNB_SUBSONIC_CUSTOM_CLIENTS", "BONOB_SUBSONIC_CUSTOM_CLIENTS", "BONOB_NAVIDROME_CUSTOM_CLIENTS"].forEach(k => {
process.env["BONOB_NAVIDROME_CUSTOM_CLIENTS"] = "whoop/whoop"; it(`should be overridable for ${k}`, () => {
expect(config().navidrome.customClientsFor).toEqual("whoop/whoop"); process.env[k] = "whoop/whoop";
expect(config().subsonic.customClientsFor).toEqual("whoop/whoop");
});
}); });
}); });
}); });
["BNB_SCROBBLE_TRACKS", "BONOB_SCROBBLE_TRACKS"].forEach(k => {
describeBooleanConfigValue( describeBooleanConfigValue(
"scrobbleTracks", "scrobbleTracks",
"BONOB_SCROBBLE_TRACKS", k,
true, true,
(config) => config.scrobbleTracks (config) => config.scrobbleTracks
); );
});
["BNB_REPORT_NOW_PLAYING", "BONOB_REPORT_NOW_PLAYING"].forEach(k => {
describeBooleanConfigValue( describeBooleanConfigValue(
"reportNowPlaying", "reportNowPlaying",
"BONOB_REPORT_NOW_PLAYING", k,
true, true,
(config) => config.reportNowPlaying (config) => config.reportNowPlaying
); );
}); });
});

View File

@@ -467,9 +467,9 @@ describe("InMemoryMusicService", () => {
it("should provide an array of artists", async () => { it("should provide an array of artists", async () => {
expect(await musicLibrary.genres()).toEqual([ expect(await musicLibrary.genres()).toEqual([
HIP_HOP, HIP_HOP,
SKA,
POP, POP,
ROCK, ROCK,
SKA,
]); ]);
}); });
}); });

View File

@@ -5,6 +5,8 @@ import { pipe } from "fp-ts/lib/function";
import { ordString, fromCompare } from "fp-ts/lib/Ord"; import { ordString, fromCompare } from "fp-ts/lib/Ord";
import { shuffle } from "underscore"; import { shuffle } from "underscore";
import { b64Encode, b64Decode } from "../src/b64";
import { import {
MusicService, MusicService,
Credentials, Credentials,
@@ -37,9 +39,7 @@ export class InMemoryMusicService implements MusicService {
this.users[username] == password this.users[username] == password
) { ) {
return Promise.resolve({ return Promise.resolve({
authToken: Buffer.from(JSON.stringify({ username, password })).toString( authToken: b64Encode(JSON.stringify({ username, password })),
"base64"
),
userId: username, userId: username,
nickname: username, nickname: username,
}); });
@@ -49,9 +49,7 @@ export class InMemoryMusicService implements MusicService {
} }
login(token: string): Promise<MusicLibrary> { login(token: string): Promise<MusicLibrary> {
const credentials = JSON.parse( const credentials = JSON.parse(b64Decode(token)) as Credentials;
Buffer.from(token, "base64").toString("ascii")
) as Credentials;
if (this.users[credentials.username] != credentials.password) if (this.users[credentials.username] != credentials.password)
return Promise.reject("Invalid auth token"); return Promise.reject("Invalid auth token");
@@ -127,7 +125,7 @@ export class InMemoryMusicService implements MusicService {
), ),
stream: (_: { trackId: string; range: string | undefined }) => stream: (_: { trackId: string; range: string | undefined }) =>
Promise.reject("unsupported operation"), Promise.reject("unsupported operation"),
coverArt: (id: string, _: "album" | "artist", size?: number) => coverArt: (id: string, size?: number) =>
Promise.reject(`Cannot retrieve coverArt for ${id}, size ${size}`), Promise.reject(`Cannot retrieve coverArt for ${id}, size ${size}`),
scrobble: async (_: string) => { scrobble: async (_: string) => {
return Promise.resolve(true); return Promise.resolve(true);

View File

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

@@ -186,7 +186,7 @@ describe("server", () => {
bonobUrl, bonobUrl,
new InMemoryMusicService(), new InMemoryMusicService(),
{ {
version: "v123.456" version: "v123.456",
} }
); );
@@ -233,8 +233,7 @@ describe("server", () => {
const fakeSonos: Sonos = { const fakeSonos: Sonos = {
devices: () => Promise.resolve([]), devices: () => Promise.resolve([]),
services: () => services: () => Promise.resolve([]),
Promise.resolve([]),
remove: () => Promise.resolve(false), remove: () => Promise.resolve(false),
register: () => Promise.resolve(false), register: () => Promise.resolve(false),
}; };
@@ -397,7 +396,8 @@ describe("server", () => {
const fakeSonos: Sonos = { const fakeSonos: Sonos = {
devices: () => Promise.resolve([device1, device2]), devices: () => Promise.resolve([device1, device2]),
services: () => Promise.resolve([service1, service2, bonobService]), services: () =>
Promise.resolve([service1, service2, bonobService]),
remove: () => Promise.resolve(false), remove: () => Promise.resolve(false),
register: () => Promise.resolve(false), register: () => Promise.resolve(false),
}; };
@@ -707,7 +707,6 @@ describe("server", () => {
const musicLibrary = { const musicLibrary = {
stream: jest.fn(), stream: jest.fn(),
scrobble: jest.fn(), scrobble: jest.fn(),
nowPlaying: jest.fn(),
}; };
let now = dayjs(); let now = dayjs();
const accessTokens = new ExpiringAccessTokens({ now: () => now }); const accessTokens = new ExpiringAccessTokens({ now: () => now });
@@ -756,13 +755,14 @@ describe("server", () => {
it("should return a 401", async () => { it("should return a 401", async () => {
now = now.add(1, "day"); now = now.add(1, "day");
const res = await request(server) const res = await request(server).head(
.head(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({
pathname: `/stream/track/${trackId}`,
searchParams: { bat: accessToken },
})
.path() .path()
) );
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(401); expect(res.status).toEqual(401);
}); });
@@ -774,7 +774,8 @@ describe("server", () => {
const trackStream = { const trackStream = {
status: 200, status: 200,
headers: { headers: {
"content-type": "audio/mp3; charset=utf-8", // audio/x-flac should be mapped to x-flac
"content-type": "audio/x-flac; whoop; foo-bar",
"content-length": "123", "content-length": "123",
}, },
stream: streamContent(""), stream: streamContent(""),
@@ -786,14 +787,13 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.head( .head(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) );
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(trackStream.status); expect(res.status).toEqual(trackStream.status);
expect(res.headers["content-type"]).toEqual( expect(res.headers["content-type"]).toEqual(
"audio/mp3; charset=utf-8" "audio/flac; whoop; foo-bar"
); );
expect(res.headers["content-length"]).toEqual("123"); expect(res.headers["content-length"]).toEqual("123");
expect(res.body).toEqual({}); expect(res.body).toEqual({});
@@ -812,8 +812,10 @@ describe("server", () => {
musicLibrary.stream.mockResolvedValue(trackStream); musicLibrary.stream.mockResolvedValue(trackStream);
const res = await request(server) const res = await request(server)
.head(`/stream/track/${trackId}`) .head(bonobUrl
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path()
);
expect(res.status).toEqual(404); expect(res.status).toEqual(404);
expect(res.body).toEqual({}); expect(res.body).toEqual({});
@@ -840,10 +842,9 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) );
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(401); expect(res.status).toEqual(401);
}); });
@@ -863,14 +864,12 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) );
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(404); expect(res.status).toEqual(404);
expect(musicLibrary.nowPlaying).not.toHaveBeenCalled();
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId }); expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
}); });
}); });
@@ -883,26 +882,25 @@ describe("server", () => {
const stream = { const stream = {
status: 200, status: 200,
headers: { headers: {
"content-type": "audio/mp3", // audio/x-flac should be mapped to audio/flac
"content-type": "audio/x-flac; charset=utf-8",
}, },
stream: streamContent(content), stream: streamContent(content),
}; };
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
musicLibrary.stream.mockResolvedValue(stream); musicLibrary.stream.mockResolvedValue(stream);
musicLibrary.nowPlaying.mockResolvedValue(true);
const res = await request(server) const res = await request(server)
.get( .get(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) );
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(stream.status); expect(res.status).toEqual(stream.status);
expect(res.headers["content-type"]).toEqual( expect(res.headers["content-type"]).toEqual(
"audio/mp3; charset=utf-8" "audio/flac; charset=utf-8"
); );
expect(res.header["accept-ranges"]).toBeUndefined(); expect(res.header["accept-ranges"]).toBeUndefined();
expect(res.headers["content-length"]).toEqual( expect(res.headers["content-length"]).toEqual(
@@ -911,7 +909,6 @@ describe("server", () => {
expect(Object.keys(res.headers)).not.toContain("content-range"); expect(Object.keys(res.headers)).not.toContain("content-range");
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId }); expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
}); });
}); });
@@ -931,15 +928,13 @@ describe("server", () => {
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
musicLibrary.stream.mockResolvedValue(stream); musicLibrary.stream.mockResolvedValue(stream);
musicLibrary.nowPlaying.mockResolvedValue(true);
const res = await request(server) const res = await request(server)
.get( .get(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) );
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(stream.status); expect(res.status).toEqual(stream.status);
expect(res.headers["content-type"]).toEqual( expect(res.headers["content-type"]).toEqual(
@@ -951,7 +946,6 @@ describe("server", () => {
expect(Object.keys(res.headers)).not.toContain("content-range"); expect(Object.keys(res.headers)).not.toContain("content-range");
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId }); expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
}); });
}); });
@@ -970,15 +964,13 @@ describe("server", () => {
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
musicLibrary.stream.mockResolvedValue(stream); musicLibrary.stream.mockResolvedValue(stream);
musicLibrary.nowPlaying.mockResolvedValue(true);
const res = await request(server) const res = await request(server)
.get( .get(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) );
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(stream.status); expect(res.status).toEqual(stream.status);
expect(res.header["content-type"]).toEqual( expect(res.header["content-type"]).toEqual(
@@ -990,7 +982,6 @@ describe("server", () => {
expect(res.header["content-range"]).toBeUndefined(); expect(res.header["content-range"]).toBeUndefined();
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId }); expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
}); });
}); });
@@ -1010,15 +1001,13 @@ describe("server", () => {
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
musicLibrary.stream.mockResolvedValue(stream); musicLibrary.stream.mockResolvedValue(stream);
musicLibrary.nowPlaying.mockResolvedValue(true);
const res = await request(server) const res = await request(server)
.get( .get(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) );
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(stream.status); expect(res.status).toEqual(stream.status);
expect(res.header["content-type"]).toEqual( expect(res.header["content-type"]).toEqual(
@@ -1032,7 +1021,6 @@ describe("server", () => {
); );
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId }); expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
}); });
}); });
@@ -1053,17 +1041,15 @@ describe("server", () => {
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
musicLibrary.stream.mockResolvedValue(stream); musicLibrary.stream.mockResolvedValue(stream);
musicLibrary.nowPlaying.mockResolvedValue(true);
const requestedRange = "40-"; const requestedRange = "40-";
const res = await request(server) const res = await request(server)
.get( .get(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken)
.set("Range", requestedRange); .set("Range", requestedRange);
expect(res.status).toEqual(stream.status); expect(res.status).toEqual(stream.status);
@@ -1076,7 +1062,6 @@ describe("server", () => {
expect(res.header["content-range"]).toBeUndefined(); expect(res.header["content-range"]).toBeUndefined();
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
expect(musicLibrary.stream).toHaveBeenCalledWith({ expect(musicLibrary.stream).toHaveBeenCalledWith({
trackId, trackId,
range: requestedRange, range: requestedRange,
@@ -1099,15 +1084,13 @@ describe("server", () => {
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
musicLibrary.stream.mockResolvedValue(stream); musicLibrary.stream.mockResolvedValue(stream);
musicLibrary.nowPlaying.mockResolvedValue(true);
const res = await request(server) const res = await request(server)
.get( .get(
bonobUrl bonobUrl
.append({ pathname: `/stream/track/${trackId}` }) .append({ pathname: `/stream/track/${trackId}`, searchParams: { bat: accessToken } })
.path() .path()
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken)
.set("Range", "4000-5000"); .set("Range", "4000-5000");
expect(res.status).toEqual(stream.status); expect(res.status).toEqual(stream.status);
@@ -1122,7 +1105,6 @@ describe("server", () => {
); );
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
expect(musicLibrary.stream).toHaveBeenCalledWith({ expect(musicLibrary.stream).toHaveBeenCalledWith({
trackId, trackId,
range: "4000-5000", range: "4000-5000",
@@ -1173,7 +1155,7 @@ describe("server", () => {
describe("when there is no access-token", () => { describe("when there is no access-token", () => {
it("should return a 401", async () => { it("should return a 401", async () => {
const res = await request(server).get(`/art/album/123/size/180`); const res = await request(server).get(`/art/coverArt:123/size/180`);
expect(res.status).toEqual(401); expect(res.status).toEqual(401);
}); });
@@ -1184,7 +1166,7 @@ describe("server", () => {
now = now.add(1, "day"); now = now.add(1, "day");
const res = await request(server).get( const res = await request(server).get(
`/art/album/123/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/coverArt:123/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
); );
expect(res.status).toEqual(401); expect(res.status).toEqual(401);
@@ -1192,18 +1174,6 @@ describe("server", () => {
}); });
describe("when there is a valid access token", () => { describe("when there is a valid access token", () => {
describe("some invalid art type", () => {
it("should return a 400", async () => {
const res = await request(server)
.get(
`/art/foo/${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
)
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(400);
});
});
describe("artist art", () => { describe("artist art", () => {
["0", "-1", "foo"].forEach((size) => { ["0", "-1", "foo"].forEach((size) => {
describe(`invalid size of ${size}`, () => { describe(`invalid size of ${size}`, () => {
@@ -1211,7 +1181,7 @@ describe("server", () => {
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${albumId}/size/${size}?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/artist:${albumId}/size/${size}?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1231,7 +1201,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/artist:${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1242,8 +1212,7 @@ describe("server", () => {
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(musicLibrary.coverArt).toHaveBeenCalledWith( expect(musicLibrary.coverArt).toHaveBeenCalledWith(
albumId, `artist:${albumId}`,
"artist",
180 180
); );
}); });
@@ -1257,7 +1226,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/artist:${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1267,11 +1236,24 @@ describe("server", () => {
}); });
describe("fetching multiple images as a collage", () => { describe("fetching multiple images as a collage", () => {
const png = fs.readFileSync(path.join(__dirname, '..', 'docs', 'images', 'chartreuseFuchsia.png')); const png = fs.readFileSync(
path.join(
__dirname,
"..",
"docs",
"images",
"chartreuseFuchsia.png"
)
);
describe("fetching a collage of 4 when all are available", () => { describe("fetching a collage of 4 when all are available", () => {
it("should return the image and a 200", async () => { it("should return the image and a 200", async () => {
const ids = ["1", "2", "3", "4"]; const ids = [
"artist:1",
"artist:2",
"coverArt:3",
"coverArt:4",
];
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
@@ -1285,7 +1267,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${ids.join( `/art/${ids.join(
"&" "&"
)}/size/200?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` )}/size/200?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
@@ -1296,11 +1278,7 @@ describe("server", () => {
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
ids.forEach((id) => { ids.forEach((id) => {
expect(musicLibrary.coverArt).toHaveBeenCalledWith( expect(musicLibrary.coverArt).toHaveBeenCalledWith(id, 200);
id,
"artist",
200
);
}); });
const image = await Image.load(res.body); const image = await Image.load(res.body);
@@ -1311,7 +1289,7 @@ describe("server", () => {
describe("fetching a collage of 4, however only 1 is available", () => { describe("fetching a collage of 4, however only 1 is available", () => {
it("should return the single image", async () => { it("should return the single image", async () => {
const ids = ["1", "2", "3", "4"]; const ids = ["artist:1", "artist:2", "artist:3", "artist:4"];
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
@@ -1321,26 +1299,28 @@ describe("server", () => {
musicLibrary.coverArt.mockResolvedValueOnce( musicLibrary.coverArt.mockResolvedValueOnce(
coverArtResponse({ coverArtResponse({
data: png, data: png,
contentType: "image/some-mime-type" contentType: "image/some-mime-type",
}) })
); );
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${ids.join( `/art/${ids.join(
"&" "&"
)}/size/200?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` )}/size/200?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
expect(res.status).toEqual(200); expect(res.status).toEqual(200);
expect(res.header["content-type"]).toEqual("image/some-mime-type"); expect(res.header["content-type"]).toEqual(
"image/some-mime-type"
);
}); });
}); });
describe("fetching a collage of 4 and all are missing", () => { describe("fetching a collage of 4 and all are missing", () => {
it("should return a 404", async () => { it("should return a 404", async () => {
const ids = ["1", "2", "3", "4"]; const ids = ["artist:1", "artist:2", "artist:3", "artist:4"];
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
@@ -1350,7 +1330,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${ids.join( `/art/${ids.join(
"&" "&"
)}/size/200?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` )}/size/200?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
@@ -1362,7 +1342,17 @@ describe("server", () => {
describe("fetching a collage of 9 when all are available", () => { describe("fetching a collage of 9 when all are available", () => {
it("should return the image and a 200", async () => { it("should return the image and a 200", async () => {
const ids = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]; const ids = [
"artist:1",
"artist:2",
"coverArt:3",
"artist:4",
"artist:5",
"artist:6",
"artist:7",
"artist:8",
"artist:9",
];
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
@@ -1376,7 +1366,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${ids.join( `/art/${ids.join(
"&" "&"
)}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` )}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
@@ -1387,11 +1377,7 @@ describe("server", () => {
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
ids.forEach((id) => { ids.forEach((id) => {
expect(musicLibrary.coverArt).toHaveBeenCalledWith( expect(musicLibrary.coverArt).toHaveBeenCalledWith(id, 180);
id,
"artist",
180
);
}); });
const image = await Image.load(res.body); const image = await Image.load(res.body);
@@ -1402,7 +1388,17 @@ describe("server", () => {
describe("fetching a collage of 9 when only 2 are available", () => { describe("fetching a collage of 9 when only 2 are available", () => {
it("should still return an image and a 200", async () => { it("should still return an image and a 200", async () => {
const ids = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]; const ids = [
"artist:1",
"artist:2",
"artist:3",
"artist:4",
"artist:5",
"artist:6",
"artist:7",
"artist:8",
"artist:9",
];
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
@@ -1426,7 +1422,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${ids.join( `/art/${ids.join(
"&" "&"
)}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` )}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
@@ -1437,11 +1433,7 @@ describe("server", () => {
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
ids.forEach((id) => { ids.forEach((id) => {
expect(musicLibrary.coverArt).toHaveBeenCalledWith( expect(musicLibrary.coverArt).toHaveBeenCalledWith(id, 180);
id,
"artist",
180
);
}); });
const image = await Image.load(res.body); const image = await Image.load(res.body);
@@ -1452,7 +1444,19 @@ describe("server", () => {
describe("fetching a collage of 11", () => { describe("fetching a collage of 11", () => {
it("should still return an image and a 200, though will only display 9", async () => { it("should still return an image and a 200, though will only display 9", async () => {
const ids = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]; const ids = [
"artist:1",
"artist:2",
"artist:3",
"artist:4",
"artist:5",
"artist:6",
"artist:7",
"artist:8",
"artist:9",
"artist:10",
"artist:11",
];
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
@@ -1466,7 +1470,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${ids.join( `/art/${ids.join(
"&" "&"
)}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` )}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
@@ -1477,11 +1481,7 @@ describe("server", () => {
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
ids.forEach((id) => { ids.forEach((id) => {
expect(musicLibrary.coverArt).toHaveBeenCalledWith( expect(musicLibrary.coverArt).toHaveBeenCalledWith(id, 180);
id,
"artist",
180
);
}); });
const image = await Image.load(res.body); const image = await Image.load(res.body);
@@ -1498,7 +1498,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/coverArt:${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1515,7 +1515,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/artist/${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/artist:${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1531,7 +1531,7 @@ describe("server", () => {
musicService.login.mockResolvedValue(musicLibrary); musicService.login.mockResolvedValue(musicLibrary);
const res = await request(server) const res = await request(server)
.get( .get(
`/art/album/${albumId}/size/${size}?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/coverArt:${albumId}/size/${size}?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1553,7 +1553,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/album/${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/coverArt:${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1564,8 +1564,7 @@ describe("server", () => {
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(musicLibrary.coverArt).toHaveBeenCalledWith( expect(musicLibrary.coverArt).toHaveBeenCalledWith(
albumId, `coverArt:${albumId}`,
"album",
180 180
); );
}); });
@@ -1578,7 +1577,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/album/${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/album:${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1593,7 +1592,7 @@ describe("server", () => {
const res = await request(server) const res = await request(server)
.get( .get(
`/art/album/${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}` `/art/album:${albumId}/size/180?${BONOB_ACCESS_TOKEN_HEADER}=${accessToken}`
) )
.set(BONOB_ACCESS_TOKEN_HEADER, accessToken); .set(BONOB_ACCESS_TOKEN_HEADER, accessToken);
@@ -1723,7 +1722,13 @@ describe("server", () => {
expect(svg).toContain(`fill="brightpink"`); expect(svg).toContain(`fill="brightpink"`);
}); });
function itShouldBeFestive(theme: string, date: string, id: string, color1: string, color2: string) { function itShouldBeFestive(
theme: string,
date: string,
id: string,
color1: string,
color2: string
) {
it(`should return a ${theme} icon on ${date}`, async () => { it(`should return a ${theme} icon on ${date}`, async () => {
const response = await request( const response = await request(
server({ now: () => dayjs(date) }) server({ now: () => dayjs(date) })
@@ -1737,14 +1742,50 @@ describe("server", () => {
}); });
} }
itShouldBeFestive("christmas '22", "2022/12/25", "christmas", "red", "green") itShouldBeFestive(
itShouldBeFestive("christmas '23", "2023/12/25", "christmas", "red", "green") "christmas '22",
"2022/12/25",
"christmas",
"red",
"green"
);
itShouldBeFestive(
"christmas '23",
"2023/12/25",
"christmas",
"red",
"green"
);
itShouldBeFestive("halloween", "2022/10/31", "halloween", "black", "orange") itShouldBeFestive(
itShouldBeFestive("halloween", "2023/10/31", "halloween", "black", "orange") "halloween",
"2022/10/31",
"halloween",
"black",
"orange"
);
itShouldBeFestive(
"halloween",
"2023/10/31",
"halloween",
"black",
"orange"
);
itShouldBeFestive("cny '22", "2022/02/01", "yoTiger", "red", "yellow") itShouldBeFestive(
itShouldBeFestive("cny '23", "2023/01/22", "yoRabbit", "red", "yellow") "cny '22",
"2022/02/01",
"yoTiger",
"red",
"yellow"
);
itShouldBeFestive(
"cny '23",
"2023/01/22",
"yoRabbit",
"red",
"yellow"
);
}); });
}); });
}); });

View File

@@ -23,6 +23,7 @@ import {
searchResult, searchResult,
iconArtURI, iconArtURI,
playlistAlbumArtURL, playlistAlbumArtURL,
sonosifyMimeType,
} from "../src/smapi"; } from "../src/smapi";
import { import {
@@ -48,7 +49,7 @@ import {
} from "../src/music_service"; } from "../src/music_service";
import { AccessTokens } from "../src/access_tokens"; import { AccessTokens } from "../src/access_tokens";
import dayjs from "dayjs"; import dayjs from "dayjs";
import url from "../src/url_builder"; import url, { URLBuilder } from "../src/url_builder";
import { iconForGenre } from "../src/icon"; import { iconForGenre } from "../src/icon";
const parseXML = (value: string) => new DOMParserImpl().parseFromString(value); const parseXML = (value: string) => new DOMParserImpl().parseFromString(value);
@@ -252,7 +253,8 @@ describe("track", () => {
const bonobUrl = url("http://localhost:4567/foo?access-token=1234"); const bonobUrl = url("http://localhost:4567/foo?access-token=1234");
const someTrack = aTrack({ const someTrack = aTrack({
id: uuid(), id: uuid(),
mimeType: "audio/something", // audio/x-flac should be mapped to audio/flac
mimeType: "audio/x-flac",
name: "great song", name: "great song",
duration: randomInt(1000), duration: randomInt(1000),
number: randomInt(100), number: randomInt(100),
@@ -262,22 +264,23 @@ describe("track", () => {
genre: { id: "genre101", name: "some genre" }, genre: { id: "genre101", name: "some genre" },
}), }),
artist: anArtist({ name: "great artist", id: uuid() }), artist: anArtist({ name: "great artist", id: uuid() }),
coverArt:"coverArt:887766"
}); });
expect(track(bonobUrl, someTrack)).toEqual({ expect(track(bonobUrl, someTrack)).toEqual({
itemType: "track", itemType: "track",
id: `track:${someTrack.id}`, id: `track:${someTrack.id}`,
mimeType: someTrack.mimeType, mimeType: 'audio/flac',
title: someTrack.name, title: someTrack.name,
trackMetadata: { trackMetadata: {
album: someTrack.album.name, album: someTrack.album.name,
albumId: someTrack.album.id, albumId: `album:${someTrack.album.id}`,
albumArtist: someTrack.artist.name, albumArtist: someTrack.artist.name,
albumArtistId: someTrack.artist.id, albumArtistId: `artist:${someTrack.artist.id}`,
albumArtURI: `http://localhost:4567/foo/art/album/${someTrack.album.id}/size/180?access-token=1234`, albumArtURI: `http://localhost:4567/foo/art/${someTrack.coverArt}/size/180?access-token=1234`,
artist: someTrack.artist.name, artist: someTrack.artist.name,
artistId: someTrack.artist.id, artistId: `artist:${someTrack.artist.id}`,
duration: someTrack.duration, duration: someTrack.duration,
genre: someTrack.album.genre?.name, genre: someTrack.album.genre?.name,
genreId: someTrack.album.genre?.id, genreId: someTrack.album.genre?.id,
@@ -304,12 +307,28 @@ describe("album", () => {
}); });
}); });
describe("sonosifyMimeType", () => {
describe("when is audio/x-flac", () => {
it("should be mapped to audio/flac", () => {
expect(sonosifyMimeType("audio/x-flac")).toEqual("audio/flac");
});
});
describe("when it is not audio/x-flac", () => {
it("should be returned as is", () => {
expect(sonosifyMimeType("audio/flac")).toEqual("audio/flac");
expect(sonosifyMimeType("audio/mpeg")).toEqual("audio/mpeg");
expect(sonosifyMimeType("audio/whoop")).toEqual("audio/whoop");
});
});
});
describe("playlistAlbumArtURL", () => { describe("playlistAlbumArtURL", () => {
describe("when the playlist has no albumIds", () => { describe("when the playlist has no coverArt ids", () => {
it("should return question mark icon", () => { it("should return question mark icon", () => {
const bonobUrl = url("http://localhost:1234/context-path?search=yes"); const bonobUrl = url("http://localhost:1234/context-path?search=yes");
const playlist = aPlaylist({ const playlist = aPlaylist({
entries: [aTrack({ album: undefined }), aTrack({ album: undefined })], entries: [aTrack({ coverArt: undefined }), aTrack({ coverArt: undefined })],
}); });
expect(playlistAlbumArtURL(bonobUrl, playlist).href()).toEqual( expect(playlistAlbumArtURL(bonobUrl, playlist).href()).toEqual(
@@ -318,20 +337,20 @@ describe("playlistAlbumArtURL", () => {
}); });
}); });
describe("when the playlist has 2 distinct albumIds", () => { describe("when the playlist has 2 distinct coverArt ids", () => {
it("should return them on the url to the image", () => { it("should return them on the url to the image", () => {
const bonobUrl = url("http://localhost:1234/context-path?search=yes"); const bonobUrl = url("http://localhost:1234/context-path?search=yes");
const playlist = aPlaylist({ const playlist = aPlaylist({
entries: [ entries: [
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "1" })) }), aTrack({ coverArt: "1" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "2" })) }), aTrack({ coverArt: "2" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "1" })) }), aTrack({ coverArt: "1" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "2" })) }), aTrack({ coverArt: "2" }),
], ],
}); });
expect(playlistAlbumArtURL(bonobUrl, playlist).href()).toEqual( expect(playlistAlbumArtURL(bonobUrl, playlist).href()).toEqual(
`http://localhost:1234/context-path/art/album/1&2/size/180?search=yes` `http://localhost:1234/context-path/art/1&2/size/180?search=yes`
); );
}); });
}); });
@@ -341,62 +360,85 @@ describe("playlistAlbumArtURL", () => {
const bonobUrl = url("http://localhost:1234/context-path?search=yes"); const bonobUrl = url("http://localhost:1234/context-path?search=yes");
const playlist = aPlaylist({ const playlist = aPlaylist({
entries: [ entries: [
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "1" })) }), aTrack({ coverArt: "1" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "2" })) }), aTrack({ coverArt: "2" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "2" })) }), aTrack({ coverArt: "2" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "3" })) }), aTrack({ coverArt: "3" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "4" })) }), aTrack({ coverArt: "4" }),
], ],
}); });
expect(playlistAlbumArtURL(bonobUrl, playlist).href()).toEqual( expect(playlistAlbumArtURL(bonobUrl, playlist).href()).toEqual(
`http://localhost:1234/context-path/art/album/1&2&3&4/size/180?search=yes` `http://localhost:1234/context-path/art/1&2&3&4/size/180?search=yes`
); );
}); });
}); });
describe("when the playlist has 9 distinct albumIds", () => { describe("when the playlist has at least 9 distinct albumIds", () => {
it("should return 9 of the ids on the url", () => { it("should return the first 9 of the ids on the url", () => {
const bonobUrl = url("http://localhost:1234/context-path?search=yes"); const bonobUrl = url("http://localhost:1234/context-path?search=yes");
const playlist = aPlaylist({ const playlist = aPlaylist({
entries: [ entries: [
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "1" })) }), aTrack({ coverArt: "1" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "2" })) }), aTrack({ coverArt: "2" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "3" })) }), aTrack({ coverArt: "2" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "4" })) }), aTrack({ coverArt: "2" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "5" })) }), aTrack({ coverArt: "3" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "6" })) }), aTrack({ coverArt: "4" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "7" })) }), aTrack({ coverArt: "5" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "8" })) }), aTrack({ coverArt: "6" }),
aTrack({ album: albumToAlbumSummary(anAlbum({ id: "9" })) }), aTrack({ coverArt: "7" }),
aTrack({ coverArt: "8" }),
aTrack({ coverArt: "9" }),
aTrack({ coverArt: "10" }),
aTrack({ coverArt: "11" }),
], ],
}); });
expect(playlistAlbumArtURL(bonobUrl, playlist).href()).toEqual( expect(playlistAlbumArtURL(bonobUrl, playlist).href()).toEqual(
`http://localhost:1234/context-path/art/album/1&2&3&4&5&6&7&8&9/size/180?search=yes` `http://localhost:1234/context-path/art/1&2&3&4&5&6&7&8&9/size/180?search=yes`
); );
}); });
}); });
}); });
describe("defaultAlbumArtURI", () => { describe("defaultAlbumArtURI", () => {
it("should create the correct URI", () => { const bonobUrl = new URLBuilder("http://bonob.example.com:8080/context?search=yes");
const bonobUrl = url("http://localhost:1234/context-path?search=yes");
const album = anAlbum();
expect(defaultAlbumArtURI(bonobUrl, album).href()).toEqual( describe("when there is an album coverArt", () => {
`http://localhost:1234/context-path/art/album/${album.id}/size/180?search=yes` it("should use it in the image url", () => {
expect(
defaultAlbumArtURI(
bonobUrl,
anAlbum({ coverArt: "coverArt:123" })
).href()
).toEqual(
"http://bonob.example.com:8080/context/art/coverArt:123/size/180?search=yes"
); );
}); });
}); });
describe("when there is no album coverArt", () => {
it("should return a vinly icon image", () => {
expect(
defaultAlbumArtURI(
bonobUrl,
anAlbum({ coverArt: undefined })
).href()
).toEqual(
"http://bonob.example.com:8080/context/icon/vinyl/size/legacy?search=yes"
);
});
});
});
describe("defaultArtistArtURI", () => { describe("defaultArtistArtURI", () => {
it("should create the correct URI", () => { it("should create the correct URI", () => {
const bonobUrl = url("http://localhost:1234/something?s=123"); const bonobUrl = url("http://localhost:1234/something?s=123");
const artist = anArtist(); const artist = anArtist();
expect(defaultArtistArtURI(bonobUrl, artist).href()).toEqual( expect(defaultArtistArtURI(bonobUrl, artist).href()).toEqual(
`http://localhost:1234/something/art/artist/${artist.id}/size/180?s=123` `http://localhost:1234/something/art/artist:${artist.id}/size/180?s=123`
); );
}); });
}); });
@@ -430,6 +472,7 @@ describe("api", () => {
deletePlaylist: jest.fn(), deletePlaylist: jest.fn(),
removeFromPlaylist: jest.fn(), removeFromPlaylist: jest.fn(),
scrobble: jest.fn(), scrobble: jest.fn(),
nowPlaying: jest.fn(),
}; };
const accessTokens = { const accessTokens = {
mint: jest.fn(), mint: jest.fn(),
@@ -448,7 +491,7 @@ describe("api", () => {
const accessToken = `accessToken-${uuid()}`; const accessToken = `accessToken-${uuid()}`;
const bonobUrlWithAccessToken = bonobUrl.append({ const bonobUrlWithAccessToken = bonobUrl.append({
searchParams: { "bat": accessToken }, searchParams: { bat: accessToken },
}); });
const service = bonobService("test-api", 133, bonobUrl, "AppLink"); const service = bonobService("test-api", 133, bonobUrl, "AppLink");
@@ -1020,7 +1063,7 @@ describe("api", () => {
title: genre.name, title: genre.name,
albumArtURI: iconArtURI( albumArtURI: iconArtURI(
bonobUrl, bonobUrl,
iconForGenre(genre.name), iconForGenre(genre.name)
).href(), ).href(),
})), })),
index: 0, index: 0,
@@ -1045,7 +1088,7 @@ describe("api", () => {
title: genre.name, title: genre.name,
albumArtURI: iconArtURI( albumArtURI: iconArtURI(
bonobUrl, bonobUrl,
iconForGenre(genre.name), iconForGenre(genre.name)
).href(), ).href(),
})), })),
index: 1, index: 1,
@@ -2302,14 +2345,17 @@ describe("api", () => {
artistId: `artist:${track.artist.id}`, artistId: `artist:${track.artist.id}`,
artist: track.artist.name, artist: track.artist.name,
albumId: `album:${track.album.id}`, albumId: `album:${track.album.id}`,
albumArtist: track.artist.name,
albumArtistId: `artist:${track.artist.id}`,
album: track.album.name, album: track.album.name,
genre: track.genre?.name, genre: track.genre?.name,
genreId: track.genre?.id, genreId: track.genre?.id,
duration: track.duration, duration: track.duration,
albumArtURI: defaultAlbumArtURI( albumArtURI: defaultAlbumArtURI(
bonobUrlWithAccessToken, bonobUrlWithAccessToken,
track.album track
).href(), ).href(),
trackNumber: track.number,
}, },
}, },
}, },
@@ -2425,12 +2471,9 @@ describe("api", () => {
getMediaURIResult: bonobUrl getMediaURIResult: bonobUrl
.append({ .append({
pathname: `/stream/track/${trackId}`, pathname: `/stream/track/${trackId}`,
searchParams: { "bat": accessToken }
}) })
.href(), .href(),
httpHeaders: {
header: "bat",
value: accessToken,
},
}); });
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
@@ -2510,7 +2553,7 @@ describe("api", () => {
expect(root[0]).toEqual({ expect(root[0]).toEqual({
getMediaMetadataResult: track( getMediaMetadataResult: track(
bonobUrl.with({ bonobUrl.with({
searchParams: { "bat": accessToken }, searchParams: { bat: accessToken },
}), }),
someTrack someTrack
), ),
@@ -2765,9 +2808,11 @@ describe("api", () => {
function itShouldScroble({ function itShouldScroble({
trackId, trackId,
secondsPlayed, secondsPlayed,
shouldMarkNowPlaying,
}: { }: {
trackId: string; trackId: string;
secondsPlayed: number; secondsPlayed: number;
shouldMarkNowPlaying: boolean,
}) { }) {
it("should scrobble", async () => { it("should scrobble", async () => {
musicLibrary.scrobble.mockResolvedValue(true); musicLibrary.scrobble.mockResolvedValue(true);
@@ -2782,15 +2827,22 @@ describe("api", () => {
expect(accessTokens.mint).toHaveBeenCalledWith(authToken); expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
expect(musicLibrary.track).toHaveBeenCalledWith(trackId); expect(musicLibrary.track).toHaveBeenCalledWith(trackId);
expect(musicLibrary.scrobble).toHaveBeenCalledWith(trackId); expect(musicLibrary.scrobble).toHaveBeenCalledWith(trackId);
if(shouldMarkNowPlaying) {
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
} else {
expect(musicLibrary.nowPlaying).not.toHaveBeenCalled();
}
}); });
} }
function itShouldNotScroble({ function itShouldNotScroble({
trackId, trackId,
secondsPlayed, secondsPlayed,
shouldMarkNowPlaying,
}: { }: {
trackId: string; trackId: string;
secondsPlayed: number; secondsPlayed: number;
shouldMarkNowPlaying: boolean,
}) { }) {
it("should scrobble", async () => { it("should scrobble", async () => {
const result = await ws.setPlayedSecondsAsync({ const result = await ws.setPlayedSecondsAsync({
@@ -2803,6 +2855,11 @@ describe("api", () => {
expect(accessTokens.mint).toHaveBeenCalledWith(authToken); expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
expect(musicLibrary.track).toHaveBeenCalledWith(trackId); expect(musicLibrary.track).toHaveBeenCalledWith(trackId);
expect(musicLibrary.scrobble).not.toHaveBeenCalled(); expect(musicLibrary.scrobble).not.toHaveBeenCalled();
if(shouldMarkNowPlaying) {
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
} else {
expect(musicLibrary.nowPlaying).not.toHaveBeenCalled();
}
}); });
} }
@@ -2813,16 +2870,24 @@ describe("api", () => {
); );
}); });
describe("when the played length is 30 seconds", () => { describe("when the seconds played is 30 seconds", () => {
itShouldScroble({ trackId, secondsPlayed: 30 }); itShouldScroble({ trackId, secondsPlayed: 30, shouldMarkNowPlaying: true });
}); });
describe("when the played length is > 30 seconds", () => { describe("when the seconds played is > 30 seconds", () => {
itShouldScroble({ trackId, secondsPlayed: 90 }); itShouldScroble({ trackId, secondsPlayed: 90, shouldMarkNowPlaying: true });
}); });
describe("when the played length is < 30 seconds", () => { describe("when the seconds played is < 30 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 29 }); itShouldNotScroble({ trackId, secondsPlayed: 29, shouldMarkNowPlaying: true });
});
describe("when the seconds played is 1 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 1, shouldMarkNowPlaying: true });
});
describe("when the seconds played is 0 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 0, shouldMarkNowPlaying: false });
}); });
}); });
@@ -2833,16 +2898,24 @@ describe("api", () => {
); );
}); });
describe("when the played length is 30 seconds", () => { describe("when the seconds played is 30 seconds", () => {
itShouldScroble({ trackId, secondsPlayed: 30 }); itShouldScroble({ trackId, secondsPlayed: 30, shouldMarkNowPlaying: true });
}); });
describe("when the played length is > 30 seconds", () => { describe("when the seconds played is > 30 seconds", () => {
itShouldScroble({ trackId, secondsPlayed: 90 }); itShouldScroble({ trackId, secondsPlayed: 90, shouldMarkNowPlaying: true });
}); });
describe("when the played length is < 30 seconds", () => { describe("when the seconds played is < 30 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 29 }); itShouldNotScroble({ trackId, secondsPlayed: 29, shouldMarkNowPlaying: true });
});
describe("when the seconds played is 1 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 1, shouldMarkNowPlaying: true });
});
describe("when the seconds played is 0 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 0, shouldMarkNowPlaying: false });
}); });
}); });
@@ -2853,20 +2926,28 @@ describe("api", () => {
); );
}); });
describe("when the played length is 29 seconds", () => { describe("when the seconds played is 29 seconds", () => {
itShouldScroble({ trackId, secondsPlayed: 30 }); itShouldScroble({ trackId, secondsPlayed: 30, shouldMarkNowPlaying: true });
}); });
describe("when the played length is > 29 seconds", () => { describe("when the seconds played is > 29 seconds", () => {
itShouldScroble({ trackId, secondsPlayed: 30 }); itShouldScroble({ trackId, secondsPlayed: 30, shouldMarkNowPlaying: true });
}); });
describe("when the played length is 10 seconds", () => { describe("when the seconds played is 10 seconds", () => {
itShouldScroble({ trackId, secondsPlayed: 10 }); itShouldScroble({ trackId, secondsPlayed: 10, shouldMarkNowPlaying: true });
}); });
describe("when the played length is < 10 seconds", () => { describe("when the seconds played is < 10 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 9 }); itShouldNotScroble({ trackId, secondsPlayed: 9, shouldMarkNowPlaying: true });
});
describe("when the seconds played is 1 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 1, shouldMarkNowPlaying: true });
});
describe("when the seconds played is 0 seconds", () => {
itShouldNotScroble({ trackId, secondsPlayed: 0, shouldMarkNowPlaying: false });
}); });
}); });
}); });
@@ -2881,6 +2962,7 @@ describe("api", () => {
expect(result[0]).toEqual({ setPlayedSecondsResult: null }); expect(result[0]).toEqual({ setPlayedSecondsResult: null });
expect(musicService.login).toHaveBeenCalledWith(authToken); expect(musicService.login).toHaveBeenCalledWith(authToken);
expect(accessTokens.mint).toHaveBeenCalledWith(authToken); expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
expect(musicLibrary.nowPlaying).not.toHaveBeenCalled();
expect(musicLibrary.scrobble).not.toHaveBeenCalled(); expect(musicLibrary.scrobble).not.toHaveBeenCalled();
}); });
}); });

View File

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

View File

@@ -3,14 +3,15 @@ import { v4 as uuid } from "uuid";
import { import {
isDodgyImage, isDodgyImage,
Navidrome, Subsonic,
t, t,
BROWSER_HEADERS, BROWSER_HEADERS,
DODGY_IMAGE_NAME, DODGY_IMAGE_NAME,
asGenre, asGenre,
appendMimeTypeToClientFor, appendMimeTypeToClientFor,
asURLSearchParams, asURLSearchParams,
} from "../src/navidrome"; splitCoverArtId,
} from "../src/subsonic";
import encryption from "../src/encryption"; import encryption from "../src/encryption";
import axios from "axios"; import axios from "axios";
@@ -38,12 +39,16 @@ import {
SimilarArtist, SimilarArtist,
} from "../src/music_service"; } from "../src/music_service";
import { import {
aGenre,
anAlbum, anAlbum,
anArtist, anArtist,
aPlaylist, aPlaylist,
aPlaylistSummary, aPlaylistSummary,
aTrack, aTrack,
POP,
ROCK,
} from "./builders"; } from "./builders";
import { b64Encode } from "../src/b64";
describe("t", () => { describe("t", () => {
it("should be an md5 of the password and the salt", () => { it("should be an md5 of the password and the salt", () => {
@@ -179,6 +184,9 @@ const getArtistInfoXml = (
</artistInfo2> </artistInfo2>
</subsonic-response>`; </subsonic-response>`;
const maybeIdFromCoverArtId = (coverArt: string | undefined) =>
coverArt ? splitCoverArtId(coverArt)[1] : "";
const albumXml = ( const albumXml = (
artist: Artist, artist: Artist,
album: AlbumSummary, album: AlbumSummary,
@@ -189,7 +197,7 @@ const albumXml = (
title="${album.name}" name="${album.name}" album="${album.name}" title="${album.name}" name="${album.name}" album="${album.name}"
artist="${artist.name}" artist="${artist.name}"
genre="${album.genre?.name}" genre="${album.genre?.name}"
coverArt="foo" coverArt="${maybeIdFromCoverArtId(album.coverArt)}"
duration="123" duration="123"
playCount="4" playCount="4"
year="${album.year}" year="${album.year}"
@@ -207,7 +215,7 @@ const songXml = (track: Track) => `<song
track="${track.number}" track="${track.number}"
genre="${track.genre?.name}" genre="${track.genre?.name}"
isDir="false" isDir="false"
coverArt="71381" coverArt="${maybeIdFromCoverArtId(track.coverArt)}"
created="2004-11-08T23:36:11" created="2004-11-08T23:36:11"
duration="${track.duration}" duration="${track.duration}"
bitRate="128" bitRate="128"
@@ -339,7 +347,7 @@ const getPlayList = (
track="${it.number}" track="${it.number}"
year="${it.album.year}" year="${it.album.year}"
genre="${it.album.genre?.name}" genre="${it.album.genre?.name}"
coverArt="..." coverArt="${splitCoverArtId(it.coverArt!)[1]}"
size="123" size="123"
contentType="${it.mimeType}" contentType="${it.mimeType}"
suffix="mp3" suffix="mp3"
@@ -428,14 +436,34 @@ const EMPTY = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok
const PING_OK = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)"></subsonic-response>`; const PING_OK = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)"></subsonic-response>`;
describe("Navidrome", () => { describe("splitCoverArtId", () => {
it("should split correctly", () => {
expect(splitCoverArtId("foo:bar")).toEqual(["foo", "bar"]);
expect(splitCoverArtId("foo:bar:car:jar")).toEqual(["foo", "bar:car:jar"]);
});
it("should blow up when the id is invalid", () => {
expect(() => splitCoverArtId("")).toThrow(`'' is an invalid coverArt id`);
expect(() => splitCoverArtId("foo:")).toThrow(
`'foo:' is an invalid coverArt id`
);
expect(() => splitCoverArtId("foo:")).toThrow(
`'foo:' is an invalid coverArt id`
);
expect(() => splitCoverArtId(":dog")).toThrow(
`':dog' is an invalid coverArt id`
);
});
});
describe("Subsonic", () => {
const url = "http://127.0.0.22:4567"; const url = "http://127.0.0.22:4567";
const username = "user1"; const username = "user1";
const password = "pass1"; const password = "pass1";
const salt = "saltysalty"; const salt = "saltysalty";
const streamClientApplication = jest.fn(); const streamClientApplication = jest.fn();
const navidrome = new Navidrome( const navidrome = new Subsonic(
url, url,
encryption("secret"), encryption("secret"),
streamClientApplication streamClientApplication
@@ -498,7 +526,7 @@ describe("Navidrome", () => {
const token = await navidrome.generateToken({ username, password }); const token = await navidrome.generateToken({ username, password });
expect(token).toEqual({ expect(token).toEqual({
message: "Navidrome error:Wrong username or password", message: "Subsonic error:Wrong username or password",
}); });
}); });
}); });
@@ -547,7 +575,7 @@ describe("Navidrome", () => {
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.genres()); .then((it) => it.genres());
expect(result).toEqual([{ id: "genre1", name: "genre1" }]); expect(result).toEqual([{ id: b64Encode("genre1"), name: "genre1" }]);
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getGenres`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getGenres`, {
params: asURLSearchParams(authParams), params: asURLSearchParams(authParams),
@@ -579,10 +607,10 @@ describe("Navidrome", () => {
.then((it) => it.genres()); .then((it) => it.genres());
expect(result).toEqual([ expect(result).toEqual([
{ id: "g1", name: "g1" }, { id: b64Encode("g1"), name: "g1" },
{ id: "g2", name: "g2" }, { id: b64Encode("g2"), name: "g2" },
{ id: "g3", name: "g3" }, { id: b64Encode("g3"), name: "g3" },
{ id: "g4", name: "g4" }, { id: b64Encode("g4"), name: "g4" },
]); ]);
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getGenres`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getGenres`, {
@@ -1234,11 +1262,11 @@ describe("Navidrome", () => {
); );
}); });
it("should pass the filter to navidrome", async () => { it("should map the 64 encoded genre back into the subsonic genre", async () => {
const q: AlbumQuery = { const q: AlbumQuery = {
_index: 0, _index: 0,
_count: 100, _count: 100,
genre: "Pop", genre: b64Encode("Pop"),
type: "byGenre", type: "byGenre",
}; };
const result = await navidrome const result = await navidrome
@@ -1656,17 +1684,10 @@ describe("Navidrome", () => {
// the artists have 5 albums in the getArtists endpoint // the artists have 5 albums in the getArtists endpoint
const artist1 = anArtist({ const artist1 = anArtist({
albums: [ albums: [album1, album2, album3, album4],
album1,
album2,
album3,
album4,
],
}); });
const artist2 = anArtist({ const artist2 = anArtist({
albums: [ albums: [album5],
album5,
],
}); });
const artists = [artist1, artist2]; const artists = [artist1, artist2];
@@ -1706,12 +1727,7 @@ describe("Navidrome", () => {
.then((it) => it.albums(q)); .then((it) => it.albums(q));
expect(result).toEqual({ expect(result).toEqual({
results: [ results: [album1, album2, album3, album5],
album1,
album2,
album3,
album5,
],
total: 4, total: 4,
}); });
@@ -1720,7 +1736,9 @@ describe("Navidrome", () => {
headers, headers,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getAlbumList2`, { expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getAlbumList2`,
{
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
type: "alphabeticalByArtist", type: "alphabeticalByArtist",
@@ -1728,7 +1746,8 @@ describe("Navidrome", () => {
offset: q._index, offset: q._index,
}), }),
headers, headers,
}); }
);
}); });
}); });
@@ -1768,10 +1787,7 @@ describe("Navidrome", () => {
.then((it) => it.albums(q)); .then((it) => it.albums(q));
expect(result).toEqual({ expect(result).toEqual({
results: [ results: [album1, album2],
album1,
album2,
],
total: 4, total: 4,
}); });
@@ -1780,7 +1796,9 @@ describe("Navidrome", () => {
headers, headers,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getAlbumList2`, { expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getAlbumList2`,
{
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
type: "alphabeticalByArtist", type: "alphabeticalByArtist",
@@ -1788,7 +1806,8 @@ describe("Navidrome", () => {
offset: q._index, offset: q._index,
}), }),
headers, headers,
}); }
);
}); });
}); });
@@ -1827,10 +1846,7 @@ describe("Navidrome", () => {
.then((it) => it.albums(q)); .then((it) => it.albums(q));
expect(result).toEqual({ expect(result).toEqual({
results: [ results: [album3, album5],
album3,
album5,
],
total: 4, total: 4,
}); });
@@ -1839,7 +1855,9 @@ describe("Navidrome", () => {
headers, headers,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getAlbumList2`, { expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getAlbumList2`,
{
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
type: "alphabeticalByArtist", type: "alphabeticalByArtist",
@@ -1847,7 +1865,8 @@ describe("Navidrome", () => {
offset: q._index, offset: q._index,
}), }),
headers, headers,
}); }
);
}); });
}); });
}); });
@@ -1858,11 +1877,15 @@ describe("Navidrome", () => {
mockGET mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK))) .mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => .mockImplementationOnce(() =>
Promise.resolve(ok(getArtistsXml([ Promise.resolve(
ok(
getArtistsXml([
// artist1 has lost 2 albums on the getArtists end point // artist1 has lost 2 albums on the getArtists end point
{ ...artist1, albums: [album1, album2] }, { ...artist1, albums: [album1, album2] },
artist2, artist2,
]))) ])
)
)
) )
.mockImplementationOnce(() => .mockImplementationOnce(() =>
Promise.resolve( Promise.resolve(
@@ -1892,13 +1915,7 @@ describe("Navidrome", () => {
.then((it) => it.albums(q)); .then((it) => it.albums(q));
expect(result).toEqual({ expect(result).toEqual({
results: [ results: [album1, album2, album3, album4, album5],
album1,
album2,
album3,
album4,
album5,
],
total: 5, total: 5,
}); });
@@ -1907,7 +1924,9 @@ describe("Navidrome", () => {
headers, headers,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getAlbumList2`, { expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getAlbumList2`,
{
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
type: "alphabeticalByArtist", type: "alphabeticalByArtist",
@@ -1915,7 +1934,8 @@ describe("Navidrome", () => {
offset: q._index, offset: q._index,
}), }),
headers, headers,
}); }
);
}); });
}); });
@@ -1924,11 +1944,15 @@ describe("Navidrome", () => {
mockGET mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK))) .mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => .mockImplementationOnce(() =>
Promise.resolve(ok(getArtistsXml([ Promise.resolve(
ok(
getArtistsXml([
// artist1 has lost 2 albums on the getArtists end point // artist1 has lost 2 albums on the getArtists end point
{ ...artist1, albums: [album1, album2] }, { ...artist1, albums: [album1, album2] },
artist2, artist2,
]))) ])
)
)
) )
.mockImplementationOnce(() => .mockImplementationOnce(() =>
Promise.resolve( Promise.resolve(
@@ -1958,10 +1982,7 @@ describe("Navidrome", () => {
.then((it) => it.albums(q)); .then((it) => it.albums(q));
expect(result).toEqual({ expect(result).toEqual({
results: [ results: [album1, album2],
album1,
album2,
],
total: 5, total: 5,
}); });
@@ -1970,7 +1991,9 @@ describe("Navidrome", () => {
headers, headers,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getAlbumList2`, { expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getAlbumList2`,
{
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
type: "alphabeticalByArtist", type: "alphabeticalByArtist",
@@ -1978,7 +2001,8 @@ describe("Navidrome", () => {
offset: q._index, offset: q._index,
}), }),
headers, headers,
}); }
);
}); });
}); });
@@ -1987,11 +2011,15 @@ describe("Navidrome", () => {
mockGET mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK))) .mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => .mockImplementationOnce(() =>
Promise.resolve(ok(getArtistsXml([ Promise.resolve(
ok(
getArtistsXml([
// artist1 has lost 2 albums on the getArtists end point // artist1 has lost 2 albums on the getArtists end point
{ ...artist1, albums: [album1, album2] }, { ...artist1, albums: [album1, album2] },
artist2, artist2,
]))) ])
)
)
) )
.mockImplementationOnce(() => .mockImplementationOnce(() =>
Promise.resolve( Promise.resolve(
@@ -2019,11 +2047,7 @@ describe("Navidrome", () => {
.then((it) => it.albums(q)); .then((it) => it.albums(q));
expect(result).toEqual({ expect(result).toEqual({
results: [ results: [album3, album4, album5],
album3,
album4,
album5,
],
total: 5, total: 5,
}); });
@@ -2032,7 +2056,9 @@ describe("Navidrome", () => {
headers, headers,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getAlbumList2`, { expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getAlbumList2`,
{
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
type: "alphabeticalByArtist", type: "alphabeticalByArtist",
@@ -2040,12 +2066,12 @@ describe("Navidrome", () => {
offset: q._index, offset: q._index,
}), }),
headers, headers,
}
);
}); });
}); });
}); });
}); });
});
}); });
describe("getting an album", () => { describe("getting an album", () => {
@@ -2300,7 +2326,7 @@ describe("Navidrome", () => {
describe("streaming a track", () => { describe("streaming a track", () => {
const trackId = uuid(); const trackId = uuid();
const genre = { id: "foo", name: "foo" }; const genre = aGenre("foo");
const album = anAlbum({ genre }); const album = anAlbum({ genre });
const artist = anArtist({ const artist = anArtist({
@@ -2481,7 +2507,7 @@ describe("Navidrome", () => {
return expect( return expect(
musicLibrary.stream({ trackId, range: undefined }) musicLibrary.stream({ trackId, range: undefined })
).rejects.toEqual(`Navidrome failed with a 400 status`); ).rejects.toEqual(`Subsonic failed with a 400 status`);
}); });
}); });
}); });
@@ -2658,7 +2684,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(coverArtId, "album")); .then((it) => it.coverArt(`coverArt:${coverArtId}`));
expect(result).toEqual({ expect(result).toEqual({
contentType: streamResponse.headers["content-type"], contentType: streamResponse.headers["content-type"],
@@ -2696,7 +2722,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(coverArtId, "album", size)); .then((it) => it.coverArt(`coverArt:${coverArtId}`, size));
expect(result).toEqual({ expect(result).toEqual({
contentType: streamResponse.headers["content-type"], contentType: streamResponse.headers["content-type"],
@@ -2714,6 +2740,25 @@ describe("Navidrome", () => {
}); });
}); });
}); });
describe("when an unexpected error occurs", () => {
it("should return undefined", async () => {
const coverArtId = "someCoverArt";
const size = 1879;
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => Promise.reject("BOOOM"));
const result = await navidrome
.generateToken({ username, password })
.then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(`coverArt:${coverArtId}`, size));
expect(result).toBeUndefined();
});
});
}); });
describe("fetching artist art", () => { describe("fetching artist art", () => {
@@ -2752,7 +2797,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(artistId, "artist")); .then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toEqual({ expect(result).toEqual({
contentType: streamResponse.headers["content-type"], contentType: streamResponse.headers["content-type"],
@@ -2777,11 +2822,42 @@ describe("Navidrome", () => {
responseType: "arraybuffer", responseType: "arraybuffer",
}); });
}); });
describe("and an error occurs fetching the uri", () => {
it("should return undefined", async () => {
const artistId = "someArtist123";
const images: Images = {
small: "http://example.com/images/small",
medium: "http://example.com/images/medium",
large: "http://example.com/images/large",
};
const artist = anArtist({ id: artistId, image: images });
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(ok(getArtistXml(artist)))
)
.mockImplementationOnce(() =>
Promise.resolve(ok(getArtistInfoXml(artist)))
)
.mockImplementationOnce(() => Promise.reject("BOOOM"));
const result = await navidrome
.generateToken({ username, password })
.then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toBeUndefined();
});
});
}); });
describe("when the artist doest not have a valid artist uri", () => { describe("when the artist doest not have a valid artist uri", () => {
describe("however has some albums", () => { describe("however has some albums", () => {
it("should fetch the artists first album image", async () => {
const artistId = "someArtist123"; const artistId = "someArtist123";
const images: Images = { const images: Images = {
@@ -2798,8 +2874,10 @@ describe("Navidrome", () => {
data: Buffer.from("the image", "ascii"), data: Buffer.from("the image", "ascii"),
}; };
const album1 = anAlbum(); describe("no albums have coverArt", () => {
const album2 = anAlbum(); it("should return undefined", async () => {
const album1 = anAlbum({ coverArt: undefined });
const album2 = anAlbum({ coverArt: undefined });
const artist = anArtist({ const artist = anArtist({
id: artistId, id: artistId,
@@ -2815,26 +2893,93 @@ describe("Navidrome", () => {
.mockImplementationOnce(() => .mockImplementationOnce(() =>
Promise.resolve(ok(getArtistInfoXml(artist))) Promise.resolve(ok(getArtistInfoXml(artist)))
) )
.mockImplementationOnce(() => Promise.resolve(streamResponse)); .mockImplementationOnce(() =>
Promise.resolve(streamResponse)
);
const result = await navidrome const result = await navidrome
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(artistId, "artist")); .then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toEqual(undefined);
expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getArtist`,
{
params: asURLSearchParams({
...authParams,
id: artistId,
}),
headers,
}
);
expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getArtistInfo2`,
{
params: asURLSearchParams({
...authParams,
id: artistId,
count: 50,
includeNotPresent: true,
}),
headers,
}
);
});
});
describe("some albums have coverArt", () => {
describe("all albums have coverArt", () => {
it("should fetch the coverArt from the first album", async () => {
const album1 = anAlbum({
coverArt: `coverArt:album1CoverArt`,
});
const album2 = anAlbum({
coverArt: `coverArt:album2CoverArt`,
});
const artist = anArtist({
id: artistId,
albums: [album1, album2],
image: images,
});
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(ok(getArtistXml(artist)))
)
.mockImplementationOnce(() =>
Promise.resolve(ok(getArtistInfoXml(artist)))
)
.mockImplementationOnce(() =>
Promise.resolve(streamResponse)
);
const result = await navidrome
.generateToken({ username, password })
.then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toEqual({ expect(result).toEqual({
contentType: streamResponse.headers["content-type"], contentType: streamResponse.headers["content-type"],
data: streamResponse.data, data: streamResponse.data,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtist`, { expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getArtist`,
{
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
id: artistId, id: artistId,
}), }),
headers, headers,
}); }
);
expect(axios.get).toHaveBeenCalledWith( expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getArtistInfo2`, `${url}/rest/getArtistInfo2`,
@@ -2854,7 +2999,7 @@ describe("Navidrome", () => {
{ {
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
id: album1.id, id: splitCoverArtId(album1.coverArt!)[1],
}), }),
headers, headers,
responseType: "arraybuffer", responseType: "arraybuffer",
@@ -2863,6 +3008,117 @@ describe("Navidrome", () => {
}); });
}); });
describe("the first album does not have coverArt", () => {
it("should fetch the coverArt from the first album with coverArt", async () => {
const album1 = anAlbum({ coverArt: undefined });
const album2 = anAlbum({
coverArt: `coverArt:album2CoverArt`,
});
const artist = anArtist({
id: artistId,
albums: [album1, album2],
image: images,
});
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(ok(getArtistXml(artist)))
)
.mockImplementationOnce(() =>
Promise.resolve(ok(getArtistInfoXml(artist)))
)
.mockImplementationOnce(() =>
Promise.resolve(streamResponse)
);
const result = await navidrome
.generateToken({ username, password })
.then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toEqual({
contentType: streamResponse.headers["content-type"],
data: streamResponse.data,
});
expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getArtist`,
{
params: asURLSearchParams({
...authParams,
id: artistId,
}),
headers,
}
);
expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getArtistInfo2`,
{
params: asURLSearchParams({
...authParams,
id: artistId,
count: 50,
includeNotPresent: true,
}),
headers,
}
);
expect(axios.get).toHaveBeenCalledWith(
`${url}/rest/getCoverArt`,
{
params: asURLSearchParams({
...authParams,
id: splitCoverArtId(album2.coverArt!)[1],
}),
headers,
responseType: "arraybuffer",
}
);
});
});
describe("an unexpected error occurs getting the albums coverArt", () => {
it("should fetch the coverArt from the first album", async () => {
const album1 = anAlbum({
coverArt: `coverArt:album1CoverArt`,
});
const album2 = anAlbum({
coverArt: `coverArt:album2CoverArt`,
});
const artist = anArtist({
id: artistId,
albums: [album1, album2],
image: images,
});
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(ok(getArtistXml(artist)))
)
.mockImplementationOnce(() =>
Promise.resolve(ok(getArtistInfoXml(artist)))
)
.mockImplementationOnce(() => Promise.reject("BOOOM"));
const result = await navidrome
.generateToken({ username, password })
.then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toBeUndefined();
});
});
});
});
describe("and has no albums", () => { describe("and has no albums", () => {
it("should return undefined", async () => { it("should return undefined", async () => {
const artistId = "someArtist123"; const artistId = "someArtist123";
@@ -2901,7 +3157,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(artistId, "artist")); .then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toBeUndefined(); expect(result).toBeUndefined();
@@ -2976,7 +3232,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(artistId, "artist", size)); .then((it) => it.coverArt(`artist:${artistId}`, size));
expect(result).toEqual({ expect(result).toEqual({
contentType: streamResponse.headers["content-type"], contentType: streamResponse.headers["content-type"],
@@ -3048,7 +3304,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(artistId, "artist", size)); .then((it) => it.coverArt(`artist:${artistId}`, size));
expect(result).toEqual({ expect(result).toEqual({
contentType: streamResponse.headers["content-type"], contentType: streamResponse.headers["content-type"],
@@ -3081,7 +3337,7 @@ describe("Navidrome", () => {
{ {
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
id: album1.id, id: splitCoverArtId(album1.coverArt!)[1],
size, size,
}), }),
headers, headers,
@@ -3129,7 +3385,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(artistId, "artist")); .then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toBeUndefined(); expect(result).toBeUndefined();
@@ -3176,8 +3432,14 @@ describe("Navidrome", () => {
data: Buffer.from("the image", "ascii"), data: Buffer.from("the image", "ascii"),
}; };
const album1 = anAlbum({ id: "album1Id" }); const album1 = anAlbum({
const album2 = anAlbum({ id: "album2Id" }); id: "album1Id",
coverArt: "coverArt:album1CoverArt",
});
const album2 = anAlbum({
id: "album2Id",
coverArt: "coverArt:album2CoverArt",
});
const artist = anArtist({ const artist = anArtist({
id: artistId, id: artistId,
@@ -3199,7 +3461,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(artistId, "artist", size)); .then((it) => it.coverArt(`artist:${artistId}`, size));
expect(result).toEqual({ expect(result).toEqual({
contentType: streamResponse.headers["content-type"], contentType: streamResponse.headers["content-type"],
@@ -3232,7 +3494,7 @@ describe("Navidrome", () => {
{ {
params: asURLSearchParams({ params: asURLSearchParams({
...authParams, ...authParams,
id: album1.id, id: splitCoverArtId(album1.coverArt!)[1],
size, size,
}), }),
headers, headers,
@@ -3280,7 +3542,7 @@ describe("Navidrome", () => {
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.coverArt(artistId, "artist")); .then((it) => it.coverArt(`artist:${artistId}`));
expect(result).toBeUndefined(); expect(result).toBeUndefined();
@@ -3535,7 +3797,7 @@ describe("Navidrome", () => {
it("should return true", async () => { it("should return true", async () => {
const album = anAlbum({ const album = anAlbum({
name: "foo woo", name: "foo woo",
genre: { id: "pop", name: "pop" }, genre: { id: b64Encode("pop"), name: "pop" },
}); });
const artist = anArtist({ name: "#1", albums: [album] }); const artist = anArtist({ name: "#1", albums: [album] });
@@ -3570,13 +3832,13 @@ describe("Navidrome", () => {
it("should return true", async () => { it("should return true", async () => {
const album1 = anAlbum({ const album1 = anAlbum({
name: "album1", name: "album1",
genre: { id: "pop", name: "pop" }, genre: { id: b64Encode("pop"), name: "pop" },
}); });
const artist1 = anArtist({ name: "artist1", albums: [album1] }); const artist1 = anArtist({ name: "artist1", albums: [album1] });
const album2 = anAlbum({ const album2 = anAlbum({
name: "album2", name: "album2",
genre: { id: "pop", name: "pop" }, genre: { id: b64Encode("pop"), name: "pop" },
}); });
const artist2 = anArtist({ name: "artist2", albums: [album2] }); const artist2 = anArtist({ name: "artist2", albums: [album2] });
@@ -3894,7 +4156,7 @@ describe("Navidrome", () => {
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.playlist(id)) .then((it) => it.playlist(id))
).rejects.toEqual("Navidrome error:data not found"); ).rejects.toEqual("Subsonic error:data not found");
}); });
}); });
@@ -3903,13 +4165,32 @@ describe("Navidrome", () => {
it("should return the playlist with entries", async () => { it("should return the playlist with entries", async () => {
const id = uuid(); const id = uuid();
const name = "Great Playlist"; const name = "Great Playlist";
const artist1 = anArtist();
const album1 = anAlbum({
artistId: artist1.id,
artistName: artist1.name,
genre: POP,
});
const track1 = aTrack({ const track1 = aTrack({
genre: { id: "pop", name: "pop" }, genre: POP,
number: 66, number: 66,
coverArt: album1.coverArt,
artist: artistToArtistSummary(artist1),
album: albumToAlbumSummary(album1),
});
const artist2 = anArtist();
const album2 = anAlbum({
artistId: artist2.id,
artistName: artist2.name,
genre: ROCK,
}); });
const track2 = aTrack({ const track2 = aTrack({
genre: { id: "rock", name: "rock" }, genre: ROCK,
number: 77, number: 77,
coverArt: album2.coverArt,
artist: artistToArtistSummary(artist2),
album: albumToAlbumSummary(album2),
}); });
mockGET mockGET
@@ -4261,7 +4542,7 @@ describe("Navidrome", () => {
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.similarSongs(id)) .then((it) => it.similarSongs(id))
).rejects.toEqual("Navidrome error:data not found"); ).rejects.toEqual("Subsonic error:data not found");
}); });
}); });
}); });
@@ -4321,10 +4602,9 @@ describe("Navidrome", () => {
it("should return them", async () => { it("should return them", async () => {
const artistId = "bobMarleyId"; const artistId = "bobMarleyId";
const artistName = "Bob Marley"; const artistName = "Bob Marley";
const pop = asGenre("Pop");
const album1 = anAlbum({ name: "Burnin", genre: pop }); const album1 = anAlbum({ name: "Burnin", genre: POP });
const album2 = anAlbum({ name: "Churning", genre: pop }); const album2 = anAlbum({ name: "Churning", genre: POP });
const artist = anArtist({ const artist = anArtist({
id: artistId, id: artistId,
@@ -4335,19 +4615,19 @@ describe("Navidrome", () => {
const track1 = aTrack({ const track1 = aTrack({
artist: artistToArtistSummary(artist), artist: artistToArtistSummary(artist),
album: albumToAlbumSummary(album1), album: albumToAlbumSummary(album1),
genre: pop, genre: POP,
}); });
const track2 = aTrack({ const track2 = aTrack({
artist: artistToArtistSummary(artist), artist: artistToArtistSummary(artist),
album: albumToAlbumSummary(album2), album: albumToAlbumSummary(album2),
genre: pop, genre: POP,
}); });
const track3 = aTrack({ const track3 = aTrack({
artist: artistToArtistSummary(artist), artist: artistToArtistSummary(artist),
album: albumToAlbumSummary(album1), album: albumToAlbumSummary(album1),
genre: pop, genre: POP,
}); });
mockGET mockGET