/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, rpcClient, runPromise, test, WEB } from "./t3.ts";
const paragraph = "The review checks the changed layout across local and remote connections, confirms that the question remains readable, and records the completed checks for the maintainer. Each summary explains the evidence behind the recommendation so that the final decision can be made without opening another tool or guessing which parts of the review have already finished successfully.";
const question = "Should we merge this reviewed change?\n\n" + [paragraph, paragraph, paragraph].join("\n\n");
const definition = `export default T3.workflow("pr-review", {version:1,payload:Schema.Struct({kind:Schema.String}),asks:{decides:"The change decides something",failed:"A review needs repair"}},Effect.fn(function* (payload) {
yield* T3.title("PR 46460 fix(web): keep sidebar thread titles readable across wide workflow review windows");
for(let i=1;i<=5;i++) yield* T3.step("review-"+i,{returns:Schema.String},Effect.succeed("Review check "+i+": ${paragraph}"));
yield* T3.ask("decide",{kind:payload.kind,title:${JSON.stringify(question)},choices:[{value:"merge",description:"Approve the reviewed change"},{value:"hold",description:"Wait for more evidence"},{value:"close",description:"Decline this change"}]});}));`;
test("Waiting workflow questions use wide space and retain completed review details", WEB, async ({t3,app,browser,screen}) => {
 const file = await runPromise(Effect.map(Path.Path,p=>p.join(T3_HOME,"workflows/pr-review.ts")));
 await runPromise(Effect.gen(function*(){const fs=yield* FileSystem.FileSystem;const p=yield* Path.Path;yield* fs.makeDirectory(p.dirname(file),{recursive:true});yield* fs.writeFileString(file,definition);}));
 await t3.project("demo");
 await runPromise(Effect.gen(function*(){const c=yield* rpcClient(T3_HOME,app.baseUrl??"");yield* c["server.updateSettings"]({patch:{workflowsEnabled:true}});}).pipe(Effect.scoped));
 const ids=[];
 for(const kind of ["decides","failed"]) {const result=await runPromise(makeState(T3_HOME).cli(["workflow","start","pr-review","--key",kind,"--input",JSON.stringify({kind}),"--json"]));ids.push(JSON.parse(result.stdout).run.runId);}
 await browser.setViewport({width:2560,height:1300});
 await t3.open(`/workflows?run=${ids[0]}`);
 await screen.getByRole("button","merge",{exact:true}).waitFor({timeout:30000});
 await app.screenshot("wide-waiting-question");
 await screen.getByRole("button",/^decide/).tap();
 await app.screenshot("wide-decide-selected");
 await screen.getByRole("button",/^review-1/).tap();
 await app.screenshot("wide-completed-summaries");
 // Both versions provide the kind combobox; head also provides the clickable list.
 await browser.evaluate(()=>{const list=document.querySelector('[aria-label="Questions by kind"]');const b=list?.querySelector('button');if(b instanceof HTMLElement)b.click();});
 await app.screenshot("wide-kind-filter");
 await browser.setViewport({width:700,height:1100});
 await app.screenshot("narrow-completed-details");
 await screen.getByRole("button",/^decide/).tap();
 await app.screenshot("narrow-question-selected");
 await browser.setViewport({width:390,height:1100});
 await app.screenshot("phone-waiting-question");
 await screen.getByRole("button",/^review-1/).tap();
 await app.screenshot("phone-quick-row-selected");
 await browser.evaluate(()=>document.querySelector('section[aria-label="What happened"]')?.scrollIntoView({block:"start"}));
 await app.screenshot("phone-step-details");
 await browser.setViewport({width:2560,height:1300});
 await screen.getByRole("button",/^decide/).tap();
 const width=await browser.evaluate(()=>{const h=[...document.querySelectorAll('h3')].find(x=>x.textContent?.includes('Should we merge'));return h?.getBoundingClientRect().width??0;});
 expect(width,"The waiting question has a comfortable reading width on a 2560px window").toBeGreaterThan(600);
});
