import { jest } from "@jest/globals" ; import { App , ConnectionManagerInterface } from "./background/types" ; import { Chain } from "@substrate/smoldot-light" ; import { Network } from "./types" ; export class MockPort implements chrome . runtime . Port { sender : any ; #messageListeners = [ ] ; #disconnectListeners = [ ] ; readonly name : string ; constructor ( name ) { this . name = name ; this . sender = { url : "http://test.com/" , tab : { id : 1234 } } ; } postMessage : ( message : any ) => void = jest . fn ( ) ; disconnect : ( ) => void = jest . fn ( ) ; setTabId ( id ) { this . sender . tab . id = id ; } triggerMessage ( message ) { this . #messageListeners . forEach ( ( l ) => { l ( message , this ) ; } ) ; } triggerDisconnect ( ) { this . #disconnectListeners . forEach ( ( l ) => { l ( ) ; } ) ; } onMessage = { addListener : ( listener ) => { this . #messageListeners . push ( listener ) ; } , } as any ; onDisconnect = { addListener : ( listener ) => { this . #disconnectListeners . push ( listener ) ; } , } as any ; } export class MockConnectionManager implements ConnectionManagerInterface { addChain ( ) { return Promise . resolve ( { chain : { } as Chain , tabId : 0 , } as Network ) ; } createApp : ( port : chrome . runtime . Port ) => App = jest . fn ( ) ; disconnect : ( app : App ) => void = jest . fn ( ) ; registerApp : ( ) => void = jest . fn ( ) ; unregisterApp : ( ) => void = jest . fn ( ) ; } export class ErroringMockConnectionManager implements ConnectionManagerInterface { addChain ( ) { return Promise . reject ( new Error ( "Invalid chain spec" ) ) ; } createApp : ( port : chrome . runtime . Port ) => App = jest . fn ( ) ; disconnect : ( app : App ) => void = jest . fn ( ) ; registerApp : ( ) => void = jest . fn ( ) ; unregisterApp : ( ) => void = jest . fn ( ) ; }