/// <reference lib="dom" />
import * as NodeHttp from "node:http";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

const PAGE = `<!doctype html><title>Duplicate probe</title><style>
:root {color-scheme:light dark} body{font:18px system-ui;padding:24px;margin:0} h1{font-size:24px}
</style><h1>Browser probe</h1><p id="scheme"></p><p id="ratio"></p><script>
function update(){document.querySelector('#scheme').textContent='Color scheme: '+(matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light');document.querySelector('#ratio').textContent='Pixel ratio: '+devicePixelRatio} update();setInterval(update,100);
</script>`;

test("Duplicated Browser keeps 150% zoom and Dark appearance", WEB, async ({t3,app,browser,screen}) => {
 const server=NodeHttp.createServer((_req,res)=>{res.writeHead(200,{'content-type':'text/html'});res.end(PAGE)});
 await new Promise<void>(resolve=>server.listen(0,'127.0.0.1',resolve));
 try {
 const address=server.address();if(!address||typeof address==='string')throw new Error('No port');
 const project=await t3.project('browser-demo');
 const thread=await t3.thread({project,title:'Browser duplication',messages:['Start']});
 await t3.open(thread);
 await screen.getByRole('button','Toggle right panel').tap();
 await screen.getByRole('button','Browser B').tap();
 await screen.getByRole('textbox','Search or enter URL').fill(`http://127.0.0.1:${address.port}/`);
 await browser.keyboard.press('Enter');
 await screen.getByText('Duplicate probe',{exact:true}).waitFor();
 await screen.getByRole('button','Preview menu').tap();
 for(let i=0;i<3;i++)await screen.getByRole('button','Zoom in',{exact:true}).tap();
 await screen.getByRole('menuitem','Appearance',{exact:true}).tap();
 await screen.getByRole('menuitemradio','Dark',{exact:true}).tap();
 await browser.keyboard.press('Escape');
 await browser.keyboard.press('Escape');
 await screen.getByRole('button','Preview menu').tap();
 await screen.getByRole('button','Zoom in',{exact:true}).waitFor();
 await app.screenshot('source-150-dark');
 await browser.keyboard.press('Escape');
 await browser.evaluate(()=>{const tab=document.querySelector('[data-active-tab="true"]');if(!tab)throw new Error('No active tab');tab.dispatchEvent(new MouseEvent('contextmenu',{bubbles:true,clientX:1000,clientY:40}));});
 await screen.getByRole('menuitem','Duplicate',{exact:true}).tap();
 await screen.getByRole('button','Preview menu').tap();
 await screen.getByRole('button','Zoom in',{exact:true}).waitFor();
 await app.screenshot('duplicate-zoom');
 await screen.getByRole('menuitem','Appearance',{exact:true}).tap();
 await screen.getByRole('menuitemradio','System',{exact:true}).waitFor();
 await app.screenshot('duplicate-appearance');
 const dark=await browser.evaluate(()=>document.querySelector('[role="menuitemradio"][aria-checked="true"]')?.textContent?.trim());
 await expect(dark).toBe('Dark');
 await browser.keyboard.press('Escape');
 await expect(screen.getByText('150%',{exact:true})).toBeVisible();
 } finally {await new Promise(resolve=>server.close(resolve));}
});
