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

// Recorded from a real OpenCode 2.4.4 / GPT-6.1 Sol run; only workspace metadata removed.
const transcript = "{\"type\": \"expect_outbound\", \"frame\": {\"kind\": \"request\", \"method\": \"session/prompt\", \"params\": {\"sessionId\": \"recorded-session\", \"prompt\": [{\"type\": \"text\", \"text\": \"Run exactly these three shell commands as three separate bash tool calls, without changing their text or combining them. 1: zsh --emulate sh -c 'echo hi'\\n2 (three lines):\\nzsh --emulate sh <<'EOF'\\necho hi\\nEOF\\n3: zsh -c 'echo hi'\\nDo not read files or use other tools. Then reply exactly: Three zsh commands completed.\"}]}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"session_info_update\", \"title\": \"Three zsh command tests\"}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call\", \"toolCallId\": \"call_OdHyt3PXIyU0m0UCa2zn4UP9\", \"name\": \"shell\", \"title\": \"shell\", \"kind\": \"execute\", \"status\": \"pending\", \"rawInput\": {}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_OdHyt3PXIyU0m0UCa2zn4UP9\", \"status\": \"in_progress\", \"kind\": \"execute\", \"title\": \"zsh --emulate sh -c 'echo hi'\", \"rawInput\": {\"command\": \"zsh --emulate sh -c 'echo hi'\"}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_OdHyt3PXIyU0m0UCa2zn4UP9\", \"status\": \"in_progress\", \"kind\": \"execute\", \"title\": \"zsh --emulate sh -c 'echo hi'\", \"rawInput\": {\"command\": \"zsh --emulate sh -c 'echo hi'\"}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_OdHyt3PXIyU0m0UCa2zn4UP9\", \"status\": \"completed\", \"content\": [{\"type\": \"content\", \"content\": {\"type\": \"text\", \"text\": \"hi\\n\"}}], \"rawOutput\": {\"metadata\": {\"status\": \"completed\", \"truncated\": false, \"exit\": 0}}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call\", \"toolCallId\": \"call_1z6DoqitzpXZdNFevV4fhIeh\", \"name\": \"shell\", \"title\": \"shell\", \"kind\": \"execute\", \"status\": \"pending\", \"rawInput\": {}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_1z6DoqitzpXZdNFevV4fhIeh\", \"status\": \"in_progress\", \"kind\": \"execute\", \"title\": \"zsh --emulate sh <<'EOF'\\necho hi\\nEOF\", \"rawInput\": {\"command\": \"zsh --emulate sh <<'EOF'\\necho hi\\nEOF\"}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_1z6DoqitzpXZdNFevV4fhIeh\", \"status\": \"in_progress\", \"kind\": \"execute\", \"title\": \"zsh --emulate sh <<'EOF'\\necho hi\\nEOF\", \"rawInput\": {\"command\": \"zsh --emulate sh <<'EOF'\\necho hi\\nEOF\"}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_1z6DoqitzpXZdNFevV4fhIeh\", \"status\": \"completed\", \"content\": [{\"type\": \"content\", \"content\": {\"type\": \"text\", \"text\": \"hi\\n\"}}], \"rawOutput\": {\"metadata\": {\"status\": \"completed\", \"truncated\": false, \"exit\": 0}}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call\", \"toolCallId\": \"call_cLKFnneLK7aiIY6XuIMblq8H\", \"name\": \"shell\", \"title\": \"shell\", \"kind\": \"execute\", \"status\": \"pending\", \"rawInput\": {}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_cLKFnneLK7aiIY6XuIMblq8H\", \"status\": \"in_progress\", \"kind\": \"execute\", \"title\": \"zsh -c 'echo hi'\", \"rawInput\": {\"command\": \"zsh -c 'echo hi'\"}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_cLKFnneLK7aiIY6XuIMblq8H\", \"status\": \"in_progress\", \"kind\": \"execute\", \"title\": \"zsh -c 'echo hi'\", \"rawInput\": {\"command\": \"zsh -c 'echo hi'\"}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"tool_call_update\", \"toolCallId\": \"call_cLKFnneLK7aiIY6XuIMblq8H\", \"status\": \"completed\", \"content\": [{\"type\": \"content\", \"content\": {\"type\": \"text\", \"text\": \"hi\\n\"}}], \"rawOutput\": {\"metadata\": {\"status\": \"completed\", \"truncated\": false, \"exit\": 0}}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"agent_message_chunk\", \"messageId\": \"msg_242a01861001nbMm2co3GYC0P7\", \"content\": {\"type\": \"text\", \"text\": \"Three zsh commands completed.\"}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"notification\", \"method\": \"session/update\", \"params\": {\"sessionId\": \"recorded-session\", \"update\": {\"sessionUpdate\": \"usage_update\", \"used\": 10220, \"size\": 1050000, \"cost\": {\"amount\": 0.0253193, \"currency\": \"USD\"}}}}}\n{\"type\": \"emit_inbound\", \"frame\": {\"kind\": \"response\", \"method\": \"session/prompt\", \"result\": {\"stopReason\": \"end_turn\", \"usage\": {\"inputTokens\": 10654, \"outputTokens\": 93, \"totalTokens\": 40699, \"cachedReadTokens\": 29952}, \"_meta\": {}}}}\n";

test("zsh --emulate highlights inline and stdin scripts", WEB, async ({ t3, app, screen, browser }) => {
  const recording = path.join(APP_DIR, "apps/server/scripts/dev-replay/command_output_transcript.ndjson");
  const original = await fs.readFile(recording, "utf8");
  try {
    await fs.writeFile(recording, transcript);
    const project = await t3.project("zsh-demo");
    const thread = await t3.thread({ project, title: "Zsh embedded scripts", messages: ["Run the three zsh commands"], model: "command-output" });
    await browser.emulateMedia({ colorScheme: "dark" });
    await t3.open(thread);
    await screen.getByRole("button", "Worked for", { exact: false }).tap();
    await screen.getByRole("button", "Ran 3 commands", { exact: true }).tap();
    await screen.getByRole("button", "zsh --emulate sh -c", { exact: false }).first().tap();
    await screen.getByRole("button", "zsh --emulate sh <<", { exact: false }).first().tap();
    await screen.getByRole("button", "echo hi", { exact: true }).first().tap();
    await screen.getByText("echo", { exact: true }).first().waitFor();
    await app.screenshot("expanded-zsh-scripts");
    const echoTokens = await browser.evaluate(() => [...document.querySelectorAll<HTMLElement>("span[style]")].filter(e => e.textContent === "echo").map(e => getComputedStyle(e).color));
    expect(echoTokens, "All three echo commands have the control's shell-command highlighting").toHaveLength(3);
    expect(new Set(echoTokens).size).toBe(1);
  } finally {
    await fs.writeFile(recording, original);
  }
});
