UNPKG

4.15 kBJavaScriptView Raw
1import CONFIGURATION_1 from './data/configuration_1.json';
2import INVALID_CONFIGURATION_1 from './data/invalid_configuration_1.json';
3import craftai, { errors } from '../src';
4
5describe('client.createAgent(<configuration>, [id])', function() {
6 let client;
7
8 before(function() {
9 client = craftai(CRAFT_CFG);
10 expect(client).to.be.ok;
11 });
12
13 it('should succeed when using a valid configuration and generated id', function() {
14 return client.createAgent(CONFIGURATION_1)
15 .then((agent) => {
16 expect(agent).to.be.ok;
17 expect(agent.id).to.be.a.string;
18 return client.getAgent(agent.id)
19 .then((retrieveAgent) => {
20 expect(retrieveAgent.configuration).to.be.deep.equal(CONFIGURATION_1);
21 return client.deleteAgent(agent.id);
22 });
23 });
24 });
25
26 it('should succeed when using a valid configuration and specified id', function() {
27 const agentId = `unspeakable_dermatologist_${RUN_ID}`;
28 return client.deleteAgent(agentId) // Destroy any preexisting agent with this id.
29 .then(() => client.createAgent(CONFIGURATION_1, agentId))
30 .then((agent) => {
31 expect(agent).to.be.ok;
32 expect(agent.id).to.be.equal(agentId);
33 return client.deleteAgent(agent.id);
34 })
35 .catch((err) => {
36 client.deleteAgent(agentId) // The test might fail due to duplicate id, let's make sure it doesn't fail twice.
37 .then(() => {
38 throw err;
39 });
40 });
41 });
42
43 it('should fail when trying to use the same id twice', function() {
44 const agentId = `aphasic_parrot_${RUN_ID}`;
45 return client.deleteAgent(agentId) // Delete any preexisting agent with this id.
46 .then(() => client.createAgent(CONFIGURATION_1, agentId))
47 .then((agent) => {
48 expect(agent).to.be.ok;
49 expect(agent.id).to.be.equal(agentId);
50 return client.createAgent(CONFIGURATION_1, agentId);
51 })
52 .then(
53 () => Promise.reject(new Error('Should not be reached')),
54 (err) => {
55 expect(err).to.be.an.instanceof(errors.CraftAiError);
56 expect(err).to.be.an.instanceof(errors.CraftAiBadRequestError);
57 }
58 )
59 .then(() => {
60 return client.deleteAgent(agentId);
61 });
62 });
63
64 it('should fail when using invalid id', function() {
65 const agentId = `aphasic/parrot_${RUN_ID}`;
66 return client.createAgent(CONFIGURATION_1, agentId)
67 .then(
68 () => Promise.reject(new Error('Should not be reached')),
69 (err) => {
70 expect(err).to.be.an.instanceof(errors.CraftAiError);
71 expect(err).to.be.an.instanceof(errors.CraftAiBadRequestError);
72 }
73 );
74 });
75
76 it('should fail when using an undefined configuration', function() {
77 return client.createAgent(undefined)
78 .then(
79 () => Promise.reject(new Error('Should not be reached')),
80 (err) => {
81 expect(err).to.be.an.instanceof(errors.CraftAiError);
82 expect(err).to.be.an.instanceof(errors.CraftAiBadRequestError);
83 }
84 );
85 });
86
87 it('should fail when using an invalid configuration', function() {
88 return client.createAgent(INVALID_CONFIGURATION_1)
89 .then(
90 () => Promise.reject(new Error('Should not be reached')),
91 (err) => {
92 expect(err).to.be.an.instanceof(errors.CraftAiError);
93 expect(err).to.be.an.instanceof(errors.CraftAiBadRequestError);
94 }
95 );
96 });
97});
98
99describe('client.destroyAgent(<id>)', function() {
100 let client;
101
102 before(function() {
103 client = craftai(CRAFT_CFG);
104 expect(client).to.be.ok;
105 });
106
107 it('should still work even though it is deprecated', function() {
108 return client.createAgent(CONFIGURATION_1)
109 .then((agent) => {
110 return client.destroyAgent(agent.id)
111 .then(() => client.getAgent(agent.id))
112 .then(
113 () => Promise.reject(new Error('Should not be reached')),
114 (err) => {
115 expect(err).to.be.an.instanceof(errors.CraftAiError);
116 expect(err).to.be.an.instanceof(errors.CraftAiBadRequestError);
117 }
118 );
119 });
120 });
121});