Файли
openidea/e2e/unifiedDiffGutter.spec.ts
T
l0ng-ai f10a584265 fix(vcs): keep commit diff controls outside code selections (#8324)
Revert in the unified diff moves from an inline label into the gutter, so dragging across a change selects only code. In a commit diff the block checkbox and Revert share the first displayed row of each change in fixed slots. Both viewers show a file checkbox beside Current version and report how many changes are included when part or all of the file is excluded.
2026-12-06 20:09:12 +08:00

138 рядки
6.3 KiB
TypeScript

import type { Page } from "@playwright/test";
import { expect, test } from "./fixtures";
async function openDiff(page: Page, left: string, right: string, options: Record<string, unknown> = {}) {
await page.evaluate(
async ({ left, right, options }) => {
localStorage.setItem(
"openidea.vcs.diffOptions",
JSON.stringify({ mode: "unified", collapse: false, ...options }),
);
const actions = "/src/platform/actions.ts";
const { executeAction } = await import(/* @vite-ignore */ actions);
await executeAction("diff.compareTexts", {
path: "SettlementRules.txt",
left: { text: left, label: "Original" },
right: { text: right, label: "Modified" },
});
},
{ left, right, options },
);
return page.getByRole("toolbar", { name: "Diff" });
}
async function rowNumbers(page: Page, text: string, deleted = false) {
return page
.locator(deleted ? ".cm-deletedLine" : ".cm-line")
.filter({ hasText: text })
.evaluate((row) => {
const top = row.getBoundingClientRect().top;
const editor = row.closest(".cm-editor")!;
const read = (side: string) =>
[...editor.querySelectorAll(`.cm-diffNumbers-${side} :is(.cm-gutterElement, .cm-diffWidgetNumber)`)].find(
(number) =>
getComputedStyle(number).visibility === "visible" && Math.abs(number.getBoundingClientRect().top - top) < 1,
)?.textContent ?? null;
return [read("original"), read("modified")];
});
}
test("Unified Diff keeps original and modified numbers aligned with wrapped and removed lines", async ({
workbench: page,
}) => {
await page.setViewportSize({ width: 1000, height: 640 });
const oldRule = "Obsolete settlement rule " + "requires review for each exceptional invoice. ".repeat(3);
const toolbar = await openDiff(
page,
["Settlement rules", oldRule, "Retire legacy routing", "Report totals", "Finish batch"].join("\n"),
["Settlement rules", "Validate settlement currency", "Report totals", "Finish batch"].join("\n"),
{ softWrap: true },
);
await expect(toolbar).toContainText("1 difference");
for (const width of [1000, 440]) {
await page.setViewportSize({ width, height: 640 });
await expect.poll(() => rowNumbers(page, "Obsolete settlement rule", true)).toEqual(["2", null]);
await expect.poll(() => rowNumbers(page, "Retire legacy routing", true)).toEqual(["3", null]);
await expect.poll(() => rowNumbers(page, "Validate settlement currency")).toEqual([null, "2"]);
await expect.poll(() => rowNumbers(page, "Report totals")).toEqual(["4", "3"]);
}
const removed = page.locator(".cm-deletedLine").filter({ hasText: "Obsolete settlement rule" });
const lineHeight = await removed.evaluate((el) => parseFloat(getComputedStyle(el).lineHeight));
expect((await removed.boundingBox())!.height).toBeGreaterThan(lineHeight * 2);
await toolbar.getByRole("button", { name: "Next Difference", exact: true }).click();
await expect(toolbar).toContainText("1 of 1 differences");
});
test("Unified Diff numbers account for ignored empty lines and a removal at EOF without a newline", async ({
workbench: page,
}) => {
await openDiff(
page,
"Settlement rules\n\nReport totals\nFinish batch\nRemove final rule",
"Settlement rules\nReport totals\nFinish batch",
{ ignore: "whitespaceAndLines" },
);
await expect.poll(() => rowNumbers(page, "Report totals")).toEqual(["3", "2"]);
await expect.poll(() => rowNumbers(page, "Finish batch")).toEqual(["4", "3"]);
await expect.poll(() => rowNumbers(page, "Remove final rule", true)).toEqual(["5", null]);
});
test("Unified Diff follows vertical scrolling inside one large removal", async ({ workbench: page }) => {
const removed = Array.from({ length: 250 }, (_, i) => `Retired workflow ${i + 1}.`);
await openDiff(page, ["Start batch", ...removed, "Finish batch"].join("\n"), "Start batch\nFinish batch");
const target = page.locator(".cm-deletedLine").filter({ hasText: "Retired workflow 229." });
await target.scrollIntoViewIfNeeded();
await expect.poll(() => rowNumbers(page, "Retired workflow 229.", true)).toEqual(["230", null]);
});
test("unified Revert stays beside the first displayed row and outside a dragged selection", async ({
workbench: page,
context,
}) => {
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
await page.evaluate(async () => {
const input = "/src/features/vcs/diffInput.ts";
const { openDiff } = await import(/* @vite-ignore */ input);
const before = "start\nkeep\nold value\nkeep again\ndeleted value\nend";
const after = "start\ninserted value\nkeep\nnew value\nkeep again\nend";
await openDiff({
mode: "unified",
path: "example.txt",
left: { type: "text", label: "Original", text: before },
right: { type: "text", label: "Modified", text: after, editable: true },
});
});
const editor = page.locator(".cm-editor:visible");
const buttons = editor.locator(".cm-gutter").getByRole("button", { name: "Revert", exact: true });
await expect(buttons).toHaveCount(3);
const row = (value: string) => editor.locator(".cm-line, .cm-deletedLine").getByText(value, { exact: true });
for (const [i, value] of ["inserted value", "old value", "deleted value"].entries()) {
await expect
.poll(async () => {
const button = await buttons.nth(i).boundingBox();
const text = await row(value).boundingBox();
return button && text && Math.abs(button.y + button.height / 2 - text.y - text.height / 2);
})
.toBeLessThan(2);
}
const start = (await row("start").boundingBox())!;
const end = (await row("keep").boundingBox())!;
await page.mouse.move(start.x + 1, start.y + start.height / 2);
await page.mouse.down();
await page.mouse.move(end.x + end.width - 1, end.y + end.height / 2, { steps: 12 });
await page.mouse.up();
const selected = await page.evaluate(() => String(getSelection()));
expect(selected).toContain("inserted value");
expect(selected).not.toContain("Revert");
await page.keyboard.press("ControlOrMeta+c");
await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe("start\ninserted value\nkeep");
await buttons.nth(1).focus();
await page.keyboard.press("Enter");
await expect(buttons).toHaveCount(2);
await expect(editor.locator(".cm-line").filter({ hasText: "old value" })).toBeVisible();
});