/ clis / spotify / utils.test.js
utils.test.js
 1  import { describe, expect, it } from 'vitest';
 2  import { assertSpotifyCredentialsConfigured, getFirstSpotifyTrack, hasConfiguredSpotifyCredentials, mapSpotifyTrackResults, parseDotEnv, resolveSpotifyCredentials, } from './utils.js';
 3  describe('spotify utils', () => {
 4      it('parses dotenv-style credential files', () => {
 5          const env = parseDotEnv(`
 6        # Spotify credentials
 7        SPOTIFY_CLIENT_ID=abc123
 8        SPOTIFY_CLIENT_SECRET=def456
 9      `);
10          expect(env).toEqual({
11              SPOTIFY_CLIENT_ID: 'abc123',
12              SPOTIFY_CLIENT_SECRET: 'def456',
13          });
14      });
15      it('prefers explicit process env over file values', () => {
16          const credentials = resolveSpotifyCredentials({
17              SPOTIFY_CLIENT_ID: 'file-id',
18              SPOTIFY_CLIENT_SECRET: 'file-secret',
19          }, {
20              SPOTIFY_CLIENT_ID: 'env-id',
21              SPOTIFY_CLIENT_SECRET: 'env-secret',
22          });
23          expect(credentials).toEqual({
24              clientId: 'env-id',
25              clientSecret: 'env-secret',
26          });
27      });
28      it('treats placeholder values as unconfigured credentials', () => {
29          expect(hasConfiguredSpotifyCredentials({
30              clientId: 'your_spotify_client_id_here',
31              clientSecret: 'your_spotify_client_secret_here',
32          })).toBe(false);
33      });
34      it('throws a helpful CONFIG error for empty or placeholder credentials', () => {
35          expect(() => assertSpotifyCredentialsConfigured({
36              clientId: '',
37              clientSecret: '',
38          }, '/tmp/spotify.env')).toThrow(/Missing Spotify credentials/);
39          expect(() => assertSpotifyCredentialsConfigured({
40              clientId: 'your_spotify_client_id_here',
41              clientSecret: 'real-secret',
42          }, '/tmp/spotify.env')).toThrow(/Fill in SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET/);
43      });
44      it('maps search payloads into stable track summaries', () => {
45          const results = mapSpotifyTrackResults({
46              tracks: {
47                  items: [
48                      {
49                          name: 'Numb',
50                          artists: [{ name: 'Linkin Park' }, { name: 'Jay-Z' }],
51                          album: { name: 'Encore' },
52                          uri: 'spotify:track:123',
53                      },
54                  ],
55              },
56          });
57          expect(results).toEqual([
58              {
59                  track: 'Numb',
60                  artist: 'Linkin Park, Jay-Z',
61                  album: 'Encore',
62                  uri: 'spotify:track:123',
63              },
64          ]);
65          expect(getFirstSpotifyTrack({ tracks: { items: [] } })).toBeNull();
66      });
67  });