1 | import CONFIGURATION_1 from './data/configuration_1.json';
|
2 |
|
3 | import CONFIGURATION_1_OPERATIONS_1 from './data/configuration_1_operations_1.json';
|
4 | import CONFIGURATION_1_OPERATIONS_2 from './data/configuration_1_operations_2.json';
|
5 |
|
6 | const CONFIGURATION_1_OPERATIONS_1_FROM = _.first(CONFIGURATION_1_OPERATIONS_1).timestamp;
|
7 | const CONFIGURATION_1_OPERATIONS_1_TO = _.last(CONFIGURATION_1_OPERATIONS_1).timestamp;
|
8 | const CONFIGURATION_1_OPERATIONS_1_LAST = _.reduce(
|
9 | CONFIGURATION_1_OPERATIONS_1,
|
10 | (context, operation) => _.extend(context, operation),
|
11 | {});
|
12 |
|
13 | import craftai, { errors, Time } from '../src';
|
14 |
|
15 | describe('client.addAgentContextOperations(<agentId>, <operations>)', function() {
|
16 | let client;
|
17 | let agents;
|
18 | const agentsId = [`addAgentContextOps_${RUN_ID}_1`, `addAgentContextOps_${RUN_ID}_2`];
|
19 |
|
20 | before(function() {
|
21 | client = craftai(CRAFT_CFG);
|
22 | expect(client).to.be.ok;
|
23 | expect(CONFIGURATION_1_OPERATIONS_2).to.have.lengthOf(3260);
|
24 | });
|
25 |
|
26 | beforeEach(function() {
|
27 | return Promise.all(_.map(agentsId, (agentId) => client.deleteAgent(agentId)
|
28 | .then(() => client.createAgent(CONFIGURATION_1, agentId))
|
29 | .then((createdAgent) => {
|
30 | expect(createdAgent).to.be.ok;
|
31 | return createdAgent;
|
32 | })
|
33 | ))
|
34 | .then((createdAgents) => {
|
35 | agents = createdAgents;
|
36 | });
|
37 | });
|
38 |
|
39 | afterEach(function() {
|
40 | return Promise.all(_.map(agents, (agent) => client.deleteAgent(agent.id)));
|
41 | });
|
42 |
|
43 | it('should succeed when using valid operations', function() {
|
44 | return client.addAgentContextOperations(agents[0].id, CONFIGURATION_1_OPERATIONS_1)
|
45 | .then(() => {
|
46 | return client.getAgentContext(agents[0].id, CONFIGURATION_1_OPERATIONS_1_TO + 100);
|
47 | })
|
48 | .then((context) => {
|
49 | expect(context.context).to.be.deep.equal(CONFIGURATION_1_OPERATIONS_1_LAST.context);
|
50 | expect(context.timestamp).to.equal(CONFIGURATION_1_OPERATIONS_1_TO);
|
51 | })
|
52 | .then(() => {
|
53 | return client.getAgentContextOperations(agents[0].id);
|
54 | })
|
55 | .then((retrievedOperations) => {
|
56 | expect(retrievedOperations).to.be.deep.equal(CONFIGURATION_1_OPERATIONS_1);
|
57 | return client.getAgent(agents[0].id);
|
58 | })
|
59 | .then((retrievedAgent) => {
|
60 | expect(retrievedAgent.firstTimestamp).to.be.equal(CONFIGURATION_1_OPERATIONS_1_FROM);
|
61 | expect(retrievedAgent.lastTimestamp).to.be.equal(CONFIGURATION_1_OPERATIONS_1_TO);
|
62 | });
|
63 | });
|
64 | it('should succeed when passing unordered contexts', function() {
|
65 | return client.addAgentContextOperations(agents[0].id,
|
66 | [
|
67 | {
|
68 | 'timestamp': 1464600000,
|
69 | 'context': {
|
70 | 'presence': 'robert',
|
71 | 'lightIntensity': 0.4,
|
72 | 'lightbulbColor': 'green'
|
73 | }
|
74 | },
|
75 | {
|
76 | 'timestamp': 1464601500,
|
77 | 'context': {
|
78 | 'presence': 'robert',
|
79 | 'lightIntensity': 0.6,
|
80 | 'lightbulbColor': 'green'
|
81 | }
|
82 | },
|
83 | {
|
84 | 'timestamp': 1464601000,
|
85 | 'context': {
|
86 | 'presence': 'gisele',
|
87 | 'lightIntensity': 0.4,
|
88 | 'lightbulbColor': 'blue'
|
89 | }
|
90 | },
|
91 | {
|
92 | 'timestamp': 1464600500,
|
93 | 'context': {
|
94 | 'presence': 'none',
|
95 | 'lightIntensity': 0,
|
96 | 'lightbulbColor': 'black'
|
97 | }
|
98 | }
|
99 | ]
|
100 | )
|
101 | .then(() => {
|
102 | return client.getAgentContextOperations(agents[0].id);
|
103 | })
|
104 | .then((retrievedOperations) => {
|
105 | expect(retrievedOperations).to.be.deep.equal(CONFIGURATION_1_OPERATIONS_1);
|
106 | return client.getAgent(agents[0].id);
|
107 | })
|
108 | .then((retrievedAgent) => {
|
109 | expect(retrievedAgent.firstTimestamp).to.be.equal(CONFIGURATION_1_OPERATIONS_1_FROM);
|
110 | expect(retrievedAgent.lastTimestamp).to.be.equal(CONFIGURATION_1_OPERATIONS_1_TO);
|
111 | });
|
112 | });
|
113 | it('should succeed when using operations with ISO 8601 timestamps', function() {
|
114 | return client.addAgentContextOperations(agents[0].id, [
|
115 | {
|
116 | timestamp: '1998-04-23T04:30:00-05:00',
|
117 | context: {
|
118 | presence: 'robert',
|
119 | lightIntensity: 0.4,
|
120 | lightbulbColor: 'green'
|
121 | }
|
122 | },
|
123 | {
|
124 | timestamp: '1998-04-23T04:32:25-05:00',
|
125 | context: {
|
126 | presence: 'none'
|
127 | }
|
128 | }
|
129 | ])
|
130 | .then(() => {
|
131 | return client.getAgentContextOperations(agents[0].id);
|
132 | })
|
133 | .then((operations) => {
|
134 | expect(operations).to.be.deep.equal([
|
135 | {
|
136 | timestamp: 893323800,
|
137 | context: {
|
138 | presence: 'robert',
|
139 | lightIntensity: 0.4,
|
140 | lightbulbColor: 'green'
|
141 | }
|
142 | },
|
143 | {
|
144 | timestamp: 893323945,
|
145 | context: {
|
146 | presence: 'none'
|
147 | }
|
148 | }
|
149 | ]);
|
150 | });
|
151 | });
|
152 | it('should succeed when using operations with Time timestamps', function() {
|
153 | return client.addAgentContextOperations(agents[0].id, [
|
154 | {
|
155 | timestamp: new Time('1998-04-23T04:30:00-05:00'),
|
156 | context: {
|
157 | presence: 'robert',
|
158 | lightIntensity: 0.4,
|
159 | lightbulbColor: 'green'
|
160 | }
|
161 | },
|
162 | {
|
163 | timestamp: Time('1998-04-23T04:32:25-05:00'),
|
164 | context: {
|
165 | presence: 'none'
|
166 | }
|
167 | }
|
168 | ])
|
169 | .then(() => {
|
170 | return client.getAgentContextOperations(agents[0].id);
|
171 | })
|
172 | .then((operations) => {
|
173 | expect(operations).to.be.deep.equal([
|
174 | {
|
175 | timestamp: 893323800,
|
176 | context: {
|
177 | presence: 'robert',
|
178 | lightIntensity: 0.4,
|
179 | lightbulbColor: 'green'
|
180 | }
|
181 | },
|
182 | {
|
183 | timestamp: 893323945,
|
184 | context: {
|
185 | presence: 'none'
|
186 | }
|
187 | }
|
188 | ]);
|
189 | });
|
190 | });
|
191 | it('should succeed when sending invalid operations or no operation at all', function() {
|
192 | return client.addAgentContextOperations(agents[0].id, CONFIGURATION_1_OPERATIONS_1)
|
193 | .then(() => client.addAgentContextOperations(agents[0].id, []))
|
194 | .then(() => client.addAgentContextOperations(agents[0].id, undefined))
|
195 | .then(() => client.addAgentContextOperations(agents[0].id, [undefined, undefined]))
|
196 | .then(() => client.getAgentContextOperations(agents[0].id))
|
197 | .then((retrievedOperations) => {
|
198 | expect(retrievedOperations).to.be.deep.equal(CONFIGURATION_1_OPERATIONS_1);
|
199 | });
|
200 | });
|
201 | it('should fail when using out-of-order operations', function() {
|
202 | return client.addAgentContextOperations(agents[0].id, CONFIGURATION_1_OPERATIONS_1)
|
203 | .then(() => {
|
204 | return client.getAgentContextOperations(agents[0].id);
|
205 | })
|
206 | .then((retrievedOperations) => {
|
207 | expect(retrievedOperations).to.be.deep.equal(CONFIGURATION_1_OPERATIONS_1);
|
208 | })
|
209 | .then(() => {
|
210 | return client.addAgentContextOperations(agents[0].id, CONFIGURATION_1_OPERATIONS_1[0]);
|
211 | })
|
212 | .then(
|
213 | () => Promise.reject(new Error('Should not be reached')),
|
214 | (err) => {
|
215 | expect(err).to.be.an.instanceof(errors.CraftAiError);
|
216 | expect(err).to.be.an.instanceof(errors.CraftAiBadRequestError);
|
217 | }
|
218 | );
|
219 | });
|
220 | it('should succeed with a very large payload', function() {
|
221 | return client.addAgentContextOperations(agents[0].id, CONFIGURATION_1_OPERATIONS_2)
|
222 | .then(() => {
|
223 | return client.getAgentContextOperations(agents[0].id);
|
224 | })
|
225 | .then((retrievedOperations) => {
|
226 | expect(retrievedOperations.length).to.be.equal(CONFIGURATION_1_OPERATIONS_2.length);
|
227 | expect(retrievedOperations).to.be.deep.equal(CONFIGURATION_1_OPERATIONS_2);
|
228 | });
|
229 | });
|
230 | it('should not fail when deleting the agent to which operations where added', function() {
|
231 | return client.addAgentContextOperations(agents[1].id, CONFIGURATION_1_OPERATIONS_1)
|
232 | .then(() => client.deleteAgent(agents[1].id))
|
233 | .then(() => client.getAgentContextOperations(agents[0].id))
|
234 | .then((retrievedOperations) => {
|
235 | expect(retrievedOperations).to.be.deep.empty;
|
236 | });
|
237 | });
|
238 | it('should work properly when sending operations to more than one agent', function() {
|
239 | return Promise.all([
|
240 | client.addAgentContextOperations(agents[0].id, CONFIGURATION_1_OPERATIONS_2),
|
241 | client.addAgentContextOperations(agents[1].id, CONFIGURATION_1_OPERATIONS_1)
|
242 | ])
|
243 | .then(() => client.getAgentContextOperations(agents[0].id))
|
244 | .then((retrievedOperations) => {
|
245 | expect(retrievedOperations.length).to.be.equal(CONFIGURATION_1_OPERATIONS_2.length);
|
246 | expect(retrievedOperations).to.be.deep.equal(CONFIGURATION_1_OPERATIONS_2);
|
247 | })
|
248 | .then(() => client.getAgentContextOperations(agents[1].id))
|
249 | .then((retrievedOperations) => {
|
250 | expect(retrievedOperations).to.be.deep.equal(CONFIGURATION_1_OPERATIONS_1);
|
251 | });
|
252 | });
|
253 | });
|