11import { NetworkAsCodeClient } from "../src" ;
22import "dotenv/config" ;
33import { Device } from "../src/models/device" ;
4- import { configureClient } from "./configClient" ;
4+ import { configureClient , configureNotificationServerUrl } from "./configClient" ;
5+ import { ProxyAgent } from "proxy-agent" ;
6+ import fetch from "node-fetch" ;
7+ import { v4 as uuid } from 'uuid' ;
58
69let client : NetworkAsCodeClient ;
710let device : Device ;
11+ let notificationUrl : string ;
12+ let agent : ProxyAgent
813
914beforeAll ( ( ) => {
1015 client = configureClient ( )
1116 device = client . devices . get ( {
12- phoneNumber : "+3672123456 " ,
17+ phoneNumber : "+3670123456 " ,
1318 } ) ;
19+ notificationUrl = configureNotificationServerUrl ( ) ;
20+ agent = new ProxyAgent ( ) ;
1421} ) ;
1522
1623describe ( "Congestion Insights" , ( ) => {
1724 const expirationDate = new Date ( Date . now ( ) + 5 * 60 * 1000 ) ;
1825 expirationDate . setMilliseconds ( 0 ) ;
1926
20- it . skip ( "can create subscription for congestion insights" , async ( ) => {
27+ it ( "can create subscription for congestion insights" , async ( ) => {
28+ const notificationId : string = uuid ( ) ;
2129 const subscription = await client . insights . subscribeToCongestionInfo (
2230 device ,
2331 expirationDate ,
24- "https://example.com/ notify" ,
32+ ` ${ notificationUrl } / notify/ ${ notificationId } ` ,
2533 "c8974e592c2fa383d4a3960714"
2634 ) ;
2735
2836 expect ( subscription . expiresAt ) . toEqual (
2937 new Date ( expirationDate . toISOString ( ) . replace ( ".000" , "" ) )
38+ ) ;
39+
40+ await new Promise ( resolve => setTimeout ( resolve , 5 * 1000 ) ) ;
41+ let notification = await fetch ( `${ notificationUrl } /congestion-insights/get/${ notificationId } ` ,
42+ {
43+ method : "GET" ,
44+ agent : agent
45+ }
3046 ) ;
3147
32- subscription . delete ( ) ;
33- } ) ;
48+ const data = await notification . json ( ) ;
49+
50+ expect ( data ) . not . toBeNull ( ) ;
51+
52+ notification = await fetch ( `${ notificationUrl } /congestion-insights/delete/${ notificationId } ` ,
53+ {
54+ method : 'DELETE' ,
55+ agent : agent
56+ } ) ;
57+
58+ const deletionData = await notification . json ( )
59+ expect ( deletionData ) . toEqual ( [ { "message" : "Notification deleted" } , 200 ] )
3460
35- it . skip ( "can create subscription for congestion insights without auth token" , async ( ) => {
61+ await subscription . delete ( ) ;
62+
63+ } , 20 * 1000 ) ;
64+
65+ it ( "can create subscription for congestion insights without auth token" , async ( ) => {
66+ const notificationId : string = uuid ( ) ;
3667 const subscription = await client . insights . subscribeToCongestionInfo (
3768 device ,
3869 expirationDate ,
39- "https://example.com/ notify"
70+ ` ${ notificationUrl } / notify/ ${ notificationId } `
4071 ) ;
4172
4273 expect ( subscription . expiresAt ) . toEqual (
4374 new Date ( expirationDate . toISOString ( ) . replace ( ".000" , "" ) )
4475 ) ;
76+
77+
78+ await new Promise ( resolve => setTimeout ( resolve , 5 * 1000 ) ) ;
79+ let notification = await fetch ( `${ notificationUrl } /congestion-insights/get/${ notificationId } ` ,
80+ {
81+ method : "GET" ,
82+ agent : agent
83+ }
84+ ) ;
4585
46- subscription . delete ( ) ;
47- } ) ;
86+ const data = await notification . json ( ) ;
4887
49- it . skip ( "can get a subscription by id" , async ( ) => {
88+ expect ( data ) . not . toBeNull ( ) ;
89+
90+ notification = await fetch ( `${ notificationUrl } /congestion-insights/delete/${ notificationId } ` ,
91+ {
92+ method : 'DELETE' ,
93+ agent : agent
94+ } ) ;
95+
96+ const deletionData = await notification . json ( )
97+ expect ( deletionData ) . toEqual ( [ { "message" : "Notification deleted" } , 200 ] )
98+
99+ await subscription . delete ( ) ;
100+
101+ } , 20 * 1000 ) ;
102+
103+ it ( "can get a subscription by id" , async ( ) => {
50104 const subscription = await client . insights . subscribeToCongestionInfo (
51105 device ,
52106 expirationDate ,
@@ -61,10 +115,47 @@ describe("Congestion Insights", () => {
61115 expect ( subscription2 . subscriptionId ) . toBe ( subscription . subscriptionId ) ;
62116 expect ( subscription2 . startsAt ) . toEqual ( subscription . startsAt ) ;
63117
64- subscription . delete ( ) ;
118+ await subscription . delete ( ) ;
119+ } ) ;
120+
121+ it ( "can get subscription start and expiration" , async ( ) => {
122+ const subscription = await client . insights . subscribeToCongestionInfo (
123+ device ,
124+ expirationDate ,
125+ "https://example.com/notify"
126+ ) ;
127+
128+ expect ( subscription . startsAt ) . toBeDefined ( )
129+ expect ( subscription . expiresAt ) . toBeDefined ( )
130+
131+ expect ( subscription . startsAt instanceof Date ) . toBeTruthy ( ) ;
132+
133+ await subscription . delete ( ) ;
134+ } ) ;
135+
136+ it ( "can get start and expiration from selected subscription" , async ( ) => {
137+ const subscription = await client . insights . subscribeToCongestionInfo (
138+ device ,
139+ expirationDate ,
140+ "https://example.com/notify"
141+ ) ;
142+
143+ const subscriptionFromList = ( await client . insights . getSubscriptions ( ) ) [ 0 ]
144+
145+ expect ( subscriptionFromList . startsAt ) . toBeDefined ( )
146+ expect ( subscriptionFromList . expiresAt ) . toBeDefined ( )
147+
148+ const subscriptionById = await client . insights . get (
149+ subscription . subscriptionId
150+ ) ;
151+
152+ expect ( subscriptionById . startsAt ) . toBeDefined ( )
153+ expect ( subscriptionById . expiresAt ) . toBeDefined ( )
154+
155+ await subscription . delete ( ) ;
65156 } ) ;
66157
67- it . skip ( "can get a list of subscriptions" , async ( ) => {
158+ it ( "can get a list of subscriptions" , async ( ) => {
68159 const subscription = await client . insights . subscribeToCongestionInfo (
69160 device ,
70161 expirationDate ,
@@ -82,10 +173,10 @@ describe("Congestion Insights", () => {
82173 ) . length
83174 ) . toBe ( 1 ) ;
84175
85- subscription . delete ( ) ;
176+ await subscription . delete ( ) ;
86177 } ) ;
87178
88- it . skip ( "should fetch current congestion level relevant to a given device" , async ( ) => {
179+ it ( "should fetch current congestion level relevant to a given device" , async ( ) => {
89180 const subscription = await client . insights . subscribeToCongestionInfo (
90181 device ,
91182 expirationDate ,
@@ -109,10 +200,10 @@ describe("Congestion Insights", () => {
109200 ) ;
110201 } ) ;
111202
112- subscription . delete ( ) ;
203+ await subscription . delete ( ) ;
113204 } ) ;
114205
115- it . skip ( "should fetch prediction/historical data between two time stamps:" , async ( ) => {
206+ it ( "should fetch prediction/historical data between two time stamps:" , async ( ) => {
116207 const subscription = await client . insights . subscribeToCongestionInfo (
117208 device ,
118209 expirationDate ,
@@ -137,6 +228,6 @@ describe("Congestion Insights", () => {
137228 ) ;
138229 } ) ;
139230
140- subscription . delete ( ) ;
231+ await subscription . delete ( ) ;
141232 } ) ;
142233} ) ;
0 commit comments