1- import { makeLogger } from '@launchdarkly/js-contract-test-utils/client' ;
2- import { LDLogger } from '@launchdarkly/react-native-client-sdk' ;
1+ import { makeLogger } from '../logging/makeLogger.js' ;
2+ import { CommandParams } from '../types/CommandParams.js' ;
3+ import { CreateInstanceParams } from '../types/ConfigParams.js' ;
34
4- import { ClientEntity , newSdkClientEntity } from './ClientEntity' ;
5+ export interface IClientEntity {
6+ doCommand ( params : CommandParams ) : Promise < unknown > ;
7+ close ( ) : void | Promise < void > ;
8+ }
9+
10+ export type CreateClientEntityFn = ( options : CreateInstanceParams ) => Promise < IClientEntity > ;
511
612export default class TestHarnessWebSocket {
713 private _ws ?: WebSocket ;
8- private readonly _entities : Record < string , ClientEntity > = { } ;
14+ private readonly _entities : Record < string , IClientEntity > = { } ;
915 private _clientCounter = 0 ;
10- private _logger : LDLogger = makeLogger ( 'TestHarnessWebSocket' ) ;
16+ private _logger = makeLogger ( 'TestHarnessWebSocket' ) ;
1117 private _intentionalClose = false ;
12- private _onConnectionChange ?: ( connected : boolean ) => void ;
1318
1419 constructor (
1520 private readonly _url : string ,
16- onConnectionChange ?: ( connected : boolean ) => void ,
17- ) {
18- this . _onConnectionChange = onConnectionChange ;
19- }
21+ private readonly _capabilities : string [ ] ,
22+ private readonly _createClient : CreateClientEntityFn ,
23+ private readonly _onConnectionChange ?: ( connected : boolean ) => void ,
24+ ) { }
2025
2126 connect ( ) {
2227 this . _intentionalClose = false ;
2328 this . _logger . info ( `Connecting to web socket.` ) ;
24- this . _ws = new WebSocket ( this . _url , 'v1' ) ;
29+ this . _ws = new WebSocket ( this . _url , [ 'v1' ] ) ;
2530 this . _ws . onopen = ( ) => {
2631 this . _logger . info ( 'Connected to websocket.' ) ;
2732 this . _onConnectionChange ?.( true ) ;
@@ -45,26 +50,13 @@ export default class TestHarnessWebSocket {
4550 const resData : any = { reqId : data . reqId } ;
4651 switch ( data . command ) {
4752 case 'getCapabilities' :
48- resData . capabilities = [
49- 'client-side' ,
50- 'mobile' ,
51- 'service-endpoints' ,
52- 'tags' ,
53- 'user-type' ,
54- 'inline-context-all' ,
55- 'anonymous-redaction' ,
56- 'strongly-typed' ,
57- 'client-prereq-events' ,
58- 'client-per-context-summaries' ,
59- 'track-hooks' ,
60- ] ;
61-
53+ resData . capabilities = this . _capabilities ;
6254 break ;
6355 case 'createClient' :
6456 try {
6557 resData . resourceUrl = `/clients/${ this . _clientCounter } ` ;
6658 resData . status = 201 ;
67- const entity = await newSdkClientEntity ( data . body ) ;
59+ const entity = await this . _createClient ( data . body ) ;
6860 this . _entities [ this . _clientCounter ] = entity ;
6961 this . _clientCounter += 1 ;
7062 } catch ( e : any ) {
@@ -87,7 +79,6 @@ export default class TestHarnessWebSocket {
8779 resData . status = 404 ;
8880 this . _logger . warn ( `Client did not exist: ${ data . id } ` ) ;
8981 }
90-
9182 break ;
9283 case 'deleteClient' :
9384 if ( Object . prototype . hasOwnProperty . call ( this . _entities , data . id ) ) {
@@ -102,7 +93,6 @@ export default class TestHarnessWebSocket {
10293 default :
10394 break ;
10495 }
105-
10696 this . send ( resData ) ;
10797 } ;
10898 }
0 commit comments