|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { Memento } from 'vscode'; |
| 3 | + |
| 4 | +import { GitHubClient } from '../../common/api/ghClient'; |
| 5 | +import { GitExecutor } from '../../common/git/gitExecutor'; |
| 6 | +import { |
| 7 | + IPersistedCloneOperation, |
| 8 | + PR_CLONE_IN_PLACE_STATE_KEY, |
| 9 | + PrCloneInPlaceService, |
| 10 | +} from '../../services/prCloneInPlaceService'; |
| 11 | +import { PrCloneData } from '../../services/prCloneService'; |
| 12 | +import { GitHubPR } from '../../types/dataTypes'; |
| 13 | +import { mockLogService } from '../e2e/helpers/mockLogService'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Regression tests for issue 204: "Detached HEAD silently disables PR-clone rollback". |
| 17 | + * |
| 18 | + * When a PR clone is started from a detached HEAD, `getCurrentBranch()` returns `''`, which |
| 19 | + * used to make cleanUp() short-circuit (nothing restored, created branch/stash left behind) |
| 20 | + * and persistState() refuse to write a crash-recovery record. The fix captures the detached |
| 21 | + * commit SHA via `getHeadCommit()` and restores/persists against that instead. |
| 22 | + */ |
| 23 | + |
| 24 | +function createFakeMemento(): Memento { |
| 25 | + const store = new Map<string, unknown>(); |
| 26 | + |
| 27 | + return { |
| 28 | + get: ((key: string, defaultValue?: unknown) => |
| 29 | + store.has(key) ? store.get(key) : defaultValue) as Memento['get'], |
| 30 | + update: async (key: string, value: unknown) => { |
| 31 | + if (value === undefined) { |
| 32 | + store.delete(key); |
| 33 | + } else { |
| 34 | + store.set(key, value); |
| 35 | + } |
| 36 | + }, |
| 37 | + keys: () => [...store.keys()], |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +const prData = { |
| 42 | + number: 204, |
| 43 | + title: 'Detached HEAD PR', |
| 44 | + body: 'desc', |
| 45 | + head: { ref: 'feature/source' }, |
| 46 | + base: { ref: 'main' }, |
| 47 | + labels: [], |
| 48 | + assignees: [], |
| 49 | +} as unknown as GitHubPR; |
| 50 | + |
| 51 | +const cloneData: PrCloneData = { |
| 52 | + prData, |
| 53 | + targetBranch: 'main', |
| 54 | + featureBranch: 'feature/clone', |
| 55 | + description: 'desc', |
| 56 | + selectedCommits: ['c1'], |
| 57 | + isDraft: false, |
| 58 | +}; |
| 59 | + |
| 60 | +const DETACHED_SHA = 'abc1234deadbeefabc1234deadbeefabc1234de'; |
| 61 | + |
| 62 | +function createDetachedGitStub(repositoryPath = '/repo') { |
| 63 | + const calls = { |
| 64 | + reset: 0, |
| 65 | + checkout: [] as string[], |
| 66 | + popStash: [] as string[], |
| 67 | + deleteLocalBranch: [] as string[], |
| 68 | + cherryPickAbort: 0, |
| 69 | + isCherryPickInProgress: 0, |
| 70 | + }; |
| 71 | + |
| 72 | + const git = { |
| 73 | + repositoryPath, |
| 74 | + getCurrentBranch: async () => '', |
| 75 | + getHeadCommit: async () => DETACHED_SHA, |
| 76 | + isWorkdirHasChanges: async () => false, |
| 77 | + fetchPullRequestHead: async () => {}, |
| 78 | + checkout: async (branch: string) => { |
| 79 | + calls.checkout.push(branch); |
| 80 | + }, |
| 81 | + pullCurrentBranch: async () => {}, |
| 82 | + createUniqueFeatureBranch: async () => 'feature/clone', |
| 83 | + commitExists: async () => true, |
| 84 | + hasConflicts: async () => false, |
| 85 | + cherryPick: async () => ({ conflicts: false }), |
| 86 | + isCherryPickInProgress: async () => { |
| 87 | + calls.isCherryPickInProgress += 1; |
| 88 | + return false; |
| 89 | + }, |
| 90 | + reset: async () => { |
| 91 | + calls.reset += 1; |
| 92 | + }, |
| 93 | + popStash: async (message: string) => { |
| 94 | + calls.popStash.push(message); |
| 95 | + }, |
| 96 | + deleteLocalBranch: async (branch: string) => { |
| 97 | + calls.deleteLocalBranch.push(branch); |
| 98 | + }, |
| 99 | + cherryPickAbort: async () => { |
| 100 | + calls.cherryPickAbort += 1; |
| 101 | + }, |
| 102 | + pushBranchToGitHub: async () => {}, |
| 103 | + } as unknown as GitExecutor; |
| 104 | + |
| 105 | + return { git, calls }; |
| 106 | +} |
| 107 | + |
| 108 | +describe('PrCloneInPlaceService detached-HEAD rollback (issue 204)', () => { |
| 109 | + it('captures the HEAD SHA and persists state when started from detached HEAD', async () => { |
| 110 | + const memento = createFakeMemento(); |
| 111 | + const { git } = createDetachedGitStub(); |
| 112 | + // Stall on a conflict so we can inspect the persisted record before cleanup runs. |
| 113 | + (git as any).cherryPick = async () => ({ conflicts: true }); |
| 114 | + const service = new PrCloneInPlaceService(git, {} as GitHubClient, mockLogService, memento); |
| 115 | + |
| 116 | + await service.clonePR(cloneData); |
| 117 | + |
| 118 | + const persisted = memento.get<IPersistedCloneOperation>(PR_CLONE_IN_PLACE_STATE_KEY); |
| 119 | + assert.ok(persisted, 'a record should be persisted even though originalBranch is empty'); |
| 120 | + assert.strictEqual(persisted!.originalBranch, '', 'originalBranch stays empty for compatibility'); |
| 121 | + assert.strictEqual(persisted!.originalRef, DETACHED_SHA, 'originalRef must capture the detached SHA'); |
| 122 | + assert.strictEqual(persisted!.isDetached, true); |
| 123 | + }); |
| 124 | + |
| 125 | + it('cleanUp checks out the captured SHA (not a branch name) instead of silently returning', async () => { |
| 126 | + const memento = createFakeMemento(); |
| 127 | + const { git, calls } = createDetachedGitStub(); |
| 128 | + (git as any).cherryPick = async () => ({ conflicts: true }); |
| 129 | + const service = new PrCloneInPlaceService(git, {} as GitHubClient, mockLogService, memento); |
| 130 | + |
| 131 | + await service.clonePR(cloneData); |
| 132 | + // clonePR itself checks out the target branch and the new feature branch; only the |
| 133 | + // checkout(s) issued by cleanUp (below) are relevant to this assertion. |
| 134 | + calls.checkout.length = 0; |
| 135 | + |
| 136 | + await service.abortClonePR(); |
| 137 | + await new Promise((resolve) => setImmediate(resolve)); |
| 138 | + |
| 139 | + assert.strictEqual(calls.reset, 1, 'a started clone must still hard-reset on abort'); |
| 140 | + assert.deepStrictEqual( |
| 141 | + calls.checkout, |
| 142 | + [DETACHED_SHA], |
| 143 | + 'cleanup must restore by checking out the captured SHA, not a (nonexistent) branch name' |
| 144 | + ); |
| 145 | + assert.deepStrictEqual( |
| 146 | + calls.deleteLocalBranch, |
| 147 | + ['feature/clone'], |
| 148 | + 'the created feature branch must still be deleted on abort' |
| 149 | + ); |
| 150 | + assert.strictEqual( |
| 151 | + memento.get(PR_CLONE_IN_PLACE_STATE_KEY), |
| 152 | + undefined, |
| 153 | + 'the persisted record must be cleared after abort' |
| 154 | + ); |
| 155 | + }); |
| 156 | + |
| 157 | + it('cleanUp no longer short-circuits when originalBranch is empty but originalRef is set', async () => { |
| 158 | + const { git, calls } = createDetachedGitStub(); |
| 159 | + const service = new PrCloneInPlaceService(git, {} as GitHubClient, mockLogService); |
| 160 | + |
| 161 | + (service as any).serviceStore = { |
| 162 | + originalBranch: '', |
| 163 | + originalRef: DETACHED_SHA, |
| 164 | + isDetached: true, |
| 165 | + createdBranchName: 'feature/clone', |
| 166 | + }; |
| 167 | + |
| 168 | + await (service as any).cleanUp(true); |
| 169 | + |
| 170 | + assert.deepStrictEqual(calls.checkout, [DETACHED_SHA]); |
| 171 | + assert.deepStrictEqual(calls.deleteLocalBranch, ['feature/clone']); |
| 172 | + }); |
| 173 | +}); |
0 commit comments