This JavaScript module exports a testProcessKernel
function that creates a new kernel instance and tests its functionality by executing a message and shutting down the kernel. The function uses the bashKernel
function to create a new kernel instance and sets up a custom do_respond
function to simulate a response to executed messages.
npm run import -- "test repl process using bash"
var importer = require('../Core');
var {processMetaKernel} = processMethods = importer.import("process meta kernel")
var metaKernelInterface = importer.import("meta kernel interface");
var interface = importer.import("enforcing an interface");
function do_respond(message) {
console.log(`response "${message.toString()}"`);
}
// TODO: move this to ibash kernel below
function testProcessKernel() {
return new Promise(resolve => {
var kernel = bashKernel({
do_respond: (message) => {
setTimeout(resolve.bind(kernel, kernel), 1000);
return do_respond(message);
},
kernel_config: {
autoinit: true,
// because its a test with no connection file
// so native doesn't automatically start
}
})
setTimeout(() => {
console.log(interface(kernel, metaKernelInterface, false))
console.log(kernel.do_message({do_execute: `hello`}))
}, 500)
})
.then(kernel => console.log(kernel.do_shutdown()))
}
module.exports = testProcessKernel;
if(typeof $ !== 'undefined') {
$.async();
testNodeProcessKernel();
/* expected output
undefined
undefined
2
2
*/
}
const { importCore } = require('../Core');
const { processMetaKernel, processMethods } = importCore('process meta kernel');
const { metaKernelInterface } = importCore('meta kernel interface');
const { interface } = importCore('enforcing an interface');
const bashKernel = importCore('bash kernel');
/**
* Respond to a message and log it to the console.
* @param {string} message - The message to respond to.
*/
function doRespond(message) {
console.log(`Response: "${message.toString()}"`);
}
/**
* Test the process kernel by creating a new kernel instance and executing a command.
* @returns {Promise} A promise that resolves when the kernel is shut down.
*/
async function testProcessKernel() {
try {
const kernel = await bashKernel({
doRespond: (message) => {
setTimeout(() => doRespond(message), 1000);
return doRespond(message);
},
kernelConfig: {
autoInit: true,
},
});
await new Promise(resolve => setTimeout(resolve, 500));
console.log(interface(kernel, metaKernelInterface, false));
const result = await kernel.doMessage({ doExecute: 'hello' });
console.log(result);
console.log(await kernel.doShutdown());
} catch (error) {
console.error(error);
}
}
module.exports = testProcessKernel;
if (typeof $!== 'undefined') {
$.async();
testProcessKernel();
}
This code defines a JavaScript module that exports a testProcessKernel
function. The function creates a new instance of a kernel using the bashKernel
function and tests its functionality by executing a message and shutting down the kernel.
var importer = require('../Core');
var {processMetaKernel} = processMethods = importer.import('process meta kernel')
var metaKernelInterface = importer.import('meta kernel interface');
var interface = importer.import('enforcing an interface');
../Core
) using the require
function.import
function.function do_respond(message) {
console.log(`response "${message.toString()}"`);
}
function testProcessKernel() {
//...
}
do_respond
function logs a response to the console with the provided message.testProcessKernel
function creates a new kernel instance and tests its functionality.function testProcessKernel() {
return new Promise(resolve => {
var kernel = bashKernel({
do_respond: (message) => {
setTimeout(resolve.bind(kernel, kernel), 1000);
return do_respond(message);
},
kernel_config: {
autoinit: true,
// because its a test with no connection file
// so native doesn't automatically start
}
})
setTimeout(() => {
console.log(interface(kernel, metaKernelInterface, false))
console.log(kernel.do_message({do_execute: `hello`}))
}, 500)
})
.then(kernel => console.log(kernel.do_shutdown()))
}
testProcessKernel
function creates a new kernel instance using the bashKernel
function.do_respond
function that logs a response to the console after a 1-second delay.interface
function with the kernel instance and metaKernelInterface
as arguments.do_execute
property set to "hello"
.do_shutdown
function on the kernel instance.testProcessKernel
Functionmodule.exports = testProcessKernel;
if(typeof $!== 'undefined') {
$.async();
testNodeProcessKernel();
/* expected output
undefined
undefined
2
2
*/
}
testProcessKernel
function as a module.$
variable is defined, it runs the async
function on it and then calls the testNodeProcessKernel
function.