|
| 1 | +import { expect, IJupyterLabPageFixture, test } from '@jupyterlab/galata'; |
| 2 | + |
| 3 | +/** |
| 4 | + * The command to open the file unified diff view. |
| 5 | + */ |
| 6 | +const UNIFIED_FILE_DIFF_COMMAND = 'jupyterlab-cell-diff:unified-file-diff'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Default name for new Python files. |
| 10 | + */ |
| 11 | +const DEFAULT_NAME = 'untitled.py'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Setup a file editor with unified diff view. |
| 15 | + */ |
| 16 | +async function setupFileWithUnifiedDiff( |
| 17 | + page: IJupyterLabPageFixture, |
| 18 | + originalSource: string, |
| 19 | + newSource: string, |
| 20 | +) { |
| 21 | + await page.evaluate( |
| 22 | + async ({ originalSource, newSource, command }) => { |
| 23 | + await window.jupyterapp.commands.execute(command, { |
| 24 | + originalSource, |
| 25 | + newSource, |
| 26 | + showActionButtons: true |
| 27 | + }); |
| 28 | + }, |
| 29 | + { originalSource, newSource, command: UNIFIED_FILE_DIFF_COMMAND } |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Get the content of the current file in the editor. |
| 35 | + */ |
| 36 | +async function getFileContent(page: IJupyterLabPageFixture): Promise<string> { |
| 37 | + return await page.evaluate(() => { |
| 38 | + const widget = window.jupyterapp.shell.currentWidget; |
| 39 | + if (widget && 'content' in widget) { |
| 40 | + const editor = (widget as any).content.editor; |
| 41 | + return editor.model.sharedModel.getSource(); |
| 42 | + } |
| 43 | + return ''; |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +test.describe('Unified File Diff Extension', () => { |
| 48 | + test.beforeEach(async ({ page }) => { |
| 49 | + await page.menu.clickMenuItem('File>New>Python File'); |
| 50 | + await page.sidebar.close(); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should show unified diff with action buttons', async ({ page }) => { |
| 54 | + const originalSource = 'print("Hello, World!")'; |
| 55 | + const newSource = 'print("Hello, JupyterLab!")\nprint("Testing diffs")'; |
| 56 | + |
| 57 | + await setupFileWithUnifiedDiff(page, originalSource, newSource); |
| 58 | + |
| 59 | + const acceptButton = page.getByRole('button', { name: 'Accept All' }); |
| 60 | + await expect(acceptButton).toBeVisible(); |
| 61 | + |
| 62 | + const rejectButton = page.getByRole('button', { name: 'Reject All' }); |
| 63 | + await expect(rejectButton).toBeVisible(); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should accept all changes when accept button is clicked', async ({ |
| 67 | + page |
| 68 | + }) => { |
| 69 | + const originalSource = 'x = 1'; |
| 70 | + const newSource = 'x = 2'; |
| 71 | + |
| 72 | + await setupFileWithUnifiedDiff(page, originalSource, newSource); |
| 73 | + |
| 74 | + const acceptButton = page.getByText('Accept All'); |
| 75 | + await acceptButton.click(); |
| 76 | + |
| 77 | + const fileContent = await getFileContent(page); |
| 78 | + expect(fileContent).toBe(newSource); |
| 79 | + }); |
| 80 | + |
| 81 | + test('should reject all changes when reject button is clicked', async ({ |
| 82 | + page |
| 83 | + }) => { |
| 84 | + const originalSource = 'y = 10'; |
| 85 | + const newSource = 'y = 20'; |
| 86 | + |
| 87 | + await setupFileWithUnifiedDiff(page, originalSource, newSource); |
| 88 | + |
| 89 | + const rejectButton = page.getByText('Reject All'); |
| 90 | + await rejectButton.click(); |
| 91 | + |
| 92 | + const fileContent = await getFileContent(page); |
| 93 | + expect(fileContent).toBe(originalSource); |
| 94 | + }); |
| 95 | +}); |
0 commit comments