1+ import { Yok } from "../../../lib/common/yok" ;
2+ import { assert } from "chai" ;
3+ import * as sinon from "sinon" ;
4+ import { LoggerStub , ProcessServiceStub } from "../../stubs" ;
5+ import { AndroidLivesyncTool } from "../../../lib/services/livesync/android-livesync-tool" ;
6+ import { MobileHelper } from "../../../lib/common/mobile/mobile-helper" ;
7+ import * as net from "net" ;
8+ import { Duplex } from "stream" ;
9+ import { MobilePlatformsCapabilities } from "../../../lib/mobile-platforms-capabilities" ;
10+ import { DevicePlatformsConstants } from "../../../lib/common/mobile/device-platforms-constants" ;
11+
12+ const createTestInjector = ( socket : INetSocket ) : IInjector => {
13+ const testInjector = new Yok ( ) ;
14+
15+ testInjector . register ( "fs" , {
16+ createReadStream : ( filename : string , encoding ?: IReadFileOptions | string ) : NodeJS . ReadableStream => {
17+ const fileStream = new Duplex ( ) ;
18+ fileStream . write ( "testContent" ) ;
19+ fileStream . end ( ) ;
20+ return fileStream ;
21+ } ,
22+
23+ exists : ( ) : boolean => true ,
24+
25+ readJson : ( filePath : string ) : any => null ,
26+
27+ enumerateFilesInDirectorySync : ( directoryPath : string ,
28+ filterCallback ?: ( _file : string , _stat : IFsStats ) => boolean ,
29+ opts ?: { enumerateDirectories ?: boolean , includeEmptyDirectories ?: boolean } ,
30+ foundFiles ?: string [ ] ) : string [ ] => [ ]
31+ } ) ;
32+
33+ testInjector . register ( "logger" , LoggerStub ) ;
34+ testInjector . register ( "processService" , ProcessServiceStub ) ;
35+ testInjector . register ( "injector" , testInjector ) ;
36+ testInjector . register ( "mobileHelper" , MobileHelper ) ;
37+ testInjector . register ( "androidProcessService" , {
38+ forwardFreeTcpToAbstractPort : ( ) => Promise . resolve ( "" )
39+ } ) ;
40+ testInjector . register ( "LiveSyncSocket" , ( ) => { return socket } ) ;
41+ testInjector . register ( "mobilePlatformsCapabilities" , MobilePlatformsCapabilities ) ;
42+ testInjector . register ( "devicePlatformsConstants" , DevicePlatformsConstants ) ;
43+ testInjector . register ( "errors" , {
44+ fail : ( message : string ) => {
45+ throw new Error ( message ) ;
46+ }
47+ } ) ;
48+
49+ return testInjector ;
50+ } ;
51+
52+ const getHandshakeBuffer = ( ) => {
53+ const protocolVersion = "0.2.0" ;
54+ const packageName = "org.comp.test" ;
55+ const handshakeBuffer = Buffer . alloc ( Buffer . byteLength ( protocolVersion ) + Buffer . byteLength ( packageName ) + 1 ) ;
56+
57+ let offset = handshakeBuffer . writeUInt8 ( Buffer . byteLength ( protocolVersion ) , 0 ) ;
58+ offset = + handshakeBuffer . write ( protocolVersion , offset ) ;
59+ handshakeBuffer . write ( packageName , offset ) ;
60+
61+ return handshakeBuffer ;
62+ }
63+
64+ describe . only ( "AndroidLivesyncTool" , ( ) => {
65+ let testInjector : IInjector = null ;
66+ let livesyncTool : IAndroidLivesyncTool = null ;
67+ let testSocket : INetSocket ;
68+ let sandbox : sinon . SinonSandbox = null ;
69+
70+ beforeEach ( ( ) => {
71+ sandbox = sinon . sandbox . create ( ) ;
72+ testSocket = new net . Socket ( ) ;
73+ var stub = sandbox . stub ( testSocket , 'write' ) . callsFake ( function ( ) {
74+
75+ var args = stub . args ;
76+
77+ // this will echo whatever they wrote
78+ if ( args . length > 0 )
79+ testSocket . emit ( 'data' , stub . args [ stub . callCount - 1 ] [ 0 ] ) ;
80+ } ) ;
81+ testInjector = createTestInjector ( testSocket ) ;
82+ livesyncTool = testInjector . resolve ( AndroidLivesyncTool ) ;
83+ } ) ;
84+
85+ afterEach ( ( ) => {
86+ sandbox . restore ( ) ;
87+ } ) ;
88+
89+ describe ( "methods" , ( ) => {
90+ describe ( "connect" , ( ) => {
91+ it ( "should retry if first connect fails" , ( ) => {
92+ const originalOn = testSocket . on ;
93+ const originalOnce = testSocket . once ;
94+ let dataAttachCount = 0 ;
95+ let closeAttachCount = 0 ;
96+ sandbox . stub ( testSocket , "on" ) . callsFake ( function ( event : string ) {
97+ originalOn . apply ( this , arguments ) ;
98+ if ( event === "close" ) {
99+ closeAttachCount ++ ;
100+ if ( closeAttachCount === 1 ) {
101+ testSocket . emit ( "close" , false ) ;
102+ }
103+ }
104+ } ) ;
105+
106+ sandbox . stub ( testSocket , "once" ) . callsFake ( function ( event : string ) {
107+ originalOnce . apply ( this , arguments ) ;
108+ if ( event === "data" ) {
109+ dataAttachCount ++
110+ if ( dataAttachCount === 2 ) {
111+ testSocket . write ( getHandshakeBuffer ( ) ) ;
112+ }
113+ }
114+ } ) ;
115+ const connectStub : sinon . SinonStub = sandbox . stub ( testSocket , "connect" ) ;
116+ const connectPromise = livesyncTool . connect ( { appIdentifier : "test" , deviceIdentifier : "test" , appPlatformsPath : "test" } ) ;
117+
118+ return connectPromise . then ( ( ) => {
119+ assert ( connectStub . calledTwice ) ;
120+ }
121+ ) ;
122+ } ) ;
123+
124+ it ( "should fail eventually" , ( ) => {
125+ sandbox . stub ( testSocket , "connect" ) ;
126+ const connectPromise = livesyncTool . connect ( { appIdentifier : "test" , deviceIdentifier : "test" , appPlatformsPath : "test" , connectTimeout : 400 } ) ;
127+
128+ return connectPromise . should . be . rejected ;
129+ } ) ;
130+ } ) ;
131+ } ) ;
132+ } ) ;
0 commit comments