import * as fs from "node:fs/promises";
import * as path from "node:path";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("Authenticated Cursor advertises manual compaction in the composer", WEB, async ({ t3, app, screen }) => {
  const settingsPath = path.join(T3_HOME, "userdata/settings.json");
  const settings = JSON.parse(await fs.readFile(settingsPath, "utf8"));
  settings.providerInstances.cursor = { driver: "cursor", enabled: true };
  await fs.writeFile(settingsPath, JSON.stringify(settings));
  const project = await t3.project("Compaction demo");
  const { cli } = makeState(T3_HOME);
  const started = await runPromise(cli(["thread", "start", project,
    "Reply with just the word hello. Do not use any tools.",
    "--title", "Cursor compaction", "--provider", "cursor", "--model", "composer-2.5", "--wait", "--json"]));
  const { threadId } = JSON.parse(started.stdout);
  await t3.open({ threadId });
  await screen.getByText("hello", { exact: true }).waitFor();
  await screen.getByText("Syncing messages...", { exact: false }).waitFor({ state: "hidden" });
  await screen.getByRole("textbox").fill("/comp");
  await app.screenshot("cursor-compaction-menu");
  await expect(screen.getByText("Summarize the conversation and reduce context usage", { exact: false })).toBeVisible();
});
