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

test("Successful colon guards do not report the skipped git command", WEB, async ({ t3, app, screen, browser }) => {
  const settingsPath = `${T3_HOME}/userdata/settings.json`;
  const settings = JSON.parse(readFileSync(settingsPath, "utf8"));
  settings.providerInstances.claudeAgent.enabled = true;
  settings.defaultRuntimeMode = "full-access";
  writeFileSync(settingsPath, JSON.stringify(settings));
  const project = await t3.project("colon-guard-demo");
  const { cli } = makeState(T3_HOME);
  const started = await runPromise(cli(["thread", "start", project, "Reply only Ready. Do not use tools.", "--title", "Colon guard command labels", "--provider", "claudeAgent", "--model", "claude-haiku-5-5", "--wait", "--json"]));
  const thread = JSON.parse(started.stdout);
  await t3.open(thread);
  const labels: string[] = [];
  for (const [index, command] of [": || git --version", "builtin : || git --version", "command : || git --version"].entries()) {
    const composer = browser.locator('[data-chat-composer-form="true"]').getByRole("textbox");
    await composer.fill(`Run exactly \`${command}\` with one Bash call. Do not run any other commands. Then write a detailed 600-word paragraph explaining shell short circuit evaluation.`);
    await composer.press("Enter");
    const label = screen.getByText(/^(Running|Ran) (git|command)$/).first();
    await label.waitFor();
    labels.push(await label.textContent() ?? "");
    await app.screenshot(`colon-guard-${index + 1}-live`);
    await screen.getByRole("button", "Stop generation").waitFor({ state: "hidden" });
  }
  expect(labels.every(label => /^(Running|Ran) command$/.test(label))).toBe(true);
});
