|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +import { strict as assert } from 'node:assert'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { |
| 6 | + createTestResults, |
| 7 | + printTestSummary, |
| 8 | + testFunction, |
| 9 | +} from '../helpers/test-utils.js'; |
| 10 | +import { snapshotProcessEnv } from '../helpers/env-utils.js'; |
| 11 | + |
| 12 | +const results = createTestResults(); |
| 13 | +const isolationKey = 'MCP_SEARXNG_TEST_ENV_ISOLATION'; |
| 14 | + |
| 15 | +async function runTests() { |
| 16 | + console.log('🧪 Testing: test-utils.ts\n'); |
| 17 | + |
| 18 | + await testFunction('testFunction restores process.env after callbacks pass or fail', async () => { |
| 19 | + const originalValue = process.env[isolationKey]; |
| 20 | + const originalLog = console.log; |
| 21 | + const nestedResults = createTestResults(); |
| 22 | + |
| 23 | + try { |
| 24 | + delete process.env[isolationKey]; |
| 25 | + console.log = () => {}; |
| 26 | + |
| 27 | + await testFunction('intentional failure', () => { |
| 28 | + process.env[isolationKey] = 'failed-test-value'; |
| 29 | + throw new Error('intentional harness failure'); |
| 30 | + }, nestedResults); |
| 31 | + |
| 32 | + assert.equal(nestedResults.failed, 1); |
| 33 | + assert.equal(process.env[isolationKey], undefined); |
| 34 | + |
| 35 | + process.env[isolationKey] = 'baseline-value'; |
| 36 | + await testFunction('intentional success', () => { |
| 37 | + process.env[isolationKey] = 'successful-test-value'; |
| 38 | + }, nestedResults); |
| 39 | + |
| 40 | + assert.equal(nestedResults.passed, 1); |
| 41 | + assert.equal(process.env[isolationKey], 'baseline-value'); |
| 42 | + } finally { |
| 43 | + console.log = originalLog; |
| 44 | + if (originalValue === undefined) { |
| 45 | + delete process.env[isolationKey]; |
| 46 | + } else { |
| 47 | + process.env[isolationKey] = originalValue; |
| 48 | + } |
| 49 | + } |
| 50 | + }, results); |
| 51 | + |
| 52 | + await testFunction('snapshotProcessEnv preserves prototype-like environment keys', () => { |
| 53 | + const key = '__proto__'; |
| 54 | + const originalValue = process.env[key]; |
| 55 | + |
| 56 | + try { |
| 57 | + process.env[key] = 'prototype-key-value'; |
| 58 | + const snapshot = snapshotProcessEnv(); |
| 59 | + |
| 60 | + assert.equal(Object.getPrototypeOf(snapshot), null); |
| 61 | + assert.equal(Object.hasOwn(snapshot, key), true); |
| 62 | + assert.equal(snapshot[key], 'prototype-key-value'); |
| 63 | + } finally { |
| 64 | + if (originalValue === undefined) { |
| 65 | + delete process.env[key]; |
| 66 | + } else { |
| 67 | + process.env[key] = originalValue; |
| 68 | + } |
| 69 | + } |
| 70 | + }, results); |
| 71 | + |
| 72 | + printTestSummary(results, 'Test Utilities'); |
| 73 | + return results; |
| 74 | +} |
| 75 | + |
| 76 | +if (process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1]) { |
| 77 | + runTests().then((testResults) => { |
| 78 | + process.exit(testResults.failed > 0 ? 1 : 0); |
| 79 | + }).catch(console.error); |
| 80 | +} |
| 81 | + |
| 82 | +export { runTests }; |
0 commit comments