Refactor: Simplify test 4 to basic Markdown to HTML conversion

This commit is contained in:
2025-11-26 18:09:11 -08:00
parent 714d1149a8
commit 984a4ceb5c

View File

@@ -1,65 +1,20 @@
export default { export default {
functionName: 'bundleApp', functionName: 'renderTemplate',
prompt: `// Write an async JavaScript function 'bundleApp' that acts as a mini-bundler for ES6 modules. prompt: `// Write an async JavaScript function 'renderTemplate' that renders a Handlebars template with data.
// - Input: 'entryFile' (string, e.g., 'main.js') and 'files' (object: { 'filename.js': 'code content' }). // - The function must accept a template string and a data object.
// - You MUST use dynamic import() to load '@babel/standalone' from a CDN. // - You MUST use dynamic import() to load the 'handlebars' library from a CDN.
// - Process: // - Compile the template and execute it with the provided data.
// 1. Recursively traverse dependencies starting from 'entryFile'. // - Return the resulting HTML string.`,
// 2. Use Babel to transform each file's code from ES6+ to CommonJS (presets: ['env']). runTest: async (renderTemplate) => {
// 3. Extract dependencies from the transformed code (look for 'require' calls).
// 4. Build a dependency graph. Throw an error if a file is missing.
// - Output: Return a single string of executable JavaScript code.
// - This code must contain a lightweight runtime that defines 'require', 'module', and 'exports'.
// - It must wrap each module's code in a function.
// - When executed (e.g., via eval), it should run the entry file and return its 'module.exports'.
// - The runtime must handle relative paths (e.g., './utils.js') correctly during resolution.
// - Ensure the generated code is self-contained and does not pollute the global scope.`,
runTest: async (bundleApp) => {
const assert = { const assert = {
strictEqual: (a, e, m) => { if (a !== e) throw new Error(m || `FAIL: ${a} !== ${e}`) }, strictEqual: (a, e, m) => { if (a !== e) throw new Error(m || `FAIL: ${a} !== ${e}`) },
ok: (v, m) => { if (!v) throw new Error(m) }
}; };
const files = {
'main.js': `
import { add, multiply } from './math.js';
import { format } from './utils/formatter.js';
const sum = add(5, 3);
const prod = multiply(2, 4);
export const result = format(sum + prod);
`,
'math.js': `
export const add = (a, b) => a + b;
export const multiply = (a, b) => a * b;
`,
'utils/formatter.js': `
export const format = (n) => \`Value: \${n}\`;
`
};
// 1. Test successful bundling
const bundle = await bundleApp('main.js', files);
assert.ok(typeof bundle === 'string', 'Result must be a string');
assert.ok(bundle.length > 100, 'Bundle seems too short');
assert.ok(!bundle.includes('import '), 'Bundle should not contain ES6 import statements');
// 2. Test execution of the bundle
// We wrap in an IIFE to ensure it returns the exports
const result = eval(bundle);
// Check if result matches expected logic: (5+3) + (2*4) = 8 + 8 = 16 -> "Value: 16" const template = '<div class="user"><h1>{{name}}</h1><p>Score: {{score}}</p></div>';
assert.ok(result && typeof result === 'object', 'Bundle execution should return module.exports object'); const data = { name: 'Alice', score: 100 };
assert.strictEqual(result.result, 'Value: 16', 'Logic execution failed'); const expected = '<div class="user"><h1>Alice</h1><p>Score: 100</p></div>';
// 3. Test Missing File Error const result = await renderTemplate(template, data);
const brokenFiles = { 'main.js': "import './missing.js';" }; assert.strictEqual(result, expected, 'Test Failed: Rendered output does not match expected HTML.');
try {
await bundleApp('main.js', brokenFiles);
throw new Error('Should have thrown on missing file');
} catch (e) {
assert.ok(e.message.toLowerCase().includes('missing') || e.message.includes('found'), 'Error message should mention missing file');
}
} }
}; };