// @effect-diagnostics nodeBuiltinImport:off -- The scenario stages a local ACP executable.
import * as NodeFSP from "node:fs/promises";
import * as NodePath from "node:path";

import { expect } from "e2e";

import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

const HOME_PATH = "~/.claude/CLAUDE.md";
const REPLY = "Found needle in the release notes.";
const ACP = `
import { createInterface } from "node:readline";

const sessionId = "home-path-scenario";
const path = ${JSON.stringify(HOME_PATH)};
const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n");
const update = (value) => send({ method: "session/update", params: { sessionId, update: value } });
for await (const line of createInterface({ input: process.stdin })) {
  const { id, method } = JSON.parse(line);
  if (id === undefined) continue;
  if (method === "initialize") {
    send({ id, result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] } });
  } else if (method === "session/new") {
    send({ id, result: { sessionId } });
  } else if (method === "session/prompt") {
    update({ sessionUpdate: "tool_call", toolCallId: "search", kind: "search", title: "Search", status: "pending", rawInput: { pattern: "needle", path: "docs", command: "rg --heading -n --with-filename --color never needle docs" } });
    update({ sessionUpdate: "tool_call_update", toolCallId: "search", status: "completed", content: [{ type: "content", content: { type: "text", text: "docs/Release notes.md\\n2:needle found here\\n" } }] });
    update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: ${JSON.stringify(REPLY)} } });
    send({ id, result: { stopReason: "end_turn" } });
  } else {
    send({ id, error: { code: -32601, message: "Method not found" } });
  }
}
`;

test(
  "Ripgrep heading with a space groups matches under the filename with a line number and highlight",
  WEB,
  async ({ t3, app, screen }) => {
    const project = await t3.project("ws", { "docs/Release notes.md": "Release notes\nneedle found here\n" });
    const script = NodePath.join(project, "home-path-acp.mjs");
    const settingsPath = NodePath.join(T3_HOME, "userdata/settings.json");
    const originalSettings = await NodeFSP.readFile(settingsPath, "utf8");
    const settings = JSON.parse(originalSettings);
    await NodeFSP.writeFile(script, ACP);
    settings.providerInstances["home-path-agent"] = {
      driver: "acpRegistry",
      displayName: "Repro ACP",
      enabled: true,
      config: { source: "local", commandPath: process.execPath, commandArgs: [script] },
    };
    try {
      await NodeFSP.writeFile(settingsPath, JSON.stringify(settings));
      const { cli } = makeState(T3_HOME);
      // The watcher reloads settings asynchronously. Retry only before a thread was created.
      let threadId: string | undefined;
      for (let attempt = 0; attempt < 8 && threadId === undefined; attempt++) {
        const started = await runPromise(
          cli(
            [
              "thread",
              "start",
              project,
              "Search docs for needle",
              "--title",
              "Search results",
              "--provider",
              "home-path-agent",
              "--model",
              "default",
              "--wait",
              "--json",
            ],
            [0, 1],
          ),
        );
        if (!started.stdout.includes("threadId")) continue;
        threadId = (JSON.parse(started.stdout) as { threadId: string }).threadId;
      }
      if (threadId === undefined) throw new Error("Could not start the home path turn.");

      await t3.open({ threadId });
      await screen.getByText(REPLY, { exact: true }).waitFor({ timeout: 60_000 });
      await screen.getByRole("button", "Worked for", { exact: false }).tap();
      await screen.getByRole("button", "Searched needle in docs", { exact: false }).tap();
      await app.screenshot("search-with-spaced-filename");
      await expect(screen.getByText("Release notes.md", { exact: true })).toBeVisible();
      await expect(screen.getByText("2", { exact: true })).toBeVisible();
      await expect(screen.getByText("needle", { exact: true })).toBeVisible();
    } finally {
      await NodeFSP.writeFile(settingsPath, originalSettings);
    }
  },
);
