|
| 1 | +import { expect } from 'chai' |
| 2 | + |
| 3 | +import { clearDnsProbeCache, runProbe } from '../../../src/utils/relay-probe/index' |
| 4 | + |
| 5 | +const PUBKEY = '22e804d26ed16b68db5259e78449e96dab5d464c8f470bda3eb1a70467f2c793' |
| 6 | + |
| 7 | +const makeClients = (overrides: Record<string, unknown> = {}) => ({ |
| 8 | + dns: { |
| 9 | + resolve4: async () => [{ type: 'A' as const, value: '93.184.216.34', ttl: 300 }], |
| 10 | + resolve6: async () => [], |
| 11 | + resolveCname: async () => { |
| 12 | + throw Object.assign(new Error('ENODATA'), { code: 'ENODATA' }) |
| 13 | + }, |
| 14 | + }, |
| 15 | + tls: { |
| 16 | + connect: async () => ({ |
| 17 | + valid: true, |
| 18 | + issuer: 'Example CA', |
| 19 | + subject: 'relay.example.com', |
| 20 | + expiresAt: new Date('2027-01-01T00:00:00.000Z'), |
| 21 | + daysUntilExpiry: 365, |
| 22 | + }), |
| 23 | + }, |
| 24 | + ws: { measureOpenRtt: async () => 42 }, |
| 25 | + nip11: { |
| 26 | + fetch: async () => ({ |
| 27 | + statusCode: 200, |
| 28 | + name: 'relay.example.com', |
| 29 | + pubkey: PUBKEY, |
| 30 | + }), |
| 31 | + }, |
| 32 | + ...overrides, |
| 33 | +}) |
| 34 | + |
| 35 | +describe('runProbe', () => { |
| 36 | + afterEach(() => { |
| 37 | + clearDnsProbeCache() |
| 38 | + }) |
| 39 | + |
| 40 | + it('returns structured per-check results for a clearnet relay', async () => { |
| 41 | + const result = await runProbe('wss://relay.example.com', {}, makeClients()) |
| 42 | + |
| 43 | + expect(result.target.hostname).to.equal('relay.example.com') |
| 44 | + expect(result.dns.status).to.equal('ok') |
| 45 | + expect(result.dns.data?.records).to.deep.include({ type: 'A', value: '93.184.216.34', ttl: 300 }) |
| 46 | + expect(result.tls.status).to.equal('ok') |
| 47 | + expect(result.wsRtt.status).to.equal('ok') |
| 48 | + expect(result.nip11.status).to.equal('ok') |
| 49 | + }) |
| 50 | + |
| 51 | + it('skips DNS for onion destinations', async () => { |
| 52 | + const result = await runProbe( |
| 53 | + 'wss://abc123def456.onion', |
| 54 | + {}, |
| 55 | + makeClients({ |
| 56 | + dns: { |
| 57 | + resolve4: async () => { |
| 58 | + throw new Error('DNS should not be called for onion hosts') |
| 59 | + }, |
| 60 | + resolve6: async () => [], |
| 61 | + resolveCname: async () => [], |
| 62 | + }, |
| 63 | + }), |
| 64 | + ) |
| 65 | + |
| 66 | + expect(result.target.networkType).to.equal('tor') |
| 67 | + expect(result.dns.status).to.equal('skipped') |
| 68 | + }) |
| 69 | + |
| 70 | + it('uses the DNS cache on repeated probes', async () => { |
| 71 | + let resolveCount = 0 |
| 72 | + const clients = makeClients({ |
| 73 | + dns: { |
| 74 | + resolve4: async () => { |
| 75 | + resolveCount += 1 |
| 76 | + return [{ type: 'A' as const, value: '93.184.216.34', ttl: 300 }] |
| 77 | + }, |
| 78 | + resolve6: async () => [], |
| 79 | + resolveCname: async () => { |
| 80 | + throw Object.assign(new Error('ENODATA'), { code: 'ENODATA' }) |
| 81 | + }, |
| 82 | + }, |
| 83 | + }) |
| 84 | + |
| 85 | + await runProbe('wss://relay.example.com', {}, clients) |
| 86 | + const second = await runProbe('wss://relay.example.com', {}, clients) |
| 87 | + |
| 88 | + expect(resolveCount).to.equal(1) |
| 89 | + expect(second.dns.data?.fromCache).to.equal(true) |
| 90 | + }) |
| 91 | + |
| 92 | + it('surfaces probe errors without failing the full run', async () => { |
| 93 | + const result = await runProbe( |
| 94 | + 'wss://relay.example.com', |
| 95 | + {}, |
| 96 | + makeClients({ |
| 97 | + tls: { |
| 98 | + connect: async () => { |
| 99 | + throw new Error('certificate expired') |
| 100 | + }, |
| 101 | + }, |
| 102 | + }), |
| 103 | + ) |
| 104 | + |
| 105 | + expect(result.tls.status).to.equal('error') |
| 106 | + expect(result.tls.error).to.include('certificate expired') |
| 107 | + }) |
| 108 | + |
| 109 | + it('times out stalled DNS probes without blocking other checks', async () => { |
| 110 | + const result = await runProbe( |
| 111 | + 'wss://relay.example.com', |
| 112 | + { timeouts: { dnsMs: 50, tlsMs: 50, wsRttMs: 50, nip11Ms: 50 } }, |
| 113 | + makeClients({ |
| 114 | + dns: { |
| 115 | + resolve4: () => new Promise(() => undefined), |
| 116 | + resolve6: async () => [], |
| 117 | + resolveCname: async () => { |
| 118 | + throw Object.assign(new Error('ENODATA'), { code: 'ENODATA' }) |
| 119 | + }, |
| 120 | + }, |
| 121 | + }), |
| 122 | + ) |
| 123 | + |
| 124 | + expect(result.dns.status).to.equal('error') |
| 125 | + expect(result.dns.error).to.include('DNS probe timed out') |
| 126 | + expect(result.tls.status).to.equal('ok') |
| 127 | + }).timeout(5000) |
| 128 | + |
| 129 | + it('accepts NIP-11 documents without name or pubkey fields', async () => { |
| 130 | + const result = await runProbe( |
| 131 | + 'wss://relay.example.com', |
| 132 | + {}, |
| 133 | + makeClients({ |
| 134 | + nip11: { |
| 135 | + fetch: async () => ({ statusCode: 200 }), |
| 136 | + }, |
| 137 | + }), |
| 138 | + ) |
| 139 | + |
| 140 | + expect(result.nip11.status).to.equal('ok') |
| 141 | + expect(result.nip11.data?.name).to.equal(undefined) |
| 142 | + }) |
| 143 | +}) |
0 commit comments