Skip to content

test(e2e): expose active ace editor via automation bridge - #17775

Merged
kevinushey merged 3 commits into
mainfrom
bugfix/e2e-active-editor-bridge
May 26, 2026
Merged

kevinushey merged 3 commits into
mainfrom
bugfix/e2e-active-editor-bridge

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Summary

  • New window.rstudio.documents.activeEditor() bridge method returning the native Ace JS instance for the active source document (or null). Maps through SourceColumnManager.getActiveNativeEditor -- same path GWT already uses to track the active doc.
  • Refactors getEditorContent() and a new openExistingFile() helper to use the bridge, eliminating the .ace_editor DOM scan that was the proximate cause of 8 consistent air_formatting failures.
  • air_formatting.test.ts focusEditor now wraps waitForFunction in a diagnostic catch so a future focus-drift failure reports the actual active element rather than an opaque predicate timeout.

Why the DOM scan was wrong

The bare .ace_editor selector also matches:

  • ShellWidget's console scroll panel (ShellWidget.java:279 adds the class for styling)
  • Dialog editors: IgnoreDialog, ViewFileDialog, EditSnippetsDialog, AceEditorPreview
  • Stale source editors left in the DOM after a tab close

Any of those could shadow the real source editor and surface as an empty getValue() in reformatCode, producing the Expected: not "" timeout the 8 failing tests were all hitting.

Test plan

  • npm run test:desktop-dev -- -g "Air Formatting" -- 8/8 pass (was 0/8) in 47s, no flakes
  • npm run test:desktop-dev -- -g "Edit suggestions" -- the writeAndOpenFile path now goes through openExistingFile too, worth confirming
  • CI desktop e2e suite

Adds window.rstudio.documents.activeEditor(), which returns the native
Ace JS instance backing the active source document (or null). It maps
through SourceColumnManager.getActiveNativeEditor -- the same path the
GWT side already uses to track "the active doc" -- so callers no longer
have to scan the DOM for a .ace_editor element.

The DOM scan in getEditorContent and openExistingFile was the proximate
cause of 8 consistent air_formatting test failures: the bare .ace_editor
selector also matches ShellWidget's console scroll panel, dialog
editors (IgnoreDialog, ViewFileDialog, EditSnippetsDialog,
AceEditorPreview), and stale source editors left in the DOM after a tab
close -- any of which could shadow the real editor and surface as an
empty getValue() in reformatCode.

Refactors:
- getEditorContent() reads activeEditor()?.getValue() instead of
  iterating .ace_editor.
- New openExistingFile() helper (utils/files.ts) waits on
  documents.active().path basename match + activeEditor() != null;
  writeAndOpenFile and SourcePaneActions.createAndOpenFile both delegate
  to it.
- air_formatting.test.ts: openTestFile uses openExistingFile;
  focusEditor wraps its waitForFunction in a diagnostic catch so a
  future focus-drift failure reports the actual active element, the
  in-console / in-source-panel flags, and the source-pane ace count
  instead of an opaque predicate-timeout.
@posit-snyk-bot

posit-snyk-bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

The basename-only readiness check could match the wrong active document
when two open files share a basename (or, on case-sensitive
filesystems, differ only in case). When the caller passes an absolute
path, compare the full normalized path (case-insensitive, slashes
normalized) instead. Relative / bare-basename inputs still fall back
to basename match -- R resolves them against its own cwd, so the
absolute form RStudio reports back is not known to the helper.

Addresses roborev review #701 (MEDIUM).
@kevinushey
kevinushey merged commit ad175dc into main May 26, 2026
1 of 2 checks passed
@kevinushey
kevinushey deleted the bugfix/e2e-active-editor-bridge branch May 26, 2026 22:58
gtritchie pushed a commit that referenced this pull request May 27, 2026
getCompletionsInEditor was iterating document.querySelectorAll('.ace_editor')
and picking the first non-console match -- same bug ad175dc (#17775) fixed
for getEditorContent / openFile. On the failing tests the scan landed on a
stale empty source editor at DOM index 0 instead of the live editor at
index 1, so gotoLine + focus moved into the empty editor and Ctrl+Space
fired with nothing for the completer to anchor to.

Replace both the explicit-cursor and default-cursor branches with
window.rstudio.documents.activeEditor(), which maps through
SourceColumnManager.getActiveNativeEditor and reliably returns the
currently-active doc's editor. Also adds Session.getLength() to utils/ace.ts
so the default-cursor branch can stay typed.

Restores six autocomplete_extras.test.ts editor tests (pipe completions,
roxygen tags in .R/.Rmd/.qmd, already-used args pref, backtick-quoted
columns, _$ placeholder) and keeps the 14 autocomplete.test.ts tests
passing locally.
kevinushey added a commit that referenced this pull request May 27, 2026
empty-marker AceEditor was scanning .ace_editor DOM nodes and picking the
first non-console match -- same stale-editor bug fixed in ad175dc
(#17775) for openFile/getEditorContent and f84e454 (#17784) for
autocomplete actions. on tests with a single open file the marker '' would
trivially match, but the matched node could be a stale editor left in the
DOM after a tab close rather than the live active editor.

route empty marker through window.rstudio.documents.activeEditor() and
keep the DOM-walk path for non-empty markers (still useful when a test
needs a specific non-active tab).
kevinushey added a commit that referenced this pull request May 28, 2026
…tor (#17788)

* test(e2e): pin desktop fixture window size to 1400x900

avoids dualwindowlayoutpanel auto-snapping the left column to maximize
on the small (~1024x645) macos gh actions virtual display, which was
hiding the source pane and breaking edit_suggestions tests.

* test(e2e): route AceEditor('') through activeEditor bridge

empty-marker AceEditor was scanning .ace_editor DOM nodes and picking the
first non-console match -- same stale-editor bug fixed in ad175dc
(#17775) for openFile/getEditorContent and f84e454 (#17784) for
autocomplete actions. on tests with a single open file the marker '' would
trivially match, but the matched node could be a stale editor left in the
DOM after a tab close rather than the live active editor.

route empty marker through window.rstudio.documents.activeEditor() and
keep the DOM-walk path for non-empty markers (still useful when a test
needs a specific non-active tab).

* test(e2e): drop eval-based runOnEditor in AceEditor

the previous shape stringified the user's callback via fn.toString() and
reconstituted it in the browser with (0, eval)('(' + fnSource + ')').
that was the only way to ferry an arbitrary user function across the
node->browser boundary through page.evaluate's single-arg form.

replace it with page.evaluateHandle to resolve the editor once, then call
handle.evaluate with the user's function natively (playwright serializes
the function itself, no eval). overload run() so callers that pass no arg
stay typed; callers that need multiple args wrap in an object.

no behavior change for callers; the eval was a workaround the bridge has
since obsoleted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants