import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { builtinModules } from 'node:module';
function makeCase(name, expected, actual) {
return {
name,
passed: JSON.stringify(expected) === JSON.stringify(actual),
expected,
actual,
};
}
// Case 1: verify core built-in modules referenced across Node.js quiz questions
function testCoreModulesAndRuntime() {
const checked = ['fs', 'http', 'path', 'events', 'zlib', 'url'];
const expected = {
isNodeV24: true,
allQuizModulesBuiltin: true,
};
const actual = {
isNodeV24: process.version.startsWith('v24.'),
allQuizModulesBuiltin: checked.every((name) => builtinModules.includes(name)),
};
return makeCase('core_modules_and_runtime', expected, actual);
}
// Case 2: globals (process, console.log), Buffer binary handling, and directory path
function testGlobalsBufferAndPaths() {
const buf = Buffer.from('Node', 'utf8');
const expected = {
hasProcessObject: true,
hasConsoleLog: true,
bufferByteLength: 4,
bufferIsUint8Array: true,
dirnameIsAbsolute: true,
};
const actual = {
hasProcessObject: typeof process === 'object' && typeof process.pid === 'number',
hasConsoleLog: typeof console.log === 'function',
bufferByteLength: buf.length,
bufferIsUint8Array: buf instanceof Uint8Array,
dirnameIsAbsolute: path.isAbsolute(import.meta.dirname),
};
return makeCase('globals_buffer_and_paths', expected, actual);
}
// Case 3: async callback JSON file reading and process uncaughtException API presence
async function testAsyncCallbackAndJsonIo() {
const tmpFile = path.join(os.tmpdir(), `mbsky-quiz-${process.pid}-${Date.now()}.json`);
fs.writeFileSync(tmpFile, JSON.stringify({ name: 'demo', dependencies: {} }), 'utf8');
try {
const parsed = await new Promise((resolve, reject) => {
fs.readFile(tmpFile, 'utf8', (err, data) => {
if (err) reject(err);
else resolve(JSON.parse(data));
});
});
const expected = {
jsonName: 'demo',
hasDependenciesObj: true,
hasProcessOn: true,
};
const actual = {
jsonName: parsed.name,
hasDependenciesObj: typeof parsed.dependencies === 'object',
hasProcessOn: typeof process.on === 'function',
};
return makeCase('async_callback_and_json_io', expected, actual);
} finally {
fs.unlinkSync(tmpFile);
}
}
// Case 4: deterministic 20-topic quiz scoring helper
function testQuizScoringEngine() {
const answerKey = [
'runtime', 'fs', 'npm', 'http', 'path',
'require', 'events', 'process', 'console.log', 'zlib',
'binary', '__dirname', 'fs.readFile', 'npm install', 'async',
'module.exports', 'url', 'uncaughtException', 'package.json', 'io-concurrency',
];
function grade(submissions) {
let score = 0;
for (let i = 0; i < answerKey.length; i += 1) {
if (submissions[i] === answerKey[i]) score += 1;
}
return { score, total: answerKey.length };
}
const perfect = grade(answerKey);
const twoWrong = grade(answerKey.map((a, idx) => (idx < 2 ? 'wrong' : a)));
const expected = {
questionCount: 20,
perfect: { score: 20, total: 20 },
twoWrong: { score: 18, total: 20 },
};
const actual = {
questionCount: answerKey.length,
perfect,
twoWrong,
};
return makeCase('quiz_scoring_engine', expected, actual);
}
const cases = [
testCoreModulesAndRuntime(),
testGlobalsBufferAndPaths(),
await testAsyncCallbackAndJsonIo(),
testQuizScoringEngine(),
];
console.log(JSON.stringify({ version: process.version, cases }));
本例需在 Windows x64、Node.js v24.22.3 本地运行环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、Node.js v24.22.3 本地运行环境。
入口文件:projects/nodejs-quiz-lab/explore-quiz.mjs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-quiz.mjs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、Node.js v24.22.3 本地运行环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a1041.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1068.zip