Loading and executing a single flow tab in the backend?

Hi There,

This is very specific question related to me trying to develop a collection of flows that test and verify the functionality of the NR code nodes. The test flows already exist, and the node package for Erlang-Red already works and what that looks like is shown in this animation - for Erlang-Red.

What the testing package does is deploy each test flow in turn, execute that flow in the backend and display a tick or cross in the editor. It does this in Erlang-Red because I made it do that!

Now I want to do the same for Node-RED. Why? For the purposes of verification of core functionality, so that on a very high level, nothing breaks between releases but also that others can implement Node-RED functionality in other programming languages and be assured that compatibility is guaranteed.

It doesn't work in Node-RED because I don't think its possible to load just one flow, execute that flow (by triggering an inject node in the flow) and then unloading the flow again. Without completely unloading the existing flow.

Basically each test flow is a tab with a flow definition and assert nodes to ensure correct functionality. All I want to do is load and add the tab in the backend (not in the editor) flow, execute the tab and then unload the tab - I think that makes sense?

The code that I tried was this:

        const runtime = require("@node-red/runtime");

        let flowDetails = JSON.parse(fs.readFileSync(filename))
        let flowId = req.params.flowid

        let tabNode = flowDetails.filter( d => d.type == "tab")[0]
        let nonTabNodes = flowDetails.filter(d => d.type != "tab")

        let details = {
          ...tabNode,
          nodes: nonTabNodes,
        }

        runtime._.flows.updateFlow(flowId, details, "root").then( _ignore => {
          runtime._.flows.removeFlow(flowId, "root").then( _alsoIgnore => {
            RED.comms.publish("unittesting:testresults", {
              flowid: path.basename(path.parse(filename).dir),
              status: "success"
            })
          })
        })

The runtime API that I'm using is defined here. The filename is a flows.json of the tab, an example file.

The code above does not work because the flow id is unknown and updateFlow fails on this check:

        if (!activeFlowConfig.flows[id]) {
            var e = new Error();
            e.code = 404;
            throw e;
        }

There is also an addFlow API but that assigns new flow ids, which I don't want since I have a flow id.

Important is that I don't want to do this in the editor, i.e. with an import flow dialog, since there are several hundred flow tests! It should all happen seamlessly in the backend.

I'd be happy to spawn a new process for each test and have them run in parallel, but I don't know whether that is possible from within Node-RED itself? After all, tests are independent of one another and have no context or state.

More an idea than a solution: The test suit - powered by mocha - does something very similar to what you intend: Initialize the runtime, load a single flow & execute this - server side.
Perhaps worth to validate...