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
2021-05-28 14:34:18 -07:00
let 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 ++ ) {
2023-09-22 17:58:13 +02:00
const [ error , id ] = await safe ( notifications . _add ( notifications . ALERT _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' ) ;
2023-09-22 17:58:13 +02:00
expect ( result . type ) . to . be ( notifications . ALERT _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 ( ) {
2023-09-22 17:58:13 +02:00
await notifications . update ( { id : notificationIds [ 0 ] } , { type : notifications . ALERT _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' ) ;
2023-09-22 17:58:13 +02:00
expect ( result . type ) . to . be ( notifications . ALERT _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
let alertId ;
it ( 'can add alert' , async function ( ) {
2023-03-26 15:12:00 +02:00
alertId = await notifications . alert ( notifications . ALERT _BOX _UPDATE , 'Cloudron xx is available' , 'Awesome changelog' , { persist : true } ) ;
2021-06-23 22:44:30 -07:00
const result = await notifications . get ( alertId ) ;
expect ( result . title ) . to . be ( 'Cloudron xx is available' ) ;
expect ( result . message ) . to . be ( 'Awesome changelog' ) ;
expect ( result . acknowledged ) . to . be ( false ) ;
} ) ;
2023-03-26 15:12:00 +02:00
it ( 'can update the alert (persist)' , async function ( ) {
2021-06-23 22:44:30 -07:00
await notifications . update ( { id : alertId } , { acknowledged : true } ) ; // ack the alert
2023-03-26 15:12:00 +02:00
const id = await notifications . alert ( notifications . ALERT _BOX _UPDATE , 'Cloudron xx is available' , 'Awesome new changelog' , { persist : true } ) ;
2021-06-23 22:44:30 -07:00
expect ( id ) . to . be ( alertId ) ;
const result = await notifications . get ( alertId ) ;
expect ( result . title ) . to . be ( 'Cloudron xx is available' ) ;
expect ( result . message ) . to . be ( 'Awesome new changelog' ) ;
expect ( result . acknowledged ) . to . be ( false ) ; // notification resurfaces
} ) ;
2023-03-26 15:12:00 +02:00
it ( 'can update the alert (non-persist)' , async function ( ) {
await notifications . update ( { id : alertId } , { acknowledged : true } ) ; // ack the alert
const id = await notifications . alert ( notifications . ALERT _BOX _UPDATE , 'Cloudron xx is available' , 'Awesome new changelog' , { persist : false } ) ;
expect ( id ) . to . be ( alertId ) ;
const result = await notifications . get ( alertId ) ;
expect ( result . title ) . to . be ( 'Cloudron xx is available' ) ;
expect ( result . message ) . to . be ( 'Awesome new changelog' ) ;
expect ( result . acknowledged ) . to . be ( true ) ; // notification does not resurface
} ) ;
2023-03-26 14:18:37 +02:00
it ( 'can clear the alert' , async function ( ) {
2023-09-22 17:58:13 +02:00
const id = await notifications . clearAlert ( notifications . ALERT _BOX _UPDATE ) ;
2021-06-23 22:44:30 -07:00
expect ( id ) . to . be ( null ) ;
const result = await notifications . get ( alertId ) ;
expect ( result ) . to . be ( null ) ;
} ) ;
2019-01-04 17:13:52 +01:00
} ) ;