|
| 1 | +import test from "ava"; |
| 2 | +import { KitBuilder } from "../src/kits/builder.js"; |
| 3 | +import { Board } from "../src/board.js"; |
| 4 | + |
| 5 | +test("KitBuilder can wrap a function", async (t) => { |
| 6 | + |
| 7 | + // A normal function that will be wrapped. |
| 8 | + const echo = (input: string) => input; |
| 9 | + const test = (input: string) => input; |
| 10 | + |
| 11 | + const MyKit = KitBuilder.wrap({ url: "test" }, { echo, test }); |
| 12 | + |
| 13 | + const board = new Board({ |
| 14 | + title: "Test Echo", |
| 15 | + description: "Test Breadboard Kit", |
| 16 | + version: "0.0.1", |
| 17 | + }); |
| 18 | + |
| 19 | + const myKit = board.addKit(MyKit); |
| 20 | + |
| 21 | + t.true(myKit.echo instanceof Function); |
| 22 | + t.true(myKit.test instanceof Function); |
| 23 | +}); |
| 24 | + |
| 25 | + |
| 26 | +test("KitBuilder can call a function that returns a string", async (t) => { |
| 27 | + |
| 28 | + // A normal function that will be wrapped. |
| 29 | + const echo = (echo_this: string) => echo_this; |
| 30 | + |
| 31 | + const MyKit = KitBuilder.wrap({ url: "test" }, { echo }); |
| 32 | + |
| 33 | + const board = new Board({ |
| 34 | + title: "Test Echo", |
| 35 | + description: "Test Breadboard Kit", |
| 36 | + version: "0.0.1", |
| 37 | + }); |
| 38 | + |
| 39 | + const myKit = board.addKit(MyKit); |
| 40 | + |
| 41 | + const input = board.input(); |
| 42 | + const echoNode = myKit.echo(); |
| 43 | + |
| 44 | + input.wire("an_input->echo_this", echoNode); |
| 45 | + // result because it's just a string from a dynamic function |
| 46 | + echoNode.wire("result->an_output", board.output()); |
| 47 | + |
| 48 | + const output = await board.runOnce({ |
| 49 | + "an_input": "hello world" |
| 50 | + }); |
| 51 | + |
| 52 | + t.is((<string>output["an_output"]), "hello world"); |
| 53 | + |
| 54 | +}); |
| 55 | + |
| 56 | +test("KitBuilder can call a function that returns an object", async (t) => { |
| 57 | + |
| 58 | + // A normal function that will be wrapped. |
| 59 | + const echo = (echo_this: string) => { |
| 60 | + return { "out": echo_this, "other": "stuff" } |
| 61 | + }; |
| 62 | + |
| 63 | + const MyKit = KitBuilder.wrap({ url: "test" }, { echo }); |
| 64 | + |
| 65 | + const board = new Board({ |
| 66 | + title: "Test Echo", |
| 67 | + description: "Test Breadboard Kit", |
| 68 | + version: "0.0.1", |
| 69 | + }); |
| 70 | + |
| 71 | + const myKit = board.addKit(MyKit); |
| 72 | + |
| 73 | + const input = board.input(); |
| 74 | + const echoNode = myKit.echo(); |
| 75 | + |
| 76 | + input.wire("an_input->echo_this", echoNode); |
| 77 | + // result because it's just a string from a dynamic function |
| 78 | + echoNode.wire("out->an_output", board.output()); |
| 79 | + |
| 80 | + const output = await board.runOnce({ |
| 81 | + "an_input": "hello world" |
| 82 | + }); |
| 83 | + |
| 84 | + t.is((<string>output["an_output"]), "hello world"); |
| 85 | + |
| 86 | +}); |
| 87 | + |
| 88 | +test("KitBuilder can call a function that has more than one input", async (t) => { |
| 89 | + |
| 90 | + // A normal function that will be wrapped. |
| 91 | + const add = (a: number, b: number) => { |
| 92 | + return a + b; |
| 93 | + }; |
| 94 | + |
| 95 | + const MyKit = KitBuilder.wrap({ url: "test" }, { add }); |
| 96 | + |
| 97 | + const board = new Board({ |
| 98 | + title: "Test Echo", |
| 99 | + description: "Test Breadboard Kit", |
| 100 | + version: "0.0.1", |
| 101 | + }); |
| 102 | + |
| 103 | + const myKit = board.addKit(MyKit); |
| 104 | + |
| 105 | + const inputA = board.input(); |
| 106 | + const inputB = board.input(); |
| 107 | + |
| 108 | + const addNode = myKit.add(); |
| 109 | + |
| 110 | + inputA.wire("a->a", addNode); |
| 111 | + inputB.wire("b->b", addNode); |
| 112 | + // result because it's just a string from a dynamic function |
| 113 | + addNode.wire("result->", board.output()); |
| 114 | + |
| 115 | + const output = await board.runOnce({ |
| 116 | + "a": 1, |
| 117 | + "b": 2 |
| 118 | + }); |
| 119 | + |
| 120 | + t.is((<number>output["result"]), 3); |
| 121 | +}); |
| 122 | + |
| 123 | +test("KitBuilder can call a function from an external import", async (t) => { |
| 124 | + |
| 125 | + const js = await import("jsonschema"); |
| 126 | + |
| 127 | + // Wrap the jsonschema validate function in a kit and expose function as a node. |
| 128 | + const MyKit = KitBuilder.wrap({ url: "test" }, { validate: js.default.validate }); |
| 129 | + |
| 130 | + const board = new Board({ |
| 131 | + title: "Test Echo", |
| 132 | + description: "Test Breadboard Kit", |
| 133 | + version: "0.0.1", |
| 134 | + }); |
| 135 | + |
| 136 | + const myKit = board.addKit(MyKit); |
| 137 | + |
| 138 | + const inputA = board.input(); |
| 139 | + const inputB = board.input(); |
| 140 | + const inputC = board.input(); |
| 141 | + |
| 142 | + const validateNode = myKit.validate(); |
| 143 | + |
| 144 | + inputA.wire("a->instance", validateNode); |
| 145 | + inputB.wire("b->schema", validateNode); |
| 146 | + inputC.wire("c->options", validateNode); |
| 147 | + |
| 148 | + // result because it's just a string from a dynamic function |
| 149 | + validateNode.wire("errors->", board.output()); |
| 150 | + |
| 151 | + const output = await board.runOnce({ |
| 152 | + "a": { "hello": "world" }, |
| 153 | + "b": { "type": "object" }, |
| 154 | + "c": { allowUnknownAttributes: true } |
| 155 | + }); |
| 156 | + |
| 157 | + const result = js.default.validate({ "hello": "world" }, { "type": "object" }, { allowUnknownAttributes: true }); |
| 158 | + |
| 159 | + t.is(((<Array<string>>output["errors"]).length), result.errors.length); |
| 160 | +}); |
| 161 | + |
| 162 | +test("KitBuilder can splat all the functions in the extenral library and make nodes", async (t) => { |
| 163 | + |
| 164 | + const js = await import("jsonschema"); |
| 165 | + |
| 166 | + // Wrap the jsonschema validate function in a kit and expose function as a node. |
| 167 | + const MyKit = KitBuilder.wrap({ url: "test" }, { ...js.default }); |
| 168 | + |
| 169 | + const board = new Board({ |
| 170 | + title: "Test Echo", |
| 171 | + description: "Test Breadboard Kit", |
| 172 | + version: "0.0.1", |
| 173 | + }); |
| 174 | + |
| 175 | + const myKit = board.addKit(MyKit); |
| 176 | + |
| 177 | + myKit.validate() |
| 178 | + |
| 179 | + // We really need to pick a library with more than one function. |
| 180 | + t.true(myKit.validate instanceof Function); |
| 181 | +}); |
0 commit comments