Skip to content

Commit

Permalink
Add tests for cht-conf-worker
Browse files Browse the repository at this point in the history
  • Loading branch information
kennsippell committed Dec 10, 2024
1 parent 5b21425 commit c6fd56a
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/worker/cht-conf-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class ChtConfWorker {
private static async shouldPostpone(jobData: ChtConfJobData): Promise<PostponeReason | undefined> {
try {
const { instanceUrl } = jobData;
const response = await axios.get(`${instanceUrl}/api/v2/monitoring`);
const response = await ChtConfWorker.fetchMonitoringApi(instanceUrl);
const sentinelBacklog = response.data.sentinel?.backlog;
console.log(`Sentinel backlog at ${sentinelBacklog} of ${this.MAX_SENTINEL_BACKLOG}`);

Expand All @@ -105,6 +105,10 @@ export class ChtConfWorker {
}
}

private static fetchMonitoringApi(instanceUrl: string) {
return axios.get(`${instanceUrl}/api/v2/monitoring`);
}

private static async moveContact(job: Job): Promise<JobResult> {
try {
const jobData: ChtConfJobData = job.data;
Expand Down
124 changes: 124 additions & 0 deletions test/worker/cht-conf-worker.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import Chai from 'chai';
import Sinon from 'sinon';
import chaiAsPromised from 'chai-as-promised';
Chai.use(chaiAsPromised);

import Auth from '../../src/lib/authentication';
import { ChtConfJobData, ChtConfWorker } from '../../src/worker/cht-conf-worker';
import { HierarchyAction } from '../../src/lib/manage-hierarchy';
import { mockChtSession } from '../mocks';
import { Job } from 'bullmq';

const { expect } = Chai;

const mockMonitoringResponse = (backlog) => ({ data: { sentinel: { backlog } } });
describe('cht-conf-worker', () => {
let executeStub, monitoringStub;
beforeEach(() => {
executeStub = Sinon.stub().resolves(true);
monitoringStub = Sinon.stub().resolves(mockMonitoringResponse(10));
ChtConfWorker.fetchMonitoringApi = monitoringStub;
ChtConfWorker.executeCommand = executeStub;

const now = new Date('1-1-2000');
Sinon.useFakeTimers(now);
});

afterEach(() => {
Sinon.restore();
});

describe('handleJob', () => {
it('move contacts', async () => {
const job = mockJob('move');
await ChtConfWorker.handleJob(job);

expect(executeStub.calledOnce).to.be.true;
expect(executeStub.args[0][1]).to.deep.eq([
'--url=https://hostname',
'--session-token=session-token',
'--force',
'move-contacts',
'upload-docs',
'--',
'--contacts=sourceId',
'--parent=destinationId',
]);
});

it('merge contacts', async () => {
const job = mockJob('merge')
await ChtConfWorker.handleJob(job);

expect(executeStub.calledOnce).to.be.true;
expect(executeStub.args[0][1]).to.deep.eq([
'--url=https://hostname',
'--session-token=session-token',
'--force',
'merge-contacts',
'upload-docs',
'--',
'--sources=sourceId',
'--destination=destinationId',
'--merge-primary-contacts',
'--disable-users',
]);
});

it('delete contacts', async () => {
const job = mockJob('delete');
await ChtConfWorker.handleJob(job);

expect(executeStub.calledOnce).to.be.true;
expect(executeStub.args[0][1]).to.deep.eq([
'--url=https://hostname',
'--session-token=session-token',
'--force',
'delete-contacts',
'upload-docs',
'--',
'--ids=sourceId',
'--disable-users',
]);
});

it('log and throw when execution fails', async () => {
executeStub.rejects('failure');
const job = mockJob('delete');
const actual = ChtConfWorker.handleJob(job);
await expect(actual).to.eventually.be.rejectedWith('following error: failure');

expect(job.log.calledOnce).to.be.true;
expect(job.log.args[0][0]).to.include('following error: failure');
});

it('sentinel backlog causes postpone', async () => {
monitoringStub.resolves(mockMonitoringResponse(10000));
const job = mockJob('move');
const actual = ChtConfWorker.handleJob(job);
await expect(actual).to.eventually.be.rejectedWith();
expect(executeStub.callCount).to.eq(0);

expect(job.log.calledOnce).to.be.true;
expect(job.log.args[0][0]).to.include('backlog too high');
expect(job.log.args[0][0]).to.include('until 4:00 AM');
});
});
});

function mockJob(action: HierarchyAction) {
const session = mockChtSession();

return {
log: Sinon.stub(),
moveToDelayed: Sinon.stub(),
data: {
sourceId: 'sourceId',
destinationId: 'destinationId',
action,
sessionToken: Auth.encodeTokenForWorker(session),
instanceUrl: 'https://hostname'
}
};
}

0 comments on commit c6fd56a

Please sign in to comment.