Skip to content

Commit 901873b

Browse files
Merge pull request #3 from recursivefunk/jra/tests
Sonos tests
2 parents cec282a + 077ce54 commit 901873b

File tree

13 files changed

+3741
-719
lines changed

13 files changed

+3741
-719
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v2
1212
- name: Set up NodeJS
13-
uses: actions/setup-node@v2
13+
uses: actions/setup-node@v4
1414
with:
15-
node-version: 22.12.0
15+
node-version: 22.16.0
1616
cache: 'npm'
1717
- name: Install dependencies
1818
run: npm install

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22.16.0

jest.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default {
2+
transform: {},
3+
moduleNameMapper: {
4+
'^(\\.{1,2}/.*)\\.js$': '$1',
5+
},
6+
testEnvironment: 'node',
7+
transformIgnorePatterns: [
8+
'node_modules/(?!good-env|@inquirer/prompts|cli-table|got|jshue|sonos|terminal-image)'
9+
]
10+
};

package-lock.json

Lines changed: 3240 additions & 574 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "groove-sensor",
33
"version": "0.0.1",
4+
"type": "module",
45
"main": "src/index.js",
56
"scripts": {
6-
"test": "echo \"Coming soon\" && exit 0",
7+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
78
"lint": "semistandard src/**/*.js",
89
"format": "semistandard --fix src/**/*.js",
910
"start": "node src/index.js"
@@ -21,6 +22,7 @@
2122
"terminal-image": "^3.1.1"
2223
},
2324
"devDependencies": {
25+
"jest": "^29.7.0",
2426
"nyc": "15.1.0",
2527
"semistandard": "*",
2628
"tape": "5.5.3"

src/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
const process = require('node:process');
2-
const { isIP } = require('net');
3-
const env = require('good-env');
4-
const Sonos = require('./lib/sonos');
5-
const Sensor = require('./lib/sensor');
6-
const { hue } = require('./lib/config');
7-
const log = require('./lib/log');
8-
const {
1+
import process from 'node:process';
2+
import { isIP } from 'net';
3+
import env from 'good-env';
4+
import Sonos from './lib/sonos.js';
5+
import Sensor from './lib/sensor.js';
6+
import { hue } from './lib/config.js';
7+
import log from './lib/log.js';
8+
import {
99
buildPlayQueue,
1010
chooseSystemNode,
1111
chooseVibe,
1212
printNowPlaying,
1313
randomTrack
14-
} = require('./lib/utils');
14+
} from './lib/utils.js';
15+
1516
const ipOk = str => isIP(str) === 4 || isIP(str) === 6;
1617

1718
// Ensure all the required environemnt variables are present

src/lib/config.js

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
1-
const env = require('good-env');
1+
import env from 'good-env';
22

3-
module.exports = {
4-
hue: {
5-
username: env.get('HUE_USERNAME'),
6-
bridgeIp: env.get('HUE_BRIDGE_IP'),
7-
sensorId: env.get('HUE_SENSOR_ID')
8-
},
9-
// @TODO: Consider just making these playlists
10-
eightiesTrackIds: [
11-
{
12-
// "Dirty Diana" - Michael Jackson
13-
uri: 'spotify:track:3yBlJtq86wROQpHi1goEKT',
14-
bestVolume: '25',
15-
start: 15
16-
},
17-
{
18-
// "Human" - The Human League
19-
uri: 'spotify:track:537yo062QIz16oQOgxmul3',
20-
bestVolume: '25'
21-
},
22-
{
23-
// "Everybody Wants to Rule The World" - Tears for Fears
24-
uri: 'spotify:track:4RvWPyQ5RL0ao9LPZeSouE',
25-
bestVolume: '25'
26-
},
27-
{
28-
// "Holding Out for a Hero" - Bonnie Tyler
29-
uri: 'spotify:track:5Hyr47BBGpvOfcykSCcaw9',
30-
bestVolume: '25'
31-
},
32-
{
33-
// "The Glow" - Willie Hutch
34-
uri: 'spotify:track:7nQ7iZ4FdjaIPEtEKvvzbc',
35-
bestVolume: '25'
36-
},
37-
{
38-
// "On Our Own" - Bobby Brown
39-
uri: 'spotify:track:0cAiOfc6uxr6NCyQ80ZigK',
40-
bestVolume: '25'
41-
},
42-
{
43-
// "Just Got Paid" - Johnny Kemp
44-
uri: 'spotify:track:1OK4hVcucqYKMU9Ipb3dtx',
45-
bestVolume: '25'
46-
},
47-
{
48-
// "Rock Steady" - The Whispers
49-
uri: 'spotify:track:4RRs6lOD5L1KbQ56rp61RM',
50-
bestVolume: '25'
51-
},
52-
{
53-
// "Purple Rain" - Prince
54-
uri: 'spotify:track:3FnP3j1TyiTwbJb5AR2ynT',
55-
bestVolume: '25'
56-
}
57-
],
58-
jazzTrackIds: [
59-
{
60-
// "Misty" - Ella Fitzgerald
61-
uri: 'spotify:track:4kF394GKEnI13QdZBM9mxM',
62-
bestVolume: 30
63-
},
64-
{
65-
// "Flamenco Sketches" - Miles Davis
66-
uri: 'spotify:track:6dRx7OUXfvosnXG3g9lWGi',
67-
bestVolume: '30',
68-
start: 20
69-
},
70-
{
71-
// "You Are Too Beautiful" - Thelonious Monk
72-
uri: 'spotify:track:1fYal59goMHv8Uq9sfNU8k',
73-
bestVolume: '30'
74-
},
75-
{
76-
// "When Sunny Gets Blue" - McCoy Tyner
77-
uri: 'spotify:track:1J9iVCaEriyoMXpj2XszhU',
78-
bestVolume: '30'
79-
},
80-
{
81-
// "Stockholm Love" - Malthilda June
82-
uri: 'spotify:track:3tNuzvJ5z4rN7GPZoWxQdk',
83-
bestVolume: '25'
84-
},
85-
{
86-
// "Thanks, But I Don't Dance" - Rene Marie
87-
uri: 'spotify:track:0yrwVkQZorFkCICB4jjU3p',
88-
bestVolume: '25',
89-
start: 16
90-
},
91-
{
92-
// "Smooth Talking" - Nocturnal Spirits
93-
uri: 'spotify:track:2pQ7tvFq7DgkQnJgU2eKia',
94-
bestVolume: '25'
95-
}
96-
]
3+
export const hue = {
4+
username: env.get('HUE_USERNAME'),
5+
bridgeIp: env.get('HUE_BRIDGE_IP'),
6+
sensorId: env.get('HUE_SENSOR_ID')
977
};
8+
9+
// @TODO: Consider just making these playlists
10+
export const eightiesTrackIds = [
11+
{
12+
// "Dirty Diana" - Michael Jackson
13+
uri: 'spotify:track:3yBlJtq86wROQpHi1goEKT',
14+
bestVolume: '25',
15+
start: 15
16+
},
17+
{
18+
// "Human" - The Human League
19+
uri: 'spotify:track:537yo062QIz16oQOgxmul3',
20+
bestVolume: '25'
21+
},
22+
{
23+
// "Everybody Wants to Rule The World" - Tears for Fears
24+
uri: 'spotify:track:4RvWPyQ5RL0ao9LPZeSouE',
25+
bestVolume: '25'
26+
},
27+
{
28+
// "Holding Out for a Hero" - Bonnie Tyler
29+
uri: 'spotify:track:5Hyr47BBGpvOfcykSCcaw9',
30+
bestVolume: '25'
31+
},
32+
{
33+
// "The Glow" - Willie Hutch
34+
uri: 'spotify:track:7nQ7iZ4FdjaIPEtEKvvzbc',
35+
bestVolume: '25'
36+
},
37+
{
38+
// "On Our Own" - Bobby Brown
39+
uri: 'spotify:track:0cAiOfc6uxr6NCyQ80ZigK',
40+
bestVolume: '25'
41+
},
42+
{
43+
// "Just Got Paid" - Johnny Kemp
44+
uri: 'spotify:track:1OK4hVcucqYKMU9Ipb3dtx',
45+
bestVolume: '25'
46+
},
47+
{
48+
// "Rock Steady" - The Whispers
49+
uri: 'spotify:track:4RRs6lOD5L1KbQ56rp61RM',
50+
bestVolume: '25'
51+
},
52+
{
53+
// "Purple Rain" - Prince
54+
uri: 'spotify:track:3FnP3j1TyiTwbJb5AR2ynT',
55+
bestVolume: '25'
56+
}
57+
];
58+
59+
export const jazzTrackIds = [
60+
{
61+
// "Misty" - Ella Fitzgerald
62+
uri: 'spotify:track:4kF394GKEnI13QdZBM9mxM',
63+
bestVolume: 30
64+
},
65+
{
66+
// "Flamenco Sketches" - Miles Davis
67+
uri: 'spotify:track:6dRx7OUXfvosnXG3g9lWGi',
68+
bestVolume: '30',
69+
start: 20
70+
},
71+
{
72+
// "You Are Too Beautiful" - Thelonious Monk
73+
uri: 'spotify:track:1fYal59goMHv8Uq9sfNU8k',
74+
bestVolume: '30'
75+
},
76+
{
77+
// "When Sunny Gets Blue" - McCoy Tyner
78+
uri: 'spotify:track:1J9iVCaEriyoMXpj2XszhU',
79+
bestVolume: '30'
80+
},
81+
{
82+
// "Stockholm Love" - Malthilda June
83+
uri: 'spotify:track:3tNuzvJ5z4rN7GPZoWxQdk',
84+
bestVolume: '25'
85+
},
86+
{
87+
// "Thanks, But I Don't Dance" - Rene Marie
88+
uri: 'spotify:track:0yrwVkQZorFkCICB4jjU3p',
89+
bestVolume: '25',
90+
start: 16
91+
},
92+
{
93+
// "Smooth Talking" - Nocturnal Spirits
94+
uri: 'spotify:track:2pQ7tvFq7DgkQnJgU2eKia',
95+
bestVolume: '25'
96+
}
97+
];

src/lib/log.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const env = require('good-env');
1+
import env from 'good-env';
2+
23
const isProd = env.get('NODE_ENV') === 'PRODUCTION';
34
const noOp = function () {};
45

5-
module.exports = {
6+
export default {
67
info: console.log,
78
debug: (isProd ? noOp : console.debug),
89
warn: console.warn,

src/lib/sensor.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
const EventEmitter = require('node:events');
2-
const env = require('good-env');
3-
const jsHue = require('jshue');
4-
const { debug } = require('./log');
1+
import EventEmitter from 'node:events';
2+
import env from 'good-env';
3+
import jsHue from 'jshue';
4+
import log from './log.js';
5+
6+
const { debug } = log;
57
const hue = jsHue();
68
const hueMotionStopBuff = env.num('HUE_MOTION_STOP_BUFFER', 90) * 1000;
79
const hueMotionPollingInterval = env.num('HUE_MOTION_POLLING_INTERVAL', 2) * 1000;
@@ -10,7 +12,7 @@ const hueMotionPollingInterval = env.num('HUE_MOTION_POLLING_INTERVAL', 2) * 100
1012
* @description Detect a Hue motion sensor, poll it for chances in presence state and emit motion start or stop
1113
* accordingly. Return an EventEmitter which will be the channel over which the client is notifed of state changes.
1214
*/
13-
class Sensor extends EventEmitter {
15+
export default class Sensor extends EventEmitter {
1416
constructor ({ bridgeIp, sensorId, username }) {
1517
super();
1618
EventEmitter.call(this);
@@ -60,4 +62,3 @@ class Sensor extends EventEmitter {
6062
return this;
6163
}
6264
}
63-
module.exports = Sensor;

src/lib/sonos.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const { Sonos, AsyncDeviceDiscovery } = require('sonos');
2-
const env = require('good-env');
3-
const log = require('./log');
4-
const { camelize } = require('./utils');
5-
const discovery = new AsyncDeviceDiscovery();
1+
import { Sonos, AsyncDeviceDiscovery } from 'sonos';
2+
import env from 'good-env';
3+
import log from './log.js';
4+
import { camelize } from './utils.js';
5+
66
const sonosDiscoveryTimeout = env.num('SONOS_DISCOVERY_TIMEOUT', 5) * 1000;
77

8-
function init () {
8+
export default function () {
99
let _nodes = {};
1010
let _targetDevice;
1111

@@ -42,6 +42,7 @@ function init () {
4242

4343
async discoverNodes () {
4444
log.info('\n---------- Hold tight, finding your Sonos devices... -------------\n');
45+
const discovery = new AsyncDeviceDiscovery();
4546
const sonosDevices = await discovery.discoverMultiple({ timeout: sonosDiscoveryTimeout });
4647
const nodesArr = await Promise.all(sonosDevices.map(async (device) => {
4748
const name = await device.getName();
@@ -106,5 +107,3 @@ function init () {
106107

107108
return _instance;
108109
}
109-
110-
module.exports = init;

0 commit comments

Comments
 (0)