UNPKG

2.77 kBJavaScriptView Raw
1import CONFIGURATION_MV from './data/configuration_mv.json';
2import CONFIGURATION_MV_OPERATIONS from './data/configuration_mv_operations.json';
3import craftai from '../src';
4import parse from '../src/parse';
5import semver from 'semver';
6
7const CONFIGURATION_MV_OPERATIONS_FROM = _.first(CONFIGURATION_MV_OPERATIONS).timestamp;
8const CONFIGURATION_MV_OPERATIONS_TO = _.last(CONFIGURATION_MV_OPERATIONS).timestamp;
9const CONFIGURATION_MV_OPERATIONS_LAST = _.reduce(
10 CONFIGURATION_MV_OPERATIONS,
11 (context, operation) => _.extend(context, operation),
12 {});
13
14describe('client.addAgentContextOperations(<agentId>, <operations>) with missing values', function() {
15 let client;
16 let agents;
17 const agentsId = [`addAgentContextOps_${RUN_ID}_1`, `addAgentContextOps_${RUN_ID}_2`];
18
19 before(function() {
20 client = craftai(CRAFT_CFG);
21 expect(client).to.be.ok;
22 });
23
24 beforeEach(function() {
25 return Promise.all(_.map(agentsId, (agentId) => client.deleteAgent(agentId) // Delete any preexisting agent with this id.
26 .then(() => client.createAgent(CONFIGURATION_MV, agentId))
27 .then((createdAgent) => {
28 expect(createdAgent).to.be.ok;
29 return createdAgent;
30 })
31 ))
32 .then((createdAgents) => {
33 agents = createdAgents;
34 });
35 });
36
37 afterEach(function() {
38 return Promise.all(_.map(agents, (agent) => client.deleteAgent(agent.id)));
39 });
40
41 it('should succeed when using operations', function() {
42 return client.addAgentContextOperations(agents[0].id, CONFIGURATION_MV_OPERATIONS)
43 .then(() => client.getAgentDecisionTree(agents[0].id, CONFIGURATION_MV_OPERATIONS_TO, '2'))
44 .then((treeJson) => {
45 expect(treeJson).to.be.ok;
46 const { _version, configuration, trees } = parse(treeJson);
47 expect(trees).to.be.ok;
48 expect(_version).to.be.ok;
49 expect(semver.major(_version)).to.be.equal(2);
50 expect(configuration).to.be.deep.equal(CONFIGURATION_MV);
51 return client.getAgentContext(agents[0].id, CONFIGURATION_MV_OPERATIONS_TO + 100);
52 })
53 .then((context) => {
54 expect(context.context).to.be.deep.equal(CONFIGURATION_MV_OPERATIONS_LAST.context);
55 expect(context.timestamp).to.equal(CONFIGURATION_MV_OPERATIONS_TO);
56 })
57 .then(() => {
58 return client.getAgentContextOperations(agents[0].id);
59 })
60 .then((retrievedOperations) => {
61 expect(retrievedOperations).to.be.deep.equal(CONFIGURATION_MV_OPERATIONS);
62 return client.getAgent(agents[0].id);
63 })
64 .then((retrievedAgent) => {
65 expect(retrievedAgent.firstTimestamp).to.be.equal(CONFIGURATION_MV_OPERATIONS_FROM);
66 expect(retrievedAgent.lastTimestamp).to.be.equal(CONFIGURATION_MV_OPERATIONS_TO);
67 });
68 });
69});