2019-01-04 17:13:52 +01:00
/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict' ;
2021-06-03 12:20:44 -07:00
const BoxError = require ( '../boxerror.js' ) ,
common = require ( './common.js' ) ,
2021-05-28 14:34:18 -07:00
expect = require ( 'expect.js' ) ,
2019-01-04 17:13:52 +01:00
notifications = require ( '../notifications.js' ) ,
2023-05-14 10:53:50 +02:00
safe = require ( 'safetydance' ) ,
timers = require ( 'timers/promises' ) ;
2019-01-04 17:13:52 +01:00
2021-05-28 14:34:18 -07:00
const EVENT _0 = {
2019-01-21 08:51:04 +01:00
id : 'event_0' ,
2021-05-28 14:34:18 -07:00
action : 'action' ,
2019-01-21 08:51:04 +01:00
source : { } ,
data : { }
} ;
2019-01-04 17:13:52 +01:00
describe ( 'Notifications' , function ( ) {
2021-08-13 10:41:10 -07:00
const { setup , cleanup } = common ;
before ( setup ) ;
after ( cleanup ) ;
2019-01-04 17:13:52 +01:00
2024-12-11 15:18:00 +01:00
const notificationIds = [ ] ;
2019-01-04 17:13:52 +01:00
2021-05-28 14:34:18 -07:00
it ( 'can add notifications' , async function ( ) {
for ( let i = 0 ; i < 3 ; i ++ ) {
2024-12-11 15:18:00 +01:00
const [ error , id ] = await safe ( notifications . _add ( notifications . TYPE _APP _UPDATED , ` title ${ i } ` , ` message ${ i } ` , { eventId : EVENT _0 . id } ) ) ;
2021-05-28 14:34:18 -07:00
expect ( error ) . to . equal ( null ) ;
expect ( id ) . to . be . a ( 'string' ) ;
notificationIds . push ( id ) ;
2023-05-14 10:53:50 +02:00
await timers . setTimeout ( 1000 ) ;
2021-05-28 14:34:18 -07:00
}
2019-01-04 17:13:52 +01:00
} ) ;
2021-05-28 14:34:18 -07:00
it ( 'can get by id' , async function ( ) {
const [ error , result ] = await safe ( notifications . get ( notificationIds [ 0 ] ) ) ;
expect ( error ) . to . be ( null ) ;
expect ( result . title ) . to . be ( 'title 0' ) ;
2024-12-11 15:18:00 +01:00
expect ( result . type ) . to . be ( notifications . TYPE _APP _UPDATED ) ;
2021-05-28 14:34:18 -07:00
expect ( result . message ) . to . be ( 'message 0' ) ;
expect ( result . acknowledged ) . to . be ( false ) ;
2019-01-04 17:13:52 +01:00
} ) ;
2021-05-28 14:34:18 -07:00
it ( 'cannot get non-existent id' , async function ( ) {
const result = await notifications . get ( 'random' ) ;
expect ( result ) . to . be ( null ) ;
2019-01-04 17:13:52 +01:00
} ) ;
2021-05-28 14:34:18 -07:00
it ( 'can list notifications' , async function ( ) {
const result = await notifications . list ( { } , 1 , 10 ) ;
expect ( result . length ) . to . be ( 3 ) ;
2021-06-03 12:20:44 -07:00
expect ( result [ 0 ] . title ) . to . be ( 'title 2' ) ;
2021-05-28 14:34:18 -07:00
expect ( result [ 1 ] . title ) . to . be ( 'title 1' ) ;
2021-06-03 12:20:44 -07:00
expect ( result [ 2 ] . title ) . to . be ( 'title 0' ) ;
2019-01-04 17:13:52 +01:00
} ) ;
2021-05-28 14:34:18 -07:00
it ( 'can update notification' , async function ( ) {
2024-12-11 15:18:00 +01:00
await notifications . update ( { id : notificationIds [ 0 ] } , { type : notifications . TYPE _APP _OOM , title : 'updated title 0' , message : 'updated message 0' , acknowledged : true } ) ;
2019-01-04 17:13:52 +01:00
2021-05-28 14:34:18 -07:00
const result = await notifications . get ( notificationIds [ 0 ] ) ;
expect ( result . title ) . to . be ( 'updated title 0' ) ;
2024-12-11 15:18:00 +01:00
expect ( result . type ) . to . be ( notifications . TYPE _APP _OOM ) ;
2021-05-28 14:34:18 -07:00
expect ( result . message ) . to . be ( 'updated message 0' ) ;
expect ( result . acknowledged ) . to . be ( true ) ;
2019-01-04 17:13:52 +01:00
} ) ;
2021-05-28 14:34:18 -07:00
it ( 'cannot update non-existent notification' , async function ( ) {
2023-07-11 16:32:28 +05:30
const [ error ] = await safe ( notifications . update ( { id : '1245' } , { title : 'updated title 0' , message : 'updated message 0' , acknowledged : true } ) ) ;
2021-05-28 14:34:18 -07:00
expect ( error . reason ) . to . be ( BoxError . NOT _FOUND ) ;
2019-01-04 17:13:52 +01:00
} ) ;
2021-05-28 14:34:18 -07:00
it ( 'can delete' , async function ( ) {
await notifications . del ( notificationIds [ 0 ] ) ;
2019-01-04 17:13:52 +01:00
} ) ;
2021-05-28 14:34:18 -07:00
it ( 'cannot delete non-existent notification' , async function ( ) {
2023-07-11 16:32:28 +05:30
const [ error ] = await safe ( notifications . del ( '5213' ) ) ;
2021-05-28 14:34:18 -07:00
expect ( error . reason ) . to . be ( BoxError . NOT _FOUND ) ;
2019-01-04 17:13:52 +01:00
} ) ;
2021-06-23 22:44:30 -07:00
2024-12-11 15:47:41 +01:00
let pinId ;
describe ( 'pin with context' , function ( ) {
it ( 'can add pin' , async function ( ) {
pinId = await notifications . pin ( notifications . TYPE _BOX _UPDATE , 'Cloudron xx is available' , 'Awesome changelog' , { context : 'xx' } ) ;
const result = await notifications . get ( pinId ) ;
expect ( result . title ) . to . be ( 'Cloudron xx is available' ) ;
expect ( result . message ) . to . be ( 'Awesome changelog' ) ;
expect ( result . acknowledged ) . to . be ( false ) ;
} ) ;
it ( 'updating pin with same message does nothing' , async function ( ) {
await notifications . update ( { id : pinId } , { acknowledged : true } ) ; // ack the alert
const id = await notifications . pin ( notifications . TYPE _BOX _UPDATE , 'Cloudron xx is available' , 'Awesome changelog' , { context : 'xx' } ) ;
expect ( id ) . to . be ( pinId ) ;
const result = await notifications . get ( pinId ) ;
expect ( result . title ) . to . be ( 'Cloudron xx is available' ) ;
expect ( result . message ) . to . be ( 'Awesome changelog' ) ;
expect ( result . acknowledged ) . to . be ( true ) ; // notification does not resurface
} ) ;
it ( 'updating pin with new message resurfaces' , async function ( ) {
await notifications . update ( { id : pinId } , { acknowledged : true } ) ; // ack the alert
const id = await notifications . pin ( notifications . TYPE _BOX _UPDATE , 'Cloudron xy is available' , 'Awesome new changelog' , { context : 'xx' } ) ;
expect ( id ) . to . be ( pinId ) ;
const result = await notifications . get ( pinId ) ;
expect ( result . title ) . to . be ( 'Cloudron xy is available' ) ;
expect ( result . message ) . to . be ( 'Awesome new changelog' ) ;
expect ( result . acknowledged ) . to . be ( false ) ; // notification resurfaces
} ) ;
it ( 'can unpin' , async function ( ) {
await notifications . unpin ( notifications . TYPE _BOX _UPDATE , { context : 'xx' } ) ;
const result = await notifications . get ( pinId ) ;
expect ( result . acknowledged ) . to . be ( true ) ;
} ) ;
2021-06-23 22:44:30 -07:00
} ) ;
2024-12-11 15:47:41 +01:00
describe ( 'pin without context' , function ( ) {
it ( 'can add pin' , async function ( ) {
pinId = await notifications . pin ( notifications . TYPE _REBOOT , 'Reboot required' , 'Do it now' , { } ) ;
2021-06-23 22:44:30 -07:00
2024-12-11 15:47:41 +01:00
const result = await notifications . get ( pinId ) ;
expect ( result . title ) . to . be ( 'Reboot required' ) ;
expect ( result . message ) . to . be ( 'Do it now' ) ;
expect ( result . acknowledged ) . to . be ( false ) ;
} ) ;
2021-06-23 22:44:30 -07:00
2024-12-11 15:47:41 +01:00
it ( 'can update the alert' , async function ( ) {
await notifications . update ( { id : pinId } , { acknowledged : true } ) ; // ack the alert
2023-03-26 15:12:00 +02:00
2024-12-11 15:47:41 +01:00
const id = await notifications . pin ( notifications . TYPE _REBOOT , 'Reboot required' , 'Do it now' , { } ) ;
expect ( id ) . to . be ( pinId ) ;
2023-03-26 15:12:00 +02:00
2024-12-11 15:47:41 +01:00
const result = await notifications . get ( pinId ) ;
expect ( result . title ) . to . be ( 'Reboot required' ) ;
expect ( result . message ) . to . be ( 'Do it now' ) ;
expect ( result . acknowledged ) . to . be ( false ) ; // resurfaces
} ) ;
2023-03-26 15:12:00 +02:00
2024-12-11 15:47:41 +01:00
it ( 'can unpin' , async function ( ) {
await notifications . unpin ( notifications . TYPE _REBOOT , { } ) ;
2021-06-23 22:44:30 -07:00
2024-12-11 15:47:41 +01:00
const result = await notifications . get ( pinId ) ;
expect ( result . acknowledged ) . to . be ( true ) ;
} ) ;
2021-06-23 22:44:30 -07:00
} ) ;
2019-01-04 17:13:52 +01:00
} ) ;