import { describe, it, expect, vi, afterEach } from 'vitest' import type { WalletLifecycleEvent } from '@meshconnect/uwc-types' import { SolanaWalletService } from './solana-wallet-service' import { StorageService } from '../storage-service' function mockAdapter() { const handlers: Record void> = {} return { on: (e: string, h: () => void) => { handlers[e] = h }, off: vi.fn((e: string) => { delete handlers[e] }), fire: (e: string) => handlers[e]?.() } } function serviceWith(adapter: unknown) { const service = new SolanaWalletService(new StorageService()) ;(service as unknown as { connectedAdapter: unknown }).connectedAdapter = adapter return service } describe('SolanaWalletService.onLifecycleEvent', () => { afterEach(() => { delete (window as unknown as { UWCBridgeChildInitialized?: boolean }) .UWCBridgeChildInitialized }) it('returns undefined with no connected adapter', () => { expect(serviceWith(null).onLifecycleEvent(() => {})).toBeUndefined() }) it('maps the adapter disconnect event and tears down via .off', () => { const adapter = mockAdapter() const events: WalletLifecycleEvent[] = [] const stop = serviceWith(adapter).onLifecycleEvent(e => events.push(e)) expect(typeof stop).toBe('function') adapter.fire('disconnect') expect(events).toEqual([{ type: 'disconnect' }]) stop!() expect(adapter.off).toHaveBeenCalledWith('disconnect', expect.any(Function)) }) it('returns undefined in iframe bridge mode (the adapter is a Comlink proxy)', () => { ;( window as unknown as { UWCBridgeChildInitialized?: boolean } ).UWCBridgeChildInitialized = true expect( serviceWith(mockAdapter()).onLifecycleEvent(() => {}) ).toBeUndefined() }) })