/// <reference lib="dom" />
import * as fs from "node:fs/promises";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("Shell options after -c leave nested Python highlighted", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("shell-highlighting");
  const settingsPath = `${T3_HOME}/userdata/settings.json`;
  const settings = JSON.parse(await fs.readFile(settingsPath, "utf8"));
  settings.providerInstances.codex = { driver: "codex", enabled: true, config: { binaryPath: "/usr/local/bin/codex" } };
  await fs.writeFile(settingsPath, JSON.stringify(settings));
  const { cli } = makeState(T3_HOME);
  const started = await runPromise(cli(["thread", "start", project,
    `Run exactly these three commands, each in its own shell tool call: bash -c -e "python3 -c 'print(1)'" ; bash -co pipefail "python3 -c 'print(1)'" ; bash -c "python3 -c 'print(1)'". Do not change the commands. Then reply Done.`,
    "--title", "Shell command highlighting", "--provider", "codex", "--model", "gpt-6-luna", "--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 3 commands", { exact: false }).tap();
  const rows = screen.getByRole("button", /^bash -c/);
  for (let i = 0; i < 3; i++) await rows.nth(i).tap();
  await screen.getByText("print", { exact: true }).last().waitFor();
  await app.screenshot("shell-options");
  const colors = await browser.evaluate(() => [...document.querySelectorAll('div[class="text-foreground/85"]')].filter(p => p.textContent?.includes("python3")).map(p => [...p.querySelectorAll("span")].filter(s => s.textContent === "print").length));
  expect(colors).toEqual([1, 1, 1]);
});
