1+ const host = "http://localhost:12800" ;
2+ const assert = require ( 'assert' ) ;
3+ const { default : axios } = require ( "axios" ) ;
4+ const SourcePlusPlus = require ( "../dist/SourcePlusPlus" ) ;
5+ const EventBus = require ( "@vertx/eventbus-bridge-client.js" ) ;
6+ const path = require ( "path" ) ;
7+
8+ const tokenPromise = axios . get ( `${ host } /api/new-token?access_token=change-me` )
9+ . then ( response => response . data ) ;
10+
11+ class TestUtils {
12+
13+ static lineLabels = { }
14+ static markerListeners = { }
15+ static markerEventBus ;
16+
17+ static async setupProbe ( ) {
18+ this . timeout ( 15000 ) ;
19+
20+ await SourcePlusPlus . start ( ) . then ( function ( ) {
21+ console . log ( "SourcePlusPlus started" ) ;
22+ } ) . catch ( function ( err ) {
23+ assert . fail ( err ) ;
24+ } ) ;
25+
26+ TestUtils . markerEventBus = new EventBus ( host + "/marker/eventbus" ) ;
27+ TestUtils . markerEventBus . enableReconnect ( true ) ;
28+ await new Promise ( ( resolve , reject ) => {
29+ TestUtils . markerEventBus . onopen = async function ( ) {
30+ //send marker connected
31+ TestUtils . markerEventBus . send ( "spp.platform.status.marker-connected" , {
32+ instanceId : "test-marker-id" ,
33+ connectionTime : Date . now ( ) ,
34+ meta : { }
35+ } , {
36+ "auth-token" : await tokenPromise
37+ } , async ( err ) => {
38+ if ( err ) {
39+ reject ( err ) ;
40+ } else {
41+ //listen for breakpoint events
42+ TestUtils . markerEventBus . registerHandler ( "spp.service.live-instrument.subscriber:system" , {
43+ "auth-token" : await tokenPromise
44+ } , function ( err , message ) {
45+ if ( ! err ) {
46+ if ( TestUtils . markerListeners [ message . body . eventType ] ) {
47+ TestUtils . markerListeners [ message . body . eventType ]
48+ . forEach ( listener => listener ( JSON . parse ( message . body . data ) ) ) ;
49+ }
50+ }
51+ } ) ;
52+ resolve ( ) ;
53+ }
54+ } ) ;
55+ }
56+ } ) ;
57+
58+ // Without waiting long enough, the spp.probe.command.live-instrument-remote counter isn't incremented
59+ await new Promise ( resolve => setTimeout ( resolve , 10000 ) ) ;
60+ }
61+
62+ static async teardownProbe ( ) {
63+ await SourcePlusPlus . stop ( ) ;
64+ TestUtils . markerEventBus . close ( ) ;
65+ }
66+
67+ static addLineLabel ( label , lineNumberFunc ) {
68+ TestUtils . lineLabels [ label ] = lineNumberFunc . call ( ) ;
69+ }
70+
71+ static getLineLabelNumber ( label ) {
72+ return TestUtils . lineLabels [ label ] ;
73+ }
74+
75+ static awaitMarkerEvent ( eventName ) {
76+ if ( ! TestUtils . markerListeners [ eventName ] ) {
77+ TestUtils . markerListeners [ eventName ] = [ ] ;
78+ }
79+ return new Promise ( resolve => TestUtils . markerListeners [ eventName ] . push ( data => resolve ( data ) ) ) ;
80+ }
81+
82+ static locateVariable ( name , variables ) {
83+ for ( let variable of variables ) {
84+ if ( variable . name === name ) {
85+ return variable ;
86+ }
87+ }
88+ }
89+
90+ static async addLiveBreakpoint ( location , condition , hitLimit ) {
91+ const options = {
92+ method : 'POST' ,
93+ url : `${ host } /graphql/spp` ,
94+ headers : {
95+ 'Content-Type' : 'application/json' ,
96+ Authorization : 'Bearer ' + await tokenPromise
97+ } ,
98+ data : {
99+ "query" : "mutation ($input: LiveBreakpointInput!) { addLiveBreakpoint(input: $input) { id location { source line } condition expiresAt hitLimit applyImmediately applied pending throttle { limit step } } }" ,
100+ "variables" : {
101+ "input" : {
102+ "location" : location ,
103+ "condition" : condition ,
104+ "hitLimit" : hitLimit ,
105+ "applyImmediately" : true
106+ }
107+ }
108+ }
109+ } ;
110+ return axios . request ( options ) ;
111+ }
112+
113+ static getLineNumber ( ) {
114+ var e = new Error ( ) ;
115+ if ( ! e . stack ) try {
116+ // IE requires the Error to actually be thrown or else the Error's 'stack'
117+ // property is undefined.
118+ throw e ;
119+ } catch ( e ) {
120+ if ( ! e . stack ) {
121+ return 0 ; // IE < 10, likely
122+ }
123+ }
124+ var stack = e . stack . toString ( ) . split ( / \r \n | \n / ) ;
125+ // We want our caller's frame. It's index into |stack| depends on the
126+ // browser and browser version, so we need to search for the second frame:
127+ var frameRE = / : ( \d + ) : (?: \d + ) [ ^ \d ] * $ / ;
128+ do {
129+ var frame = stack . shift ( ) ;
130+ } while ( ! frameRE . exec ( frame ) && stack . length ) ;
131+ return frameRE . exec ( stack . shift ( ) ) [ 1 ] ;
132+ }
133+
134+ static getFilename = ( ) => {
135+ return ( ) => {
136+ let basePath = process . cwd ( ) ;
137+ let str = / a t .* \( ( [ a - z 0 - 9 \/ \\ \s \- : ] * \. j s ) : \d + : \d + \) / ig. exec ( new Error ( ) . stack ) [ 1 ] ;
138+ let relative = path . relative ( basePath , str ) ;
139+ return relative . replaceAll ( '\\' , '/' ) ;
140+ }
141+ }
142+ }
143+
144+ module . exports = TestUtils ;
0 commit comments