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

test("An existing deep directory lists its child in the local folder picker", WEB, async ({t3, app, screen}) => {
  const root = "/tmp/factory-pr-47817-a1796255756-evidence-1/deep";
  const a = "a".repeat(180);
  const b = "b".repeat(180);
  const c = "c".repeat(180);
  const deep = `${root}/${a}/${b}/${c}/`;
  await fs.mkdir(`${deep}child`, {recursive:true});
  const project = await t3.project("demo");
  const thread = await t3.thread({project, title:"Folder browsing", messages:["Start"]});
  await t3.open(thread);
  await screen.getByRole("combobox", "Projects").tap();
  await screen.getByRole("option", "Add project").tap();
  await screen.getByText("Local folder", {exact:true}).tap();
  const palette = screen.getByRole("dialog", "Command palette");
  const input = palette.getByRole("combobox");
  await input.fill(`${root}/`);
  await palette.getByText(a, {exact:true}).waitFor();
  await input.fill(`${root}/${a}/${b}/`);
  await palette.getByText(c, {exact:true}).waitFor();
  await input.fill(deep);
  try {
    await expect(palette.getByText("child", {exact:true})).toBeVisible();
  } finally {
    await app.screenshot("deep-directory-children");
  }
});
