|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { mkdtempSync, mkdirSync, rmSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { spawn, type ChildProcess } from 'node:child_process'; |
| 6 | + |
| 7 | +const HOST = '127.0.0.1'; |
| 8 | +const SERVER_PORT = 43175; |
| 9 | +const WEB_PORT = 53175; |
| 10 | +const BACKEND_HTTP_URL = `http://${HOST}:${SERVER_PORT}`; |
| 11 | +const BASE_URL = `http://${HOST}:${WEB_PORT}`; |
| 12 | +const NEW_BRANCH_NAME = 'feature/e2e-create-branch'; |
| 13 | + |
| 14 | +let sandboxDir: string; |
| 15 | +let dbPath: string; |
| 16 | +let runtimeDir: string; |
| 17 | +let workspacesRoot: string; |
| 18 | +let backendProcess: ChildProcess | undefined; |
| 19 | +let webProcess: ChildProcess | undefined; |
| 20 | + |
| 21 | +const startProcess = ( |
| 22 | + command: string, |
| 23 | + args: string[], |
| 24 | + options: { |
| 25 | + cwd: string; |
| 26 | + env?: NodeJS.ProcessEnv; |
| 27 | + } |
| 28 | +): ChildProcess => { |
| 29 | + const child = spawn(command, args, { |
| 30 | + cwd: options.cwd, |
| 31 | + env: { |
| 32 | + ...process.env, |
| 33 | + ...options.env, |
| 34 | + }, |
| 35 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 36 | + }); |
| 37 | + |
| 38 | + child.stdout?.on('data', () => {}); |
| 39 | + child.stderr?.on('data', () => {}); |
| 40 | + child.on('error', (error) => { |
| 41 | + throw error; |
| 42 | + }); |
| 43 | + |
| 44 | + return child; |
| 45 | +}; |
| 46 | + |
| 47 | +const waitForHttp = async (url: string, timeoutMs = 30000): Promise<void> => { |
| 48 | + const start = Date.now(); |
| 49 | + |
| 50 | + while (Date.now() - start < timeoutMs) { |
| 51 | + try { |
| 52 | + const response = await fetch(url); |
| 53 | + if (response.ok) { |
| 54 | + return; |
| 55 | + } |
| 56 | + } catch { |
| 57 | + // keep polling |
| 58 | + } |
| 59 | + |
| 60 | + await new Promise((resolve) => setTimeout(resolve, 250)); |
| 61 | + } |
| 62 | + |
| 63 | + throw new Error(`Timed out waiting for ${url}`); |
| 64 | +}; |
| 65 | + |
| 66 | +test.describe('git branch switching acceptance', () => { |
| 67 | + test.beforeAll(async () => { |
| 68 | + sandboxDir = mkdtempSync(join(tmpdir(), 'coder-studio-branch-switcher-e2e-')); |
| 69 | + dbPath = join(sandboxDir, 'coder-studio.db'); |
| 70 | + runtimeDir = join(sandboxDir, 'runtime'); |
| 71 | + workspacesRoot = join(sandboxDir, 'workspaces'); |
| 72 | + |
| 73 | + mkdirSync(runtimeDir, { recursive: true }); |
| 74 | + mkdirSync(workspacesRoot, { recursive: true }); |
| 75 | + |
| 76 | + const seed = spawn('pnpm', [ |
| 77 | + 'exec', |
| 78 | + 'tsx', |
| 79 | + 'e2e/fixtures/seed-git-branch-switching-db.ts', |
| 80 | + dbPath, |
| 81 | + workspacesRoot, |
| 82 | + ], { |
| 83 | + cwd: '/home/spencer/workspace/coder-studio', |
| 84 | + env: process.env, |
| 85 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 86 | + }); |
| 87 | + |
| 88 | + await new Promise<void>((resolve, reject) => { |
| 89 | + let stderr = ''; |
| 90 | + seed.stderr?.on('data', (chunk) => { |
| 91 | + stderr += chunk.toString(); |
| 92 | + }); |
| 93 | + seed.on('exit', (code) => { |
| 94 | + if (code === 0) { |
| 95 | + resolve(); |
| 96 | + return; |
| 97 | + } |
| 98 | + reject(new Error(stderr || `seed exited with code ${code}`)); |
| 99 | + }); |
| 100 | + seed.on('error', reject); |
| 101 | + }); |
| 102 | + |
| 103 | + backendProcess = startProcess('pnpm', ['exec', 'tsx', 'packages/server/src/server.ts'], { |
| 104 | + cwd: '/home/spencer/workspace/coder-studio', |
| 105 | + env: { |
| 106 | + HOST, |
| 107 | + PORT: String(SERVER_PORT), |
| 108 | + DATA_DIR: dbPath, |
| 109 | + RUNTIME_DIR: runtimeDir, |
| 110 | + NO_AUTH: 'true', |
| 111 | + }, |
| 112 | + }); |
| 113 | + |
| 114 | + await waitForHttp(`${BACKEND_HTTP_URL}/healthz`); |
| 115 | + |
| 116 | + webProcess = startProcess('pnpm', ['exec', 'vite', '--host', HOST, '--port', String(WEB_PORT)], { |
| 117 | + cwd: '/home/spencer/workspace/coder-studio/packages/web', |
| 118 | + env: { |
| 119 | + VITE_BACKEND_HTTP_URL: BACKEND_HTTP_URL, |
| 120 | + VITE_BACKEND_WS_URL: `ws://${HOST}:${SERVER_PORT}/ws`, |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + await waitForHttp(`${BASE_URL}/`); |
| 125 | + }); |
| 126 | + |
| 127 | + test.afterAll(async () => { |
| 128 | + const kill = async (child: ChildProcess | undefined) => { |
| 129 | + if (!child || child.killed) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + child.kill('SIGTERM'); |
| 134 | + await new Promise((resolve) => child.once('exit', resolve)); |
| 135 | + }; |
| 136 | + |
| 137 | + await kill(webProcess); |
| 138 | + await kill(backendProcess); |
| 139 | + rmSync(sandboxDir, { recursive: true, force: true }); |
| 140 | + }); |
| 141 | + |
| 142 | + test.use({ |
| 143 | + baseURL: BASE_URL, |
| 144 | + }); |
| 145 | + |
| 146 | + test('creates a new branch only after explicit confirmation from the branch quick pick', async ({ page }) => { |
| 147 | + await page.goto('/workspace'); |
| 148 | + await expect(page.getByTestId('workspace-resolving-shell')).toHaveCount(0, { timeout: 20000 }); |
| 149 | + |
| 150 | + const branchButton = page.getByRole('button', { |
| 151 | + name: 'Open branch switcher for main', |
| 152 | + }); |
| 153 | + await expect(branchButton).toBeVisible({ timeout: 20000 }); |
| 154 | + |
| 155 | + await branchButton.click(); |
| 156 | + |
| 157 | + await expect(page.getByRole('button', { name: 'Git Diff' })).toHaveClass(/active/); |
| 158 | + await expect(page.locator('.branch-quick-pick-overlay')).toBeVisible(); |
| 159 | + await expect( |
| 160 | + page.locator('.branch-quick-pick-item').filter({ hasText: 'main' }) |
| 161 | + ).toBeVisible(); |
| 162 | + |
| 163 | + const input = page.getByPlaceholder('Search branches or create new branch...'); |
| 164 | + await input.fill(NEW_BRANCH_NAME); |
| 165 | + |
| 166 | + await expect(page.getByText(`Create branch: ${NEW_BRANCH_NAME}`)).toBeVisible(); |
| 167 | + await input.press('Enter'); |
| 168 | + |
| 169 | + await expect(page.getByText(`Confirm create branch: ${NEW_BRANCH_NAME}`)).toBeVisible(); |
| 170 | + await expect(branchButton).toBeVisible(); |
| 171 | + |
| 172 | + await input.press('Enter'); |
| 173 | + |
| 174 | + await expect(page.locator('.branch-quick-pick-overlay')).toHaveCount(0); |
| 175 | + await expect( |
| 176 | + page.getByRole('button', { |
| 177 | + name: `Open branch switcher for ${NEW_BRANCH_NAME}`, |
| 178 | + }) |
| 179 | + ).toBeVisible({ timeout: 20000 }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments