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

test("Workspace suggestions reject trailing whitespace without absorbing valid siblings", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("wsproj", {
    "docs/example.md": "Plain docs sibling",
    "docs\u00a0/example.md": "Exact NBSP directory",
    "notes/example.md": "Valid notes directory",
    "team.code-workspace": '{"folders":[{"path":"docs\\u00a0","name":"Exact"},{"path":"notes ","name":"TrailingSpace"},{"path":"notes"}]}',
  });
  const thread = await t3.thread({ project, title: "Workspace path proof", messages: ["Hello"] });
  await t3.open(thread);
  await t3.open("/settings/projects");
  await screen.getByRole("button", /^wsproj /).tap();
  await screen.getByRole("button", "Add folder", { exact: true }).waitFor();
  await browser.evaluate(() => document.getElementById("project-repositories")?.scrollIntoView());
  await screen.getByText("In workspace file", { exact: true }).first().waitFor();
  await browser.evaluate(() => {
    const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
    while (walker.nextNode()) {
      const node = walker.currentNode;
      if (node.textContent?.includes("/home/user/")) node.textContent = node.textContent.replace(/\/home\/user\/[^\s]*?\/workspaces\/w-[^/]+/g, "/workspace");
    }
  });
  await app.screenshot("workspace-folder-suggestions");
  const suggestions = await browser.evaluate(() => document.getElementById("project-repositories")?.innerText ?? "");
  expect(suggestions).not.toContain("Exact");
  expect(suggestions).not.toContain("TrailingSpace");
  await expect(screen.getByRole("button", "Add notes to the project", { exact: true })).toBeVisible();
});
