|
| 1 | +/** |
| 2 | + * Unit tests for the Genius Browser component. |
| 3 | + * |
| 4 | + * Verifies: |
| 5 | + * - Animated prompt cycling (fade in/out through example prompts) |
| 6 | + * - Shift+Enter keybinding (not Cmd+Enter) |
| 7 | + * - Onboarding state transitions |
| 8 | + * - Playlist generation flow |
| 9 | + */ |
| 10 | + |
| 11 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 12 | + |
| 13 | +// Mock agent API |
| 14 | +const mockAgent = { |
| 15 | + getOnboardingState: vi.fn(), |
| 16 | + checkOllama: vi.fn(), |
| 17 | + pullModel: vi.fn(), |
| 18 | + setOnboardingComplete: vi.fn(), |
| 19 | + generatePlaylist: vi.fn(), |
| 20 | +}; |
| 21 | + |
| 22 | +vi.mock('../js/api/agent.js', () => ({ agent: mockAgent })); |
| 23 | + |
| 24 | +// Minimal Alpine mock |
| 25 | +const registeredComponents = {}; |
| 26 | +const mockAlpine = { |
| 27 | + data: vi.fn((name, factory) => { |
| 28 | + registeredComponents[name] = factory; |
| 29 | + }), |
| 30 | +}; |
| 31 | + |
| 32 | +// Import component |
| 33 | +const { createGeniusBrowser } = await import('../js/components/genius-browser.js'); |
| 34 | + |
| 35 | +function createInstance(overrides = {}) { |
| 36 | + createGeniusBrowser(mockAlpine); |
| 37 | + const factory = registeredComponents['geniusBrowser']; |
| 38 | + const instance = factory(); |
| 39 | + |
| 40 | + // Provide Alpine reactive context stubs |
| 41 | + instance.$store = { ui: { view: 'genius', toast: vi.fn() } }; |
| 42 | + instance.$watch = vi.fn(); |
| 43 | + |
| 44 | + Object.assign(instance, overrides); |
| 45 | + return instance; |
| 46 | +} |
| 47 | + |
| 48 | +describe('Genius Browser', () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.useFakeTimers(); |
| 51 | + vi.clearAllMocks(); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + vi.useRealTimers(); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('animated prompt cycling', () => { |
| 59 | + it('initializes with example prompts list', () => { |
| 60 | + const instance = createInstance(); |
| 61 | + expect(instance._promptExamples.length).toBeGreaterThanOrEqual(3); |
| 62 | + expect(instance._promptExamples[0]).toBe('make me a chill playlist from my library'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('starts cycling on init and becomes visible', () => { |
| 66 | + const instance = createInstance(); |
| 67 | + mockAgent.getOnboardingState.mockResolvedValue({ completed: true }); |
| 68 | + |
| 69 | + instance.init(); |
| 70 | + |
| 71 | + expect(instance._animatedText).toBe(instance._promptExamples[0]); |
| 72 | + expect(instance._animatedVisible).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it('fades out after hold period and advances to next prompt', () => { |
| 76 | + const instance = createInstance(); |
| 77 | + instance._startPromptCycle(); |
| 78 | + |
| 79 | + expect(instance._animatedVisible).toBe(true); |
| 80 | + expect(instance._animatedText).toBe(instance._promptExamples[0]); |
| 81 | + |
| 82 | + // After 3.9s hold, fades out |
| 83 | + vi.advanceTimersByTime(3900); |
| 84 | + expect(instance._animatedVisible).toBe(false); |
| 85 | + |
| 86 | + // After 1.3s fade-out, advances to next |
| 87 | + vi.advanceTimersByTime(1300); |
| 88 | + expect(instance._animatedText).toBe(instance._promptExamples[1]); |
| 89 | + expect(instance._animatedVisible).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('wraps around to first prompt after cycling through all', () => { |
| 93 | + const instance = createInstance(); |
| 94 | + instance._startPromptCycle(); |
| 95 | + |
| 96 | + // Capture the shuffled order |
| 97 | + const firstPrompt = instance._promptExamples[0]; |
| 98 | + const count = instance._promptExamples.length; |
| 99 | + // Advance through all prompts (3.9s hold + 1.3s fade each) |
| 100 | + for (let i = 0; i < count; i++) { |
| 101 | + vi.advanceTimersByTime(5200); |
| 102 | + } |
| 103 | + |
| 104 | + expect(instance._animatedText).toBe(firstPrompt); |
| 105 | + }); |
| 106 | + |
| 107 | + it('shuffles prompt order on start', () => { |
| 108 | + // Run multiple starts and check that at least one produces a different first element |
| 109 | + const results = new Set(); |
| 110 | + for (let i = 0; i < 20; i++) { |
| 111 | + const inst = createInstance(); |
| 112 | + inst._startPromptCycle(); |
| 113 | + results.add(inst._promptExamples[0]); |
| 114 | + inst._stopPromptCycle(); |
| 115 | + } |
| 116 | + expect(results.size).toBeGreaterThan(1); |
| 117 | + }); |
| 118 | + |
| 119 | + it('stops cycling on destroy', () => { |
| 120 | + const instance = createInstance(); |
| 121 | + instance._startPromptCycle(); |
| 122 | + instance.destroy(); |
| 123 | + expect(instance._animateTimer).toBeNull(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('keyboard shortcut', () => { |
| 128 | + it('generate() requires non-empty prompt', async () => { |
| 129 | + const instance = createInstance(); |
| 130 | + instance.onboardingComplete = true; |
| 131 | + instance.prompt = ''; |
| 132 | + |
| 133 | + await instance.generate(); |
| 134 | + |
| 135 | + expect(mockAgent.generatePlaylist).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('generate() calls agent with trimmed prompt', async () => { |
| 139 | + const instance = createInstance(); |
| 140 | + instance.onboardingComplete = true; |
| 141 | + instance.prompt = ' chill vibes '; |
| 142 | + mockAgent.generatePlaylist.mockResolvedValue({ |
| 143 | + status: 'success', |
| 144 | + playlist_name: 'Chill Vibes', |
| 145 | + playlist_id: 1, |
| 146 | + track_count: 10, |
| 147 | + }); |
| 148 | + |
| 149 | + // Stub dispatchEvent for playlist-updated event |
| 150 | + globalThis.window = { dispatchEvent: vi.fn() }; |
| 151 | + globalThis.CustomEvent = class CustomEvent { |
| 152 | + constructor(type, opts) { |
| 153 | + this.type = type; |
| 154 | + this.detail = opts?.detail; |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + await instance.generate(); |
| 159 | + |
| 160 | + expect(mockAgent.generatePlaylist).toHaveBeenCalledWith('chill vibes'); |
| 161 | + expect(instance.result.status).toBe('success'); |
| 162 | + expect(instance.history).toHaveLength(1); |
| 163 | + expect(instance.prompt).toBe(''); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('onboarding', () => { |
| 168 | + it('skips onboarding when already completed', async () => { |
| 169 | + const instance = createInstance(); |
| 170 | + mockAgent.getOnboardingState.mockResolvedValue({ completed: true }); |
| 171 | + |
| 172 | + await instance._checkOnboarding(); |
| 173 | + |
| 174 | + expect(instance.onboardingComplete).toBe(true); |
| 175 | + expect(instance.onboardingStep).toBe('ready'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('shows install-ollama when not connected', async () => { |
| 179 | + const instance = createInstance(); |
| 180 | + mockAgent.getOnboardingState.mockResolvedValue({ completed: false }); |
| 181 | + mockAgent.checkOllama.mockResolvedValue({ connected: false, models: [] }); |
| 182 | + |
| 183 | + await instance._checkOnboarding(); |
| 184 | + |
| 185 | + expect(instance.onboardingStep).toBe('install-ollama'); |
| 186 | + }); |
| 187 | + |
| 188 | + it('shows download-model when connected but model missing', async () => { |
| 189 | + const instance = createInstance(); |
| 190 | + mockAgent.getOnboardingState.mockResolvedValue({ completed: false }); |
| 191 | + mockAgent.checkOllama.mockResolvedValue({ connected: true, models: [] }); |
| 192 | + |
| 193 | + await instance._checkOnboarding(); |
| 194 | + |
| 195 | + expect(instance.onboardingStep).toBe('download-model'); |
| 196 | + }); |
| 197 | + |
| 198 | + it('completes onboarding when model is present', async () => { |
| 199 | + const instance = createInstance(); |
| 200 | + mockAgent.getOnboardingState.mockResolvedValue({ completed: false }); |
| 201 | + mockAgent.checkOllama.mockResolvedValue({ connected: true, models: ['qwen3.5:9b'] }); |
| 202 | + mockAgent.setOnboardingComplete.mockResolvedValue(); |
| 203 | + |
| 204 | + await instance._checkOnboarding(); |
| 205 | + |
| 206 | + expect(instance.onboardingComplete).toBe(true); |
| 207 | + expect(instance.onboardingStep).toBe('ready'); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments