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

const script = 'set -l name world; echo "hi $name"';
const commands = [`fish -c '${script}'`, `fish -lc '${script}'`, `fish -iNc'${script}'`, `bash -lc 'name=world; echo "hi $name"'`, `fish -Cc 'echo hi'`];
test("Fish bundled command flags highlight the embedded script", WEB, async ({ t3, app, browser, screen }) => {
  const fixture = `${APP_DIR}/apps/server/scripts/dev-replay/command_output_transcript.ndjson`;
  const original = await fs.readFile(fixture, "utf8");
  const notification = (update: unknown) => ({type:"emit_inbound",frame:{kind:"notification",method:"session/update",params:{sessionId:"dev-replay-session",update}}});
  const entries: unknown[] = [{type:"expect_outbound",frame:{kind:"request",method:"session/prompt",params:"<any>"}}];
  commands.forEach((command,i) => {
    entries.push(notification({sessionUpdate:"tool_call",toolCallId:`fish-${i}`,kind:"execute",title:command,status:"in_progress",rawInput:{command}}));
    entries.push(notification({sessionUpdate:"tool_call_update",toolCallId:`fish-${i}`,status:"completed",rawOutput:"hi world\n"}));
  });
  entries.push(notification({sessionUpdate:"agent_message_chunk",content:{type:"text",text:"Shell highlighting comparison complete."}}));
  entries.push({type:"emit_inbound",frame:{kind:"response",method:"session/prompt",result:{stopReason:"end_turn"}}});
  await fs.writeFile(fixture,entries.map(x=>JSON.stringify(x)).join("\n"));
  try {
    const project = await t3.project("shell-demo");
    const thread = await t3.thread({project,title:"Fish command flags",messages:["Compare shell command highlighting"],model:"command-output"});
    await t3.open(thread);
    await screen.getByText("Shell highlighting comparison complete.").waitFor();
    await screen.getByRole("button","Worked for",{exact:false}).tap();
    await screen.getByRole("button","Ran 5 commands",{exact:false}).tap();
    await screen.getByRole("button",script,{exact:true}).nth(0).tap();
    await screen.getByRole("button",script,{exact:true}).nth(1).tap();
    await screen.getByRole("button",commands[2],{exact:true}).tap();
    await screen.getByRole("button",'name=world; echo "hi $name"',{exact:true}).tap();
    await screen.getByRole("button","echo hi",{exact:true}).tap();
    await app.screenshot("fish-command-highlighting");
    const highlightedScripts = () => browser.evaluate(() => Array.from(document.querySelectorAll("div.text-foreground\\/85")).filter(el=>el.textContent?.includes("set -l name world")).map(el=>Array.from(el.querySelectorAll("span[style]")).filter(span=>span.textContent === "echo").map(span=>getComputedStyle(span).color)[0]));
    await expect.poll(async()=> { const colors = await highlightedScripts(); return colors.length === 3 && colors[0] !== undefined && colors.every(color=>color === colors[0]); }).toBe(true);
  } finally { await fs.writeFile(fixture,original); }
});
