- Read the track from the Media Session API, falling back to the legacy ytmusic-player-bar scrape. Current YT Music layouts render no player bar, so the API and tray stayed empty while music played. Artist is also clean now (no "• views • likes" suffix). - Terminate connected WebSocket clients before closing the server. ws's close() waits for every client to disconnect, so quitting with an overlay open hung forever, leaving the window playing and the port bound. - Replace the tray icon with a valid PNG (the old one had a bad IDAT CRC and decoded to an empty image) and test that its chunks are well formed. - Create the tray before the window so early state updates are not dropped. - Bump version to 1.0.1. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
61 lines
1.9 KiB
JavaScript
61 lines
1.9 KiB
JavaScript
const { test } = require('node:test');
|
|
const assert = require('node:assert');
|
|
const { scrapeNowPlaying } = require('../now-playing');
|
|
|
|
const emptyDoc = { querySelector: () => null };
|
|
|
|
function nav(metadata, playbackState) {
|
|
return { mediaSession: { metadata, playbackState } };
|
|
}
|
|
|
|
test('reads track info from the Media Session when the player bar is absent', () => {
|
|
const result = scrapeNowPlaying(
|
|
emptyDoc,
|
|
nav(
|
|
{
|
|
title: 'DOMINO',
|
|
artist: 'Stray Kids',
|
|
album: '',
|
|
artwork: [
|
|
{ src: 'small.jpg', sizes: '60x60' },
|
|
{ src: 'big.jpg', sizes: '544x544' },
|
|
],
|
|
},
|
|
'playing'
|
|
)
|
|
);
|
|
assert.deepStrictEqual(result, {
|
|
title: 'DOMINO',
|
|
artist: 'Stray Kids',
|
|
albumArt: 'big.jpg',
|
|
isPlaying: true,
|
|
});
|
|
});
|
|
|
|
test('reports isPlaying false when the Media Session is paused', () => {
|
|
const result = scrapeNowPlaying(emptyDoc, nav({ title: 'T', artist: 'A', artwork: [] }, 'paused'));
|
|
assert.strictEqual(result.isPlaying, false);
|
|
assert.strictEqual(result.albumArt, null);
|
|
});
|
|
|
|
test('returns null when nothing is loaded anywhere', () => {
|
|
assert.strictEqual(scrapeNowPlaying(emptyDoc, nav(null, 'none')), null);
|
|
assert.strictEqual(scrapeNowPlaying(emptyDoc, {}), null);
|
|
});
|
|
|
|
test('falls back to the legacy player-bar DOM when Media Session has no metadata', () => {
|
|
const els = {
|
|
'ytmusic-player-bar .title': { textContent: ' Old Song ' },
|
|
'ytmusic-player-bar .byline': { textContent: 'Old Artist' },
|
|
'ytmusic-player-bar img.image': { src: 'art.jpg' },
|
|
'#play-pause-button': { getAttribute: (n) => (n === 'title' ? 'Pause' : null) },
|
|
};
|
|
const doc = { querySelector: (s) => els[s] || null };
|
|
assert.deepStrictEqual(scrapeNowPlaying(doc, nav(null, 'none')), {
|
|
title: 'Old Song',
|
|
artist: 'Old Artist',
|
|
albumArt: 'art.jpg',
|
|
isPlaying: true,
|
|
});
|
|
});
|