|
| 1 | +const path = require('path'); |
| 2 | +const simpleGit = require('simple-git'); |
| 3 | +const fs = require('fs').promises; |
| 4 | +const { Action } = require('../../src/proxy/actions'); |
| 5 | +const { exec: getDiff } = require('../../src/proxy/processors/push-action/getDiff'); |
| 6 | +const { exec: scanDiff } = require('../../src/proxy/processors/push-action/scanDiff'); |
| 7 | + |
| 8 | +const chai = require('chai'); |
| 9 | +const expect = chai.expect; |
| 10 | + |
| 11 | +describe('Force Push Integration Test', () => { |
| 12 | + let tempDir; |
| 13 | + let git; |
| 14 | + let initialCommitSHA; |
| 15 | + let rebasedCommitSHA; |
| 16 | + |
| 17 | + before(async function () { |
| 18 | + this.timeout(10000); // eslint-disable-line no-invalid-this |
| 19 | + |
| 20 | + tempDir = path.join(__dirname, '../temp-integration-repo'); |
| 21 | + await fs.mkdir(tempDir, { recursive: true }); |
| 22 | + git = simpleGit(tempDir); |
| 23 | + |
| 24 | + await git.init(); |
| 25 | + await git.addConfig('user.name', 'Test User'); |
| 26 | + await git.addConfig('user.email', 'test@example.com'); |
| 27 | + |
| 28 | + // Create initial commit |
| 29 | + await fs.writeFile(path.join(tempDir, 'base.txt'), 'base content'); |
| 30 | + await git.add('.'); |
| 31 | + await git.commit('Initial commit'); |
| 32 | + |
| 33 | + // Create feature commit |
| 34 | + await fs.writeFile(path.join(tempDir, 'feature.txt'), 'feature content'); |
| 35 | + await git.add('.'); |
| 36 | + await git.commit('Add feature'); |
| 37 | + |
| 38 | + const log = await git.log(); |
| 39 | + initialCommitSHA = log.latest.hash; |
| 40 | + |
| 41 | + // Simulate rebase by amending commit (changes SHA) |
| 42 | + await git.commit(['--amend', '-m', 'Add feature (rebased)']); |
| 43 | + |
| 44 | + const newLog = await git.log(); |
| 45 | + rebasedCommitSHA = newLog.latest.hash; |
| 46 | + |
| 47 | + console.log(`Initial SHA: ${initialCommitSHA}`); |
| 48 | + console.log(`Rebased SHA: ${rebasedCommitSHA}`); |
| 49 | + }); |
| 50 | + |
| 51 | + after(async () => { |
| 52 | + try { |
| 53 | + await fs.rmdir(tempDir, { recursive: true }); |
| 54 | + } catch (e) { |
| 55 | + // Ignore cleanup errors |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + describe('Complete force push pipeline', () => { |
| 60 | + it('should handle valid diff after rebase scenario', async function () { |
| 61 | + this.timeout(5000); // eslint-disable-line no-invalid-this |
| 62 | + |
| 63 | + // Create action simulating force push with valid SHAs that have actual changes |
| 64 | + const action = new Action( |
| 65 | + 'valid-diff-integration', |
| 66 | + 'push', |
| 67 | + 'POST', |
| 68 | + Date.now(), |
| 69 | + 'test/repo.git', |
| 70 | + ); |
| 71 | + action.proxyGitPath = path.dirname(tempDir); |
| 72 | + action.repoName = path.basename(tempDir); |
| 73 | + |
| 74 | + // Parent of initial commit to get actual diff content |
| 75 | + const parentSHA = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; |
| 76 | + action.commitFrom = parentSHA; |
| 77 | + action.commitTo = rebasedCommitSHA; |
| 78 | + action.commitData = [ |
| 79 | + { |
| 80 | + parent: parentSHA, |
| 81 | + commit: rebasedCommitSHA, |
| 82 | + message: 'Add feature (rebased)', |
| 83 | + author: 'Test User', |
| 84 | + }, |
| 85 | + ]; |
| 86 | + |
| 87 | + const afterGetDiff = await getDiff({}, action); |
| 88 | + expect(afterGetDiff.steps).to.have.length.greaterThan(0); |
| 89 | + |
| 90 | + const diffStep = afterGetDiff.steps.find((s) => s.stepName === 'diff'); |
| 91 | + expect(diffStep).to.exist; |
| 92 | + expect(diffStep.error).to.be.false; |
| 93 | + expect(diffStep.content).to.be.a('string'); |
| 94 | + expect(diffStep.content.length).to.be.greaterThan(0); |
| 95 | + |
| 96 | + const afterScanDiff = await scanDiff({}, afterGetDiff); |
| 97 | + const scanStep = afterScanDiff.steps.find((s) => s.stepName === 'scanDiff'); |
| 98 | + |
| 99 | + expect(scanStep).to.exist; |
| 100 | + expect(scanStep.error).to.be.false; |
| 101 | + }); |
| 102 | + |
| 103 | + it('should handle unreachable commit SHA error', async function () { |
| 104 | + this.timeout(5000); // eslint-disable-line no-invalid-this |
| 105 | + |
| 106 | + // Invalid SHA to trigger error |
| 107 | + const action = new Action( |
| 108 | + 'unreachable-sha-integration', |
| 109 | + 'push', |
| 110 | + 'POST', |
| 111 | + Date.now(), |
| 112 | + 'test/repo.git', |
| 113 | + ); |
| 114 | + action.proxyGitPath = path.dirname(tempDir); |
| 115 | + action.repoName = path.basename(tempDir); |
| 116 | + action.commitFrom = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; // Invalid SHA |
| 117 | + action.commitTo = rebasedCommitSHA; |
| 118 | + action.commitData = [ |
| 119 | + { |
| 120 | + parent: 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef', |
| 121 | + commit: rebasedCommitSHA, |
| 122 | + message: 'Add feature (rebased)', |
| 123 | + author: 'Test User', |
| 124 | + }, |
| 125 | + ]; |
| 126 | + |
| 127 | + const afterGetDiff = await getDiff({}, action); |
| 128 | + expect(afterGetDiff.steps).to.have.length.greaterThan(0); |
| 129 | + |
| 130 | + const diffStep = afterGetDiff.steps.find((s) => s.stepName === 'diff'); |
| 131 | + expect(diffStep).to.exist; |
| 132 | + expect(diffStep.error).to.be.true; |
| 133 | + expect(diffStep.errorMessage).to.be.a('string'); |
| 134 | + expect(diffStep.errorMessage.length).to.be.greaterThan(0); |
| 135 | + expect(diffStep.errorMessage).to.satisfy( |
| 136 | + (msg) => msg.includes('fatal:') && msg.includes('Invalid revision range'), |
| 137 | + 'Error message should contain git diff specific error for invalid SHA', |
| 138 | + ); |
| 139 | + |
| 140 | + // scanDiff should not block on missing diff due to error |
| 141 | + const afterScanDiff = await scanDiff({}, afterGetDiff); |
| 142 | + const scanStep = afterScanDiff.steps.find((s) => s.stepName === 'scanDiff'); |
| 143 | + |
| 144 | + expect(scanStep).to.exist; |
| 145 | + expect(scanStep.error).to.be.false; |
| 146 | + }); |
| 147 | + |
| 148 | + it('should handle missing diff step gracefully', async function () { |
| 149 | + const action = new Action( |
| 150 | + 'missing-diff-integration', |
| 151 | + 'push', |
| 152 | + 'POST', |
| 153 | + Date.now(), |
| 154 | + 'test/repo.git', |
| 155 | + ); |
| 156 | + |
| 157 | + const result = await scanDiff({}, action); |
| 158 | + |
| 159 | + expect(result.steps).to.have.length(1); |
| 160 | + expect(result.steps[0].stepName).to.equal('scanDiff'); |
| 161 | + expect(result.steps[0].error).to.be.false; |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments