Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/spotify

scripts/check.mjs

62 lines · 2.5 KB · JavaScript
import assert from 'node:assert/strict'
import {
	connectOauthUrl,
	resolveSpotifyIntegration,
	SPOTIFY_FULL_CONNECT_URL,
	SPOTIFY_READ_CONNECT_URL,
} from '../src/lib/accounts.ts'
import { buildDeviceMatchError, matchDevice } from '../src/lib/device-target.ts'
import {
	dryRunPreview,
	isDryRun,
	normalizeSpotifyPlaylistUri,
	normalizeSpotifyTrackUri,
	normalizeTrackUris,
} from '../src/lib/normalize.ts'

assert.equal(resolveSpotifyIntegration({}), 'spotify')
assert.equal(resolveSpotifyIntegration({ integration: 'spotify-work' }), 'spotify-work')
assert.equal(resolveSpotifyIntegration({ integrationName: 'spotify-kids' }), 'spotify-kids')
assert.equal(resolveSpotifyIntegration({ account: 'spotify-home' }), 'spotify-home')

for (const alias of ['personal', 'family', 'kent']) {
	assert.throws(() => resolveSpotifyIntegration({ account: alias }), /removed from @kody\/spotify/)
}
assert.throws(() => resolveSpotifyIntegration({ integration: 'google' }), /must be "spotify"/)

assert.match(SPOTIFY_READ_CONNECT_URL, /^https:\/\/kody\.codes\/connect\/oauth\?/)
assert.match(SPOTIFY_READ_CONNECT_URL, /provider=spotify/)
assert.match(SPOTIFY_READ_CONNECT_URL, /authorizeUrl=https%3A%2F%2Faccounts\.spotify\.com%2Fauthorize/)
assert.match(SPOTIFY_READ_CONNECT_URL, /tokenUrl=https%3A%2F%2Faccounts\.spotify\.com%2Fapi%2Ftoken/)
assert.match(SPOTIFY_READ_CONNECT_URL, /allowedHosts=api\.spotify\.com/)
assert.match(SPOTIFY_FULL_CONNECT_URL, /user-modify-playback-state/)
assert.match(connectOauthUrl('spotify-work'), /provider=spotify-work/)

const devices = [
	{ id: 'aaa', name: 'Kitchen Homepod', type: 'Speaker', is_active: false },
	{ id: 'bbb', name: 'Living Room', type: 'Speaker', is_active: true },
]
assert.equal(matchDevice(devices, { deviceName: 'living' })?.id, 'bbb')
assert.equal(matchDevice(devices, { deviceName: 'missing' }), null)
assert.match(buildDeviceMatchError(devices, { deviceName: 'Office' }), /deviceName "Office"/)

assert.equal(
	normalizeSpotifyTrackUri('https://open.spotify.com/track/4cOdK2wGLETKBW3PvgPWqT'),
	'spotify:track:4cOdK2wGLETKBW3PvgPWqT',
)
assert.equal(
	normalizeSpotifyPlaylistUri('37i9dQZF1DX0XUsuxWHRQd'),
	'spotify:playlist:37i9dQZF1DX0XUsuxWHRQd',
)
assert.deepEqual(normalizeTrackUris(['4cOdK2wGLETKBW3PvgPWqT']), [
	'spotify:track:4cOdK2wGLETKBW3PvgPWqT',
])
assert.equal(isDryRun({ dryRun: true }), true)
assert.equal(isDryRun({}), false)
assert.deepEqual(dryRunPreview({ action: 'queued', uri: 'spotify:track:x' }), {
	dryRun: true,
	action: 'queued',
	uri: 'spotify:track:x',
})

console.log('ok')