MCPcopy Create free account
hub / github.com/adcontextprotocol/adcp / testJavaScriptSnippet

Function testJavaScriptSnippet

tests/snippet-validation.test.cjs:229–308  ·  view source on GitHub ↗

* Test a JavaScript/TypeScript snippet

(snippet)

Source from the content-addressed store, hash-verified

227 * Test a JavaScript/TypeScript snippet
228 */
229async function testJavaScriptSnippet(snippet) {
230 // Detect ESM syntax and use .mjs extension to avoid Node warnings
231 const hasESMSyntax = snippet.code.includes('import ') || snippet.code.includes('export ');
232 const extension = hasESMSyntax ? '.mjs' : '.js';
233 const tempFile = path.join(__dirname, `temp-snippet-${Date.now()}${extension}`);
234
235 try {
236 // Write snippet to temporary file
237 fs.writeFileSync(tempFile, snippet.code);
238
239 // Execute with Node.js from project root to access node_modules
240 // Set auth token env var - the SDK looks for env var named by auth_token_env value
241 const { stdout, stderr } = await execFileAsync('node', [tempFile], {
242 timeout: 60000, // 60 second timeout (API calls can take time)
243 cwd: path.join(__dirname, '..'), // Run from project root
244 env: {
245 ...process.env,
246 [MCP_TOKEN]: MCP_TOKEN, // SDK expects env var named by token value to contain token
247 [A2A_TOKEN]: A2A_TOKEN
248 }
249 });
250
251 // Check if stderr contains only warnings (not errors)
252 const hasRealErrors = stderr && !stderr.includes('[MODULE_TYPELESS_PACKAGE_JSON]');
253
254 // Check if the output contains a failed API response (result.success === false)
255 const apiError = checkForFailedApiResponse(stdout);
256 if (apiError) {
257 return {
258 success: false,
259 error: `API call failed: ${apiError}`,
260 output: stdout,
261 stderr: stderr
262 };
263 }
264
265 return {
266 success: true,
267 output: stdout,
268 error: hasRealErrors ? stderr : null
269 };
270 } catch (error) {
271 // Tests may fail with errors but still produce valid output
272 // If we got stdout output, treat it as a success (the actual test ran)
273 if (error.stdout && error.stdout.trim().length > 0) {
274 // But check if it's a failed API response
275 const apiError = checkForFailedApiResponse(error.stdout);
276 if (apiError) {
277 return {
278 success: false,
279 error: `API call failed: ${apiError}`,
280 output: error.stdout,
281 stderr: error.stderr
282 };
283 }
284
285 return {
286 success: true,

Callers 1

validateSnippetFunction · 0.85

Calls 3

nowMethod · 0.80
trimMethod · 0.65

Tested by

no test coverage detected