|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + createCommsQueryString, |
| 5 | + parseCommsQueryString, |
| 6 | + getCommsParamsFromCurrentLocation, |
| 7 | +} from './comms-query-string.ts'; |
| 8 | + |
| 9 | +// Mock the logger module |
| 10 | +vi.mock('@metamask/logger', () => ({ |
| 11 | + Logger: vi.fn().mockImplementation(function () { |
| 12 | + return { |
| 13 | + error: vi.fn(), |
| 14 | + warn: vi.fn(), |
| 15 | + }; |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +describe('comms-query-string', () => { |
| 20 | + describe('createCommsQueryString', () => { |
| 21 | + it('returns URLSearchParams with relays only', () => { |
| 22 | + const relays = ['/ip4/127.0.0.1/tcp/9001/ws']; |
| 23 | + const result = createCommsQueryString({ relays }); |
| 24 | + expect(result.get('relays')).toBe(JSON.stringify(relays)); |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns URLSearchParams with allowedWsHosts only', () => { |
| 28 | + const allowedWsHosts = ['localhost', 'relay.example.com']; |
| 29 | + const result = createCommsQueryString({ allowedWsHosts }); |
| 30 | + expect(result.get('allowedWsHosts')).toBe(JSON.stringify(allowedWsHosts)); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns URLSearchParams with both params', () => { |
| 34 | + const relays = ['/ip4/127.0.0.1/tcp/9001/ws']; |
| 35 | + const allowedWsHosts = ['localhost']; |
| 36 | + const result = createCommsQueryString({ relays, allowedWsHosts }); |
| 37 | + expect(result.has('relays')).toBe(true); |
| 38 | + expect(result.has('allowedWsHosts')).toBe(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns empty URLSearchParams for empty arrays', () => { |
| 42 | + expect( |
| 43 | + createCommsQueryString({ relays: [], allowedWsHosts: [] }).toString(), |
| 44 | + ).toBe(''); |
| 45 | + expect(createCommsQueryString({}).toString()).toBe(''); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns URLSearchParams with number options and directListenAddresses', () => { |
| 49 | + const result = createCommsQueryString({ |
| 50 | + relays: ['/ip4/127.0.0.1/tcp/9001/ws'], |
| 51 | + maxRetryAttempts: 3, |
| 52 | + maxQueue: 100, |
| 53 | + directListenAddresses: ['/ip4/0.0.0.0/udp/0/quic-v1'], |
| 54 | + }); |
| 55 | + expect(result.get('relays')).toBe( |
| 56 | + JSON.stringify(['/ip4/127.0.0.1/tcp/9001/ws']), |
| 57 | + ); |
| 58 | + expect(result.get('maxRetryAttempts')).toBe('3'); |
| 59 | + expect(result.get('maxQueue')).toBe('100'); |
| 60 | + expect(result.get('directListenAddresses')).toBe( |
| 61 | + JSON.stringify(['/ip4/0.0.0.0/udp/0/quic-v1']), |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('round-trips full options via createCommsQueryString and parseCommsQueryString', () => { |
| 66 | + const options = { |
| 67 | + relays: ['/dns4/relay.example.com/tcp/443/wss/p2p/QmRelay'], |
| 68 | + allowedWsHosts: ['relay.example.com'], |
| 69 | + maxRetryAttempts: 5, |
| 70 | + maxQueue: 200, |
| 71 | + }; |
| 72 | + const params = createCommsQueryString(options); |
| 73 | + expect(parseCommsQueryString(`?${params.toString()}`)).toStrictEqual( |
| 74 | + options, |
| 75 | + ); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('parseCommsQueryString', () => { |
| 80 | + it('returns both relays and allowedWsHosts', () => { |
| 81 | + const queryString = `?relays=${encodeURIComponent(JSON.stringify(['/ip4/127.0.0.1/tcp/9001/ws']))}&allowedWsHosts=${encodeURIComponent(JSON.stringify(['localhost']))}`; |
| 82 | + expect(parseCommsQueryString(queryString)).toStrictEqual({ |
| 83 | + relays: ['/ip4/127.0.0.1/tcp/9001/ws'], |
| 84 | + allowedWsHosts: ['localhost'], |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns empty object when no comms params present', () => { |
| 89 | + expect(parseCommsQueryString('?foo=bar')).toStrictEqual({}); |
| 90 | + }); |
| 91 | + |
| 92 | + it('parses directListenAddresses and number options', () => { |
| 93 | + const queryString = `?directListenAddresses=${encodeURIComponent(JSON.stringify(['/ip4/0.0.0.0/udp/0/quic-v1']))}&maxRetryAttempts=5&maxQueue=100`; |
| 94 | + expect(parseCommsQueryString(queryString)).toStrictEqual({ |
| 95 | + directListenAddresses: ['/ip4/0.0.0.0/udp/0/quic-v1'], |
| 96 | + maxRetryAttempts: 5, |
| 97 | + maxQueue: 100, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('parses mnemonic when present', () => { |
| 102 | + const mnemonic = |
| 103 | + 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; |
| 104 | + const queryString = `?mnemonic=${encodeURIComponent(mnemonic)}`; |
| 105 | + expect(parseCommsQueryString(queryString)).toStrictEqual({ mnemonic }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('ignores invalid number values', () => { |
| 109 | + expect(parseCommsQueryString('?maxRetryAttempts=-1')).toStrictEqual({}); |
| 110 | + expect(parseCommsQueryString('?maxRetryAttempts=1.5')).toStrictEqual({}); |
| 111 | + expect(parseCommsQueryString('?maxRetryAttempts=10')).toStrictEqual({ |
| 112 | + maxRetryAttempts: 10, |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('getCommsParamsFromCurrentLocation', () => { |
| 118 | + const originalLocation = globalThis.location; |
| 119 | + |
| 120 | + beforeEach(() => { |
| 121 | + Object.defineProperty(globalThis, 'location', { |
| 122 | + value: { search: '' }, |
| 123 | + writable: true, |
| 124 | + configurable: true, |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + afterEach(() => { |
| 129 | + if (originalLocation) { |
| 130 | + Object.defineProperty(globalThis, 'location', { |
| 131 | + value: originalLocation, |
| 132 | + writable: true, |
| 133 | + configurable: true, |
| 134 | + }); |
| 135 | + } else { |
| 136 | + // @ts-expect-error - deleting global property |
| 137 | + delete globalThis.location; |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + it('returns relays and allowedWsHosts from location', () => { |
| 142 | + const relays = ['/ip4/127.0.0.1/tcp/9001/ws']; |
| 143 | + const allowedWsHosts = ['localhost']; |
| 144 | + globalThis.location.search = `?relays=${encodeURIComponent(JSON.stringify(relays))}&allowedWsHosts=${encodeURIComponent(JSON.stringify(allowedWsHosts))}`; |
| 145 | + expect(getCommsParamsFromCurrentLocation()).toStrictEqual({ |
| 146 | + relays, |
| 147 | + allowedWsHosts, |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('returns empty object when location is undefined', () => { |
| 152 | + // @ts-expect-error - testing undefined location |
| 153 | + delete globalThis.location; |
| 154 | + expect(getCommsParamsFromCurrentLocation()).toStrictEqual({}); |
| 155 | + }); |
| 156 | + |
| 157 | + it('returns all parsed options including numbers and directListenAddresses', () => { |
| 158 | + globalThis.location.search = `?relays=${encodeURIComponent(JSON.stringify(['/ip4/127.0.0.1/tcp/9001/ws']))}&maxQueue=50&stalePeerTimeoutMs=3600000`; |
| 159 | + expect(getCommsParamsFromCurrentLocation()).toStrictEqual({ |
| 160 | + relays: ['/ip4/127.0.0.1/tcp/9001/ws'], |
| 161 | + maxQueue: 50, |
| 162 | + stalePeerTimeoutMs: 3600000, |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments