|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForAlpine } from './fixtures/helpers.js'; |
| 3 | + |
| 4 | +test.describe('Audio Settings UI', () => { |
| 5 | + test.beforeEach(async ({ page }) => { |
| 6 | + await page.goto('/'); |
| 7 | + await waitForAlpine(page); |
| 8 | + |
| 9 | + await page.click('[data-testid="sidebar-settings"]'); |
| 10 | + await page.waitForSelector('[data-testid="settings-nav-audio"]', { |
| 11 | + state: 'visible', |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + test('should display Audio nav item in settings sidebar', async ({ page }) => { |
| 16 | + const audioNav = page.locator('[data-testid="settings-nav-audio"]'); |
| 17 | + await expect(audioNav).toBeVisible(); |
| 18 | + await expect(audioNav).toHaveText('Audio'); |
| 19 | + }); |
| 20 | + |
| 21 | + test('should navigate to Audio section when clicked', async ({ page }) => { |
| 22 | + await page.click('[data-testid="settings-nav-audio"]'); |
| 23 | + const audioSection = page.locator( |
| 24 | + '[data-testid="settings-section-audio"]', |
| 25 | + ); |
| 26 | + await expect(audioSection).toBeVisible(); |
| 27 | + }); |
| 28 | + |
| 29 | + test('should display device selector with Default option', async ({ page }) => { |
| 30 | + await page.click('[data-testid="settings-nav-audio"]'); |
| 31 | + |
| 32 | + const select = page.locator('[data-testid="audio-device-select"]'); |
| 33 | + await expect(select).toBeVisible(); |
| 34 | + |
| 35 | + const defaultOption = select.locator('option[value="default"]'); |
| 36 | + await expect(defaultOption).toHaveText('Default'); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +test.describe('Audio Settings with Mocked Tauri', () => { |
| 41 | + test.beforeEach(async ({ page }) => { |
| 42 | + await page.addInitScript(() => { |
| 43 | + const mockDevices = ['Built-in Output', 'External DAC']; |
| 44 | + let selectedDevice = 'default'; |
| 45 | + window.__tauriInvocations = []; |
| 46 | + |
| 47 | + window.__TAURI__ = { |
| 48 | + core: { |
| 49 | + invoke: (cmd, args) => { |
| 50 | + window.__tauriInvocations.push({ cmd, args }); |
| 51 | + if (cmd === 'audio_list_devices') { |
| 52 | + return Promise.resolve({ devices: mockDevices }); |
| 53 | + } |
| 54 | + if (cmd === 'audio_set_device') { |
| 55 | + selectedDevice = args?.deviceName || 'default'; |
| 56 | + return Promise.resolve(null); |
| 57 | + } |
| 58 | + if (cmd === 'app_get_info') { |
| 59 | + return Promise.resolve({ version: 'test', build: 'test', platform: 'test' }); |
| 60 | + } |
| 61 | + if (cmd === 'watched_folders_list') { |
| 62 | + return Promise.resolve([]); |
| 63 | + } |
| 64 | + if (cmd === 'lastfm_get_settings') { |
| 65 | + return Promise.resolve({ |
| 66 | + enabled: false, |
| 67 | + authenticated: false, |
| 68 | + scrobble_threshold: 90, |
| 69 | + }); |
| 70 | + } |
| 71 | + if (cmd === 'settings_get') { |
| 72 | + if (args?.key === 'audio_output_device') { |
| 73 | + return Promise.resolve({ key: 'audio_output_device', value: selectedDevice }); |
| 74 | + } |
| 75 | + return Promise.resolve({ key: args?.key, value: null }); |
| 76 | + } |
| 77 | + if (cmd === 'settings_set') { |
| 78 | + return Promise.resolve({ key: args?.key, value: args?.value }); |
| 79 | + } |
| 80 | + return Promise.resolve(null); |
| 81 | + }, |
| 82 | + }, |
| 83 | + event: { |
| 84 | + listen: () => Promise.resolve(() => {}), |
| 85 | + }, |
| 86 | + dialog: { |
| 87 | + confirm: () => Promise.resolve(true), |
| 88 | + }, |
| 89 | + }; |
| 90 | + }); |
| 91 | + |
| 92 | + await page.goto('/'); |
| 93 | + await waitForAlpine(page); |
| 94 | + |
| 95 | + await page.click('[data-testid="sidebar-settings"]'); |
| 96 | + await page.waitForSelector('[data-testid="settings-nav-audio"]', { |
| 97 | + state: 'visible', |
| 98 | + }); |
| 99 | + await page.click('[data-testid="settings-nav-audio"]'); |
| 100 | + }); |
| 101 | + |
| 102 | + test('should list mocked audio devices in dropdown', async ({ page }) => { |
| 103 | + const select = page.locator('[data-testid="audio-device-select"]'); |
| 104 | + await expect(select).toBeVisible(); |
| 105 | + |
| 106 | + const options = select.locator('option'); |
| 107 | + // Default + 2 mocked devices = 3 options |
| 108 | + await expect(options).toHaveCount(3); |
| 109 | + |
| 110 | + await expect(options.nth(0)).toHaveText('Default'); |
| 111 | + await expect(options.nth(1)).toHaveText('Built-in Output'); |
| 112 | + await expect(options.nth(2)).toHaveText('External DAC'); |
| 113 | + }); |
| 114 | + |
| 115 | + test('should call audio_set_device when device is selected', async ({ page }) => { |
| 116 | + // Clear prior invocations from init |
| 117 | + await page.evaluate(() => { |
| 118 | + window.__tauriInvocations = []; |
| 119 | + }); |
| 120 | + |
| 121 | + const select = page.locator('[data-testid="audio-device-select"]'); |
| 122 | + await select.selectOption('External DAC'); |
| 123 | + |
| 124 | + await page.waitForFunction( |
| 125 | + () => |
| 126 | + window.__tauriInvocations.some( |
| 127 | + (inv) => inv.cmd === 'audio_set_device', |
| 128 | + ), |
| 129 | + { timeout: 5000 }, |
| 130 | + ); |
| 131 | + |
| 132 | + const setDeviceCall = await page.evaluate(() => |
| 133 | + window.__tauriInvocations.find((inv) => inv.cmd === 'audio_set_device') |
| 134 | + ); |
| 135 | + expect(setDeviceCall).toBeDefined(); |
| 136 | + expect(setDeviceCall.args.deviceName).toBe('External DAC'); |
| 137 | + }); |
| 138 | + |
| 139 | + test('should send null deviceName when Default is selected', async ({ page }) => { |
| 140 | + // First select a non-default device |
| 141 | + const select = page.locator('[data-testid="audio-device-select"]'); |
| 142 | + await select.selectOption('Built-in Output'); |
| 143 | + |
| 144 | + // Clear invocations and select default |
| 145 | + await page.evaluate(() => { |
| 146 | + window.__tauriInvocations = []; |
| 147 | + }); |
| 148 | + |
| 149 | + await select.selectOption('default'); |
| 150 | + |
| 151 | + await page.waitForFunction( |
| 152 | + () => |
| 153 | + window.__tauriInvocations.some( |
| 154 | + (inv) => inv.cmd === 'audio_set_device', |
| 155 | + ), |
| 156 | + { timeout: 5000 }, |
| 157 | + ); |
| 158 | + |
| 159 | + const setDeviceCall = await page.evaluate(() => |
| 160 | + window.__tauriInvocations.find((inv) => inv.cmd === 'audio_set_device') |
| 161 | + ); |
| 162 | + expect(setDeviceCall).toBeDefined(); |
| 163 | + expect(setDeviceCall.args.deviceName).toBeNull(); |
| 164 | + }); |
| 165 | +}); |
0 commit comments