|
| 1 | +import { gotoHash, createTestRepo, closeAndCleanup, monitorErrors, waitForToast } from '../helpers.js'; |
| 2 | +import { execSync } from 'child_process'; |
| 3 | +import { writeFileSync } from 'fs'; |
| 4 | +import { join } from 'path'; |
| 5 | + |
| 6 | +describe('stash workflow', () => { |
| 7 | + let testRepo: string; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + testRepo = createTestRepo('stash'); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(async () => { |
| 14 | + await closeAndCleanup(testRepo); |
| 15 | + }); |
| 16 | + |
| 17 | + it('stashes uncommitted changes and pops them back', async () => { |
| 18 | + const assertNoErrors = monitorErrors(); |
| 19 | + |
| 20 | + const defaultBranch = execSync('git symbolic-ref --short HEAD', { cwd: testRepo }).toString().trim(); |
| 21 | + |
| 22 | + await gotoHash(`/workspace?path=${encodeURIComponent(testRepo)}`); |
| 23 | + await expect($(`[data-testid="worktree-item"][data-branch="${defaultBranch}"]`)).toBeDisplayed(); |
| 24 | + |
| 25 | + const worktreePath = join(testRepo, '.sproutgit', 'worktrees', defaultBranch); |
| 26 | + writeFileSync(join(worktreePath, 'README.md'), '# test\nlocal change\n'); |
| 27 | + |
| 28 | + // Switch to the staging tab. |
| 29 | + await $('//*[contains(@class,"sg-tab") and contains(.,"Changes")]').click(); |
| 30 | + await expect($('//*[contains(@class,"sg-file-row") and contains(.,"README.md")]')).toBeDisplayed(); |
| 31 | + |
| 32 | + // Expand the stash panel and create a stash. |
| 33 | + await $('[data-testid="btn-toggle-stash-panel"]').click(); |
| 34 | + await $('[data-testid="input-stash-message"]').setValue('wip changes'); |
| 35 | + await $('[data-testid="btn-create-stash"]').click(); |
| 36 | + await waitForToast('success'); |
| 37 | + |
| 38 | + // The working tree is now clean and one stash entry exists. |
| 39 | + await expect($('//*[contains(@class,"sg-file-row") and contains(.,"README.md")]')).not.toBeDisplayed(); |
| 40 | + await expect($('[data-testid="stash-row"]')).toBeDisplayed(); |
| 41 | + expect(await $('[data-testid="stash-row"]').getText()).toContain('wip changes'); |
| 42 | + |
| 43 | + // Pop it back. |
| 44 | + await $('[data-testid="btn-pop-stash"]').click(); |
| 45 | + await waitForToast('success'); |
| 46 | + |
| 47 | + await expect($('[data-testid="stash-row"]')).not.toBeDisplayed(); |
| 48 | + await expect($('//*[contains(@class,"sg-file-row") and contains(.,"README.md")]')).toBeDisplayed(); |
| 49 | + |
| 50 | + await assertNoErrors(); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('cherry-pick workflow', () => { |
| 55 | + let testRepo: string; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + testRepo = createTestRepo('cherry-pick'); |
| 59 | + }); |
| 60 | + |
| 61 | + afterEach(async () => { |
| 62 | + await closeAndCleanup(testRepo); |
| 63 | + }); |
| 64 | + |
| 65 | + it('cherry-picks a commit from another branch onto the active branch via the graph context menu', async () => { |
| 66 | + const assertNoErrors = monitorErrors(); |
| 67 | + |
| 68 | + const defaultBranch = execSync('git symbolic-ref --short HEAD', { cwd: testRepo }).toString().trim(); |
| 69 | + |
| 70 | + // Create a second branch with a commit to cherry-pick, then return to default. |
| 71 | + execSync('git checkout -b feature', { cwd: testRepo }); |
| 72 | + writeFileSync(join(testRepo, 'feature.txt'), 'feature content\n'); |
| 73 | + execSync('git add feature.txt', { cwd: testRepo }); |
| 74 | + execSync('git commit -m "add feature file"', { cwd: testRepo }); |
| 75 | + execSync(`git checkout ${defaultBranch}`, { cwd: testRepo }); |
| 76 | + |
| 77 | + await gotoHash(`/workspace?path=${encodeURIComponent(testRepo)}`); |
| 78 | + await expect($(`[data-testid="worktree-item"][data-branch="${defaultBranch}"]`)).toBeDisplayed(); |
| 79 | + |
| 80 | + // Right-click the "add feature file" commit row in the graph. |
| 81 | + // CDP pointer actions don't reliably generate a contextmenu event in |
| 82 | + // Electron, so dispatch it directly (same approach as the worktree |
| 83 | + // sidebar's context-menu test). |
| 84 | + await expect($('//*[contains(@class,"commit-row") and contains(.,"add feature file")]')).toBeDisplayed(); |
| 85 | + await browser.execute(() => { |
| 86 | + const rows = Array.from(document.querySelectorAll('.commit-row')); |
| 87 | + const el = rows.find(r => r.textContent?.includes('add feature file')) as HTMLElement | undefined; |
| 88 | + if (!el) throw new Error('commit row for "add feature file" not found'); |
| 89 | + const rect = el.getBoundingClientRect(); |
| 90 | + el.dispatchEvent(new MouseEvent('contextmenu', { |
| 91 | + bubbles: true, |
| 92 | + cancelable: true, |
| 93 | + button: 2, |
| 94 | + clientX: rect.left + rect.width / 2, |
| 95 | + clientY: rect.top + rect.height / 2, |
| 96 | + })); |
| 97 | + }); |
| 98 | + |
| 99 | + await expect($('[data-testid="context-menu"]')).toBeDisplayed(); |
| 100 | + await $('[data-testid="context-menu"]') |
| 101 | + .$('.//button[contains(.,"Cherry-pick")]') |
| 102 | + .click(); |
| 103 | + |
| 104 | + await waitForToast('success'); |
| 105 | + |
| 106 | + // The default branch's worktree should now contain feature.txt. |
| 107 | + const worktreePath = join(testRepo, '.sproutgit', 'worktrees', defaultBranch); |
| 108 | + const log = execSync('git log --format=%s -1', { cwd: worktreePath }).toString().trim(); |
| 109 | + expect(log).toBe('add feature file'); |
| 110 | + |
| 111 | + await assertNoErrors(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments