import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

const control = async (action: string) => (await fetch(`http://127.0.0.1:18938/${action}`)).json();

test("A client can reconnect while Git executable lookup is held by FUSE", WEB, async ({ t3, app, screen, browser }) => {
  const first = await t3.project("Local project");
  const second = await t3.project("Other project");
  const a = await t3.thread({ project: first, title: "Earlier work", messages: ["first"] });
  const b = await t3.thread({ project: second, title: "Unrelated work", messages: ["first", "second"] });
  await t3.open(a);
  await expect(screen.getByText("first fixture turn complete", { exact: false })).toBeVisible();
  await control("arm");
  try {
    await screen.getByRole("button", "Unrelated work", { exact: false }).tap();
    const held = await control("entered");
    if (!held.entered || !held.held) throw new Error("Git lookup did not reach the delayed FUSE directory");
    await browser.reload();
    try {
      await expect(screen.getByText("second fixture turn complete", { exact: false })).toBeVisible({ timeout: 8000 });
      await expect(screen.getByText("Syncing messages...", { exact: true })).not.toBeVisible({ timeout: 8000 });
    } finally {
      await app.screenshot("reconnect-during-filesystem-stall");
      const stillHeld = await control("state");
      if (!stillHeld.held) throw new Error("The filesystem was released before the capture");
    }
  } finally {
    await control("release");
  }
});
