// Tests that the network guard actually blocks outbound network access. // // This is a safety-of-the-harness test: it proves that once blockNetwork() is // installed, fetch / raw socket connect / DNS lookup all throw instead of // dialing out, and that restoreNetwork() puts the originals back. No real // connection is ever attempted because every primitive throws synchronously. import test from 'node:test'; import assert from 'node:assert/strict'; import dns from 'node:dns'; import net from 'node:net'; import { blockNetwork, restoreNetwork } from './setup.network-guard.mjs'; test('fetch throws once the guard is installed', async () => { blockNetwork(); try { await assert.rejects( async () => globalThis.fetch('https://example.com'), /network-guard/ ); } finally { restoreNetwork(); } }); test('raw TCP socket connect throws once the guard is installed', () => { blockNetwork(); try { const socket = new net.Socket(); assert.throws(() => socket.connect(80, 'example.com'), /network-guard/); socket.destroy(); } finally { restoreNetwork(); } }); test('dns.lookup throws once the guard is installed', () => { blockNetwork(); try { assert.throws(() => dns.lookup('example.com', () => {}), /network-guard/); } finally { restoreNetwork(); } }); test('restoreNetwork puts the original primitives back', () => { const originalFetch = globalThis.fetch; const originalConnect = net.Socket.prototype.connect; const originalLookup = dns.lookup; blockNetwork(); assert.notEqual(globalThis.fetch, originalFetch); restoreNetwork(); assert.equal(globalThis.fetch, originalFetch); assert.equal(net.Socket.prototype.connect, originalConnect); assert.equal(dns.lookup, originalLookup); }); test('blockNetwork is idempotent (double install does not double-wrap)', () => { blockNetwork(); const wrapped = globalThis.fetch; blockNetwork(); assert.equal(globalThis.fetch, wrapped); restoreNetwork(); });