Skip to content

Commit

Permalink
feat: Introduce alternative notifications mode settings
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jul 16, 2021
1 parent 84c9599 commit d3ffc7a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 57 deletions.
19 changes: 14 additions & 5 deletions process-backend-notification-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ const logError = (message) => {
process.stdout.write(`Notifications error: ${message}\n`);
};

const NOTIFICATIONS_MODE_OFF = '0';
const NOTIFICATIONS_MODE_ONLY_OUTDATED_VERSION = '1';
const NOTIFICATIONS_MODE_ON = '2';
const NOTIFICATIONS_MODE_FORCE = '3';
const NOTIFICATIONS_MODE_OFF = 'off';
const NOTIFICATIONS_MODE_ONLY_OUTDATED_VERSION = 'upgrades-only';
const NOTIFICATIONS_MODE_ON = 'on';
const NOTIFICATIONS_MODE_FORCE = 'force';

const oldNotationMap = [
NOTIFICATIONS_MODE_OFF,
NOTIFICATIONS_MODE_ONLY_OUTDATED_VERSION,
NOTIFICATIONS_MODE_ON,
NOTIFICATIONS_MODE_FORCE,
];

const ALLOWED_NOTIFICATIONS_MODES = new Set([
NOTIFICATIONS_MODE_ON,
Expand All @@ -27,7 +34,9 @@ const ALLOWED_NOTIFICATIONS_MODES = new Set([
]);

const getNotificationsMode = () => {
const modeFromEnv = process.env.SLS_NOTIFICATIONS_MODE;
const modeFromEnv =
oldNotationMap[Number(process.env.SLS_NOTIFICATIONS_MODE)] ||
process.env.SLS_NOTIFICATIONS_MODE;

if (modeFromEnv && ALLOWED_NOTIFICATIONS_MODES.has(modeFromEnv)) return modeFromEnv;

Expand Down
124 changes: 72 additions & 52 deletions test/process-backend-notification-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,57 +90,77 @@ describe('process-backend-notification-request', () => {
expect((await processTargetNotifications(fixture)).code).to.equal('CODE0C');
});

it('Should ignore all notifications if SLS_NOTIFICATIONS_MODE set to 0', async () => {
let notification;
await overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: '0' } }, async () => {
notification = await processTargetNotifications([
{ code: 'CODE123', message: 'Some notification #1' },
{ code: 'CODE456', message: 'Some notification #2' },
]);
});

expect(notification).to.be.null;
});

it('Should only consider outdated version notifications if SLS_NOTIFICATIONS_MODE set to 1', async () => {
let notification;
await overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: '1' } }, async () => {
notification = await processTargetNotifications([
{ code: 'CODE456', message: 'Some notification' },
{ code: 'OUTDATED_MINOR_VERSION', message: 'outdated' },
]);
});

expect(notification.code).to.equal('OUTDATED_MINOR_VERSION');
});

it('Should consider all notifications if SLS_NOTIFICATIONS_MODE set to 2', async () => {
let notification;
await overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: '2' } }, async () => {
notification = await processTargetNotifications([
{ code: 'CODE123', message: 'Some notification #1' },
{ code: 'CODE456', message: 'Some notification #2' },
]);
});

expect(notification.code).to.equal('CODE123');
});

it('Should force not shown or oldest shown with SLS_NOTIFICATIONS_MODE set to 3', async () => {
await overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: '3' } }, async () => {
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE24');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE12');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE6');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0A');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0B');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0C');

expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE24');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE12');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE6');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0A');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0B');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0C');
});
describe('Notifications mode', () => {
const suite = (map) => {
it(`Should ignore all notifications if SLS_NOTIFICATIONS_MODE set to ${map(
'off'
)}`, async () => {
let notification;
await overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: map('off') } }, async () => {
notification = await processTargetNotifications([
{ code: 'CODE123', message: 'Some notification #1' },
{ code: 'CODE456', message: 'Some notification #2' },
]);
});

expect(notification).to.be.null;
});

it(`Should only consider outdated version notifications if SLS_NOTIFICATIONS_MODE set to ${map(
'upgrades-only'
)}`, async () => {
let notification;
await overrideEnv(
{ variables: { SLS_NOTIFICATIONS_MODE: map('upgrades-only') } },
async () => {
notification = await processTargetNotifications([
{ code: 'CODE456', message: 'Some notification' },
{ code: 'OUTDATED_MINOR_VERSION', message: 'outdated' },
]);
}
);

expect(notification.code).to.equal('OUTDATED_MINOR_VERSION');
});

it(`Should consider all notifications if SLS_NOTIFICATIONS_MODE set to ${map(
'on'
)}`, async () => {
let notification;
await overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: map('on') } }, async () => {
notification = await processTargetNotifications([
{ code: 'CODE123', message: 'Some notification #1' },
{ code: 'CODE456', message: 'Some notification #2' },
]);
});

expect(notification.code).to.equal('CODE123');
});

it(`Should force not shown or oldest shown with SLS_NOTIFICATIONS_MODE set to ${map(
'force'
)}`, async () => {
await overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: map('force') } }, async () => {
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE24');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE12');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE6');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0A');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0B');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0C');

expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE24');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE12');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE6');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0A');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0B');
expect((await processTargetNotifications(defaultFixture)).code).to.equal('CODE0C');
});
});
};

suite((mode) => mode);

const oldNotationMap = { 'off': '0', 'upgrades-only': '1', 'on': '2', 'force': '3' };
suite((mode) => oldNotationMap[mode]);
});
});

0 comments on commit d3ffc7a

Please sign in to comment.