const {chromium} = await import(process.env.PLAYWRIGHT_MODULE || 'playwright');
import assert from 'node:assert/strict';
const browser=await chromium.launch({...(process.env.CHROME_PATH?{executablePath:process.env.CHROME_PATH}:{}),headless:true});
try {
const context=await browser.newContext({viewport:{width:1280,height:800}});
const external=[];
await context.route(/https:\/\/(music\.youtube\.com|www\.youtube\.com|open\.spotify\.com|waze\.com|media\.cunatbrule\.fr)\//,r=>{external.push(r.request().url());return r.fulfill({contentType:'text/html',body:'
Service de test'});});
const page=await context.newPage(),errors=[];page.on('pageerror',e=>errors.push(e.message));
const base=process.env.CARIA_URL||'http://127.0.0.1:8080';
await page.goto(base+'/#apps');
await page.locator('#applications-grid .application-card').first().waitFor();
assert.equal(await page.locator('#favorite-apps a').count(),5);
assert.equal(await page.locator('#applications-grid .application-card').count(),9);
assert.deepEqual(external,[]);
await page.locator('#applications-customize').click();
await page.locator('[data-favorite-slot="0"]').selectOption('youtube-music');
await page.locator('[data-favorite-slot="1"]').selectOption('youtube-music');
assert.equal(await page.locator('[data-favorite-slot="0"]').inputValue(),'drive');
await page.getByLabel('Afficher Spotify',{exact:true}).uncheck();
await page.getByLabel('Ouverture de YouTube',{exact:true}).selectOption('same');
await page.locator('#application-settings-close').click();
await page.reload();
assert.equal(await page.locator('#favorite-apps a').nth(1).getAttribute('data-application'),'youtube-music');
assert.equal(await page.locator('#applications-grid [data-application="spotify"]').count(),0);
assert.equal(await page.locator('#applications-grid [data-application="youtube"]').getAttribute('target'),'_self');
const popupEvent=page.waitForEvent('popup');
await page.locator('#applications-grid [data-application="youtube-music"]').click();
const popup=await popupEvent;await popup.waitForLoadState();
assert.equal(popup.url(),'https://music.youtube.com/');
assert.equal(page.url(),base+'/#apps');await popup.close();
await page.locator('#applications-customize').click();await page.locator('#applications-reset').click();await page.locator('#application-settings-close').click();
await page.locator('#favorite-apps [data-view="music"]').click();
const wav=Buffer.alloc(44+16000*60);wav.write('RIFF');wav.writeUInt32LE(wav.length-8,4);wav.write('WAVE',8);wav.write('fmt ',12);wav.writeUInt32LE(16,16);wav.writeUInt16LE(1,20);wav.writeUInt16LE(1,22);wav.writeUInt32LE(8000,24);wav.writeUInt32LE(16000,28);wav.writeUInt16LE(2,32);wav.writeUInt16LE(16,34);wav.write('data',36);wav.writeUInt32LE(wav.length-44,40);
await page.locator('#music-files').setInputFiles({name:'Test.wav',mimeType:'audio/wav',buffer:wav});await page.locator('#play').click();
await page.waitForFunction(()=>!document.querySelector('#audio').paused);
await page.locator('#favorite-apps [data-view="apps"]').click();await page.locator('#applications-customize').click();
await page.getByLabel('Afficher Musique locale',{exact:true}).uncheck();await page.locator('#application-settings-close').click();
assert.equal(await page.evaluate(()=>document.querySelector('#audio').paused),false);
assert.equal(await page.locator('#favorite-apps [data-application="music"]').count(),0);
assert.equal(await page.locator('#applications-grid [data-application="music"]').count(),0);
await page.locator('#applications-customize').click();
const names=['Accueil','Conduite','Navigation','Musique locale','Jellyfin','Spotify','YouTube','YouTube Music','Waze'];
for (const name of names) await page.getByLabel(`Afficher ${name}`,{exact:true}).uncheck();
await page.locator('#application-settings-close').click();assert.equal(await page.locator('#favorite-apps a').count(),1);await page.locator('#applications-empty').waitFor({state:'visible'});
await page.locator('#applications-customize').click();await page.locator('#applications-reset').click();await page.locator('#application-settings-close').click();
for (const size of [{width:1280,height:800},{width:1024,height:600},{width:800,height:480},{width:390,height:844}]) {await page.setViewportSize(size);assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);}
await page.setViewportSize({width:1280,height:800});
if(process.env.QA_SCREENSHOT) await page.screenshot({path:process.env.QA_SCREENSHOT,fullPage:true});
assert.deepEqual(errors,[]);console.log('PASS catalog; favorites swap; hide/restore; persistence; opening modes; YouTube Music popup; no preloading; audio survives hiding; empty catalog; responsive layouts.');
} finally {await browser.close();}