import { describe, it, expect } from 'vitest' import { WalletConnectorError, NoCommonNetworkError } from './errors' describe('WalletConnectorError', () => { it('preserves a numeric EIP-1193 code (MFS-667)', () => { const err = new WalletConnectorError({ type: 'unknown', message: 'already pending', code: -32002 }) expect(err.code).toBe(-32002) expect(err.type).toBe('unknown') expect(err.message).toBe('already pending') expect(err).toBeInstanceOf(Error) }) it('omits code when none is provided', () => { const err = new WalletConnectorError({ type: 'rejected', message: 'no' }) expect(err.code).toBeUndefined() }) it('ignores a non-numeric code so the structural slot never carries free-form data', () => { // The `code` field feeds telemetry as a structural signal; a wrapper that // passes a string/NaN must not smuggle it onto the numeric slot. const err = new WalletConnectorError({ type: 'unknown', message: 'x', // @ts-expect-error — a wallet-controlled value may be the wrong type at runtime code: 'ACTION_REJECTED' }) expect(err.code).toBeUndefined() }) it('rejects non-integer numeric codes (NaN / Infinity / float)', () => { // A wallet controls `code`; only a finite integer is a valid JSON-RPC/EIP-1193 // code, so NaN/Infinity/floats must not flip the classifier's hasNumericCode. for (const code of [NaN, Infinity, -Infinity, 1.5]) { const err = new WalletConnectorError({ type: 'unknown', message: 'x', code }) expect(err.code).toBeUndefined() } }) it('keeps a valid negative integer code (e.g. -32002)', () => { const err = new WalletConnectorError({ type: 'unknown', message: 'x', code: -32002 }) expect(err.code).toBe(-32002) }) it('NoCommonNetworkError carries no code and keeps its id lists', () => { const err = new NoCommonNetworkError(['eip155:1'], ['solana:101']) expect(err.code).toBeUndefined() expect(err.walletNetworkIds).toEqual(['eip155:1']) expect(err.configuredNetworkIds).toEqual(['solana:101']) }) })