import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

// start-app.ts sets server HOME to this synthetic directory; no private files are scanned.
const home = "/tmp/workspace-search-proof/home";

test("Workspace search refuses home while ordinary project files remain searchable", WEB, async ({ t3, app, screen }) => {
  const control = await t3.project("Ordinary project", { "local-part.stl": "solid local\nendsolid local\n" });
  await t3.thread({ project: control, title: "Ordinary folder control", messages: ["Ready"] });
  await t3.open();
  await screen.getByRole("button", { name: "Ordinary folder control, Done, Ordinary project" }).tap();
  await screen.getByRole("textbox").fill("@local-part.stl");
  await screen.getByRole("option").first().waitFor({ state: "visible" });
  await app.screenshot("ordinary-folder-control");
  await screen.getByRole("textbox").press("Escape");
  const state = makeState(T3_HOME);
  await runPromise(state.cli(["project", "add", home, "--title", "Home directory"]));
  await t3.thread({ project: home, title: "Home directory search", messages: ["Ready"] });
  await app.open("/");
  await screen.getByRole("button", { name: "Home directory search, Done, Home directory" }).tap();
  await screen.getByRole("textbox").fill("@part.stl");
  // Both outcomes settle the menu: a leaked option or the guarded empty state.
  await screen.getByText(/part.stl|No matching files or folders\./).last().waitFor({ state: "visible" });
  await app.screenshot("home-directory-search");
  await expect(screen.getByText("No matching files or folders.").last()).toBeVisible();
});
