1+ import { jest } from '@jest/globals' ;
2+ import {
3+ buildPlayQueue ,
4+ printNowPlaying ,
5+ randomTrack ,
6+ camelize
7+ } from '../../src/lib/utils.js' ;
8+
9+ // Mock external dependencies
10+ jest . mock ( 'terminal-image' ) ;
11+ jest . mock ( 'cli-table' ) ;
12+ jest . mock ( 'got' ) ;
13+
14+ describe ( 'Utility Functions' , ( ) => {
15+ beforeEach ( ( ) => {
16+ jest . clearAllMocks ( ) ;
17+ } ) ;
18+
19+ describe ( 'buildPlayQueue' , ( ) => {
20+ it ( 'should filter out the current track from the queue' , ( ) => {
21+ const tracks = [
22+ { uri : 'spotify:track:1' , title : 'Track 1' } ,
23+ { uri : 'spotify:track:2' , title : 'Track 2' } ,
24+ { uri : 'spotify:track:3' , title : 'Track 3' }
25+ ] ;
26+ const currentTrack = { uri : 'spotify:track:2' , title : 'Track 2' } ;
27+
28+ const result = buildPlayQueue ( { tracks, currentTrack } ) ;
29+
30+ expect ( result ) . toHaveLength ( 2 ) ;
31+ expect ( result ) . not . toContainEqual ( currentTrack ) ;
32+ } ) ;
33+
34+ it ( 'should return all tracks if current track is not in the list' , ( ) => {
35+ const tracks = [
36+ { uri : 'spotify:track:1' , title : 'Track 1' } ,
37+ { uri : 'spotify:track:2' , title : 'Track 2' }
38+ ] ;
39+ const currentTrack = { uri : 'spotify:track:3' , title : 'Track 3' } ;
40+
41+ const result = buildPlayQueue ( { tracks, currentTrack } ) ;
42+
43+ expect ( result ) . toEqual ( tracks ) ;
44+ } ) ;
45+
46+ it ( 'should return all tracks if remaining tracks is empty' , ( ) => {
47+ const tracks = [ { uri : 'spotify:track:1' , title : 'Track 1' } ] ;
48+ const currentTrack = { uri : 'spotify:track:1' , title : 'Track 1' } ;
49+
50+ const result = buildPlayQueue ( { tracks, currentTrack } ) ;
51+
52+ expect ( result ) . toEqual ( tracks ) ;
53+ } ) ;
54+ } ) ;
55+
56+ describe ( 'chooseSystemNode' , ( ) => {
57+ it ( 'should return the selected node' , async ( ) => {
58+ const mockNode = { name : 'Test Node' } ;
59+ const mockSystem = {
60+ discoverNodes : jest . fn ( ) . mockResolvedValue ( [ mockNode ] )
61+ } ;
62+
63+ await jest . resetModules ( ) ;
64+ // ESM-compliant mocking
65+ await jest . unstable_mockModule ( '@inquirer/prompts' , ( ) => ( {
66+ select : jest . fn ( ) . mockResolvedValue ( mockNode )
67+ } ) ) ;
68+
69+ // Dynamically import after the mock is set up
70+ const { chooseSystemNode } = await import ( '../../src/lib/utils.js' ) ;
71+ const result = await chooseSystemNode ( mockSystem ) ;
72+
73+ expect ( result ) . toEqual ( mockNode ) ;
74+ } ) ;
75+ } ) ;
76+
77+ describe ( 'chooseVibe' , ( ) => {
78+ it ( 'should return the selected vibe' , async ( ) => {
79+ const mockVibe = [ 'track1' , 'track2' ] ;
80+ await jest . resetModules ( ) ;
81+ // ESM-compliant mocking
82+ await jest . unstable_mockModule ( '@inquirer/prompts' , ( ) => ( {
83+ select : jest . fn ( ) . mockResolvedValue ( mockVibe )
84+ } ) ) ;
85+
86+ // Dynamically import after the mock is set up
87+ const { chooseVibe } = await import ( '../../src/lib/utils.js' ) ;
88+ const result = await chooseVibe ( ) ;
89+
90+ expect ( result ) . toBe ( mockVibe ) ;
91+ // Optionally, check the prompt message and choices if needed
92+ } ) ;
93+ } ) ;
94+
95+ describe ( 'randomTrack' , ( ) => {
96+ it ( 'should return a random track from the array' , ( ) => {
97+ const items = [ 'track1' , 'track2' , 'track3' ] ;
98+ const result = randomTrack ( items ) ;
99+ expect ( items ) . toContain ( result ) ;
100+ } ) ;
101+
102+ it ( 'should return undefined for empty array' , ( ) => {
103+ const result = randomTrack ( [ ] ) ;
104+ expect ( result ) . toBeUndefined ( ) ;
105+ } ) ;
106+ } ) ;
107+
108+ describe ( 'camelize' , ( ) => {
109+ it ( 'should convert string to camelCase' , ( ) => {
110+ expect ( camelize ( 'hello world' ) ) . toBe ( 'helloWorld' ) ;
111+ expect ( camelize ( 'Hello World' ) ) . toBe ( 'helloWorld' ) ;
112+ expect ( camelize ( 'hello-world' ) ) . toBe ( 'helloWorld' ) ;
113+ } ) ;
114+
115+ it ( 'should handle special characters' , ( ) => {
116+ expect ( camelize ( "don't stop" ) ) . toBe ( 'dontStop' ) ;
117+ expect ( camelize ( "it's working" ) ) . toBe ( 'itsWorking' ) ;
118+ } ) ;
119+
120+ it ( 'should handle already camelCase strings' , ( ) => {
121+ expect ( camelize ( 'helloWorld' ) ) . toBe ( 'helloWorld' ) ;
122+ } ) ;
123+ } ) ;
124+ } ) ;
0 commit comments