Skip to content

e2e-test: add r debug test #7753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions test/e2e/tests/_test.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,12 @@ export const test = base.extend<TestFixtures & CurrentsFixtures, WorkerFixtures

// ex: await executeCode('Python', 'print("Hello, world!")');
executeCode: async ({ app }, use) => {
await use(async (language: 'Python' | 'R', code: string) => {
await app.workbench.console.executeCode(language, code);
await use(async (language: 'Python' | 'R', code: string, options?: {
timeout?: number;
waitForReady?: boolean;
maximizeConsole?: boolean;
}) => {
await app.workbench.console.executeCode(language, code, options);
});
},

Expand Down Expand Up @@ -470,7 +474,11 @@ interface TestFixtures {
openDataFile: (filePath: string) => Promise<void>;
openFolder: (folderPath: string) => Promise<void>;
runCommand: (command: string, options?: { keepOpen?: boolean; exactMatch?: boolean }) => Promise<void>;
executeCode: (language: 'Python' | 'R', code: string) => Promise<void>;
executeCode: (language: 'Python' | 'R', code: string, options?: {
timeout?: number;
waitForReady?: boolean;
maximizeConsole?: boolean;
}) => Promise<void>;
hotKeys: HotKeys;
cleanup: TestTeardown;
}
Expand Down
126 changes: 126 additions & 0 deletions test/e2e/tests/debug/r-debug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2024 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/

import { test, tags, expect } from '../_test.setup';

test.use({
suiteId: __filename
});

test.describe('R Debugging', {
tag: [tags.DEBUG, tags.WEB, tags.WIN]
}, () => {

test('R - Verify manual break point and debugging with `browser()`',
async ({ r, page, executeCode }) => {
await executeCode('R', scriptWithBrowser);

// Call the function and hit the breakpoint
await executeCode('R', 'fruit_avg(dat, "berry")', { waitForReady: false });
await expect(page.getByText('Browse[1]>')).toBeVisible();

// Verify debug variables section is visible
await expect(page.getByRole('button', { name: 'Debug Variables Section' })).toBeVisible();
await expect(page.getByLabel('pattern, value "berry"')).toBeVisible();
await expect(page.getByLabel('dat, value dat')).toBeVisible();

// Verify the call stack section is visible
await expect(page.getByRole('button', { name: 'Call Stack Section' })).toBeVisible();
const debugCallStack = page.locator('.debug-call-stack');
await expect(debugCallStack.getByText('fruit_avg()fruit_avg()2:')).toBeVisible();
await expect(debugCallStack.getByText('<global>fruit_avg(dat, "berry")')).toBeVisible();

// Inspect the pattern variable
await page.keyboard.type('pattern');
await page.keyboard.press('Enter');
await expect(page.getByText('[1] "berry"')).toBeVisible();

// Inspect the structure of dat
await page.keyboard.type('names(dat)');
await page.keyboard.press('Enter');
await expect(page.getByText('[1] "blackberry" "blueberry" "peach" "plum"')).toBeVisible({ timeout: 30000 });

// Step to the next line with 'n'
await page.keyboard.type('n');
await page.keyboard.press('Enter');
await expect(page.getByText('debug at #3: cols <- grep(pattern, names(dat))')).toBeVisible();

// Finally, continue
await page.keyboard.type('c');
await page.keyboard.press('Enter');

// Confirm expected message appears
await expect(page.getByText('Found 2 fruits!')).toBeVisible();
});

test('R - Verify interactive recovery mode using `options(error = recover)`', async ({ r, page, app, executeCode }) => {
// Set up the R environment and define the function with an intentional bug
await executeCode('R', script);

// Enable interactive recovery mode for errors
await executeCode('R', 'options(error = recover)');

// Trigger an error by passing a pattern that matches only one column
// This causes `rowMeans(mini_dat)` to fail since it's not a matrix
await executeCode('R', 'fruit_avg(dat, "black")', { waitForReady: false });

// Verify the interactive recovery prompt is displayed
await expect(page.getByText('Enter a frame number, or 0 to exit')).toBeVisible();
await expect(page.getByText('1: fruit_avg(dat, "black")')).toBeVisible();

// Select the first frame in the call stack (inside the function)
await page.keyboard.type('1');
await page.keyboard.press('Enter');

// Validate the error message
await expect(page.locator('.activity-error-message'))
.toContainText("'x' must be an array of at least two dimensions");

// Inspect a local variable in the selected frame
await app.workbench.console.focus();
await page.keyboard.type('mini_dat');
await page.keyboard.press('Enter');

// Confirm that `mini_dat` contains the expected values (only blackberry column)
await expect(page.getByText('[1] 4 9 6')).toBeVisible();

// Exit recovery mode
await page.keyboard.type('Q');
await page.keyboard.press('Enter');
await app.workbench.console.waitForReady('>');
});
});


const scriptWithBrowser = `dat <- data.frame(
blackberry = c(4, 9, 6),
blueberry = c(1, 2, 8),
peach = c(59, 150, 10),
plum = c(30, 78, 5)
)
rownames(dat) <- c("calories", "weight", "yumminess")

fruit_avg <- function(dat, pattern) {
browser()
cols <- grep(pattern, names(dat))
mini_dat <- dat[ , cols]
message("Found ", ncol(mini_dat), " fruits!")
rowMeans(mini_dat)
}`;

const script = `dat <- data.frame(
blackberry = c(4, 9, 6),
blueberry = c(1, 2, 8),
peach = c(59, 150, 10),
plum = c(30, 78, 5)
)
rownames(dat) <- c("calories", "weight", "yumminess")

fruit_avg <- function(dat, pattern) {
cols <- grep(pattern, names(dat))
mini_dat <- dat[ , cols]
message("Found ", ncol(mini_dat), " fruits!")
rowMeans(mini_dat)
}`;
Loading