import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { run } from "./shell.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("Thread Details selects a base without checking out the previous worktree", WEB, async ({t3,app,browser,screen}) => {
 const project = await t3.project("demo");
 const git = (...args:string[]) => runPromise(run("git",args,{cwd:project}));
 await git("branch","feature");
 await git("clone","--bare",project,project+"-origin.git");
 await git("remote","add","origin",project+"-origin.git");
 const started = await runPromise(makeState(T3_HOME).cli(["thread","start",project,"Showcase","--worktree","main","--branch","t3/go","--provider","devReplay","--model","showcase","--wait","--json"]));
 const worktrees = (await git("worktree","list","--porcelain")).stdout;
 const previous = worktrees.split("\n\n").find(x=>x.includes("branch refs/heads/t3/go"))!.split("\n")[0]!.slice(9);
 await browser.setViewport({width:1440,height:1000});
 await t3.open(JSON.parse(started.stdout));
 await screen.getByRole("heading","Done",{exact:true}).waitFor();
 await screen.getByRole("button", "New thread", {exact:true}).first().click();
 await screen.getByRole("textbox","Message Showcase").waitFor({state:"hidden"});
 await browser.locator('[data-composer-shortcut~="composer.workspace"]:visible').first().click();
 await screen.getByRole("option", /^Previous worktree/).click();
 await app.screenshot("previous-worktree-draft");
 await screen.getByRole("button", "Showcase", {exact:true}).first().click();
 await browser.keyboard.type("Plan and reads");
 await browser.keyboard.press("Shift+Enter");
 await browser.keyboard.press("Escape");
 await browser.locator("[data-model-picker-content]").waitFor({state:"detached"});
 await screen.getByRole("button", "Showcase, Plan and reads",{exact:false}).waitFor();
 if (!(await browser.locator('[role="region"][aria-label="Thread details"]:visible').count())) await screen.getByRole("button", /Thread details/i).click();
 const branch = browser.locator('[role="region"][aria-label="Thread details"] '+'[data-slot="combobox-trigger"][aria-label^="Branch:"]');
 await branch.waitFor();
 const label = await branch.getAttribute("aria-label");
 await app.screenshot("forced-new-worktree-base");
 await branch.click();
 await screen.getByRole("option",/^feature\b/).waitFor();
 const remote = await screen.getByText("Start from remote",{exact:true}).count();
 await app.screenshot("panel-branch-picker");
 await screen.getByRole("option",/^feature\b/).click();
 await branch.filter({hasText:"feature"}).waitFor();
 await browser.locator('[data-composer-shortcut] [aria-label="Branch: From feature"], [data-composer-shortcut][aria-label="Branch: From feature"], [data-slot="combobox-trigger"][aria-label="Branch: From feature"]:visible').first().waitFor();
 await browser.locator('[role="option"]').first().waitFor({state:"hidden"});
 await app.screenshot("after-selecting-feature");
 const checkedOut = (await runPromise(run("git",["branch","--show-current"],{cwd:previous}))).stdout.trim();
 await expect.poll(async () => {
 const draft = await browser.evaluate(() => {
 const stored = JSON.parse(localStorage.getItem("t3code:composer-drafts:v1")!);
 const draft = stored.state.draftThreadsByThreadKey[location.pathname.split("/").at(-1)!];
 return {baseBranch:draft?.branch,hasPreviousWorktree:draft?.worktreePath != null};
 });
 return {label,remote,checkedOut,...draft};
 }).toEqual({label:"Branch: From t3/go",remote:1,checkedOut:"t3/go",baseBranch:"feature",hasPreviousWorktree:false});
});
