|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (C) 2025 Posit Software, PBC. All rights reserved. |
| 3 | + * Licensed under the Elastic License 2.0. See LICENSE.txt for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { join } from 'path'; |
| 7 | +import * as fs from 'fs'; |
| 8 | +import { test, expect, tags } from '../_test.setup'; |
| 9 | + |
| 10 | +test.use({ |
| 11 | + suiteId: __filename |
| 12 | +}); |
| 13 | + |
| 14 | +test.describe('Reticulate - F1 Help', { |
| 15 | + tag: [tags.RETICULATE, tags.WEB, tags.HELP, tags.SOFT_FAIL], |
| 16 | +}, () => { |
| 17 | + let reticulateVersionSupported = false; |
| 18 | + |
| 19 | + test.beforeAll(async function ({ app, sessions, logger }) { |
| 20 | + try { |
| 21 | + // Start R session and check reticulate version |
| 22 | + const { console, variables } = app.workbench; |
| 23 | + await sessions.start('r'); |
| 24 | + |
| 25 | + await console.pasteCodeToConsole('supported <- packageVersion("reticulate") >= "1.44.1.9000"', true); |
| 26 | + await console.waitForExecutionComplete(); |
| 27 | + |
| 28 | + try { |
| 29 | + await variables.expectVariableToBe('supported', 'TRUE'); |
| 30 | + reticulateVersionSupported = true; |
| 31 | + } catch (e) { |
| 32 | + logger.log('Reticulate version < 1.44.1.9000 does not support F1 help for Python objects. Tests will be skipped.'); |
| 33 | + reticulateVersionSupported = false; |
| 34 | + } |
| 35 | + } catch (e) { |
| 36 | + await app.code.driver.takeScreenshot('reticulateF1HelpSetup'); |
| 37 | + throw e; |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + test.afterEach(async function ({ app, cleanup }) { |
| 42 | + await app.workbench.quickaccess.runCommand('workbench.action.closeAllEditors'); |
| 43 | + await app.workbench.quickaccess.runCommand('workbench.action.toggleAuxiliaryBar'); |
| 44 | + // Clear both the console input and output |
| 45 | + await app.workbench.console.clearInput(); |
| 46 | + await app.workbench.console.clearButton.click(); |
| 47 | + await app.workbench.quickaccess.runCommand('workbench.action.toggleAuxiliaryBar'); |
| 48 | + await cleanup.removeTestFiles(['reticulate-help-test.R']); |
| 49 | + }); |
| 50 | + |
| 51 | + test('R - Verify F1 help for reticulate Python object method in console', async function ({ app, page }) { |
| 52 | + test.skip(!reticulateVersionSupported, 'Reticulate version < 1.44.1.9000'); |
| 53 | + |
| 54 | + const { console } = app.workbench; |
| 55 | + |
| 56 | + // Import numpy via reticulate (R session already started in beforeAll) |
| 57 | + await console.pasteCodeToConsole('np <- reticulate::import("numpy")', true); |
| 58 | + await console.waitForExecutionComplete(); |
| 59 | + |
| 60 | + // Type code that uses numpy in the console (without executing) |
| 61 | + await console.pasteCodeToConsole('np$sum(c(1, 2, 3))'); |
| 62 | + |
| 63 | + // Double-click on 'sum' to select it |
| 64 | + await console.doubleClickConsoleText('sum'); |
| 65 | + |
| 66 | + // Press F1 to trigger help |
| 67 | + await page.keyboard.press('F1'); |
| 68 | + |
| 69 | + // Verify numpy.sum help appears |
| 70 | + const helpFrame = await app.workbench.help.getHelpFrame(0); |
| 71 | + await expect(helpFrame.locator('body')).toContainText('numpy.sum', { timeout: 30000 }); |
| 72 | + }); |
| 73 | + |
| 74 | + test('R - Verify F1 help for reticulate Python object method in editor', async function ({ app, page }) { |
| 75 | + test.skip(!reticulateVersionSupported, 'Reticulate version < 1.44.1.9000'); |
| 76 | + |
| 77 | + const { console } = app.workbench; |
| 78 | + |
| 79 | + // Import numpy via reticulate (R session already started in beforeAll) |
| 80 | + await console.pasteCodeToConsole('np <- reticulate::import("numpy")', true); |
| 81 | + await console.waitForExecutionComplete(); |
| 82 | + |
| 83 | + // Create a test R file with reticulate code |
| 84 | + // Using np$mean() instead of np$sum() to ensure the help page changes from the first test |
| 85 | + const testFilePath = join(app.workspacePathOrFolder, 'reticulate-help-test.R'); |
| 86 | + const testFileContent = `# Test file for reticulate F1 help |
| 87 | +np <- reticulate::import("numpy") |
| 88 | +np$mean(c(1, 2, 3)) |
| 89 | +`; |
| 90 | + fs.writeFileSync(testFilePath, testFileContent); |
| 91 | + |
| 92 | + // Open the test file |
| 93 | + await app.workbench.quickaccess.openFile(testFilePath); |
| 94 | + |
| 95 | + // Wait for the file to be fully loaded and the language server to be ready |
| 96 | + await page.waitForTimeout(1000); |
| 97 | + |
| 98 | + // Find and double-click on 'mean' within the np$mean expression |
| 99 | + // The editor should have 'mean' as a separate token after the '$' operator |
| 100 | + const editorContent = app.code.driver.page.locator('.monaco-editor .view-lines'); |
| 101 | + const meanSpan = editorContent.locator('span').filter({ hasText: /^mean$/ }).first(); |
| 102 | + await meanSpan.dblclick(); |
| 103 | + |
| 104 | + // Press F1 to trigger help |
| 105 | + await page.keyboard.press('F1'); |
| 106 | + |
| 107 | + // Verify numpy.mean help appears |
| 108 | + const helpFrame = await app.workbench.help.getHelpFrame(0); |
| 109 | + await expect(helpFrame.locator('body')).toContainText('numpy.mean', { timeout: 30000 }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments