Skip to content

Commit ca86b71

Browse files
committed
test: add tests for checkhiddencommit
1 parent e51f22d commit ca86b71

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

test/chain.test.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const mockPushProcessors = {
2323
checkAuthorEmails: sinon.stub(),
2424
checkUserPushPermission: sinon.stub(),
2525
checkIfWaitingAuth: sinon.stub(),
26+
checkHiddenCommits: sinon.stub(),
2627
pullRemote: sinon.stub(),
2728
writePack: sinon.stub(),
2829
preReceive: sinon.stub(),
@@ -39,6 +40,7 @@ mockPushProcessors.checkCommitMessages.displayName = 'checkCommitMessages';
3940
mockPushProcessors.checkAuthorEmails.displayName = 'checkAuthorEmails';
4041
mockPushProcessors.checkUserPushPermission.displayName = 'checkUserPushPermission';
4142
mockPushProcessors.checkIfWaitingAuth.displayName = 'checkIfWaitingAuth';
43+
mockPushProcessors.checkHiddenCommits.displayName = 'checkHiddenCommits';
4244
mockPushProcessors.pullRemote.displayName = 'pullRemote';
4345
mockPushProcessors.writePack.displayName = 'writePack';
4446
mockPushProcessors.preReceive.displayName = 'preReceive';
@@ -110,7 +112,9 @@ describe('proxy chain', function () {
110112
mockPushProcessors.checkCommitMessages.resolves(continuingAction);
111113
mockPushProcessors.checkAuthorEmails.resolves(continuingAction);
112114
mockPushProcessors.checkUserPushPermission.resolves(continuingAction);
113-
115+
mockPushProcessors.checkHiddenCommits.resolves(continuingAction);
116+
mockPushProcessors.pullRemote.resolves(continuingAction);
117+
mockPushProcessors.writePack.resolves(continuingAction);
114118
// this stops the chain from further execution
115119
mockPushProcessors.checkIfWaitingAuth.resolves({
116120
type: 'push',
@@ -126,7 +130,10 @@ describe('proxy chain', function () {
126130
expect(mockPushProcessors.checkAuthorEmails.called).to.be.true;
127131
expect(mockPushProcessors.checkUserPushPermission.called).to.be.true;
128132
expect(mockPushProcessors.checkIfWaitingAuth.called).to.be.true;
129-
expect(mockPushProcessors.pullRemote.called).to.be.false;
133+
expect(mockPushProcessors.pullRemote.called).to.be.true;
134+
expect(mockPushProcessors.checkHiddenCommits.called).to.be.true;
135+
expect(mockPushProcessors.writePack.called).to.be.true;
136+
expect(mockPushProcessors.getMissingData.called).to.be.false;
130137
expect(mockPushProcessors.audit.called).to.be.true;
131138

132139
expect(result.type).to.equal('push');
@@ -143,6 +150,9 @@ describe('proxy chain', function () {
143150
mockPushProcessors.checkCommitMessages.resolves(continuingAction);
144151
mockPushProcessors.checkAuthorEmails.resolves(continuingAction);
145152
mockPushProcessors.checkUserPushPermission.resolves(continuingAction);
153+
mockPushProcessors.checkHiddenCommits.resolves(continuingAction);
154+
mockPushProcessors.pullRemote.resolves(continuingAction);
155+
mockPushProcessors.writePack.resolves(continuingAction);
146156
// this stops the chain from further execution
147157
mockPushProcessors.checkIfWaitingAuth.resolves({
148158
type: 'push',
@@ -158,7 +168,10 @@ describe('proxy chain', function () {
158168
expect(mockPushProcessors.checkAuthorEmails.called).to.be.true;
159169
expect(mockPushProcessors.checkUserPushPermission.called).to.be.true;
160170
expect(mockPushProcessors.checkIfWaitingAuth.called).to.be.true;
161-
expect(mockPushProcessors.pullRemote.called).to.be.false;
171+
expect(mockPushProcessors.pullRemote.called).to.be.true;
172+
expect(mockPushProcessors.checkHiddenCommits.called).to.be.true;
173+
expect(mockPushProcessors.writePack.called).to.be.true;
174+
expect(mockPushProcessors.getMissingData.called).to.be.false;
162175
expect(mockPushProcessors.audit.called).to.be.true;
163176

164177
expect(result.type).to.equal('push');
@@ -178,6 +191,7 @@ describe('proxy chain', function () {
178191
mockPushProcessors.checkIfWaitingAuth.resolves(continuingAction);
179192
mockPushProcessors.pullRemote.resolves(continuingAction);
180193
mockPushProcessors.writePack.resolves(continuingAction);
194+
mockPushProcessors.checkHiddenCommits.resolves(continuingAction);
181195
mockPushProcessors.preReceive.resolves(continuingAction);
182196
mockPushProcessors.getDiff.resolves(continuingAction);
183197
mockPushProcessors.clearBareClone.resolves(continuingAction);
@@ -195,6 +209,7 @@ describe('proxy chain', function () {
195209
expect(mockPushProcessors.checkUserPushPermission.called).to.be.true;
196210
expect(mockPushProcessors.checkIfWaitingAuth.called).to.be.true;
197211
expect(mockPushProcessors.pullRemote.called).to.be.true;
212+
expect(mockPushProcessors.checkHiddenCommits.called).to.be.true;
198213
expect(mockPushProcessors.writePack.called).to.be.true;
199214
expect(mockPushProcessors.preReceive.called).to.be.true;
200215
expect(mockPushProcessors.getDiff.called).to.be.true;
@@ -270,6 +285,7 @@ describe('proxy chain', function () {
270285
mockPushProcessors.checkIfWaitingAuth.resolves(action);
271286
mockPushProcessors.pullRemote.resolves(action);
272287
mockPushProcessors.writePack.resolves(action);
288+
mockPushProcessors.checkHiddenCommits.resolves(action);
273289

274290
mockPushProcessors.preReceive.resolves({
275291
...action,
@@ -316,6 +332,7 @@ describe('proxy chain', function () {
316332
mockPushProcessors.checkIfWaitingAuth.resolves(action);
317333
mockPushProcessors.pullRemote.resolves(action);
318334
mockPushProcessors.writePack.resolves(action);
335+
mockPushProcessors.checkHiddenCommits.resolves(action);
319336

320337
mockPushProcessors.preReceive.resolves({
321338
...action,
@@ -363,6 +380,7 @@ describe('proxy chain', function () {
363380
mockPushProcessors.checkIfWaitingAuth.resolves(action);
364381
mockPushProcessors.pullRemote.resolves(action);
365382
mockPushProcessors.writePack.resolves(action);
383+
mockPushProcessors.checkHiddenCommits.resolves(action);
366384

367385
mockPushProcessors.preReceive.resolves({
368386
...action,
@@ -409,6 +427,7 @@ describe('proxy chain', function () {
409427
mockPushProcessors.checkIfWaitingAuth.resolves(action);
410428
mockPushProcessors.pullRemote.resolves(action);
411429
mockPushProcessors.writePack.resolves(action);
430+
mockPushProcessors.checkHiddenCommits.resolves(action);
412431

413432
mockPushProcessors.preReceive.resolves({
414433
...action,

test/checkHiddenCommit.test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// test/checkHiddenCommits.test.js
2+
const fs = require('fs');
3+
const childProcess = require('child_process');
4+
import * as sinon from 'sinon';
5+
const { expect } = require('chai');
6+
7+
const { exec: checkHidden } = require('../src/proxy/processors/push-action/checkHiddenCommits');
8+
const { Action } = require('../src/proxy/actions');
9+
10+
describe('checkHiddenCommits.exec', () => {
11+
let action;
12+
let sandbox;
13+
let spawnSyncStub;
14+
let readdirSyncStub;
15+
16+
beforeEach(() => {
17+
sandbox = sinon.createSandbox();
18+
19+
// stub spawnSync and fs.readdirSync
20+
spawnSyncStub = sandbox.stub(childProcess, 'spawnSync');
21+
readdirSyncStub = sandbox.stub(fs, 'readdirSync');
22+
23+
// prepare a fresh Action
24+
action = new Action('some-id', 'push', 'POST', Date.now(), 'repo.git');
25+
action.proxyGitPath = '/fake';
26+
action.commitFrom = '0000000000000000000000000000000000000000';
27+
action.commitTo = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
28+
action.newIdxFiles = ['pack-test.idx'];
29+
});
30+
31+
afterEach(() => {
32+
sandbox.restore();
33+
});
34+
35+
it('reports all commits unreferenced and sets error=true', async () => {
36+
// 1) rev-list → no introduced commits
37+
// 2) verify-pack → two commits in pack
38+
spawnSyncStub.onFirstCall().returns({ stdout: '' }).onSecondCall().returns({
39+
stdout: 'deadbeef commit 100 1\ncafebabe commit 100 2\n',
40+
});
41+
42+
readdirSyncStub.returns(['pack-test.idx']);
43+
44+
await checkHidden({ body: '' }, action);
45+
46+
const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
47+
expect(step.logs).to.include('checkHiddenCommits - ❌ Unreferenced commits: 2');
48+
expect(action.error).to.be.true;
49+
});
50+
51+
it('mixes referenced & unreferenced correctly', async () => {
52+
// 1) git rev-list → introduces one commit “deadbeef”
53+
// 2) git verify-pack → the pack contains two commits
54+
spawnSyncStub.onFirstCall().returns({ stdout: 'deadbeef\n' }).onSecondCall().returns({
55+
stdout: 'deadbeef commit 100 1\ncafebabe commit 100 2\n',
56+
});
57+
58+
readdirSyncStub.returns(['pack-test.idx']);
59+
60+
await checkHidden({ body: '' }, action);
61+
62+
const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
63+
expect(step.logs).to.include('checkHiddenCommits - ✅ Referenced commits: 1');
64+
expect(step.logs).to.include('checkHiddenCommits - ❌ Unreferenced commits: 1');
65+
expect(action.error).to.be.true;
66+
});
67+
68+
it.only('reports all commits referenced and sets error=false', async () => {
69+
// 1) rev-list → introduces both commits
70+
// 2) verify-pack → the pack contains the same two commits
71+
spawnSyncStub.onFirstCall().returns({ stdout: 'deadbeef\ncafebabe\n' }).onSecondCall().returns({
72+
stdout: 'deadbeef commit 100 1\ncafebabe commit 100 2\n',
73+
});
74+
75+
readdirSyncStub.returns(['pack-test.idx']);
76+
77+
await checkHidden({ body: '' }, action);
78+
79+
const step = action.steps.find((s) => s.stepName === 'checkHiddenCommits');
80+
expect(step.logs).to.include('checkHiddenCommits - ✅ Referenced commits: 2');
81+
expect(step.logs).to.include('checkHiddenCommits - ❌ Unreferenced commits: 0');
82+
expect(action.error).to.be.false;
83+
});
84+
85+
it('throws if commitFrom or commitTo is missing', async () => {
86+
delete action.commitFrom;
87+
88+
try {
89+
await checkHidden({ body: '' }, action);
90+
throw new Error('Expected checkHidden to throw');
91+
} catch (err) {
92+
expect(err.message).to.match(/Both action.commitFrom and action.commitTo must be defined/);
93+
}
94+
});
95+
});

0 commit comments

Comments
 (0)