Skip to content

Commit 989e866

Browse files
committed
Merge branch '1313-config-race-condition-c' into 1313-config-race-condition-b
2 parents 4f56a3d + 9b1d2df commit 989e866

15 files changed

+151
-146
lines changed

src/db/file/pushes.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import fs from 'fs';
21
import _ from 'lodash';
32
import Datastore from '@seald-io/nedb';
43
import { Action } from '../../proxy/actions/Action';
54
import { toClass } from '../helper';
65
import { PushQuery } from '../types';
6+
import { initializeFolders } from './helper';
77

88
const COMPACTION_INTERVAL = 1000 * 60 * 60 * 24; // once per day
99

1010
// these don't get coverage in tests as they have already been run once before the test
1111
/* istanbul ignore if */
12-
if (!fs.existsSync('./.data')) fs.mkdirSync('./.data');
13-
/* istanbul ignore if */
14-
if (!fs.existsSync('./.data/db')) fs.mkdirSync('./.data/db');
1512

1613
// export for testing purposes
1714
export let db: Datastore;

src/proxy/actions/Action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,4 @@ class Action {
151151
}
152152
}
153153

154-
export { Action };
154+
export { Action, CommitData };

test/1.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
import { describe, it, beforeAll, afterAll, beforeEach, afterEach, expect, vi } from 'vitest';
1212
import request from 'supertest';
13-
import service from '../src/service';
13+
import { Service } from '../src/service';
1414
import * as db from '../src/db';
15-
import Proxy from '../src/proxy';
15+
import { Proxy } from '../src/proxy';
1616

1717
// Create constants for values used in multiple tests
1818
const TEST_REPO = {
@@ -29,7 +29,7 @@ describe('init', () => {
2929
beforeAll(async function () {
3030
// Starts the service and returns the express app
3131
const proxy = new Proxy();
32-
app = await service.start(proxy);
32+
app = await Service.start(proxy);
3333
});
3434

3535
// Runs before each test
@@ -52,7 +52,7 @@ describe('init', () => {
5252
// Runs after all tests
5353
afterAll(function () {
5454
// Must close the server to avoid EADDRINUSE errors when running tests in parallel
55-
service.httpServer.close();
55+
Service.httpServer.close();
5656
});
5757

5858
// Example test: check server is running

test/ConfigLoader.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, it, beforeEach, afterEach, afterAll, expect, vi } from 'vitest';
22
import fs from 'fs';
33
import path from 'path';
4-
import { configFile } from '../src/config/file';
4+
import { getConfigFile } from '../src/config/file';
55
import {
66
ConfigLoader,
77
isValidGitUrl,
@@ -39,6 +39,7 @@ describe('ConfigLoader', () => {
3939

4040
afterAll(async () => {
4141
// reset config to default after all tests have run
42+
const configFile = getConfigFile();
4243
console.log(`Restoring config to defaults from file ${configFile}`);
4344
configLoader = new ConfigLoader({});
4445
await configLoader.loadFromFile({

test/processors/checkAuthorEmails.test.ts

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { exec } from '../../src/proxy/processors/push-action/checkAuthorEmails';
33
import { Action } from '../../src/proxy/actions';
44
import * as configModule from '../../src/config';
55
import * as validator from 'validator';
6-
import { Commit } from '../../src/proxy/actions/Action';
6+
import { CommitData } from '../../src/proxy/actions/Action';
77

88
// mock dependencies
99
vi.mock('../../src/config', async (importOriginal) => {
@@ -66,8 +66,8 @@ describe('checkAuthorEmails', () => {
6666
describe('basic email validation', () => {
6767
it('should allow valid email addresses', async () => {
6868
mockAction.commitData = [
69-
{ authorEmail: '[email protected]' } as Commit,
70-
{ authorEmail: '[email protected]' } as Commit,
69+
{ authorEmail: '[email protected]' } as CommitData,
70+
{ authorEmail: '[email protected]' } as CommitData,
7171
];
7272

7373
const result = await exec(mockReq, mockAction);
@@ -78,7 +78,7 @@ describe('checkAuthorEmails', () => {
7878
});
7979

8080
it('should reject empty email', async () => {
81-
mockAction.commitData = [{ authorEmail: '' } as Commit];
81+
mockAction.commitData = [{ authorEmail: '' } as CommitData];
8282

8383
const result = await exec(mockReq, mockAction);
8484

@@ -88,7 +88,7 @@ describe('checkAuthorEmails', () => {
8888

8989
it('should reject null/undefined email', async () => {
9090
vi.mocked(validator.isEmail).mockReturnValue(false);
91-
mockAction.commitData = [{ authorEmail: null as any } as Commit];
91+
mockAction.commitData = [{ authorEmail: null as any } as CommitData];
9292

9393
const result = await exec(mockReq, mockAction);
9494

@@ -99,9 +99,9 @@ describe('checkAuthorEmails', () => {
9999
it('should reject invalid email format', async () => {
100100
vi.mocked(validator.isEmail).mockReturnValue(false);
101101
mockAction.commitData = [
102-
{ authorEmail: 'not-an-email' } as Commit,
103-
{ authorEmail: 'missing@domain' } as Commit,
104-
{ authorEmail: '@nodomain.com' } as Commit,
102+
{ authorEmail: 'not-an-email' } as CommitData,
103+
{ authorEmail: 'missing@domain' } as CommitData,
104+
{ authorEmail: '@nodomain.com' } as CommitData,
105105
];
106106

107107
const result = await exec(mockReq, mockAction);
@@ -127,8 +127,8 @@ describe('checkAuthorEmails', () => {
127127
} as any);
128128

129129
mockAction.commitData = [
130-
{ authorEmail: '[email protected]' } as Commit,
131-
{ authorEmail: '[email protected]' } as Commit,
130+
{ authorEmail: '[email protected]' } as CommitData,
131+
{ authorEmail: '[email protected]' } as CommitData,
132132
];
133133

134134
const result = await exec(mockReq, mockAction);
@@ -152,8 +152,8 @@ describe('checkAuthorEmails', () => {
152152
} as any);
153153

154154
mockAction.commitData = [
155-
{ authorEmail: '[email protected]' } as Commit,
156-
{ authorEmail: '[email protected]' } as Commit,
155+
{ authorEmail: '[email protected]' } as CommitData,
156+
{ authorEmail: '[email protected]' } as CommitData,
157157
];
158158

159159
const result = await exec(mockReq, mockAction);
@@ -177,8 +177,8 @@ describe('checkAuthorEmails', () => {
177177
} as any);
178178

179179
mockAction.commitData = [
180-
{ authorEmail: '[email protected]' } as Commit,
181-
{ authorEmail: '[email protected]' } as Commit,
180+
{ authorEmail: '[email protected]' } as CommitData,
181+
{ authorEmail: '[email protected]' } as CommitData,
182182
];
183183

184184
const result = await exec(mockReq, mockAction);
@@ -203,8 +203,8 @@ describe('checkAuthorEmails', () => {
203203
} as any);
204204

205205
mockAction.commitData = [
206-
{ authorEmail: '[email protected]' } as Commit,
207-
{ authorEmail: '[email protected]' } as Commit,
206+
{ authorEmail: '[email protected]' } as CommitData,
207+
{ authorEmail: '[email protected]' } as CommitData,
208208
];
209209

210210
const result = await exec(mockReq, mockAction);
@@ -230,8 +230,8 @@ describe('checkAuthorEmails', () => {
230230
} as any);
231231

232232
mockAction.commitData = [
233-
{ authorEmail: '[email protected]' } as Commit,
234-
{ authorEmail: '[email protected]' } as Commit,
233+
{ authorEmail: '[email protected]' } as CommitData,
234+
{ authorEmail: '[email protected]' } as CommitData,
235235
];
236236

237237
const result = await exec(mockReq, mockAction);
@@ -255,8 +255,8 @@ describe('checkAuthorEmails', () => {
255255
} as any);
256256

257257
mockAction.commitData = [
258-
{ authorEmail: '[email protected]' } as Commit,
259-
{ authorEmail: '[email protected]' } as Commit,
258+
{ authorEmail: '[email protected]' } as CommitData,
259+
{ authorEmail: '[email protected]' } as CommitData,
260260
];
261261

262262
const result = await exec(mockReq, mockAction);
@@ -280,9 +280,9 @@ describe('checkAuthorEmails', () => {
280280
} as any);
281281

282282
mockAction.commitData = [
283-
{ authorEmail: '[email protected]' } as Commit,
284-
{ authorEmail: '[email protected]' } as Commit,
285-
{ authorEmail: '[email protected]' } as Commit,
283+
{ authorEmail: '[email protected]' } as CommitData,
284+
{ authorEmail: '[email protected]' } as CommitData,
285+
{ authorEmail: '[email protected]' } as CommitData,
286286
];
287287

288288
const result = await exec(mockReq, mockAction);
@@ -306,8 +306,8 @@ describe('checkAuthorEmails', () => {
306306
} as any);
307307

308308
mockAction.commitData = [
309-
{ authorEmail: '[email protected]' } as Commit,
310-
{ authorEmail: '[email protected]' } as Commit,
309+
{ authorEmail: '[email protected]' } as CommitData,
310+
{ authorEmail: '[email protected]' } as CommitData,
311311
];
312312

313313
const result = await exec(mockReq, mockAction);
@@ -333,9 +333,9 @@ describe('checkAuthorEmails', () => {
333333
} as any);
334334

335335
mockAction.commitData = [
336-
{ authorEmail: '[email protected]' } as Commit, // valid
337-
{ authorEmail: '[email protected]' } as Commit, // invalid: blocked local
338-
{ authorEmail: '[email protected]' } as Commit, // invalid: wrong domain
336+
{ authorEmail: '[email protected]' } as CommitData, // valid
337+
{ authorEmail: '[email protected]' } as CommitData, // invalid: blocked local
338+
{ authorEmail: '[email protected]' } as CommitData, // invalid: wrong domain
339339
];
340340

341341
const result = await exec(mockReq, mockAction);
@@ -348,7 +348,7 @@ describe('checkAuthorEmails', () => {
348348

349349
describe('exec function behavior', () => {
350350
it('should create a step with name "checkAuthorEmails"', async () => {
351-
mockAction.commitData = [{ authorEmail: '[email protected]' } as Commit];
351+
mockAction.commitData = [{ authorEmail: '[email protected]' } as CommitData];
352352

353353
await exec(mockReq, mockAction);
354354

@@ -361,11 +361,11 @@ describe('checkAuthorEmails', () => {
361361

362362
it('should handle unique author emails correctly', async () => {
363363
mockAction.commitData = [
364-
{ authorEmail: '[email protected]' } as Commit,
365-
{ authorEmail: '[email protected]' } as Commit,
366-
{ authorEmail: '[email protected]' } as Commit, // Duplicate
367-
{ authorEmail: '[email protected]' } as Commit,
368-
{ authorEmail: '[email protected]' } as Commit, // Duplicate
364+
{ authorEmail: '[email protected]' } as CommitData,
365+
{ authorEmail: '[email protected]' } as CommitData,
366+
{ authorEmail: '[email protected]' } as CommitData, // Duplicate
367+
{ authorEmail: '[email protected]' } as CommitData,
368+
{ authorEmail: '[email protected]' } as CommitData, // Duplicate
369369
];
370370

371371
await exec(mockReq, mockAction);
@@ -395,15 +395,15 @@ describe('checkAuthorEmails', () => {
395395

396396
it('should log error message when illegal emails found', async () => {
397397
vi.mocked(validator.isEmail).mockReturnValue(false);
398-
mockAction.commitData = [{ authorEmail: 'invalid-email' } as Commit];
398+
mockAction.commitData = [{ authorEmail: 'invalid-email' } as CommitData];
399399

400400
await exec(mockReq, mockAction);
401401
});
402402

403403
it('should log success message when all emails are legal', async () => {
404404
mockAction.commitData = [
405-
{ authorEmail: '[email protected]' } as Commit,
406-
{ authorEmail: '[email protected]' } as Commit,
405+
{ authorEmail: '[email protected]' } as CommitData,
406+
{ authorEmail: '[email protected]' } as CommitData,
407407
];
408408

409409
await exec(mockReq, mockAction);
@@ -415,7 +415,7 @@ describe('checkAuthorEmails', () => {
415415

416416
it('should set error on step when illegal emails found', async () => {
417417
vi.mocked(validator.isEmail).mockReturnValue(false);
418-
mockAction.commitData = [{ authorEmail: 'bad@email' } as Commit];
418+
mockAction.commitData = [{ authorEmail: 'bad@email' } as CommitData];
419419

420420
await exec(mockReq, mockAction);
421421

@@ -425,7 +425,7 @@ describe('checkAuthorEmails', () => {
425425

426426
it('should call step.setError with user-friendly message', async () => {
427427
vi.mocked(validator.isEmail).mockReturnValue(false);
428-
mockAction.commitData = [{ authorEmail: 'bad' } as Commit];
428+
mockAction.commitData = [{ authorEmail: 'bad' } as CommitData];
429429

430430
await exec(mockReq, mockAction);
431431

@@ -437,7 +437,7 @@ describe('checkAuthorEmails', () => {
437437
});
438438

439439
it('should return the action object', async () => {
440-
mockAction.commitData = [{ authorEmail: '[email protected]' } as Commit];
440+
mockAction.commitData = [{ authorEmail: '[email protected]' } as CommitData];
441441

442442
const result = await exec(mockReq, mockAction);
443443

@@ -446,9 +446,9 @@ describe('checkAuthorEmails', () => {
446446

447447
it('should handle mixed valid and invalid emails', async () => {
448448
mockAction.commitData = [
449-
{ authorEmail: '[email protected]' } as Commit,
450-
{ authorEmail: 'invalid' } as Commit,
451-
{ authorEmail: '[email protected]' } as Commit,
449+
{ authorEmail: '[email protected]' } as CommitData,
450+
{ authorEmail: 'invalid' } as CommitData,
451+
{ authorEmail: '[email protected]' } as CommitData,
452452
];
453453

454454
vi.mocked(validator.isEmail).mockImplementation((email: string) => {
@@ -471,7 +471,7 @@ describe('checkAuthorEmails', () => {
471471
describe('edge cases', () => {
472472
it('should handle email with multiple @ symbols', async () => {
473473
vi.mocked(validator.isEmail).mockReturnValue(false);
474-
mockAction.commitData = [{ authorEmail: 'user@@example.com' } as Commit];
474+
mockAction.commitData = [{ authorEmail: 'user@@example.com' } as CommitData];
475475

476476
const result = await exec(mockReq, mockAction);
477477

@@ -481,7 +481,7 @@ describe('checkAuthorEmails', () => {
481481

482482
it('should handle email without domain', async () => {
483483
vi.mocked(validator.isEmail).mockReturnValue(false);
484-
mockAction.commitData = [{ authorEmail: 'user@' } as Commit];
484+
mockAction.commitData = [{ authorEmail: 'user@' } as CommitData];
485485

486486
const result = await exec(mockReq, mockAction);
487487

@@ -492,7 +492,7 @@ describe('checkAuthorEmails', () => {
492492
it('should handle very long email addresses', async () => {
493493
const longLocal = 'a'.repeat(64);
494494
const longEmail = `${longLocal}@example.com`;
495-
mockAction.commitData = [{ authorEmail: longEmail } as Commit];
495+
mockAction.commitData = [{ authorEmail: longEmail } as CommitData];
496496

497497
const result = await exec(mockReq, mockAction);
498498

@@ -501,9 +501,9 @@ describe('checkAuthorEmails', () => {
501501

502502
it('should handle special characters in local part', async () => {
503503
mockAction.commitData = [
504-
{ authorEmail: '[email protected]' } as Commit,
505-
{ authorEmail: '[email protected]' } as Commit,
506-
{ authorEmail: '[email protected]' } as Commit,
504+
{ authorEmail: '[email protected]' } as CommitData,
505+
{ authorEmail: '[email protected]' } as CommitData,
506+
{ authorEmail: '[email protected]' } as CommitData,
507507
];
508508

509509
const result = await exec(mockReq, mockAction);
@@ -527,8 +527,8 @@ describe('checkAuthorEmails', () => {
527527
} as any);
528528

529529
mockAction.commitData = [
530-
{ authorEmail: '[email protected]' } as Commit,
531-
{ authorEmail: '[email protected]' } as Commit,
530+
{ authorEmail: '[email protected]' } as CommitData,
531+
{ authorEmail: '[email protected]' } as CommitData,
532532
];
533533

534534
const result = await exec(mockReq, mockAction);

0 commit comments

Comments
 (0)