import test from 'node:test'; import assert from 'node:assert/strict'; import { registrableApex, applyHostRole, analyseMailAuth } from '../src/checks/mail-auth.mjs'; import { resolvePublic } from '../src/lib/net-guard.mjs'; test('registrableApex keeps a Malaysian second-level suffix intact', () => { assert.equal(registrableApex('example.com.my'), 'example.com.my'); assert.equal(registrableApex('www.example.com.my'), 'example.com.my'); assert.equal(registrableApex('tools.hoelee.com'), 'hoelee.com'); assert.equal(registrableApex('hoelee.com'), 'hoelee.com'); assert.equal(registrableApex('a.b.example.org.my'), 'example.org.my'); assert.equal(registrableApex('localhost'), 'localhost'); }); const NOTHING = { domain: 'tools.example.com', fatal: null, spfRecords: [], dmarc: { checked: true, code: null, record: null, parsed: null }, dkim: [{ selector: 'default', status: 'none', key: false }], mx: { checked: true, code: null, records: [], nullMx: false, addresses: [] } }; function envelopeOf(state, findings) { return { id: 'mail-auth', state, summary: 'x', evidence: [], findings, checkedAt: new Date(0).toISOString(), fromCache: false }; } test('a subdomain that publishes no mail reads as OK, pointing at the domain that does', () => { const analysed = analyseMailAuth(NOTHING, { now: 0 }); assert.equal(analysed.state, 'fail', 'the raw analysis does accuse a bare subdomain'); const fixed = applyHostRole(analysed, 'tools.example.com', NOTHING); assert.equal(fixed.state, 'ok'); assert.equal(fixed.findings.length, 1); assert.equal(fixed.findings[0].code, 'subdomain_no_mail'); assert.match(fixed.summary, /example\.com/); assert.match(fixed.summary, /does not send or receive email/); }); test('a subdomain that does publish SPF keeps its real verdict', () => { const gathered = { ...NOTHING, spfRecords: ['v=spf1 include:_spf.example.com -all'] }; const analysed = analyseMailAuth(gathered, { now: 0 }); const out = applyHostRole(analysed, 'mail.example.com', gathered); assert.equal(out.state, analysed.state); assert.equal(out.findings[0].code === 'subdomain_no_mail', false); }); test('the apex itself is never downgraded', () => { const analysed = analyseMailAuth(NOTHING, { now: 0 }); const out = applyHostRole(analysed, 'example.com', NOTHING); assert.equal(out.state, 'fail'); }); test('a null MX subdomain is a deliberate setup, not "publishes nothing"', () => { const gathered = { ...NOTHING, mx: { checked: true, code: null, records: [], nullMx: true, addresses: [] } }; const out = applyHostRole(analyseMailAuth(gathered, { now: 0 }), 'sub.example.com', gathered); assert.equal(out.findings.some((f) => f.code === 'subdomain_no_mail'), false); }); test('resolvePublic hands back IPv4 first so a broken IPv6 route cannot stall a check', async () => { const lookup = async () => [ { address: '2606:4700:4700::1111', family: 6 }, { address: '93.184.216.34', family: 4 } ]; const out = await resolvePublic('example.com', { lookup }); assert.equal(out[0].family, 4); assert.equal(out[1].family, 6); });