|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | +import path from 'path' |
| 3 | +import fs from 'fs-extra' |
| 4 | +import os from 'os' |
| 5 | +import { exec } from 'child_process' |
| 6 | +import { promisify } from 'util' |
| 7 | +import { REPO_ROOT, startVSCode, SUSHI_SOURCE_PATH } from './utils' |
| 8 | + |
| 9 | +const execAsync = promisify(exec) |
| 10 | + |
| 11 | +/** |
| 12 | + * Helper function to create and set up a Python virtual environment |
| 13 | + */ |
| 14 | +async function setupPythonEnvironment(envDir: string): Promise<string> { |
| 15 | + // Create virtual environment |
| 16 | + const pythonCmd = process.platform === 'win32' ? 'python' : 'python3' |
| 17 | + const { stderr } = await execAsync(`${pythonCmd} -m venv "${envDir}"`) |
| 18 | + if (stderr && !stderr.includes('WARNING')) { |
| 19 | + throw new Error(`Failed to create venv: ${stderr}`) |
| 20 | + } |
| 21 | + |
| 22 | + // Get paths |
| 23 | + const isWindows = process.platform === 'win32' |
| 24 | + const binDir = path.join(envDir, isWindows ? 'Scripts' : 'bin') |
| 25 | + const pythonPath = path.join(binDir, isWindows ? 'python.exe' : 'python') |
| 26 | + const pipPath = path.join(binDir, isWindows ? 'pip.exe' : 'pip') |
| 27 | + |
| 28 | + // Install the mock tcloud package |
| 29 | + const mockTcloudPath = path.join(__dirname, 'tcloud') |
| 30 | + const { stderr: pipErr1 } = await execAsync( |
| 31 | + `"${pipPath}" install -e "${mockTcloudPath}"`, |
| 32 | + ) |
| 33 | + if (pipErr1 && !pipErr1.includes('WARNING') && !pipErr1.includes('notice')) { |
| 34 | + throw new Error(`Failed to install mock tcloud: ${pipErr1}`) |
| 35 | + } |
| 36 | + |
| 37 | + // Install sqlmesh from the local repository with LSP support |
| 38 | + const sqlmeshRepoPath = path.join(__dirname, '..', '..', '..') // Navigate to repo root from tests dir |
| 39 | + const { stderr: pipErr2 } = await execAsync( |
| 40 | + `"${pipPath}" install -e "${sqlmeshRepoPath}[lsp,bigquery]" "${REPO_ROOT}/examples/custom_materializations"`, |
| 41 | + ) |
| 42 | + if (pipErr2 && !pipErr2.includes('WARNING') && !pipErr2.includes('notice')) { |
| 43 | + throw new Error(`Failed to install sqlmesh: ${pipErr2}`) |
| 44 | + } |
| 45 | + |
| 46 | + return pythonPath |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Helper function to set up a pre-authenticated tcloud state |
| 51 | + */ |
| 52 | +async function setupAuthenticatedState(tempDir: string): Promise<void> { |
| 53 | + const authStateFile = path.join(tempDir, '.tcloud_auth_state.json') |
| 54 | + const authState = { |
| 55 | + is_logged_in: true, |
| 56 | + id_token: { |
| 57 | + iss: 'https://mock.tobikodata.com', |
| 58 | + aud: 'mock-audience', |
| 59 | + sub: 'user-123', |
| 60 | + scope: 'openid email profile', |
| 61 | + iat: Math.floor(Date.now() / 1000), |
| 62 | + exp: Math.floor(Date.now() / 1000) + 3600, // Valid for 1 hour |
| 63 | + email: 'test@example.com', |
| 64 | + name: 'Test User', |
| 65 | + }, |
| 66 | + } |
| 67 | + await fs.writeJson(authStateFile, authState) |
| 68 | +} |
| 69 | + |
| 70 | +test.describe('Tcloud', () => { |
| 71 | + test('not signed in, shows sign in window', async ({}, testInfo) => { |
| 72 | + testInfo.setTimeout(120_000) // 2 minutes for venv creation and package installation |
| 73 | + const tempDir = await fs.mkdtemp( |
| 74 | + path.join(os.tmpdir(), 'vscode-test-tcloud-'), |
| 75 | + ) |
| 76 | + const pythonEnvDir = path.join(tempDir, '.venv') |
| 77 | + |
| 78 | + try { |
| 79 | + // Copy sushi project |
| 80 | + await fs.copy(SUSHI_SOURCE_PATH, tempDir) |
| 81 | + |
| 82 | + // Create a tcloud.yaml to mark this as a tcloud project |
| 83 | + const tcloudConfig = { |
| 84 | + url: 'https://mock.tobikodata.com', |
| 85 | + org: 'test-org', |
| 86 | + project: 'test-project', |
| 87 | + } |
| 88 | + await fs.writeFile( |
| 89 | + path.join(tempDir, 'tcloud.yaml'), |
| 90 | + `url: ${tcloudConfig.url}\norg: ${tcloudConfig.org}\nproject: ${tcloudConfig.project}\n`, |
| 91 | + ) |
| 92 | + |
| 93 | + // Set up Python environment with mock tcloud and sqlmesh |
| 94 | + const pythonPath = await setupPythonEnvironment(pythonEnvDir) |
| 95 | + |
| 96 | + // Configure VS Code settings to use our Python environment |
| 97 | + const settings = { |
| 98 | + 'python.defaultInterpreterPath': pythonPath, |
| 99 | + 'sqlmesh.environmentPath': pythonEnvDir, |
| 100 | + } |
| 101 | + await fs.ensureDir(path.join(tempDir, '.vscode')) |
| 102 | + await fs.writeJson( |
| 103 | + path.join(tempDir, '.vscode', 'settings.json'), |
| 104 | + settings, |
| 105 | + { spaces: 2 }, |
| 106 | + ) |
| 107 | + |
| 108 | + // Start VS Code |
| 109 | + const { window, close } = await startVSCode(tempDir) |
| 110 | + |
| 111 | + // Open a SQL file to trigger SQLMesh activation |
| 112 | + // Wait for the models folder to be visible |
| 113 | + await window.waitForSelector('text=models') |
| 114 | + |
| 115 | + // Click on the models folder |
| 116 | + await window |
| 117 | + .getByRole('treeitem', { name: 'models', exact: true }) |
| 118 | + .locator('a') |
| 119 | + .click() |
| 120 | + |
| 121 | + // Open the top_waiters model |
| 122 | + await window |
| 123 | + .getByRole('treeitem', { name: 'customers.sql', exact: true }) |
| 124 | + .locator('a') |
| 125 | + .click() |
| 126 | + |
| 127 | + // Wait for the file to open |
| 128 | + await window.waitForTimeout(2000) |
| 129 | + |
| 130 | + await window.waitForSelector( |
| 131 | + 'text=Please sign in to Tobiko Cloud to use SQLMesh', |
| 132 | + ) |
| 133 | + |
| 134 | + // Close VS Code |
| 135 | + await close() |
| 136 | + } finally { |
| 137 | + // Clean up |
| 138 | + await fs.remove(tempDir) |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + test('signed in and not installed shows installation window and can see output', async ({}, testInfo) => { |
| 143 | + testInfo.setTimeout(120_000) // 2 minutes for venv creation and package installation |
| 144 | + const tempDir = await fs.mkdtemp( |
| 145 | + path.join(os.tmpdir(), 'vscode-test-tcloud-'), |
| 146 | + ) |
| 147 | + const pythonEnvDir = path.join(tempDir, '.venv') |
| 148 | + |
| 149 | + try { |
| 150 | + // Copy sushi project |
| 151 | + await fs.copy(SUSHI_SOURCE_PATH, tempDir) |
| 152 | + |
| 153 | + // Create a tcloud.yaml to mark this as a tcloud project |
| 154 | + const tcloudConfig = { |
| 155 | + url: 'https://mock.tobikodata.com', |
| 156 | + org: 'test-org', |
| 157 | + project: 'test-project', |
| 158 | + } |
| 159 | + await fs.writeFile( |
| 160 | + path.join(tempDir, 'tcloud.yaml'), |
| 161 | + `url: ${tcloudConfig.url}\norg: ${tcloudConfig.org}\nproject: ${tcloudConfig.project}\n`, |
| 162 | + ) |
| 163 | + |
| 164 | + // Write mock ".tcloud_auth_state.json" file |
| 165 | + await setupAuthenticatedState(tempDir) |
| 166 | + |
| 167 | + // Set up Python environment with mock tcloud and sqlmesh |
| 168 | + const pythonPath = await setupPythonEnvironment(pythonEnvDir) |
| 169 | + |
| 170 | + // Configure VS Code settings to use our Python environment |
| 171 | + const settings = { |
| 172 | + 'python.defaultInterpreterPath': pythonPath, |
| 173 | + 'sqlmesh.environmentPath': pythonEnvDir, |
| 174 | + } |
| 175 | + await fs.ensureDir(path.join(tempDir, '.vscode')) |
| 176 | + await fs.writeJson( |
| 177 | + path.join(tempDir, '.vscode', 'settings.json'), |
| 178 | + settings, |
| 179 | + { spaces: 2 }, |
| 180 | + ) |
| 181 | + |
| 182 | + // Start VS Code |
| 183 | + const { window, close } = await startVSCode(tempDir) |
| 184 | + |
| 185 | + // Open a SQL file to trigger SQLMesh activation |
| 186 | + // Wait for the models folder to be visible |
| 187 | + await window.waitForSelector('text=models') |
| 188 | + |
| 189 | + // Click on the models folder |
| 190 | + await window |
| 191 | + .getByRole('treeitem', { name: 'models', exact: true }) |
| 192 | + .locator('a') |
| 193 | + .click() |
| 194 | + |
| 195 | + // Open the top_waiters model |
| 196 | + await window |
| 197 | + .getByRole('treeitem', { name: 'customers.sql', exact: true }) |
| 198 | + .locator('a') |
| 199 | + .click() |
| 200 | + |
| 201 | + await window.waitForSelector('text=Installing enterprise python package') |
| 202 | + expect( |
| 203 | + await window.locator('text=Installing enterprise python package'), |
| 204 | + ).toHaveCount(2) |
| 205 | + |
| 206 | + await window.waitForSelector('text=Loaded SQLMesh context') |
| 207 | + |
| 208 | + // Close VS Code |
| 209 | + await close() |
| 210 | + } finally { |
| 211 | + // Clean up |
| 212 | + await fs.remove(tempDir) |
| 213 | + } |
| 214 | + }) |
| 215 | +}) |
0 commit comments