/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

const notebook = {
  nbformat: 4, nbformat_minor: 5,
  metadata: { language_info: { name: "python" } },
  cells: [{ id: "progress", cell_type: "code", source: [], metadata: {}, execution_count: 1,
    outputs: [
      { output_type: "stream", name: "stderr", text: ["\u001b[?25lepoch 1\u001b[K\n", "\u001b[Aepoch 2\u001b[", "2K\u001b[?25h\n"] },
      { output_type: "execute_result", execution_count: 1, metadata: {}, data: { "text/plain": "\u001b[1Gready\u001b[0K" } },
    ],
  }],
};

test("Saved notebook output drops cursor and erase escape sequences", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("demo", { "progress.ipynb": JSON.stringify(notebook) });
  const thread = await t3.thread({ project, title: "Review progress", messages: ["Start"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor();
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("progress.ipynb");
  await screen.getByRole("option", "progress.ipynb", { exact: false }).tap();
  await browser.locator('section[aria-label="Cell 1"] > div.border-t:last-child > pre').waitFor({ timeout: 120_000 });
  await app.screenshot("saved-notebook-output");
  const outputs = await browser.evaluate(() => Array.from(document.querySelectorAll('section[aria-label="Cell 1"] > div.border-t > pre'), node => node.textContent?.trim()));
  expect(outputs).toEqual(["epoch 1\nepoch 2", "ready"]);
});
