2016-11-22 17:13:44 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-05-12 09:11:13 +02:00
|
|
|
require('../common');
|
2016-11-22 17:13:44 +01:00
|
|
|
const assert = require('assert');
|
2017-05-26 17:53:06 +02:00
|
|
|
const util = require('util');
|
2016-11-22 17:13:44 +01:00
|
|
|
|
|
|
|
function findInGraph(graph, type, n) {
|
|
|
|
let found = 0;
|
|
|
|
for (let i = 0; i < graph.length; i++) {
|
|
|
|
const node = graph[i];
|
|
|
|
if (node.type === type) found++;
|
|
|
|
if (found === n) return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function pruneTickObjects(activities) {
|
2018-12-10 13:27:32 +01:00
|
|
|
// Remove one TickObject on each pass until none is left anymore
|
2016-11-22 17:13:44 +01:00
|
|
|
// not super efficient, but simplest especially to handle
|
|
|
|
// multiple TickObjects in a row
|
2019-02-17 15:03:29 +01:00
|
|
|
const tickObject = {
|
|
|
|
found: true,
|
|
|
|
index: null,
|
|
|
|
data: null
|
|
|
|
};
|
2016-11-22 17:13:44 +01:00
|
|
|
|
2019-02-17 21:10:01 +01:00
|
|
|
if (!Array.isArray(activities))
|
|
|
|
return activities;
|
|
|
|
|
2019-02-17 15:03:29 +01:00
|
|
|
while (tickObject.found) {
|
2016-11-22 17:13:44 +01:00
|
|
|
for (let i = 0; i < activities.length; i++) {
|
2019-02-17 15:03:29 +01:00
|
|
|
if (activities[i].type === 'TickObject') {
|
|
|
|
tickObject.index = i;
|
|
|
|
break;
|
2019-02-17 21:10:01 +01:00
|
|
|
} else if (i + 1 >= activities.length) {
|
2019-02-17 15:03:29 +01:00
|
|
|
tickObject.found = false;
|
|
|
|
}
|
2016-11-22 17:13:44 +01:00
|
|
|
}
|
|
|
|
|
2019-02-17 15:03:29 +01:00
|
|
|
if (tickObject.found) {
|
2018-12-10 13:27:32 +01:00
|
|
|
// Point all triggerAsyncIds that point to the tickObject
|
2018-01-06 19:34:27 +01:00
|
|
|
// to its triggerAsyncId and finally remove it from the activities
|
2019-02-17 15:03:29 +01:00
|
|
|
tickObject.data = activities[tickObject.index];
|
|
|
|
const triggerId = {
|
|
|
|
new: tickObject.data.triggerAsyncId,
|
|
|
|
old: tickObject.data.uid
|
|
|
|
};
|
|
|
|
|
2016-11-22 17:13:44 +01:00
|
|
|
activities.forEach(function repointTriggerId(x) {
|
2019-02-17 15:03:29 +01:00
|
|
|
if (x.triggerAsyncId === triggerId.old)
|
|
|
|
x.triggerAsyncId = triggerId.new;
|
2016-11-22 17:13:44 +01:00
|
|
|
});
|
2019-02-17 15:03:29 +01:00
|
|
|
|
|
|
|
activities.splice(tickObject.index, 1);
|
2016-11-22 17:13:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return activities;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function verifyGraph(hooks, graph) {
|
|
|
|
pruneTickObjects(hooks);
|
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Map actual ids to standin ids defined in the graph
|
2016-11-22 17:13:44 +01:00
|
|
|
const idtouid = {};
|
|
|
|
const uidtoid = {};
|
|
|
|
const typeSeen = {};
|
|
|
|
const errors = [];
|
|
|
|
|
|
|
|
const activities = pruneTickObjects(hooks.activities);
|
|
|
|
activities.forEach(processActivity);
|
|
|
|
|
|
|
|
function processActivity(x) {
|
|
|
|
if (!typeSeen[x.type]) typeSeen[x.type] = 0;
|
|
|
|
typeSeen[x.type]++;
|
|
|
|
|
|
|
|
const node = findInGraph(graph, x.type, typeSeen[x.type]);
|
|
|
|
if (node == null) return;
|
|
|
|
|
|
|
|
idtouid[node.id] = x.uid;
|
|
|
|
uidtoid[x.uid] = node.id;
|
2017-06-14 12:39:53 +02:00
|
|
|
if (node.triggerAsyncId == null) return;
|
2016-11-22 17:13:44 +01:00
|
|
|
|
2017-06-14 12:39:53 +02:00
|
|
|
const tid = idtouid[node.triggerAsyncId];
|
|
|
|
if (x.triggerAsyncId === tid) return;
|
2016-11-22 17:13:44 +01:00
|
|
|
|
|
|
|
errors.push({
|
|
|
|
id: node.id,
|
2017-06-14 12:39:53 +02:00
|
|
|
expectedTid: node.triggerAsyncId,
|
|
|
|
actualTid: uidtoid[x.triggerAsyncId]
|
2016-11-22 17:13:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length) {
|
|
|
|
errors.forEach((x) =>
|
|
|
|
console.error(
|
|
|
|
`'${x.id}' expected to be triggered by '${x.expectedTid}', ` +
|
|
|
|
`but was triggered by '${x.actualTid}' instead.`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-05-26 17:53:06 +02:00
|
|
|
assert.strictEqual(errors.length, 0);
|
2019-04-23 00:57:12 +02:00
|
|
|
|
2019-05-17 00:59:57 +02:00
|
|
|
// Verify that all expected types are present (but more/others are allowed)
|
2019-04-23 00:57:12 +02:00
|
|
|
const expTypes = Object.create(null);
|
|
|
|
for (let i = 0; i < graph.length; i++) {
|
|
|
|
if (expTypes[graph[i].type] == null) expTypes[graph[i].type] = 0;
|
|
|
|
expTypes[graph[i].type]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const type in expTypes) {
|
2019-05-17 00:59:57 +02:00
|
|
|
assert.ok(typeSeen[type] >= expTypes[type],
|
|
|
|
`Type '${type}': expecting: ${expTypes[type]} ` +
|
|
|
|
`found: ${typeSeen[type]}`);
|
2019-04-23 00:57:12 +02:00
|
|
|
}
|
2016-11-22 17:13:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Helper to generate the input to the verifyGraph tests
|
|
|
|
//
|
|
|
|
function inspect(obj, depth) {
|
2017-05-26 17:53:06 +02:00
|
|
|
console.error(util.inspect(obj, false, depth || 5, true));
|
2016-11-22 17:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.printGraph = function printGraph(hooks) {
|
|
|
|
const ids = {};
|
|
|
|
const uidtoid = {};
|
|
|
|
const activities = pruneTickObjects(hooks.activities);
|
|
|
|
const graph = [];
|
|
|
|
activities.forEach(procesNode);
|
|
|
|
|
|
|
|
function procesNode(x) {
|
|
|
|
const key = x.type.replace(/WRAP/, '').toLowerCase();
|
|
|
|
if (!ids[key]) ids[key] = 1;
|
2017-07-16 09:28:44 +02:00
|
|
|
const id = `${key}:${ids[key]++}`;
|
2016-11-22 17:13:44 +01:00
|
|
|
uidtoid[x.uid] = id;
|
2017-06-14 12:39:53 +02:00
|
|
|
const triggerAsyncId = uidtoid[x.triggerAsyncId] || null;
|
|
|
|
graph.push({ type: x.type, id, triggerAsyncId });
|
2016-11-22 17:13:44 +01:00
|
|
|
}
|
|
|
|
inspect(graph);
|
|
|
|
};
|