pico-engine-core
Advanced tools
Comparing version 0.33.0 to 0.34.0
{ | ||
"name": "pico-engine-core", | ||
"version": "0.33.0", | ||
"version": "0.34.0", | ||
"description": "The core javascript api for the pico-engine. (no http, logging, process management etc...)", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
188
src/DB.js
@@ -5,2 +5,3 @@ var _ = require("lodash"); | ||
var crypto = require("crypto"); | ||
var ktypes = require("krl-stdlib/types"); | ||
var dbRange = require("./dbRange"); | ||
@@ -55,2 +56,8 @@ var levelup = require("levelup"); | ||
}, | ||
/////////////////////////////////////////////////////////////////////// | ||
// | ||
// Picos | ||
// | ||
getPicoIDByECI: function(eci, callback){ | ||
@@ -65,2 +72,18 @@ ldb.get(["channel", eci], function(err, data){ | ||
}, | ||
assertPicoID: function(id, callback){ | ||
if( ! _.isString(id)){ | ||
return callback(new Error("Invalid pico_id: " + ktypes.toString(id))); | ||
} | ||
ldb.get(["pico", id], function(err){ | ||
if(err && err.notFound){ | ||
err = new levelup.errors.NotFoundError("Invalid pico_id: " + id); | ||
err.notFound = true; | ||
} | ||
callback(err, err ? null : id); | ||
}); | ||
}, | ||
getRootPico: function(callback){ | ||
@@ -72,20 +95,45 @@ ldb.get(["root_pico"], callback); | ||
}, | ||
hasPico: function(id, callback){ | ||
ldb.get(["pico", id], function(err){ | ||
if(err){ | ||
if(err.notFound){ | ||
callback(null, false); | ||
return; | ||
} | ||
callback(err); | ||
return; | ||
} | ||
callback(null, true); | ||
getParent: function(pico_id, callback){ | ||
ldb.get(["pico", pico_id], function(err, data){ | ||
callback(err, (data && data.parent_id) || null); | ||
}); | ||
}, | ||
listChildren: function(pico_id, callback){ | ||
var children = []; | ||
dbRange(ldb, { | ||
prefix: ["pico-children", pico_id], | ||
values: false, | ||
}, function(key){ | ||
children.push(key[2]); | ||
}, function(err){ | ||
callback(err, children); | ||
}); | ||
}, | ||
newPico: function(opts, callback){ | ||
var new_pico = { | ||
id: newID() | ||
id: newID(), | ||
parent_id: _.isString(opts.parent_id) && opts.parent_id.length > 0 | ||
? opts.parent_id | ||
: null, | ||
}; | ||
ldb.put(["pico", new_pico.id], new_pico, function(err){ | ||
var ops = [ | ||
{ | ||
type: "put", | ||
key: ["pico", new_pico.id], | ||
value: new_pico, | ||
}, | ||
]; | ||
if(new_pico.parent_id){ | ||
ops.push({ | ||
type: "put", | ||
key: ["pico-children", new_pico.parent_id, new_pico.id], | ||
value: true, | ||
}); | ||
} | ||
ldb.batch(ops, function(err){ | ||
if(err) return callback(err); | ||
@@ -95,2 +143,4 @@ callback(undefined, new_pico); | ||
}, | ||
removePico: function(id, callback){ | ||
@@ -122,2 +172,17 @@ var to_batch = []; | ||
}), | ||
keyRange(["pico-children", id], function(key){ | ||
to_batch.push({type: "del", key: key}); | ||
}), | ||
function(next){ | ||
ldb.get(["pico", id], function(err, pico){ | ||
if(err) return next(err); | ||
if(pico && pico.parent_id){ | ||
keyRange(["pico-children", pico.parent_id, id], function(key){ | ||
to_batch.push({type: "del", key: key}); | ||
})(next); | ||
}else{ | ||
next(); | ||
} | ||
}); | ||
}, | ||
], function(err){ | ||
@@ -128,26 +193,8 @@ if(err)return callback(err); | ||
}, | ||
newChannel: function(opts, callback){ | ||
var new_channel = { | ||
id: newID(), | ||
pico_id: opts.pico_id, | ||
name: opts.name, | ||
type: opts.type | ||
}; | ||
var ops = [ | ||
{ | ||
type: "put", | ||
key: ["channel", new_channel.id], | ||
value: new_channel, | ||
}, | ||
{ | ||
type: "put", | ||
key: ["pico-eci-list", new_channel.pico_id, new_channel.id], | ||
value: true, | ||
} | ||
]; | ||
ldb.batch(ops, function(err){ | ||
if(err) return callback(err); | ||
callback(undefined, new_channel); | ||
}); | ||
}, | ||
//////////////////////////////////////////////////////////////////////// | ||
// | ||
// installed rulesets | ||
// | ||
ridsOnPico: function(pico_id, callback){ | ||
@@ -198,2 +245,32 @@ var pico_rids = {}; | ||
}, | ||
//////////////////////////////////////////////////////////////////////// | ||
// | ||
// channels | ||
// | ||
newChannel: function(opts, callback){ | ||
var new_channel = { | ||
id: newID(), | ||
pico_id: opts.pico_id, | ||
name: opts.name, | ||
type: opts.type | ||
}; | ||
var ops = [ | ||
{ | ||
type: "put", | ||
key: ["channel", new_channel.id], | ||
value: new_channel, | ||
}, | ||
{ | ||
type: "put", | ||
key: ["pico-eci-list", new_channel.pico_id, new_channel.id], | ||
value: true, | ||
} | ||
]; | ||
ldb.batch(ops, function(err){ | ||
if(err) return callback(err); | ||
callback(undefined, new_channel); | ||
}); | ||
}, | ||
listChannels: function(pico_id, callback){ | ||
@@ -224,2 +301,8 @@ var eci_list = []; | ||
}, | ||
//////////////////////////////////////////////////////////////////////// | ||
// | ||
// ent:* | ||
// | ||
putEntVar: function(pico_id, rid, var_name, val, callback){ | ||
@@ -239,2 +322,7 @@ ldb.put(["entvars", pico_id, rid, var_name], val, callback); | ||
}, | ||
//////////////////////////////////////////////////////////////////////// | ||
// | ||
// app:* | ||
// | ||
putAppVar: function(rid, var_name, val, callback){ | ||
@@ -254,2 +342,8 @@ ldb.put(["appvars", rid, var_name], val, callback); | ||
}, | ||
//////////////////////////////////////////////////////////////////////// | ||
// | ||
// event state machine and aggregators | ||
// | ||
getStateMachineState: function(pico_id, rule, callback){ | ||
@@ -275,2 +369,3 @@ var key = ["state_machine", pico_id, rule.rid, rule.name]; | ||
getStateMachineStartTime: function(pico_id, rule, callback){ | ||
@@ -294,2 +389,3 @@ var key = ["state_machine_starttime", pico_id, rule.rid, rule.name]; | ||
updateAggregatorVar: function(pico_id, rule, var_key, updater, callback){ | ||
@@ -319,2 +415,8 @@ var key = [ | ||
}, | ||
//////////////////////////////////////////////////////////////////////// | ||
// | ||
// rulesets | ||
// | ||
storeRuleset: function(krl_src, meta, callback){ | ||
@@ -502,2 +604,8 @@ var timestamp = (new Date()).toISOString(); | ||
}, | ||
//////////////////////////////////////////////////////////////////////// | ||
// | ||
// scheduled events | ||
// | ||
scheduleEventAt: function(at, event, callback){ | ||
@@ -585,2 +693,8 @@ var id = newID(); | ||
}, | ||
//////////////////////////////////////////////////////////////////////// | ||
// | ||
// db migrations | ||
// | ||
getMigrationLog: getMigrationLog, | ||
@@ -587,0 +701,0 @@ recordMigration: recordMigration, |
@@ -29,4 +29,6 @@ var _ = require("lodash"); | ||
chan2: async.apply(db.newChannel, {pico_id: "id0", name: "two", type: "t"}), | ||
pico1: async.apply(db.newPico, {parent_id: "id0"}), | ||
end_db: async.apply(db.toObj), | ||
rmpico0: async.apply(db.removePico, "id0"), | ||
rmpico1: async.apply(db.removePico, "id3"), | ||
post_del_db: async.apply(db.toObj) | ||
@@ -56,6 +58,12 @@ }, function(err, data){ | ||
id: "id0", | ||
} | ||
parent_id: null, | ||
}, | ||
"id3": { | ||
id: "id3", | ||
parent_id: "id0", | ||
}, | ||
}, | ||
"pico-ruleset": {"id0": {"rs0": {on: true}}}, | ||
"ruleset-pico": {"rs0": {"id0": {on: true}}}, | ||
"pico-children": {"id0": {"id3": true}}, | ||
"pico-eci-list": { | ||
@@ -647,1 +655,90 @@ "id0": { | ||
}); | ||
test("DB - parent/child", function(t){ | ||
var db = mkTestDB(); | ||
var assertParent = function(pico_id, expected_parent_id){ | ||
return function(next){ | ||
db.getParent(pico_id, function(err, parent_id){ | ||
if(err) return next(err); | ||
t.equals(parent_id, expected_parent_id, "testing db.getParent"); | ||
next(); | ||
}); | ||
}; | ||
}; | ||
var assertChildren = function(pico_id, expected_children_ids){ | ||
return function(next){ | ||
db.listChildren(pico_id, function(err, list){ | ||
if(err) return next(err); | ||
t.deepEquals(list, expected_children_ids, "testing db.listChildren"); | ||
next(); | ||
}); | ||
}; | ||
}; | ||
async.series([ | ||
async.apply(db.newPico, {}),// id0 | ||
async.apply(db.newPico, {parent_id: "id0"}),// id1 | ||
async.apply(db.newPico, {parent_id: "id0"}),// id2 | ||
async.apply(db.newPico, {parent_id: "id0"}),// id3 | ||
async.apply(db.newPico, {parent_id: "id3"}),// id4 | ||
async.apply(db.newPico, {parent_id: "id3"}),// id5 | ||
assertParent("id0", null), | ||
assertParent("id1", "id0"), | ||
assertParent("id2", "id0"), | ||
assertParent("id3", "id0"), | ||
assertParent("id4", "id3"), | ||
assertParent("id5", "id3"), | ||
assertChildren("id0", ["id1", "id2", "id3"]), | ||
assertChildren("id1", []), | ||
assertChildren("id2", []), | ||
assertChildren("id3", ["id4", "id5"]), | ||
assertChildren("id4", []), | ||
assertChildren("id5", []), | ||
async.apply(db.removePico, "id5"), | ||
assertChildren("id3", ["id4"]), | ||
async.apply(db.removePico, "id3"), | ||
assertChildren("id3", []), | ||
], t.end); | ||
}); | ||
test("DB - assertPicoID", function(t){ | ||
var db = mkTestDB(); | ||
var tstPID = function(id, expected_it){ | ||
return function(next){ | ||
db.assertPicoID(id, function(err, got_id){ | ||
if(expected_it){ | ||
t.notOk(err); | ||
t.equals(got_id, id); | ||
}else{ | ||
t.ok(err); | ||
t.notOk(got_id); | ||
} | ||
next(); | ||
}); | ||
}; | ||
}; | ||
async.series([ | ||
async.apply(db.newPico, {}), | ||
tstPID(null, false), | ||
tstPID(void 0, false), | ||
tstPID({}, false), | ||
tstPID(0, false), | ||
tstPID("id0", true), | ||
tstPID("id2", false), | ||
], t.end); | ||
}); |
@@ -509,5 +509,4 @@ var _ = require("lodash"); | ||
core.installRuleset = function(pico_id, rid, callback){ | ||
db.hasPico(pico_id, function(err, has_pico){ | ||
db.assertPicoID(pico_id, function(err, pico_id){ | ||
if(err) return callback(err); | ||
if(!has_pico) return callback(new Error("Invalid pico_id: " + pico_id)); | ||
@@ -524,3 +523,7 @@ db.hasEnabledRid(rid, function(err, has){ | ||
core.uninstallRuleset = function(pico_id, rid, callback){ | ||
db.removeRulesetFromPico(pico_id, rid, callback); | ||
db.assertPicoID(pico_id, function(err, pico_id){ | ||
if(err) return callback(err); | ||
db.removeRulesetFromPico(pico_id, rid, callback); | ||
}); | ||
}; | ||
@@ -585,2 +588,3 @@ | ||
async.series([ | ||
db.checkAndRunMigrations, | ||
function(next){ | ||
@@ -635,3 +639,2 @@ if(_.isEmpty(rootRIDs)){ | ||
}, | ||
db.checkAndRunMigrations, | ||
registerAllEnabledRulesets, | ||
@@ -638,0 +641,0 @@ resumeScheduler, |
@@ -16,2 +16,3 @@ /** | ||
"20170804T214426_pico-ruleset": require("./20170804T214426_pico-ruleset"), | ||
"20170810T170618_parent-child": require("./20170810T170618_parent-child"), | ||
}; |
@@ -8,2 +8,3 @@ var _ = require("lodash"); | ||
var fns = { | ||
getPicoIDByECI: mkKRLfn([ | ||
@@ -14,7 +15,59 @@ "eci", | ||
}), | ||
getParent: mkKRLfn([ | ||
"pico_id", | ||
], function(args, ctx, callback){ | ||
var pico_id = args.pico_id || ctx.pico_id; | ||
core.db.assertPicoID(pico_id, function(err, pico_id){ | ||
if(err) return callback(err); | ||
core.db.getParent(pico_id, callback); | ||
}); | ||
}), | ||
listChildren: mkKRLfn([ | ||
"pico_id", | ||
], function(args, ctx, callback){ | ||
var pico_id = args.pico_id || ctx.pico_id; | ||
core.db.assertPicoID(pico_id, function(err, pico_id){ | ||
if(err) return callback(err); | ||
core.db.listChildren(pico_id, callback); | ||
}); | ||
}), | ||
listChannels: mkKRLfn([ | ||
"pico_id", | ||
], function(args, ctx, callback){ | ||
core.db.listChannels(args.pico_id, callback); | ||
var pico_id = args.pico_id || ctx.pico_id; | ||
core.db.assertPicoID(pico_id, function(err, pico_id){ | ||
if(err) return callback(err); | ||
core.db.listChannels(pico_id, callback); | ||
}); | ||
}), | ||
listInstalledRIDs: mkKRLfn([ | ||
"pico_id", | ||
], function(args, ctx, callback){ | ||
var pico_id = args.pico_id || ctx.pico_id; | ||
core.db.assertPicoID(pico_id, function(err, pico_id){ | ||
if(err) return callback(err); | ||
core.db.ridsOnPico(pico_id, function(err, rid_set){ | ||
if(err) return callback(err); | ||
callback(null, _.keys(rid_set)); | ||
}); | ||
}); | ||
}), | ||
listAllEnabledRIDs: mkKRLfn([ | ||
@@ -24,2 +77,4 @@ ], function(args, ctx, callback){ | ||
}), | ||
describeRuleset: mkKRLfn([ | ||
@@ -46,14 +101,35 @@ "rid", | ||
}), | ||
}; | ||
var actions = { | ||
newPico: mkKRLfn([ | ||
"parent_id", | ||
], function(args, ctx, callback){ | ||
core.db.newPico({}, callback); | ||
var parent_id = args.parent_id || ctx.pico_id; | ||
core.db.assertPicoID(parent_id, function(err, parent_id){ | ||
if(err) return callback(err); | ||
core.db.newPico({ | ||
parent_id: parent_id, | ||
}, callback); | ||
}); | ||
}), | ||
removePico: mkKRLfn([ | ||
"pico_id", | ||
], function(args, ctx, callback){ | ||
core.db.removePico(args.pico_id, callback); | ||
var pico_id = args.pico_id || ctx.pico_id; | ||
core.db.assertPicoID(pico_id, function(err, pico_id){ | ||
if(err) return callback(err); | ||
core.db.removePico(pico_id, callback); | ||
}); | ||
}), | ||
newChannel: mkKRLfn([ | ||
@@ -64,4 +140,16 @@ "pico_id", | ||
], function(args, ctx, callback){ | ||
core.db.newChannel(args, callback); | ||
var pico_id = args.pico_id || ctx.pico_id; | ||
core.db.assertPicoID(pico_id, function(err, pico_id){ | ||
if(err) return callback(err); | ||
core.db.newChannel({ | ||
pico_id: pico_id, | ||
name: args.name, | ||
type: args.type, | ||
}, callback); | ||
}); | ||
}), | ||
removeChannel: mkKRLfn([ | ||
@@ -72,2 +160,4 @@ "eci", | ||
}), | ||
registerRuleset: mkKRLfn([ | ||
@@ -88,2 +178,4 @@ "url", | ||
}), | ||
unregisterRuleset: mkKRLfn([ | ||
@@ -98,2 +190,4 @@ "rid", | ||
}), | ||
installRuleset: mkKRLfn([ | ||
@@ -105,4 +199,7 @@ "pico_id", | ||
], function(args, ctx, callback){ | ||
var pico_id = args.pico_id || ctx.pico_id; | ||
var install = function(rid, callback){ | ||
core.installRuleset(args.pico_id, rid, function(err){ | ||
core.installRuleset(pico_id, rid, function(err){ | ||
callback(err, rid); | ||
@@ -143,2 +240,4 @@ }); | ||
}), | ||
uninstallRuleset: mkKRLfn([ | ||
@@ -148,2 +247,3 @@ "pico_id", | ||
], function(args, ctx, callback){ | ||
var pico_id = args.pico_id || ctx.pico_id; | ||
var rids = _.isArray(args.rid) | ||
@@ -154,5 +254,6 @@ ? _.uniq(args.rid) | ||
async.each(rids, function(rid, next){ | ||
core.uninstallRuleset(args.pico_id, rid, next); | ||
core.uninstallRuleset(pico_id, rid, next); | ||
}, callback); | ||
}), | ||
}; | ||
@@ -159,0 +260,0 @@ |
@@ -17,2 +17,18 @@ var _ = require("lodash"); | ||
var testPE = function(test_name, genfn){ | ||
test(test_name, function(t){ | ||
mkTestPicoEngine({ | ||
rootRIDs: ["io.picolabs.engine"], | ||
}, function(err, pe){ | ||
if(err) return t.end(err); | ||
cocb.run(function*(){ | ||
yield genfn(t, pe); | ||
}, t.end); | ||
}); | ||
}); | ||
}; | ||
test("engine:getPicoIDByECI", function(t){ | ||
@@ -49,29 +65,3 @@ cocb.run(function*(){ | ||
test("engine:removeChannel", function(t){ | ||
cocb.run(function*(){ | ||
var engine = kengine({ | ||
db: { | ||
removeChannel: tick(function(eci, callback){ | ||
if(eci === "foo"){ | ||
return callback(); | ||
} | ||
callback("NOT FOUND:" + eci); | ||
}) | ||
} | ||
}); | ||
var rm = function*(eci){ | ||
return yield engine.actions.removeChannel({}, {eci: eci,}); | ||
}; | ||
t.equals(yield rm("foo"), void 0); | ||
try{ | ||
yield rm("quux"); | ||
t.fail("should throw b/c not found"); | ||
}catch(err){ | ||
t.equals(err, "NOT FOUND:quux"); | ||
} | ||
}, t.end); | ||
}); | ||
test("engine:registerRuleset", function(t){ | ||
@@ -241,45 +231,226 @@ cocb.run(function*(){ | ||
test("engine:describeRuleset", function(t){ | ||
mkTestPicoEngine({}, function(err, pe){ | ||
if(err) return t.end(err); | ||
testPE("engine:describeRuleset", function * (t, pe){ | ||
var ctx = {}; | ||
var descRID = yield pe.modules.get(ctx, "engine", "describeRuleset"); | ||
cocb.run(function*(){ | ||
var ctx = {}; | ||
var descRID = yield pe.modules.get(ctx, "engine", "describeRuleset"); | ||
var desc = yield descRID(ctx, {rid: "io.picolabs.hello_world"}); | ||
var desc = yield descRID(ctx, {rid: "io.picolabs.hello_world"}); | ||
var isIsoString = function(str){ | ||
return str === (new Date(str)).toISOString(); | ||
}; | ||
var isIsoString = function(str){ | ||
return str === (new Date(str)).toISOString(); | ||
}; | ||
t.deepEquals(_.keys(desc), [ | ||
"rid", | ||
"src", | ||
"hash", | ||
"url", | ||
"timestamp_stored", | ||
"timestamp_enable", | ||
"meta", | ||
]); | ||
t.equals(desc.rid, "io.picolabs.hello_world"); | ||
t.ok(_.isString(desc.src)); | ||
t.ok(_.isString(desc.hash)); | ||
t.ok(_.isString(desc.url)); | ||
t.ok(isIsoString(desc.timestamp_stored)); | ||
t.ok(isIsoString(desc.timestamp_enable)); | ||
t.deepEquals(desc.meta, { | ||
name: "Hello World", | ||
description: "\nA first ruleset for the Quickstart\n ", | ||
author: "Phil Windley", | ||
}); | ||
t.deepEquals(_.keys(desc), [ | ||
"rid", | ||
"src", | ||
"hash", | ||
"url", | ||
"timestamp_stored", | ||
"timestamp_enable", | ||
"meta", | ||
]); | ||
t.equals(desc.rid, "io.picolabs.hello_world"); | ||
t.ok(_.isString(desc.src)); | ||
t.ok(_.isString(desc.hash)); | ||
t.ok(_.isString(desc.url)); | ||
t.ok(isIsoString(desc.timestamp_stored)); | ||
t.ok(isIsoString(desc.timestamp_enable)); | ||
t.deepEquals(desc.meta, { | ||
name: "Hello World", | ||
description: "\nA first ruleset for the Quickstart\n ", | ||
author: "Phil Windley", | ||
}); | ||
try{ | ||
yield descRID(ctx, {rid: "not.found"}); | ||
t.fail("should fail b/c not found"); | ||
}catch(err){ | ||
t.ok(err && err.notFound); | ||
} | ||
}); | ||
try{ | ||
yield descRID(ctx, {rid: "not.found"}); | ||
t.fail("should fail b/c not found"); | ||
}catch(err){ | ||
t.ok(err && err.notFound); | ||
} | ||
}, t.end); | ||
testPE("engine:listInstalledRIDs", function * (t, pe){ | ||
var ctx = {}; | ||
var listRIDs = yield pe.modules.get(ctx, "engine", "listInstalledRIDs"); | ||
var rids = yield listRIDs(ctx, {pico_id: "id0"}); | ||
t.deepEquals(rids, [ | ||
"io.picolabs.engine", | ||
]); | ||
}); | ||
testPE("engine:newPico", function * (t, pe){ | ||
var action = function*(ctx, name, args){ | ||
return yield pe.modules.action(ctx, "engine", name, args); | ||
}; | ||
var pico2 = yield action({}, "newPico", { | ||
parent_id: "id0", | ||
}); | ||
t.deepEquals(pico2, { | ||
id: "id2", | ||
parent_id: "id0", | ||
}); | ||
//default to ctx.pico_id | ||
var pico3 = yield action({ | ||
pico_id: "id2",//called by pico2 | ||
}, "newPico", {}); | ||
t.deepEquals(pico3, { | ||
id: "id3", | ||
parent_id: "id2", | ||
}); | ||
//no parent_id | ||
try{ | ||
yield action({}, "newPico", {}); | ||
t.fail("should have thrown"); | ||
}catch(e){ | ||
t.equals(e + "", "Error: Invalid pico_id: null"); | ||
} | ||
}); | ||
testPE("engine:getParent, engine:listChildren, engine:removePico", function * (t, pe){ | ||
var newPico = function*(parent_id){ | ||
return yield pe.modules.action({pico_id: parent_id}, "engine", "newPico", []); | ||
}; | ||
var removePico = function*(ctx, args){ | ||
return yield pe.modules.action(ctx, "engine", "removePico", args); | ||
}; | ||
var getParent = yield pe.modules.get({}, "engine", "getParent"); | ||
var listChildren = yield pe.modules.get({}, "engine", "listChildren"); | ||
yield newPico("id0");// id2 | ||
yield newPico("id0");// id3 | ||
yield newPico("id2");// id4 | ||
t.equals(yield getParent({}, ["id0"]), null); | ||
t.equals(yield getParent({}, ["id2"]), "id0"); | ||
t.equals(yield getParent({}, ["id3"]), "id0"); | ||
t.equals(yield getParent({}, ["id4"]), "id2"); | ||
t.deepEquals(yield listChildren({}, ["id0"]), ["id2", "id3"]); | ||
t.deepEquals(yield listChildren({}, ["id2"]), ["id4"]); | ||
t.deepEquals(yield listChildren({}, ["id3"]), []); | ||
t.deepEquals(yield listChildren({}, ["id4"]), []); | ||
//fallback on ctx.pico_id | ||
t.equals(yield getParent({pico_id: "id4"}, []), "id2"); | ||
t.deepEquals(yield listChildren({pico_id: "id2"}, []), ["id4"]); | ||
t.equals(yield removePico({}, ["id4"]), void 0); | ||
t.deepEquals(yield listChildren({}, ["id2"]), []); | ||
//report error on invalid pico_id | ||
var assertInvalidPicoID = function * (genfn, id, expected){ | ||
try{ | ||
yield genfn({pico_id: id}, []); | ||
t.fail("should have thrown on invalid pico_id"); | ||
}catch(e){ | ||
t.equals(e + "", expected); | ||
} | ||
}; | ||
yield assertInvalidPicoID(getParent , "id404", "NotFoundError: Invalid pico_id: id404"); | ||
yield assertInvalidPicoID(listChildren, "id404", "NotFoundError: Invalid pico_id: id404"); | ||
yield assertInvalidPicoID(removePico , "id404", "NotFoundError: Invalid pico_id: id404"); | ||
yield assertInvalidPicoID(getParent , void 0, "Error: Invalid pico_id: null"); | ||
yield assertInvalidPicoID(listChildren, void 0, "Error: Invalid pico_id: null"); | ||
yield assertInvalidPicoID(removePico , void 0, "Error: Invalid pico_id: null"); | ||
}); | ||
testPE("engine:newChannel, engine:listChannels, engine:removeChannel", function * (t, pe){ | ||
var newChannel = function*(ctx, args){ | ||
return yield pe.modules.action(ctx, "engine", "newChannel", args); | ||
}; | ||
var removeChannel = function*(ctx, args){ | ||
return yield pe.modules.action(ctx, "engine", "removeChannel", args); | ||
}; | ||
var listChannels = yield pe.modules.get({}, "engine", "listChannels"); | ||
t.deepEquals(yield listChannels({}, ["id0"]), [ | ||
{id: "id1", pico_id: "id0", name: "root", type: "secret"}, | ||
]); | ||
t.deepEquals(yield newChannel({}, ["id0"]), {id: "id2", pico_id: "id0", name: void 0, type: void 0}); | ||
t.deepEquals(yield listChannels({}, ["id0"]), [ | ||
{id: "id1", pico_id: "id0", name: "root", type: "secret"}, | ||
{id: "id2", pico_id: "id0"}, | ||
]); | ||
t.equals(yield removeChannel({}, ["id1"]), void 0); | ||
t.deepEquals(yield listChannels({}, ["id0"]), [ | ||
{id: "id2", pico_id: "id0"}, | ||
]); | ||
t.equals(yield removeChannel({}, ["id2"]), void 0); | ||
t.deepEquals(yield listChannels({}, ["id0"]), [ | ||
]); | ||
//report error on invalid pico_id | ||
var assertInvalidPicoID = function * (genfn, id, expected){ | ||
try{ | ||
yield genfn({pico_id: id}, []); | ||
t.fail("should have thrown on invalid pico_id"); | ||
}catch(e){ | ||
t.equals(e + "", expected); | ||
} | ||
}; | ||
yield assertInvalidPicoID(newChannel , "id404", "NotFoundError: Invalid pico_id: id404"); | ||
yield assertInvalidPicoID(listChannels, "id404", "NotFoundError: Invalid pico_id: id404"); | ||
yield assertInvalidPicoID(newChannel , void 0, "Error: Invalid pico_id: null"); | ||
yield assertInvalidPicoID(listChannels, void 0, "Error: Invalid pico_id: null"); | ||
}); | ||
testPE("engine:installRuleset, engine:listInstalledRIDs, engine:uninstallRuleset", function * (t, pe){ | ||
var installRS = function*(ctx, args){ | ||
return yield pe.modules.action(ctx, "engine", "installRuleset", args); | ||
}; | ||
var uninstallRID = function*(ctx, args){ | ||
return yield pe.modules.action(ctx, "engine", "uninstallRuleset", args); | ||
}; | ||
var listRIDs = yield pe.modules.get({}, "engine", "listInstalledRIDs"); | ||
t.deepEquals(yield listRIDs({pico_id: "id0"}, []), [ | ||
"io.picolabs.engine", | ||
]); | ||
t.equals(yield installRS({}, ["id0", "io.picolabs.hello_world"]), "io.picolabs.hello_world"); | ||
t.deepEquals(yield listRIDs({pico_id: "id0"}, []), [ | ||
"io.picolabs.engine", | ||
"io.picolabs.hello_world", | ||
]); | ||
t.equals(yield uninstallRID({}, ["id0", "io.picolabs.engine"]), void 0); | ||
t.deepEquals(yield listRIDs({pico_id: "id0"}, []), [ | ||
"io.picolabs.hello_world", | ||
]); | ||
//report error on invalid pico_id | ||
var assertInvalidPicoID = function * (genfn, id, expected){ | ||
try{ | ||
yield genfn({pico_id: id}, {rid: "io.picolabs.hello_world"}); | ||
t.fail("should have thrown on invalid pico_id"); | ||
}catch(e){ | ||
t.equals(e + "", expected); | ||
} | ||
}; | ||
yield assertInvalidPicoID(installRS , "id404", "NotFoundError: Invalid pico_id: id404"); | ||
yield assertInvalidPicoID(uninstallRID, "id404", "NotFoundError: Invalid pico_id: id404"); | ||
yield assertInvalidPicoID(installRS , void 0, "Error: Invalid pico_id: null"); | ||
yield assertInvalidPicoID(uninstallRID, void 0, "Error: Invalid pico_id: null"); | ||
}); |
@@ -7,7 +7,5 @@ var _ = require("lodash"); | ||
cocb.run(function*(){ | ||
var has_pico = yield core.db.hasPicoYieldable(ctx.pico_id); | ||
var err; | ||
if(!has_pico){ | ||
throw new Error("Invalid eci: " + ctx.query.eci); | ||
} | ||
yield core.db.assertPicoIDYieldable(ctx.pico_id); | ||
var pico_rids = yield core.db.ridsOnPicoYieldable(ctx.pico_id); | ||
@@ -17,2 +15,4 @@ if(pico_rids[ctx.query.rid] !== true){ | ||
} | ||
var err; | ||
var rs = core.rsreg.get(ctx.query.rid); | ||
@@ -19,0 +19,0 @@ if(!rs){ |
Sorry, the diff of this file is too big to display
253362
52
6898