Skip to content

Commit 813aa61

Browse files
address comments
1 parent bb54886 commit 813aa61

1 file changed

Lines changed: 195 additions & 160 deletions

File tree

vscode/extension/tests/rename_cte.spec.ts

Lines changed: 195 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -9,179 +9,214 @@ const RENAME_KEY = 'F2'
99
const FIND_ALL_REFERENCES_KEY =
1010
process.platform === 'darwin' ? 'Alt+Shift+F12' : 'Ctrl+Shift+F12'
1111

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-
})
12+
// Helper function to set up a test environment
13+
async function setupTestEnvironment() {
14+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'vscode-test-sushi-'))
15+
await fs.copy(SUSHI_SOURCE_PATH, tempDir)
16+
const { window, close } = await startVSCode(tempDir)
17+
18+
// Navigate to customers.sql which contains CTEs
19+
await window.waitForSelector('text=models')
20+
await window
21+
.getByRole('treeitem', { name: 'models', exact: true })
22+
.locator('a')
23+
.click()
24+
await window
25+
.getByRole('treeitem', { name: 'customers.sql', exact: true })
26+
.locator('a')
27+
.click()
28+
await window.waitForSelector('text=grain')
29+
await window.waitForSelector('text=Loaded SQLMesh Context')
30+
31+
return { window, close, tempDir }
32+
}
4233

34+
test.describe('CTE Rename', () => {
4335
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()
36+
const { window, close, tempDir } = await setupTestEnvironment()
37+
38+
try {
39+
// Click on the inner CTE definition "current_marketing" (not the outer one)
40+
await window.locator('text=WITH current_marketing AS').click({
41+
position: { x: 100, y: 5 },
42+
})
43+
44+
// Press F2 to trigger rename
45+
await window.keyboard.press(RENAME_KEY)
46+
await expect(window.locator('text=Rename')).toBeVisible()
47+
const renameInput = window.locator('input:focus')
48+
await expect(renameInput).toBeVisible()
49+
50+
// Type new name and confirm
51+
await window.keyboard.type('new_marketing')
52+
await window.keyboard.press('Enter')
53+
await window.waitForTimeout(1000)
54+
55+
// Verify the rename was applied
56+
await expect(window.locator('text=WITH new_marketing AS')).toBeVisible()
57+
} finally {
58+
await close()
59+
await fs.remove(tempDir)
60+
}
6261
})
6362

6463
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()
64+
const { window, close, tempDir } = await setupTestEnvironment()
65+
66+
try {
67+
// Click on CTE usage in FROM clause
68+
await window.locator('text=FROM current_marketing_outer').click({
69+
position: { x: 80, y: 5 },
70+
})
71+
72+
// Press F2 to trigger rename
73+
await window.keyboard.press(RENAME_KEY)
74+
75+
// Wait for rename input to appear
76+
await expect(window.locator('text=Rename')).toBeVisible()
77+
const renameInput = window.locator('input:focus')
78+
await expect(renameInput).toBeVisible()
79+
80+
// Type new name
81+
await window.keyboard.type('updated_marketing_out')
82+
83+
// Confirm rename
84+
await window.keyboard.press('Enter')
85+
await window.waitForTimeout(1000)
86+
87+
// Verify both definition and usage were renamed
88+
await expect(
89+
window.locator('text=WITH updated_marketing_out AS'),
90+
).toBeVisible()
91+
await expect(
92+
window.locator('text=FROM updated_marketing_out'),
93+
).toBeVisible()
94+
} finally {
95+
await close()
96+
await fs.remove(tempDir)
97+
}
9298
})
9399

94100
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()
101+
const { window, close, tempDir } = await setupTestEnvironment()
102+
103+
try {
104+
// Click on the CTE to rename
105+
await window.locator('text=current_marketing_outer').first().click()
106+
107+
// Press F2 to trigger rename
108+
await window.keyboard.press(RENAME_KEY)
109+
110+
// Wait for rename input to appear
111+
await expect(window.locator('text=Rename')).toBeVisible()
112+
const renameInput = window.locator('input:focus')
113+
await expect(renameInput).toBeVisible()
114+
115+
// Type new name but cancel
116+
await window.keyboard.type('cancelled_name')
117+
await window.keyboard.press('Escape')
118+
119+
// Wait for UI to update
120+
await window.waitForTimeout(500)
121+
122+
// Verify CTE name was NOT changed
123+
await expect(
124+
window.locator('text=current_marketing_outer').first(),
125+
).toBeVisible()
126+
await expect(window.locator('text=cancelled_name')).not.toBeVisible()
127+
} finally {
128+
await close()
129+
await fs.remove(tempDir)
130+
}
118131
})
119132

120133
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')
134+
const { window, close, tempDir } = await setupTestEnvironment()
135+
136+
try {
137+
// Click on the CTE definition
138+
await window.locator('text=WITH current_marketing AS').click({
139+
position: { x: 100, y: 5 },
140+
})
141+
142+
// Press F2 to trigger rename
143+
await window.keyboard.press(RENAME_KEY)
144+
// Wait for rename input to appear
145+
await expect(window.locator('text=Rename')).toBeVisible()
146+
const renameInput = window.locator('input:focus')
147+
await expect(renameInput).toBeVisible()
148+
149+
// Type new name and confirm
150+
await window.keyboard.type('renamed_cte')
151+
await window.keyboard.press('Enter')
152+
153+
// Click on the renamed CTE
154+
await window.locator('text=WITH renamed_cte AS').click({
155+
position: { x: 100, y: 5 },
156+
})
157+
158+
// Find all references using keyboard shortcut
159+
await window.keyboard.press(FIND_ALL_REFERENCES_KEY)
160+
161+
// Verify references panel shows all occurrences
162+
await window.waitForSelector('text=References')
163+
await expect(window.locator('text=customers.sql').first()).toBeVisible()
164+
await window.waitForSelector('text=WITH renamed_cte AS')
165+
await window.waitForSelector('text=renamed_cte.*')
166+
await window.waitForSelector('text=FROM renamed_cte')
167+
await window.waitForSelector('text=renamed_cte.customer_id != 100')
168+
} finally {
169+
await close()
170+
await fs.remove(tempDir)
171+
}
152172
})
153173

154174
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()
175+
const { window, close, tempDir } = await setupTestEnvironment()
176+
177+
try {
178+
// Click on the CTE to rename
179+
await window.locator('text=WITH current_marketing AS').click({
180+
position: { x: 100, y: 5 },
181+
})
182+
183+
// Press F2 to trigger rename
184+
await window.keyboard.press(RENAME_KEY)
185+
await expect(window.locator('text=Rename')).toBeVisible()
186+
const renameInput = window.locator('input:focus')
187+
await expect(renameInput).toBeVisible()
188+
189+
// Type new name
190+
await window.keyboard.type('preview_marketing')
191+
192+
// Press Cmd+Enter (Meta+Enter) to preview changes
193+
await window.keyboard.press('Meta+Enter')
194+
195+
// Verify preview UI is showing
196+
await expect(
197+
window.locator('text=Refactor Preview').first(),
198+
).toBeVisible()
199+
await expect(window.locator('text=Apply').first()).toBeVisible()
200+
await expect(window.locator('text=Discard').first()).toBeVisible()
201+
202+
// Verify the preview shows both old and new names
203+
await expect(
204+
window.locator('text=current_marketing').first(),
205+
).toBeVisible()
206+
await expect(
207+
window.locator('text=preview_marketing').first(),
208+
).toBeVisible()
209+
210+
// Apply the changes
211+
await window.locator('text=Apply').click()
212+
213+
// Verify the rename was applied
214+
await expect(
215+
window.locator('text=WITH preview_marketing AS'),
216+
).toBeVisible()
217+
} finally {
218+
await close()
219+
await fs.remove(tempDir)
220+
}
186221
})
187222
})

0 commit comments

Comments
 (0)