From a3ab95699952796cd7246453e619f186a6e69793 Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Sun, 23 Aug 2026 22:38:12 +0800 Subject: [PATCH] test(terminal): enforce macOS selection regression --- .github/workflows/build.yml | 6 ++++ scripts/github-workflow-ci.test.cjs | 6 ++++ .../patch-xterm-macos-column-selection.cjs | 32 +++++++++++++++---- ...atch-xterm-macos-column-selection.test.cjs | 11 ++++--- ...xterm-macos-column-selection.live.test.cjs | 12 +++++-- 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3aaa605eb..a85c19045 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -72,10 +72,12 @@ on: - "scripts/mosh-extra-resources.cjs" - "scripts/nodePtyConptyPatch.cjs" - "scripts/patch-xterm-webgl-atlas.cjs" + - "scripts/patch-xterm-macos-column-selection.cjs" - "scripts/rebuildPatchedNodePty.cjs" - "scripts/resolve-et-bin-release.cjs" - "scripts/resolve-mosh-bin-release.cjs" - "scripts/verify-linux-*.sh" + - "scripts/xterm-macos-column-selection.live.test.cjs" - "skills/**" # A newer run for the same main push or PR cancels older in-progress work. @@ -243,6 +245,10 @@ jobs: - name: Install deps run: npm ci + - name: Test macOS Option column selection + if: matrix.name == 'macos' + run: npm run test:xterm-macos-selection + - name: Compile ConPTY test helpers if: matrix.name == 'windows' shell: pwsh diff --git a/scripts/github-workflow-ci.test.cjs b/scripts/github-workflow-ci.test.cjs index a42e5c817..d002f4235 100644 --- a/scripts/github-workflow-ci.test.cjs +++ b/scripts/github-workflow-ci.test.cjs @@ -78,6 +78,10 @@ test("PR validation runs once per commit and includes a production build", () => testWorkflow, /- name: Test terminal keyword highlight performance\s*\n\s*env:\s*\n\s*NETCATTY_TERMINAL_PERF_SHOW_WINDOW: "1"\s*\n\s*# GitHub-hosted runners do not configure Electron's SUID sandbox helper\.\s*\n\s*run: xvfb-run -a \.\/node_modules\/\.bin\/electron --no-sandbox scripts\/xterm-keyword-highlight-performance\.live\.test\.cjs/, ); + assert.match( + buildWorkflow, + /- name: Test macOS Option column selection\s*\n\s*if: matrix\.name == 'macos'\s*\n\s*run: npm run test:xterm-macos-selection/, + ); assert.match(testWorkflow, /- name: Build\s*\n\s*run: npm run build/); assert.doesNotMatch(testWorkflow, /\n mosh-windows-conpty:/); }); @@ -112,6 +116,8 @@ test("package validation avoids duplicate branch runs and scopes PR builds", () "scripts/afterPackMacUuid.cjs", "scripts/beforePackCursorSdk.cjs", "scripts/nodePtyConptyPatch.cjs", + "scripts/patch-xterm-macos-column-selection.cjs", + "scripts/xterm-macos-column-selection.live.test.cjs", "scripts/linux/**", "skills/**", ]) { diff --git a/scripts/patch-xterm-macos-column-selection.cjs b/scripts/patch-xterm-macos-column-selection.cjs index 2d8f52360..d7fd3efdb 100644 --- a/scripts/patch-xterm-macos-column-selection.cjs +++ b/scripts/patch-xterm-macos-column-selection.cjs @@ -26,17 +26,20 @@ const PATCHES = [ { file: "node_modules/@xterm/xterm/src/browser/services/SelectionService.ts", original: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionClickForcesSelection);", - replacement: "return event.altKey;", + replacement: "return event.altKey && true;", + preserveStatementLength: true, }, { file: "node_modules/@xterm/xterm/lib/xterm.js.map", original: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionClickForcesSelection);", - replacement: "return event.altKey;", + replacement: "return event.altKey && true;", + preserveStatementLength: true, }, { file: "node_modules/@xterm/xterm/lib/xterm.mjs.map", original: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionClickForcesSelection);", - replacement: "return event.altKey;", + replacement: "return event.altKey && true;", + preserveStatementLength: true, }, ]; @@ -46,11 +49,24 @@ function sameLengthTrueExpression(length) { return `(!0${" ".repeat(length - 4)})`; } +function sameLengthStatement(statement, length) { + if (!statement.endsWith(";") || statement.length > length) { + throw new Error("replacement statement cannot preserve the target length"); + } + return `${statement.slice(0, -1)}${" ".repeat(length - statement.length)};`; +} + +function replacementForPatch(patch) { + if (patch.preserveLength) return sameLengthTrueExpression(patch.original.length); + if (!patch.replacement) throw new Error(`missing replacement for ${patch.file}`); + if (patch.preserveStatementLength) { + return sameLengthStatement(patch.replacement, patch.original.length); + } + return patch.replacement; +} + function patchXtermSource(source, patch) { - const replacement = patch.preserveLength - ? sameLengthTrueExpression(patch.original.length) - : patch.replacement; - if (!replacement) throw new Error(`missing replacement for ${patch.file}`); + const replacement = replacementForPatch(patch); const originalCount = source.split(patch.original).length - 1; const patchedCount = source.split(replacement).length - 1; @@ -106,5 +122,7 @@ module.exports = { PATCHES, patchInstalledXterm, patchXtermSource, + replacementForPatch, + sameLengthStatement, sameLengthTrueExpression, }; diff --git a/scripts/patch-xterm-macos-column-selection.test.cjs b/scripts/patch-xterm-macos-column-selection.test.cjs index 32089adf6..c11df46e2 100644 --- a/scripts/patch-xterm-macos-column-selection.test.cjs +++ b/scripts/patch-xterm-macos-column-selection.test.cjs @@ -6,6 +6,7 @@ const test = require("node:test"); const { PATCHES, patchXtermSource, + replacementForPatch, sameLengthTrueExpression, } = require("./patch-xterm-macos-column-selection.cjs"); @@ -25,17 +26,17 @@ test("patches both distributed bundles without shifting source-map offsets", () test("patches readable xterm sources and source-map content", () => { for (const patch of PATCHES.filter((entry) => !entry.preserveLength)) { - const result = patchXtermSource(`before:${patch.original}:after`, patch); + const input = `before:${patch.original}:after`; + const result = patchXtermSource(input, patch); assert.equal(result.changed, true); - assert.equal(result.source, `before:${patch.replacement}:after`); + assert.equal(result.source, `before:${replacementForPatch(patch)}:after`); + assert.equal(result.source.length, input.length); } }); test("is idempotent and fails closed on an unknown package shape", () => { for (const patch of PATCHES) { - const replacement = patch.preserveLength - ? sameLengthTrueExpression(patch.original.length) - : patch.replacement; + const replacement = replacementForPatch(patch); assert.deepEqual(patchXtermSource(`before:${replacement}:after`, patch), { source: `before:${replacement}:after`, changed: false, diff --git a/scripts/xterm-macos-column-selection.live.test.cjs b/scripts/xterm-macos-column-selection.live.test.cjs index bd7b080d5..144afdecd 100644 --- a/scripts/xterm-macos-column-selection.live.test.cjs +++ b/scripts/xterm-macos-column-selection.live.test.cjs @@ -14,6 +14,7 @@ if (!process.versions.electron) { const fs = require("node:fs"); const os = require("node:os"); const path = require("node:path"); + const { pathToFileURL } = require("node:url"); const electron = require("electron"); const appRoot = path.resolve(__dirname, ".."); @@ -37,6 +38,8 @@ if (!process.versions.electron) { contextIsolation: false, nodeIntegration: true, sandbox: false, + // The harness imports the shipped ESM bundle directly from disk. + webSecurity: false, backgroundThrottling: false, }, }); @@ -47,8 +50,9 @@ if (!process.versions.electron) { ); const xtermPath = require.resolve("@xterm/xterm", { paths: [appRoot] }); - await window.webContents.executeJavaScript(`(() => { - const { Terminal } = require(${JSON.stringify(xtermPath)}); + const xtermEsmUrl = pathToFileURL(path.join(path.dirname(xtermPath), "xterm.mjs")).href; + await window.webContents.executeJavaScript(`(async () => { + const { Terminal } = await import(${JSON.stringify(xtermEsmUrl)}); const term = new Terminal({ cols: 20, rows: 6, @@ -101,8 +105,10 @@ if (!process.versions.electron) { const end = point(5, 2); /** @type {Array<"alt">} */ const modifiers = alt ? ["alt"] : []; + /** @type {Array<"alt" | "leftbuttondown">} */ + const moveModifiers = alt ? ["alt", "leftbuttondown"] : ["leftbuttondown"]; window.webContents.sendInputEvent({ type: "mouseDown", ...start, button: "left", clickCount: 1, modifiers }); - window.webContents.sendInputEvent({ type: "mouseMove", ...end, button: "left", modifiers }); + window.webContents.sendInputEvent({ type: "mouseMove", ...end, button: "left", modifiers: moveModifiers }); window.webContents.sendInputEvent({ type: "mouseUp", ...end, button: "left", clickCount: 1, modifiers }); await delay(100); return window.webContents.executeJavaScript("window.harness.state()");