Skip to content

Commit

Permalink
feat: implements proper timeout/wait behaviour for notify-send
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed Aug 10, 2020
1 parent 1c74ea9 commit 860c06e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
@@ -1,4 +1,5 @@
{
"printWidth": 80,
"singleQuote": true
"singleQuote": true,
"trailingComma": "none"
}
11 changes: 11 additions & 0 deletions lib/utils.js
Expand Up @@ -162,13 +162,24 @@ module.exports.mapToNotifySend = function (options) {
options = mapAppIcon(options);
options = mapText(options);

if (options.timeout === false) {
delete options.timeout;
}
if (options.wait === true) {
options['expire-time'] = 5; // 5 seconds default time (multipled below)
}
for (var key in options) {
if (key === 'message' || key === 'title') continue;
if (options.hasOwnProperty(key) && notifySendFlags[key] !== key) {
options[notifySendFlags[key]] = options[key];
delete options[key];
}
}
if (typeof options['expire-time'] === 'undefined') {
options['expire-time'] = 10 * 1000; // 10 sec timeout by default
} else if (typeof options['expire-time'] === 'number') {
options['expire-time'] = options['expire-time'] * 1000; // notify send uses milliseconds
}

return options;
};
Expand Down
52 changes: 33 additions & 19 deletions test/notify-send.js
Expand Up @@ -2,83 +2,97 @@ var Notify = require('../notifiers/notifysend');
var utils = require('../lib/utils');
var os = require('os');

describe('notify-send', function() {
describe('notify-send', function () {
var original = utils.command;
var originalType = os.type;

beforeEach(function() {
os.type = function() {
beforeEach(function () {
os.type = function () {
return 'Linux';
};
});

afterEach(function() {
afterEach(function () {
utils.command = original;
os.type = originalType;
});

function expectArgsListToBe(expected, done) {
utils.command = function(notifier, argsList, callback) {
utils.command = function (notifier, argsList, callback) {
expect(argsList).toEqual(expected);
done();
};
}

it('should pass on title and body', function(done) {
var expected = ['"title"', '"body"'];
it('should pass on title and body', function (done) {
var expected = ['"title"', '"body"', '--expire-time', '"10000"'];
expectArgsListToBe(expected, done);
var notifier = new Notify({ suppressOsdCheck: true });
notifier.notify({ title: 'title', message: 'body' });
});

it('should pass have default title', function(done) {
var expected = ['"Node Notification:"', '"body"'];
it('should pass have default title', function (done) {
var expected = [
'"Node Notification:"',
'"body"',
'--expire-time',
'"10000"'
];

expectArgsListToBe(expected, done);
var notifier = new Notify({ suppressOsdCheck: true });
notifier.notify({ message: 'body' });
});

it('should throw error if no message is passed', function(done) {
utils.command = function(notifier, argsList, callback) {
it('should throw error if no message is passed', function (done) {
utils.command = function (notifier, argsList, callback) {
expect(argsList).toBeUndefined();
};

var notifier = new Notify({ suppressOsdCheck: true });
notifier.notify({}, function(err) {
notifier.notify({}, function (err) {
expect(err.message).toBe('Message is required.');
done();
});
});

it('should escape message input', function(done) {
it('should escape message input', function (done) {
var excapedNewline = process.platform === 'win32' ? '\\r\\n' : '\\n';
var expected = [
'"Node Notification:"',
'"some' + excapedNewline + ' \\"me\'ss\\`age\\`\\""'
'"some' + excapedNewline + ' \\"me\'ss\\`age\\`\\""',
'--expire-time',
'"10000"'
];

expectArgsListToBe(expected, done);
var notifier = new Notify({ suppressOsdCheck: true });
notifier.notify({ message: 'some\n "me\'ss`age`"' });
});

it('should send additional parameters as --"keyname"', function(done) {
var expected = ['"title"', '"body"', '--icon', '"icon-string"'];
it('should send additional parameters as --"keyname"', function (done) {
var expected = [
'"title"',
'"body"',
'--icon',
'"icon-string"',
'--expire-time',
'"10000"'
];

expectArgsListToBe(expected, done);
var notifier = new Notify({ suppressOsdCheck: true });
notifier.notify({ title: 'title', message: 'body', icon: 'icon-string' });
});

it('should remove extra options that are not supported by notify-send', function(done) {
it('should remove extra options that are not supported by notify-send', function (done) {
var expected = [
'"title"',
'"body"',
'--icon',
'"icon-string"',
'--expire-time',
'"100"'
'"1000"'
];

expectArgsListToBe(expected, done);
Expand All @@ -87,7 +101,7 @@ describe('notify-send', function() {
title: 'title',
message: 'body',
icon: 'icon-string',
time: 100,
time: 1,
tullball: 'notValid'
});
});
Expand Down
25 changes: 15 additions & 10 deletions test/utils.js
Expand Up @@ -2,9 +2,9 @@ var path = require('path');
var fs = require('fs');
var _ = require('../lib/utils');

describe('utils', function() {
describe('clone', function() {
it('should clone nested objects', function() {
describe('utils', function () {
describe('clone', function () {
it('should clone nested objects', function () {
var obj = { a: { b: 42 }, c: 123 };
var obj2 = _.clone(obj);

Expand All @@ -15,9 +15,14 @@ describe('utils', function() {
});
});

describe('mapping', function() {
it('should map icon for notify-send', function() {
var expected = { title: 'Foo', message: 'Bar', icon: 'foobar' };
describe('mapping', function () {
it('should map icon for notify-send', function () {
var expected = {
title: 'Foo',
message: 'Bar',
icon: 'foobar',
'expire-time': 10000
};

expect(
_.mapToNotifySend({ title: 'Foo', message: 'Bar', appIcon: 'foobar' })
Expand All @@ -28,7 +33,7 @@ describe('utils', function() {
).toEqual(expected);
});

it('should map short hand for notify-sned', function() {
it('should map short hand for notify-sned', function () {
var expected = {
urgency: 'a',
'expire-time': 'b',
Expand All @@ -42,7 +47,7 @@ describe('utils', function() {
).toEqual(expected);
});

it('should map icon for notification center', function() {
it('should map icon for notification center', function () {
var expected = {
title: 'Foo',
message: 'Bar',
Expand All @@ -60,7 +65,7 @@ describe('utils', function() {
);
});

it('should map icon for growl', function() {
it('should map icon for growl', function () {
var icon = path.join(__dirname, 'fixture', 'coulson.jpg');
var iconRead = fs.readFileSync(icon);

Expand All @@ -78,7 +83,7 @@ describe('utils', function() {
expect(Buffer.isBuffer(obj.icon)).toBeTruthy();
});

it('should not map icon url for growl', function() {
it('should not map icon url for growl', function () {
var icon = 'http://hostname.com/logo.png';

var expected = { title: 'Foo', message: 'Bar', icon: icon };
Expand Down

0 comments on commit 860c06e

Please sign in to comment.