|
| 1 | +import { afterAll, beforeAll, describe, expect, inject, it } from 'vitest'; |
| 2 | +import { _electron as electron, type ElectronApplication, type Page } from 'playwright-core'; |
| 3 | +import { execFileSync } from 'node:child_process'; |
| 4 | +import fs from 'node:fs/promises'; |
| 5 | +import os from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | + |
| 8 | +const ROOT = path.resolve(__dirname, '../..'); |
| 9 | +let app: ElectronApplication; |
| 10 | +let page: Page; |
| 11 | +let fixture: string; |
| 12 | +let project: string; |
| 13 | +let clean: string; |
| 14 | +const errors: string[] = []; |
| 15 | +const git = (cwd: string, ...args: string[]) => execFileSync('git', args, { cwd, stdio: 'pipe' }); |
| 16 | +const status = () => page.getByRole('region', { name: 'Workspace Git status' }); |
| 17 | +async function capture(name: string) { |
| 18 | + for (const directory of ['.tmp/proof', 'test-results/workspace-status']) { |
| 19 | + await fs.mkdir(path.join(ROOT, directory), { recursive: true }); |
| 20 | + await page.screenshot({ path: path.join(ROOT, directory, name) }); |
| 21 | + } |
| 22 | +} |
| 23 | +async function select(name: string) { |
| 24 | + await page.getByLabel('Workspace group').selectOption({ label: name }); |
| 25 | + await page.getByRole('heading', { name: 'Start a session', exact: true }).waitFor(); |
| 26 | +} |
| 27 | + |
| 28 | +describe('workspace status in Electron', () => { |
| 29 | + beforeAll(async () => { |
| 30 | + fixture = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), 'pidex-status-'))); |
| 31 | + project = path.join(fixture, 'project'); |
| 32 | + clean = path.join(fixture, 'clean'); |
| 33 | + const plain = path.join(fixture, 'plain'); |
| 34 | + const userData = path.join(fixture, 'user-data'); |
| 35 | + await Promise.all([project, clean, plain, userData].map((dir) => fs.mkdir(dir))); |
| 36 | + await fs.mkdir(path.join(fixture, 'sessions')); |
| 37 | + for (const cwd of [project, clean]) { |
| 38 | + git(cwd, 'init', '-b', 'main'); |
| 39 | + await fs.writeFile(path.join(cwd, 'app.ts'), 'export const value = 1;\n'); |
| 40 | + git(cwd, 'add', '.'); |
| 41 | + git(cwd, '-c', 'user.name=Test', '-c', '[email protected]', '-c', 'commit.gpgsign=false', 'commit', '-m', 'fixture'); |
| 42 | + } |
| 43 | + git(project, 'switch', '-c', 'feature/workspace-status'); |
| 44 | + await fs.writeFile(path.join(project, 'app.ts'), 'export const value = 2;\n'); |
| 45 | + git(project, 'add', 'app.ts'); |
| 46 | + await fs.writeFile(path.join(project, 'app.ts'), 'export const value = 3;\n'); |
| 47 | + await fs.writeFile(path.join(project, 'notes.txt'), 'Review before starting Pi.\n'); |
| 48 | + await fs.writeFile(path.join(userData, 'workspaces.json'), JSON.stringify([project, clean, plain, path.join(fixture, 'missing')])); |
| 49 | + app = await electron.launch({ |
| 50 | + executablePath: require('electron'), args: ['.'], cwd: ROOT, |
| 51 | + env: { |
| 52 | + ...process.env, NODE_ENV: 'development', PIDEX_E2E_CWD: project, |
| 53 | + PIDEX_E2E_RENDERER_URL: inject('rendererUrl'), PIDEX_E2E_USER_DATA: userData, |
| 54 | + PIDEX_SESSIONS_ROOT: path.join(fixture, 'sessions'), |
| 55 | + PIDEX_PI: path.join(ROOT, 'tests/fixtures/pi-rpc.cjs'), |
| 56 | + }, |
| 57 | + }); |
| 58 | + page = await app.firstWindow(); |
| 59 | + page.on('pageerror', (error) => errors.push(error.message)); |
| 60 | + await page.setViewportSize({ width: 1440, height: 1000 }); |
| 61 | + await page.getByRole('heading', { name: 'Start a session', exact: true }).waitFor(); |
| 62 | + await select('project'); |
| 63 | + }); |
| 64 | + afterAll(async () => { |
| 65 | + await app?.close(); |
| 66 | + if (fixture) await fs.rm(fixture, { recursive: true, force: true }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('shows mixed Git changes before starting Pi and opens Changes without losing the task draft', async () => { |
| 70 | + await expect.poll(() => status().textContent()).toContain('2 changed files'); |
| 71 | + expect(await status().textContent()).toContain('feature/workspace-status · 1 staged · 1 unstaged · 1 untracked'); |
| 72 | + await page.getByLabel('Message pi').fill('Review the existing changes before editing.'); |
| 73 | + await page.evaluate(() => document.fonts.ready); |
| 74 | + await capture('workspace-status.png'); |
| 75 | + await page.setViewportSize({ width: 760, height: 900 }); |
| 76 | + await status().scrollIntoViewIfNeeded(); |
| 77 | + const bounds = await status().boundingBox(); |
| 78 | + const button = await status().getByRole('button').boundingBox(); |
| 79 | + if (!bounds || !button) throw new Error('Workspace status is not visible.'); |
| 80 | + expect(button.x + button.width).toBeLessThanOrEqual(bounds.x + bounds.width); |
| 81 | + await capture('workspace-status-narrow.png'); |
| 82 | + await page.setViewportSize({ width: 1440, height: 1000 }); |
| 83 | + await status().getByRole('button', { name: 'Review workspace changes' }).click(); |
| 84 | + await page.getByRole('heading', { name: 'Changes review', exact: true }).waitFor(); |
| 85 | + expect(await page.getByLabel('Message pi').inputValue()).toBe('Review the existing changes before editing.'); |
| 86 | + await page.getByLabel('Commit message').waitFor(); |
| 87 | + await page.getByRole('button', { name: 'New session', exact: true }).click(); |
| 88 | + expect(await page.getByLabel('Message pi').inputValue()).toBe('Review the existing changes before editing.'); |
| 89 | + expect(await page.locator('.wb-sidebar .wb-session-item').filter({ hasText: 'this window' }).count()).toBe(0); |
| 90 | + }); |
| 91 | + |
| 92 | + it('refreshes changed counts on focus and keeps clean and detached roots distinct', async () => { |
| 93 | + await fs.unlink(path.join(project, 'notes.txt')); |
| 94 | + git(project, 'add', 'app.ts'); |
| 95 | + await page.evaluate(() => window.dispatchEvent(new Event('focus'))); |
| 96 | + await expect.poll(() => status().textContent()).toContain('1 changed file'); |
| 97 | + expect(await status().textContent()).toContain('1 staged · 0 unstaged · 0 untracked'); |
| 98 | + await select('clean'); |
| 99 | + await expect.poll(() => status().textContent()).toContain('No changes in this root'); |
| 100 | + expect(await status().textContent()).toContain('main · 0 staged · 0 unstaged · 0 untracked'); |
| 101 | + git(clean, 'checkout', '--detach'); |
| 102 | + await page.evaluate(() => window.dispatchEvent(new Event('focus'))); |
| 103 | + await expect.poll(() => status().textContent()).toContain('Detached HEAD'); |
| 104 | + expect(await status().textContent()).not.toContain('feature/workspace-status'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('does not describe non-Git folders or missing roots as clean', async () => { |
| 108 | + await select('plain'); |
| 109 | + await expect.poll(() => status().textContent()).toContain('Git changes are unavailable for this folder.'); |
| 110 | + expect(await status().getByRole('button').isDisabled()).toBe(true); |
| 111 | + await select('missing'); |
| 112 | + await expect.poll(() => status().textContent()).toContain('Restore this folder to inspect Git changes.'); |
| 113 | + expect(await status().getByRole('button').isDisabled()).toBe(true); |
| 114 | + expect(await status().textContent()).not.toContain('No changes in this root'); |
| 115 | + expect(errors).toEqual([]); |
| 116 | + }); |
| 117 | +}); |
0 commit comments