kernels | TODO: repl process kernel | , jupyter wire interface | Search

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.

Run example

npm run import -- "test repl process using bash"

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
    */

}

What the code could have been:

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();
}

Overview

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.

Importing and Initializing Variables

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');

Defining Functions

function do_respond(message) {
    console.log(`response "${message.toString()}"`);
}

function testProcessKernel() {
    //...
}

Creating a Kernel Instance and Testing 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()))
}

Exporting and Running the testProcessKernel Function

module.exports = testProcessKernel;

if(typeof $!== 'undefined') {
    $.async();
    testNodeProcessKernel();
    
    /* expected output
    undefined
    undefined
    2
    2
    */

}