import test from 'node:test'; import assert from 'node:assert/strict'; import { analyseHttp, analyseHttpWithImageProbe, probePreviewImage, describeImageProbe } from '../src/checks/http.mjs'; import { NetGuardError } from '../src/lib/net-guard.mjs'; // The declared og:image is the half of the preview check that used to be taken on faith: the tag // was parsed and its address printed, but nobody asked what that address returns. Every test here // runs offline through the injectable `request` transport - no socket, no resolver, no server. const NOW = Date.UTC(2026, 0, 15, 12, 0, 0); const IMAGE = 'https://www.hoelee.com/card.png'; const SECURE_HEADERS = { 'strict-transport-security': 'max-age=31536000; includeSubDomains', 'content-security-policy': "default-src 'self'; frame-ancestors 'none'", 'x-content-type-options': 'nosniff', 'x-frame-options': 'DENY', 'referrer-policy': 'strict-origin-when-cross-origin', 'permissions-policy': 'camera=(), microphone=()', 'content-type': 'text/html; charset=utf-8' }; function bodyWithImage(image = IMAGE) { return `
hello
`; } const NO_IMAGE_BODY = `hello
`; function page(body, { status = 200, url = 'https://www.hoelee.com/check/', headers = SECURE_HEADERS } = {}) { return { hops: [{ url, status, location: null, totalMs: 40 }], final: { status, headers, body, url, truncated: false, tls: { authorized: true }, timing: { totalMs: 90 } }, body, headers, truncated: false }; } /** A response shaped like net-guard's requestOnce(), built without a request. */ function response({ status = 200, headers = {}, truncated = false } = {}) { return { status, headers, body: '', url: IMAGE, address: '203.0.113.10', family: 4, truncated, tls: null, timing: { totalMs: 12 } }; } /** Recording stub transport: `handler(url, options, callNumber)` decides each answer. */ function transport(handler) { const calls = []; const fn = async (url, options = {}) => { calls.push({ url, ...options }); return handler(url, options, calls.length); }; fn.calls = calls; return fn; } function byCode(analysis, code) { return analysis.findings.find((finding) => finding.code === code); } // --- the image is fine ------------------------------------------------------------------------ test('a 200 image/png at 120 KB is fetched with one HEAD and reported as usable', async () => { const request = transport(() => response({ status: 200, headers: { 'content-type': 'image/png', 'content-length': String(120 * 1024) } })); const probe = await probePreviewImage(IMAGE, { request }); assert.equal(probe.checked, true); assert.equal(probe.status, 200); assert.equal(probe.type, 'image/png'); assert.equal(probe.bytes, 122_880); assert.equal(probe.problem, null); assert.equal(request.calls.length, 1); assert.equal(request.calls[0].method, 'HEAD', 'a healthy image must not be downloaded'); const analysis = await analyseHttpWithImageProbe(page(bodyWithImage()), { host: 'www.hoelee.com', now: NOW, request }); assert.equal(analysis.state, 'ok'); assert.deepEqual(analysis.findings.filter((finding) => finding.level === 'warn' || finding.level === 'fail'), []); assert.equal(analysis.preview.imageProbe.checked, true); assert.match(describeImageProbe(analysis.preview.imageProbe), /200 image\/png, 120 KB/); }); // --- the image is broken ---------------------------------------------------------------------- test('an og:image that answers 404 is a warning that names the status', async () => { const request = transport(() => response({ status: 404, headers: { 'content-type': 'text/html' } })); const analysis = await analyseHttpWithImageProbe(page(bodyWithImage()), { host: 'www.hoelee.com', now: NOW, request }); const finding = byCode(analysis, 'og_image_unreachable'); assert.ok(finding, 'a 404 on the declared image must produce og_image_unreachable'); assert.equal(finding.level, 'warn'); assert.match(finding.message, /404/); assert.equal(analysis.state, 'warn', 'a missing preview image is a warning, never a failure'); assert.equal(analysis.preview.hasImage, true, 'the tag is still declared, the card still points at it'); }); test('a 2 MB og:image warns about the size with the real number', async () => { const request = transport(() => response({ status: 200, headers: { 'content-type': 'image/png', 'content-length': String(2 * 1024 * 1024) } })); const analysis = await analyseHttpWithImageProbe(page(bodyWithImage()), { host: 'www.hoelee.com', now: NOW, request }); const finding = byCode(analysis, 'og_image_too_large'); assert.ok(finding); assert.equal(finding.level, 'warn'); assert.match(finding.message, /2\.0 MB/); assert.match(finding.message, /300 KB/); assert.equal(analysis.state, 'warn'); assert.notEqual(analysis.state, 'fail'); }); test('an og:image that returns text/html is a warning about the wrong type', async () => { const request = transport(() => response({ status: 200, headers: { 'content-type': 'text/html; charset=utf-8', 'content-length': '5321' } })); const analysis = await analyseHttpWithImageProbe(page(bodyWithImage()), { host: 'www.hoelee.com', now: NOW, request }); const finding = byCode(analysis, 'og_image_not_an_image'); assert.ok(finding); assert.equal(finding.level, 'warn'); assert.match(finding.message, /text\/html/); assert.match(finding.message, /blank or wrong card/); }); test('a WebP image is mentioned as a rendering risk, never called broken', async () => { const request = transport(() => response({ status: 200, headers: { 'content-type': 'image/webp', 'content-length': '48000' } })); const analysis = await analyseHttpWithImageProbe(page(bodyWithImage()), { host: 'www.hoelee.com', now: NOW, request }); const finding = byCode(analysis, 'og_image_type_weak'); assert.ok(finding); assert.equal(finding.level, 'info', 'WebP renders in most current clients: a note, not a warning'); assert.match(finding.message, /WebP/); assert.doesNotMatch(finding.message, /broken|invalid|corrupt/i); assert.match(finding.message, /may appear without a picture/); assert.equal(analysis.state, 'ok', 'an info row must not change the state'); }); // --- we could not look ------------------------------------------------------------------------ test('a loopback og:image is never requested and never called broken', async () => { const request = transport(() => { throw new Error('the stub transport must not be called for a blocked address'); }); const probe = await probePreviewImage('http://127.0.0.1/card.png', { request }); assert.equal(probe.checked, false); assert.equal(request.calls.length, 0, 'net-guard refuses the address before any socket is opened'); assert.doesNotMatch(probe.reason, /broken/i); const analysis = await analyseHttpWithImageProbe(page(bodyWithImage('http://127.0.0.1/card.png')), { host: 'www.hoelee.com', now: NOW, request }); assert.equal(request.calls.length, 0); const finding = byCode(analysis, 'og_image_not_checked'); assert.ok(finding); assert.equal(finding.level, 'info'); assert.doesNotMatch(finding.message, /broken|fails|failed/i); assert.equal(analysis.state, 'ok', 'an image we refused to fetch says nothing about the page'); }); test('a private hostname resolved by DNS is reported as not checked', async () => { const request = transport(() => { throw new NetGuardError('private_address', 'That domain points at a private address.'); }); const probe = await probePreviewImage(IMAGE, { request }); assert.equal(probe.checked, false); assert.match(probe.reason, /not one this tool will fetch/); }); test('a timed-out og:image is not checked, and the page keeps the state it had', async () => { const request = transport(() => { throw new NetGuardError('timeout', 'That took too long.'); }); const analysis = await analyseHttpWithImageProbe(page(bodyWithImage()), { host: 'www.hoelee.com', now: NOW, request }); const finding = byCode(analysis, 'og_image_not_checked'); assert.ok(finding); assert.equal(finding.level, 'info'); assert.match(finding.message, /did not answer within the time allowed/); assert.equal(analysis.state, 'ok'); assert.equal(analysis.preview.imageProbe.checked, false); assert.equal(analysis.preview.imageProbe.problem, null); }); // --- when there is nothing to probe ----------------------------------------------------------- test('a page with no og:image is not probed at all', async () => { const request = transport(() => { throw new Error('the stub transport must not be called when no image is declared'); }); const analysis = await analyseHttpWithImageProbe(page(NO_IMAGE_BODY), { host: 'www.hoelee.com', now: NOW, request }); assert.equal(request.calls.length, 0); assert.equal(byCode(analysis, 'og_image_missing').level, 'warn'); assert.equal(analysis.preview.imageProbe, undefined); assert.equal(analysis.state, 'warn'); }); test('a challenge page is not probed, even when it carries an og:image tag', async () => { const request = transport(() => { throw new Error('the stub transport must not be called for a challenge page'); }); const body = `