|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import path from 'path' |
| 3 | +import fs from 'fs-extra' |
| 4 | +import os from 'os' |
| 5 | +import { startVSCode, SUSHI_SOURCE_PATH } from './utils' |
| 6 | + |
| 7 | +// Keyboard shortcuts |
| 8 | +const RENAME_KEY = 'F2' |
| 9 | +const FIND_ALL_REFERENCES_KEY = |
| 10 | + process.platform === 'darwin' ? 'Alt+Shift+F12' : 'Ctrl+Shift+F12' |
| 11 | + |
| 12 | +test.describe('CTE Rename', () => { |
| 13 | + let tempDir: string |
| 14 | + let window: any |
| 15 | + let close: () => Promise<void> |
| 16 | + |
| 17 | + test.beforeEach(async () => { |
| 18 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'vscode-test-sushi-')) |
| 19 | + await fs.copy(SUSHI_SOURCE_PATH, tempDir) |
| 20 | + const vscode = await startVSCode(tempDir) |
| 21 | + window = vscode.window |
| 22 | + close = vscode.close |
| 23 | + |
| 24 | + // Navigate to customers.sql which contains CTEs |
| 25 | + await window.waitForSelector('text=models') |
| 26 | + await window |
| 27 | + .getByRole('treeitem', { name: 'models', exact: true }) |
| 28 | + .locator('a') |
| 29 | + .click() |
| 30 | + await window |
| 31 | + .getByRole('treeitem', { name: 'customers.sql', exact: true }) |
| 32 | + .locator('a') |
| 33 | + .click() |
| 34 | + await window.waitForSelector('text=grain') |
| 35 | + await window.waitForSelector('text=Loaded SQLMesh Context') |
| 36 | + }) |
| 37 | + |
| 38 | + test.afterEach(async () => { |
| 39 | + await close() |
| 40 | + fs.removeSync(tempDir) |
| 41 | + }) |
| 42 | + |
| 43 | + test('Rename CTE from definition', async () => { |
| 44 | + // Click on the inner CTE definition "current_marketing" (not the outer one) |
| 45 | + await window.locator('text=WITH current_marketing AS').click({ |
| 46 | + position: { x: 100, y: 5 }, |
| 47 | + }) |
| 48 | + |
| 49 | + // Press F2 to trigger rename |
| 50 | + await window.keyboard.press(RENAME_KEY) |
| 51 | + await expect(window.locator('text=Rename')).toBeVisible() |
| 52 | + const renameInput = window.locator('input:focus') |
| 53 | + await expect(renameInput).toBeVisible() |
| 54 | + |
| 55 | + // Type new name and confirm |
| 56 | + await window.keyboard.type('new_marketing') |
| 57 | + await window.keyboard.press('Enter') |
| 58 | + await window.waitForTimeout(1000) |
| 59 | + |
| 60 | + // Verify the rename was applied |
| 61 | + await expect(window.locator('text=WITH new_marketing AS')).toBeVisible() |
| 62 | + }) |
| 63 | + |
| 64 | + test('Rename CTE from usage', async () => { |
| 65 | + // Click on CTE usage in FROM clause |
| 66 | + await window.locator('text=FROM current_marketing_outer').click({ |
| 67 | + position: { x: 80, y: 5 }, |
| 68 | + }) |
| 69 | + |
| 70 | + // Press F2 to trigger rename |
| 71 | + await window.keyboard.press(RENAME_KEY) |
| 72 | + |
| 73 | + // Wait for rename input to appear |
| 74 | + await expect(window.locator('text=Rename')).toBeVisible() |
| 75 | + const renameInput = window.locator('input:focus') |
| 76 | + await expect(renameInput).toBeVisible() |
| 77 | + |
| 78 | + // Type new name |
| 79 | + await window.keyboard.type('updated_marketing_out') |
| 80 | + |
| 81 | + // Confirm rename |
| 82 | + await window.keyboard.press('Enter') |
| 83 | + await window.waitForTimeout(1000) |
| 84 | + |
| 85 | + // Verify both definition and usage were renamed |
| 86 | + await expect( |
| 87 | + window.locator('text=WITH updated_marketing_out AS'), |
| 88 | + ).toBeVisible() |
| 89 | + await expect( |
| 90 | + window.locator('text=FROM updated_marketing_out'), |
| 91 | + ).toBeVisible() |
| 92 | + }) |
| 93 | + |
| 94 | + test('Cancel CTE rename', async () => { |
| 95 | + // Click on the CTE to rename |
| 96 | + await window.locator('text=current_marketing_outer').first().click() |
| 97 | + |
| 98 | + // Press F2 to trigger rename |
| 99 | + await window.keyboard.press(RENAME_KEY) |
| 100 | + |
| 101 | + // Wait for rename input to appear |
| 102 | + await expect(window.locator('text=Rename')).toBeVisible() |
| 103 | + const renameInput = window.locator('input:focus') |
| 104 | + await expect(renameInput).toBeVisible() |
| 105 | + |
| 106 | + // Type new name but cancel |
| 107 | + await window.keyboard.type('cancelled_name') |
| 108 | + await window.keyboard.press('Escape') |
| 109 | + |
| 110 | + // Wait for UI to update |
| 111 | + await window.waitForTimeout(500) |
| 112 | + |
| 113 | + // Verify CTE name was NOT changed |
| 114 | + await expect( |
| 115 | + window.locator('text=current_marketing_outer').first(), |
| 116 | + ).toBeVisible() |
| 117 | + await expect(window.locator('text=cancelled_name')).not.toBeVisible() |
| 118 | + }) |
| 119 | + |
| 120 | + test('Rename CTE updates all references', async () => { |
| 121 | + // Click on the CTE definition |
| 122 | + await window.locator('text=WITH current_marketing AS').click({ |
| 123 | + position: { x: 100, y: 5 }, |
| 124 | + }) |
| 125 | + |
| 126 | + // Press F2 to trigger rename |
| 127 | + await window.keyboard.press(RENAME_KEY) |
| 128 | + // Wait for rename input to appear |
| 129 | + await expect(window.locator('text=Rename')).toBeVisible() |
| 130 | + const renameInput = window.locator('input:focus') |
| 131 | + await expect(renameInput).toBeVisible() |
| 132 | + |
| 133 | + // Type new name and confirm |
| 134 | + await window.keyboard.type('renamed_cte') |
| 135 | + await window.keyboard.press('Enter') |
| 136 | + |
| 137 | + // Click on the renamed CTE |
| 138 | + await window.locator('text=WITH renamed_cte AS').click({ |
| 139 | + position: { x: 100, y: 5 }, |
| 140 | + }) |
| 141 | + |
| 142 | + // Find all references using keyboard shortcut |
| 143 | + await window.keyboard.press(FIND_ALL_REFERENCES_KEY) |
| 144 | + |
| 145 | + // Verify references panel shows all occurrences |
| 146 | + await window.waitForSelector('text=References') |
| 147 | + await expect(window.locator('text=customers.sql').first()).toBeVisible() |
| 148 | + await window.waitForSelector('text=WITH renamed_cte AS') |
| 149 | + await window.waitForSelector('text=renamed_cte.*') |
| 150 | + await window.waitForSelector('text=FROM renamed_cte') |
| 151 | + await window.waitForSelector('text=renamed_cte.customer_id != 100') |
| 152 | + }) |
| 153 | + |
| 154 | + test('Rename CTE with preview', async () => { |
| 155 | + // Click on the CTE to rename |
| 156 | + await window.locator('text=WITH current_marketing AS').click({ |
| 157 | + position: { x: 100, y: 5 }, |
| 158 | + }) |
| 159 | + |
| 160 | + // Press F2 to trigger rename |
| 161 | + await window.keyboard.press(RENAME_KEY) |
| 162 | + await expect(window.locator('text=Rename')).toBeVisible() |
| 163 | + const renameInput = window.locator('input:focus') |
| 164 | + await expect(renameInput).toBeVisible() |
| 165 | + |
| 166 | + // Type new name |
| 167 | + await window.keyboard.type('preview_marketing') |
| 168 | + |
| 169 | + // Press Cmd+Enter (Meta+Enter) to preview changes |
| 170 | + await window.keyboard.press('Meta+Enter') |
| 171 | + |
| 172 | + // Verify preview UI is showing |
| 173 | + await expect(window.locator('text=Refactor Preview').first()).toBeVisible() |
| 174 | + await expect(window.locator('text=Apply').first()).toBeVisible() |
| 175 | + await expect(window.locator('text=Discard').first()).toBeVisible() |
| 176 | + |
| 177 | + // Verify the preview shows both old and new names |
| 178 | + await expect(window.locator('text=current_marketing').first()).toBeVisible() |
| 179 | + await expect(window.locator('text=preview_marketing').first()).toBeVisible() |
| 180 | + |
| 181 | + // Apply the changes |
| 182 | + await window.locator('text=Apply').click() |
| 183 | + |
| 184 | + // Verify the rename was applied |
| 185 | + await expect(window.locator('text=WITH preview_marketing AS')).toBeVisible() |
| 186 | + }) |
| 187 | +}) |
0 commit comments