@@ -10,6 +10,7 @@ import { SfuClient, SfuClientState } from "#src/client";
1010import { Channel } from "#src/models/channel" ;
1111import type { Session } from "#src/models/session" ;
1212import type { JWTClaims } from "#src/services/auth" ;
13+ import { StringLike } from "#src/shared/types.ts" ;
1314
1415/**
1516 * HMAC key for JWT signing in tests
@@ -21,10 +22,11 @@ const HMAC_KEY = Buffer.from(HMAC_B64_KEY, "base64");
2122 * Creates a JWT token for testing
2223 *
2324 * @param data - Claims to include in the JWT
25+ * @param [key] - Key to sign the JWT with
2426 * @returns Signed JWT string
2527 */
26- export function makeJwt ( data : JWTClaims ) : string {
27- return auth . sign ( data , HMAC_KEY , { algorithm : auth . ALGORITHM . HS256 } ) ;
28+ export function makeJwt ( data : JWTClaims , key : StringLike = HMAC_KEY ) : string {
29+ return auth . sign ( data , key , { algorithm : auth . ALGORITHM . HS256 } ) ;
2830}
2931
3032/**
@@ -51,7 +53,7 @@ export class LocalNetwork {
5153 public port ?: number ;
5254
5355 /** JWT creation function (can be overridden for testing) */
54- public makeJwt : ( data : JWTClaims ) => string = makeJwt ;
56+ public makeJwt : ( data : JWTClaims , key ?: StringLike ) => string = makeJwt ;
5557
5658 /** Active SFU client instances */
5759 private readonly _sfuClients : SfuClient [ ] = [ ] ;
@@ -74,17 +76,19 @@ export class LocalNetwork {
7476
7577 /**
7678 * Creates a new channel and returns its UUID
77- *
78- * @param useWebRtc - Whether to enable WebRTC for the channel
79+ * @param [param0] - options
80+ * @param [param0.useWebRtc=true] - Whether to enable WebRTC for the channel
81+ * @param [param0.key=HMAC_B64_KEY] - Channel key
7982 * @returns Promise resolving to channel UUID
8083 */
81- async getChannelUUID ( useWebRtc : boolean = true ) : Promise < string > {
84+ async getChannelUUID ( { useWebRtc = true , key = HMAC_B64_KEY } = { } ) : Promise < string > {
8285 if ( ! this . hostname || ! this . port ) {
8386 throw new Error ( "Network not started - call start() first" ) ;
8487 }
8588
8689 const jwt = this . makeJwt ( {
87- iss : `http://${ this . hostname } :${ this . port } /`
90+ iss : `http://${ this . hostname } :${ this . port } /` ,
91+ key
8892 } ) ;
8993
9094 const response = await fetch (
@@ -110,10 +114,16 @@ export class LocalNetwork {
110114 *
111115 * @param channelUUID - Channel UUID to connect to
112116 * @param sessionId - Session identifier
117+ * @param [param2]
118+ * @param [param2.key=HMAC_B64_KEY] - Channel key
113119 * @returns Promise resolving to connection result
114120 * @throws {Error } If client is closed before authentication
115121 */
116- async connect ( channelUUID : string , sessionId : number ) : Promise < ConnectionResult > {
122+ async connect (
123+ channelUUID : string ,
124+ sessionId : number ,
125+ { key = HMAC_KEY } : { key ?: StringLike } = { }
126+ ) : Promise < ConnectionResult > {
117127 if ( ! this . hostname || ! this . port ) {
118128 throw new Error ( "Network not started - call start() first" ) ;
119129 }
@@ -122,16 +132,16 @@ export class LocalNetwork {
122132 const sfuClient = new SfuClient ( ) ;
123133 this . _sfuClients . push ( sfuClient ) ;
124134
125- // Override device creation for testing environment
126- ( sfuClient as any ) . _createDevice = ( ) : Device => {
135+ // @ts -expect-error private property
136+ sfuClient . _createDevice = ( ) : Device => {
127137 // Mock device creation since we're in a server environment without real WebRTC
128138 return new Device ( {
129139 handlerFactory : FakeHandler . createFactory ( fakeParameters )
130140 } ) ;
131141 } ;
132142
133- // Override WebSocket creation for Node.js environment
134- ( sfuClient as any ) . _createWebSocket = ( url : string ) : WebSocket => {
143+ // @ts -expect-error private property
144+ sfuClient . _createWebSocket = ( url : string ) : WebSocket => {
135145 // Replace browser WebSocket with Node.js ws package
136146 return new WebSocket ( url ) ;
137147 } ;
@@ -164,10 +174,13 @@ export class LocalNetwork {
164174 // Start connection
165175 sfuClient . connect (
166176 `ws://${ this . hostname } :${ this . port } ` ,
167- this . makeJwt ( {
168- sfu_channel_uuid : channelUUID ,
169- session_id : sessionId
170- } ) ,
177+ this . makeJwt (
178+ {
179+ sfu_channel_uuid : channelUUID ,
180+ session_id : sessionId
181+ } ,
182+ key
183+ ) ,
171184 { channelUUID }
172185 ) ;
173186
0 commit comments