import { expect } from "e2e";
import { test, WEB } from "./t3.ts";
import { T3_HOME } from "../e2e.config.ts";
import { readFile, writeFile } from "node:fs/promises";

test("Stash restores trailing skill and file chips and preserves unfinished queries", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("chip-demo", { "README.md": "# Example\nA synthetic project for composer evidence.\n", ".agents/skills/grill-me/SKILL.md": "---\nname: grill-me\ndescription: Ask focused questions about a plan.\n---\nAsk focused questions.\n" });
  // The replay provider advertises a synthetic skill through its real ACP boundary.
  const settingsPath = `${T3_HOME}/userdata/settings.json`;
  const settings = JSON.parse(await readFile(settingsPath, "utf8"));
  const replay = settings.providerInstances.devReplay;
  const wrapper = `${T3_HOME}/skill-replay.cjs`;
  await writeFile(wrapper, `
const { spawn } = require("node:child_process");
const { createInterface } = require("node:readline");
const child = spawn(process.execPath, process.argv.slice(2), { stdio: ["pipe", "pipe", "inherit"] });
process.stdin.pipe(child.stdin);
createInterface({ input: child.stdout }).on("line", line => {
  process.stdout.write(line + "\\n");
  const message = JSON.parse(line);
  if (message.result?.sessionId) process.stdout.write(JSON.stringify({jsonrpc:"2.0",method:"session/update",params:{sessionId:message.result.sessionId,update:{sessionUpdate:"available_commands_update",availableCommands:[{name:"$grill-me",description:"Ask focused questions about a plan"}]}}}) + "\\n");

});
process.on("SIGTERM", () => child.kill("SIGTERM"));
child.on("exit", code => process.exit(code ?? 0));
`);
  replay.config.commandArgs = [wrapper, ...replay.config.commandArgs];
  await writeFile(settingsPath, JSON.stringify(settings));
  await t3.thread({ project, title: "Prompt stash", messages: ["Ready for composer checks"] });
  await t3.open();
  await screen.getByRole("button", "Prompt stash, Done, chip-demo").tap();
  const editor = screen.getByRole("textbox");
  await editor.fill("@Rea");
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Control+s");
  await browser.keyboard.press("Control+s");
  await browser.keyboard.press("Escape");
  await app.screenshot("unfinished-query-restored");
  await expect(editor).toHaveText("@Rea");
  await editor.fill("$grill");
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Control+s");
  await browser.keyboard.press("Control+s");
  await browser.keyboard.press("Escape");
  await app.screenshot("unfinished-skill-query-restored");
  await expect(editor).toHaveText("$grill");
  await editor.fill("Please review @README");
  await screen.getByRole("option").first().tap();
  await app.screenshot("file-selected-before-stash");
  await editor.tap();
  await browser.keyboard.press("End");
  await browser.keyboard.press("Control+s");
  await browser.keyboard.press("Control+s");
  await app.screenshot("file-restored");
  await expect.soft(browser.locator('[data-composer-mention-chip="true"]')).toBeVisible();
  await editor.fill("Please review $grill");
  await screen.getByRole("option").first().textContent();
  await browser.keyboard.press("Tab");
  await app.screenshot("skill-selected-before-stash");
  await editor.tap();
  await browser.keyboard.press("End");
  await browser.keyboard.press("Control+s");
  await browser.keyboard.press("Control+s");
  await app.screenshot("skill-restored");
  await expect.soft(editor.getByRole("button", "Skill grill-me. Show details")).toBeVisible();
});
