import { readFile, writeFile } from "node:fs/promises";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB, makeState, runPromise } from "./t3.ts";

test("Bundled and separate env unset options both highlight inline Python", WEB, async ({t3,app,screen,browser}) => {
 const project = await t3.project("inline-python");
 const path = `${T3_HOME}/userdata/settings.json`;
 const settings = JSON.parse(await readFile(path,"utf8"));
 settings.providerInstances.opencode = {driver:"opencode",enabled:true,config:{binaryPath:`${process.env.HOME}/.local/bin/opencode`}};
 await writeFile(path,JSON.stringify(settings));
 const {cli}=makeState(T3_HOME);
 const started=await runPromise(cli(["thread","start",project,"Run exactly these two commands as two separate shell tool calls, without changing their spelling: env -iu UNUSED python3 -c 'print(42)' and env -i -u UNUSED python3 -c 'print(42)'. Then reply Done.","--title","Inline Python highlighting","--provider","opencode","--model","openai/gpt-6.1-sol","--wait","--json"]));
 await t3.open({threadId:JSON.parse(started.stdout).threadId});
 await screen.getByRole("button","Worked for",{exact:false}).tap();
 await screen.getByRole("button","Ran 2 commands",{exact:false}).tap();
 await screen.getByRole("button", "env -iu UNUSED python3 -c 'print(42)'",{exact:true}).first().tap();
 await screen.getByRole("button", "env -i -u UNUSED python3 -c 'print(42)'",{exact:true}).first().tap();
 await screen.getByText("42",{exact:true}).first().waitFor();
 await expect.poll(()=>browser.evaluate(`Array.from(document.querySelectorAll('span')).filter(s=>s.textContent==='print').length`)).toBeGreaterThanOrEqual(1);
 await app.screenshot("inline-python");
 const tokens=await browser.evaluate(`Array.from(document.querySelectorAll('span')).filter(s=>s.textContent==='print').map(s=>s.getAttribute('style'))`);
 expect(tokens.length).toBe(2);
});
