Files
openidea/e2e/search.spec.ts
T
l0ng-ai 3d80e661b7 fix(search): keep Find results usable in narrow docks (#4681)
The pinned Find tool window moves its result actions to a vertical toolbar and the replacement controls to a wrapping footer. Wide docks show results beside the source preview, narrow docks stack them and remember the divider per layout; result rows use the editor code size and truncate long paths before file names and match counts.
2026-11-20 11:52:49 +08:00

245 lines
11 KiB
TypeScript

import type { SearchQuery } from "../src/bindings";
import { doubleShift, expect, ipcCalls, openFromTree, stubDesktop, test } from "./fixtures";
test("double Shift opens Search Everywhere with its tabs; Escape closes it", async ({ workbench: page }) => {
await doubleShift(page);
const dialog = page.getByRole("dialog", { name: "Search Everywhere" });
await expect(dialog).toBeVisible();
await expect(dialog.getByRole("combobox")).toBeFocused();
for (const tab of ["All", "Classes", "Files", "Symbols", "Actions", "Text"]) {
await expect(dialog.getByRole("tab", { name: tab })).toBeVisible();
}
await page.keyboard.press("Escape");
await expect(dialog).toBeHidden();
});
test("Search Everywhere finds and runs an action", async ({ workbench: page }) => {
await doubleShift(page);
const dialog = page.getByRole("dialog", { name: "Search Everywhere" });
await dialog.getByRole("combobox").fill("Language Servers");
const option = dialog.getByRole("option", { name: /Language Servers/ }).first();
await expect(option).toBeVisible();
await page.keyboard.press("Enter");
await expect(page.getByRole("dialog", { name: "Settings" })).toBeVisible();
await expect(
page.getByRole("dialog", { name: "Settings" }).getByRole("list", { name: "Languages", exact: true }),
).toBeVisible();
});
const ROOT = "/dev/acme-api";
const SERVICE = `${ROOT}/src/main/kotlin/com/acme/orders/service/OrderService.kt`;
test("Search Everywhere finds a file and opens it", async ({ workbench: page }) => {
// The project's file index is stubbed at the IPC layer.
await stubDesktop(page);
await page.evaluate((path) => {
window.__ipc.handlers.find_files = (args) =>
String(args.query).startsWith("OrderSer")
? [{ path, relPath: path.slice("/dev/acme-api/".length), score: 100, indices: [] }]
: [];
}, SERVICE);
await doubleShift(page);
const dialog = page.getByRole("dialog", { name: "Search Everywhere" });
await dialog.getByRole("combobox").fill("OrderService");
await expect(dialog.getByRole("option", { name: /OrderService\.kt/ })).toBeVisible();
await page.keyboard.press("Enter");
await expect(page.getByRole("tab", { name: "OrderService.kt" })).toHaveAttribute("aria-selected", "true");
await expect(page.getByRole("textbox", { name: `Editor for ${SERVICE}` })).toBeVisible();
});
test("Find in Files streams matches by file and opens the chosen one at its line", async ({ workbench: page }) => {
const other = await openFromTree(page, "settings.gradle.kts");
const originalOther = await other.locator(".cm-content").textContent();
await stubDesktop(page);
await page.evaluate(
({ root, service }) => {
const match = (line: number, text: string, word: string) => ({
line,
text,
ranges: [[text.indexOf(word), text.indexOf(word) + word.length]],
textOffset: 0,
});
window.__ipc.handlers.search_validate = () => null;
window.__ipc.handlers.search_start = (args) => {
const send = (event: unknown) => window.__ipc.send(args.onEvent, event);
setTimeout(() => {
send({
type: "file",
file: {
path: `${root}/build.gradle.kts`,
relPath: "build.gradle.kts",
matches: [
match(7, "class OrderController(private val service: OrderService) {", "OrderService"),
match(3, "import com.acme.orders.service.OrderService", "OrderService"),
],
},
});
send({
type: "file",
file: {
path: service,
relPath: service.slice(root.length + 1),
matches: [match(0, "// OrderService", "OrderService")],
},
});
send({
type: "done",
summary: { files: 2, matches: 3, limitHit: false, cancelled: false, elapsedMs: 4 },
});
}, 20);
return null;
};
},
{ root: ROOT, service: SERVICE },
);
await page.keyboard.press("Meta+Shift+f");
const dialog = page.getByRole("dialog", { name: "Find in Files" });
await dialog.getByRole("textbox", { name: "Text to find" }).fill("OrderService");
const matches = dialog.getByRole("listbox", { name: "Matches" });
await expect(matches.getByRole("option")).toHaveCount(3);
// The TODO index scans the project through `search_start` too, on its own schedule.
await expect
.poll(
async () =>
(await ipcCalls(page, "search_start"))
.filter(({ query }) => {
const pattern = query && typeof query === "object" && "pattern" in query ? query.pattern : undefined;
return typeof pattern !== "string" || !/\bTODO\b/.test(pattern);
})
.at(-1)?.query,
)
.toMatchObject({ pattern: "OrderService" } satisfies Pick<SearchQuery, "pattern">);
const preview = dialog.getByRole("region", { name: "Preview", exact: true }).locator(".cm-content");
await expect(preview).toHaveAttribute("contenteditable", "true");
// Matches take the editor scheme's search result background (IDEA's SEARCH_RESULT_ATTRIBUTES) and keep the syntax colours.
const match = preview.locator(".cm-openidea-searchResult").first();
await expect(match).toHaveText("OrderService");
expect(
await match.evaluate((el) => {
const probe = document.createElement("span");
probe.style.backgroundColor = "var(--bg-search-result)";
el.parentElement!.append(probe);
const expected = getComputedStyle(probe).backgroundColor;
probe.remove();
const own = getComputedStyle(el);
return own.backgroundColor === expected && own.color === getComputedStyle(el.parentElement!).color;
}),
).toBe(true);
const originalPreview = await preview.textContent();
await preview.focus();
await page.keyboard.press("Meta+a");
await page.keyboard.insertText("OrderService preview edit");
await expect(preview).toHaveText("OrderService preview edit");
await expect(other.locator(".cm-content")).toHaveText(originalOther!);
await page.keyboard.press("Meta+Backspace");
await expect(preview).toHaveText("");
await expect(other.locator(".cm-content")).toHaveText(originalOther!);
await page.keyboard.press("Meta+z");
await expect(preview).toHaveText(originalPreview!);
await page.keyboard.press("Meta+a");
await page.keyboard.insertText("OrderService preview edit");
await page.keyboard.press("Enter");
await expect(dialog).toBeVisible();
await expect(preview.locator(".cm-line")).toHaveCount(2);
await page.keyboard.press("Meta+z");
await page.keyboard.press("Meta+z");
await expect(preview).toHaveText(originalPreview!);
await matches.getByRole("option", { name: /import com\.acme\.orders\.service\.OrderService/ }).dblclick();
await expect(page.getByRole("tab", { name: "build.gradle.kts" })).toHaveAttribute("aria-selected", "true");
const caretLine = () =>
page.evaluate((label) => {
const content = document.querySelector(`.cm-content[aria-label="${label}"]`) as unknown as {
cmTile: {
view: { state: { doc: { lineAt(pos: number): { text: string } }; selection: { main: { head: number } } } };
};
} | null;
if (!content) return null;
const { state } = content.cmTile.view;
return state.doc.lineAt(state.selection.main.head).text;
}, `Editor for ${ROOT}/build.gradle.kts`);
await expect.poll(caretLine).toBe("import com.acme.orders.service.OrderService");
});
test("the Find tool window keeps the selected preview and replacement actions when narrowed", async ({
workbench: page,
}) => {
await page.setViewportSize({ width: 1500, height: 820 });
await stubDesktop(page);
await page.evaluate(
({ path, relPath }) => {
window.__ipc.handlers.search_validate = () => null;
window.__ipc.handlers.search_start = ({ query, onEvent }) => {
if ((query as SearchQuery).pattern !== "OrderService") return null;
setTimeout(() => {
window.__ipc.send(onEvent, {
type: "file",
file: {
path,
relPath,
matches: [
{
line: 2,
text: "import com.acme.orders.service.OrderService",
ranges: [[31, 43]],
textOffset: 0,
},
],
},
});
window.__ipc.send(onEvent, {
type: "done",
summary: { files: 1, matches: 1, limitHit: false, cancelled: false, elapsedMs: 1 },
});
}, 20);
return null;
};
},
{ path: `${ROOT}/build.gradle.kts`, relPath: "build.gradle.kts" },
);
await page.keyboard.press("Meta+Shift+r");
const popup = page.getByRole("dialog", { name: "Replace in Files" });
await popup.getByRole("textbox", { name: "Text to find" }).fill("OrderService");
await popup.getByRole("textbox", { name: "Replace with" }).fill("OrdersApi");
await expect(popup.getByRole("listbox", { name: "Matches" }).getByRole("option")).toHaveCount(1);
await popup.getByRole("button", { name: "Open in Find Window", exact: true }).click();
const find = page.getByRole("region", { name: "Find", exact: true });
await find.getByRole("button", { name: "Options", exact: true }).click();
await page.getByRole("menuitem", { name: "Move To", exact: true }).hover();
await page.getByRole("menuitem", { name: "Right Top", exact: true }).click();
const dockDivider = page.getByRole("separator", { name: "Resize right tool window", exact: true });
const resizeDock = async (width: number) => {
await expect(dockDivider).toBeVisible();
const handle = (await dockDivider.boundingBox())!;
const dock = (await find.boundingBox())!;
const x = handle.x + handle.width / 2;
const y = handle.y + handle.height / 2;
await page.mouse.move(x, y);
await page.mouse.down();
await page.mouse.move(x + dock.width - width, y, { steps: 5 });
await page.mouse.up();
};
await resizeDock(690);
const row = find.getByRole("treeitem", { name: /OrderService.*OrdersApi/ });
await row.click();
const preview = find.getByRole("region", { name: "Preview", exact: true });
await expect(preview.locator(".cm-content")).toContainText("OrderService");
const divider = find.getByRole("separator", { name: "Resize find preview" });
await expect(divider).toHaveAttribute("aria-orientation", "vertical");
await resizeDock(325);
await expect(divider).toHaveAttribute("aria-orientation", "horizontal");
await expect(row).toHaveAttribute("aria-selected", "true");
await expect(preview.locator(".cm-content")).toContainText("OrderService");
await expect(find.getByRole("toolbar", { name: "Find actions" })).toHaveAttribute("aria-orientation", "vertical");
for (const label of ["Preview", "Replace Selected", "Replace All"]) {
await expect(find.getByRole("button", { name: label, exact: true })).toBeInViewport({ ratio: 1 });
}
await find.getByRole("button", { name: "Replace All", exact: true }).click();
const confirmation = page.getByRole("dialog", { name: "Replace All" });
await expect(confirmation).toBeVisible();
await confirmation.getByRole("button", { name: "Cancel" }).click();
await expect(confirmation).toBeHidden();
});