import { expect } from "e2e";
import { writeFile } from "node:fs/promises";
import { T3_HOME } from "../e2e.config.ts";
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";
test("Concurrent worktrees receive available semantic branch names", WEB, async ({t3, app, screen, browser}) => {
 // Replay turns need a deterministic text response too; keep branch availability and renaming real.
 await writeFile(`${T3_HOME}/bin/no-model`, `#!/usr/bin/env node\nimport fs from 'node:fs'; const a=process.argv.slice(2); if(a.includes('--output-last-message')) { fs.writeFileSync(a[a.indexOf('--output-last-message')+1], JSON.stringify({branch:'feat/filter-items-list',title:'Filter items list'})); } else if(a.includes('--version')) console.log('codex-cli 0.1.0'); else console.log('[]');\n`, {mode:0o755});
 const project = await t3.project("search-app", {"items.ts": 'export const items = ["Apple", "Banana", "Cherry"];\n'});
 await t3.open("/settings/worktrees");
 await screen.getByRole("combobox", "Worktree branch naming").tap();
 await screen.getByRole("option", "Semantic prefix").tap();
 await t3.open();
 await screen.getByRole("button", "Showcase", {exact:false}).tap();
 await screen.getByRole("option", "Multi turn", {exact:false}).waitFor();
 await browser.evaluate(() => { const e=[...document.querySelectorAll('[role="option"]')].find(e=>e.textContent?.includes('Multi turn')); if(!e) throw Error('Multi turn option absent'); e.dispatchEvent(new MouseEvent('click',{bubbles:true,shiftKey:true})); });
 await browser.evaluate(() => { const e=[...document.querySelectorAll('[role="option"]')].find(e=>e.textContent?.includes('Plan and reads')); if(!e) throw Error('Plan option absent'); e.dispatchEvent(new MouseEvent('click',{bubbles:true,shiftKey:true})); });
 await browser.keyboard.press("Escape");
 await screen.getByRole("textbox").fill("Add a search box that filters the items list");
 await app.screenshot("ready-to-launch");
 await screen.getByRole("textbox").press("Enter");
 const git = async (...args:string[]) => (await runPromise(run("git",args,{cwd:project}))).stdout;
 try {
 await expect.poll(async () => (await git('branch','--list','feat/*')).trim().split('\n').filter(Boolean).length, {timeout:30000}).toBe(3);
 } finally {
 await screen.getByText('Done', {exact:true}).nth(2).waitFor({timeout:60000});
 await browser.evaluate(async () => { await Promise.all(document.getAnimations().filter(a => a.effect?.getTiming().iterations !== Infinity).map(a => a.finished.catch(() => {}))); });
 await app.screenshot('branches-after-preparation');
 }
 await expect.poll(()=>git('branch','--list','t3/*')).toBe('');
});
