2020-08-09 18:40:47 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
|
|
|
|
const configUtils = require('../../utils/configUtils');
|
2020-08-11 12:07:00 +03:00
|
|
|
const {events} = require('../../../core/server/lib/common');
|
2020-08-09 18:40:47 +03:00
|
|
|
|
|
|
|
const bootstrapSocket = require('@tryghost/bootstrap-socket');
|
|
|
|
|
2021-02-16 18:29:40 +03:00
|
|
|
describe('Notify', function () {
|
2021-02-16 16:43:26 +03:00
|
|
|
describe('notifyServerStarted', function () {
|
2021-02-16 18:29:40 +03:00
|
|
|
let notify;
|
2020-08-09 18:40:47 +03:00
|
|
|
let socketStub;
|
2020-08-11 12:07:00 +03:00
|
|
|
let eventSpy;
|
2020-08-09 18:40:47 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
// Have to re-require each time to clear the internal flag
|
2021-02-16 18:29:40 +03:00
|
|
|
delete require.cache[require.resolve('../../../core/server/notify')];
|
|
|
|
notify = require('../../../core/server/notify');
|
2020-08-09 18:40:47 +03:00
|
|
|
|
|
|
|
// process.send isn't set for tests, we can safely override;
|
|
|
|
process.send = sinon.stub();
|
|
|
|
|
|
|
|
// stub socket connectAndSend method
|
|
|
|
socketStub = sinon.stub(bootstrapSocket, 'connectAndSend');
|
2020-08-11 12:07:00 +03:00
|
|
|
|
|
|
|
// Spy for the events that get called
|
|
|
|
eventSpy = sinon.spy(events, 'emit');
|
2020-08-09 18:40:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
process.send = undefined;
|
|
|
|
configUtils.restore();
|
|
|
|
socketStub.restore();
|
2020-08-11 12:07:00 +03:00
|
|
|
eventSpy.restore();
|
2020-08-09 18:40:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('it resolves a promise', function () {
|
2021-02-16 18:29:40 +03:00
|
|
|
notify.notifyServerStarted().should.be.fulfilled();
|
2020-08-09 18:40:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('it communicates with IPC correctly on success', function () {
|
2021-02-16 18:29:40 +03:00
|
|
|
notify.notifyServerStarted();
|
2020-08-09 18:40:47 +03:00
|
|
|
|
|
|
|
process.send.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
let message = process.send.firstCall.args[0];
|
|
|
|
message.should.be.an.Object().with.properties('started', 'debug');
|
|
|
|
message.should.not.have.property('error');
|
|
|
|
message.started.should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('communicates with IPC correctly on failure', function () {
|
2021-02-16 18:29:40 +03:00
|
|
|
notify.notifyServerStarted(new Error('something went wrong'));
|
2020-08-09 18:40:47 +03:00
|
|
|
|
|
|
|
process.send.calledOnce.should.be.true();
|
|
|
|
|
|
|
|
let message = process.send.firstCall.args[0];
|
|
|
|
message.should.be.an.Object().with.properties('started', 'debug', 'error');
|
|
|
|
message.started.should.be.false();
|
|
|
|
message.error.should.be.an.Object().with.properties('message');
|
|
|
|
message.error.message.should.eql('something went wrong');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('communicates via bootstrap socket correctly on success', function () {
|
|
|
|
configUtils.set('bootstrap-socket', 'testing');
|
|
|
|
|
2021-02-16 18:29:40 +03:00
|
|
|
notify.notifyServerStarted();
|
2020-08-09 18:40:47 +03:00
|
|
|
|
|
|
|
socketStub.calledOnce.should.be.true();
|
|
|
|
socketStub.firstCall.args[0].should.eql('testing');
|
|
|
|
socketStub.firstCall.args[1].should.be.an.Object().with.properties('info', 'warn');
|
|
|
|
|
|
|
|
let message = socketStub.firstCall.args[2];
|
|
|
|
message.should.be.an.Object().with.properties('started', 'debug');
|
|
|
|
message.should.not.have.property('error');
|
|
|
|
message.started.should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('communicates via bootstrap socket correctly on failure', function () {
|
|
|
|
configUtils.set('bootstrap-socket', 'testing');
|
|
|
|
|
2021-02-16 18:29:40 +03:00
|
|
|
notify.notifyServerStarted(new Error('something went wrong'));
|
2020-08-09 18:40:47 +03:00
|
|
|
|
|
|
|
socketStub.calledOnce.should.be.true();
|
|
|
|
socketStub.firstCall.args[0].should.eql('testing');
|
|
|
|
socketStub.firstCall.args[1].should.be.an.Object().with.properties('info', 'warn');
|
|
|
|
|
|
|
|
let message = socketStub.firstCall.args[2];
|
|
|
|
message.should.be.an.Object().with.properties('started', 'debug', 'error');
|
|
|
|
message.started.should.be.false();
|
|
|
|
message.error.should.be.an.Object().with.properties('message');
|
|
|
|
message.error.message.should.eql('something went wrong');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be called multiple times, but only communicates once', function () {
|
|
|
|
configUtils.set('bootstrap-socket', 'testing');
|
|
|
|
|
2021-02-16 18:29:40 +03:00
|
|
|
notify.notifyServerStarted();
|
|
|
|
notify.notifyServerStarted(new Error('something went wrong'));
|
|
|
|
notify.notifyServerStarted();
|
2020-08-09 18:40:47 +03:00
|
|
|
|
|
|
|
process.send.calledOnce.should.be.true();
|
|
|
|
socketStub.calledOnce.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|