|
| 1 | +/* global it, describe, require, console */ |
| 2 | +const Kvass = require('../../src/main.js'); |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +const request = require('../../src/utils/superagent'); |
| 6 | +const mock = require('superagent-mocker')(request); |
| 7 | + |
| 8 | +const modelCreatedFile = require('../fetchmock/modelCreated.json'); |
| 9 | +const modelTrainingFile = require('../fetchmock/modelTraining.json'); |
| 10 | +const modelReadyFile = require('../fetchmock/modelReady.json'); |
| 11 | +const modelsFile = require('../fetchmock/models.json'); |
| 12 | +const recommendationsFile = require('../fetchmock/recommendations.json'); |
| 13 | + |
| 14 | +const endpoint = 'https://example.com/'; |
| 15 | +const sa = new Kvass({ apiKey: 'dummy', bearerToken: 'dummy', endpoint }); |
| 16 | +let url; |
| 17 | + |
| 18 | +describe('AI related tests', () => { |
| 19 | + beforeEach(() => { |
| 20 | + // Guarantee each test knows exactly which routes are defined |
| 21 | + mock.clearRoutes(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('GET ai/models', () => { |
| 25 | + it('Should return a list of models', (done) => { |
| 26 | + url = `${endpoint}ai/models`; |
| 27 | + mock.get(url, () => ({ body: modelsFile, ok: true })); |
| 28 | + sa.aiModel().getAll({}, (err, models) => { |
| 29 | + if (err) throw err; |
| 30 | + assert.ok(Array.isArray(models)); |
| 31 | + done(); |
| 32 | + }); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('POST ai/models', () => { |
| 37 | + it('Should create a new ai model based on type, source and destination', (done) => { |
| 38 | + url = `${endpoint}ai/models`; |
| 39 | + mock.post(url, () => ({ body: modelCreatedFile, ok: true })); |
| 40 | + sa.aiModel().create({ model_type: 'content_recommender', source: 'product', destination: 'product' }, (err, model) => { |
| 41 | + if (err) throw err; |
| 42 | + assert.ok(model.constructor.name === 'AIModel'); |
| 43 | + assert.ok(model.training_status === 'CREATED'); |
| 44 | + done(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('GET ai/models/<modelId>', () => { |
| 50 | + it('Should return details about one specific model', (done) => { |
| 51 | + url = `${endpoint}ai/models/:modelId:`; |
| 52 | + mock.get(url, () => ({ body: modelReadyFile, ok: true })); |
| 53 | + sa.aiModel(':modelId:').get({}, (err, model) => { |
| 54 | + if (err) throw err; |
| 55 | + assert.ok(model.constructor.name === 'AIModel'); |
| 56 | + assert.ok(model.training_status === 'READY'); |
| 57 | + done(); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + |
| 63 | + describe('POST ai/models/<modelId>/train', () => { |
| 64 | + it('Should train an existing ai model', (done) => { |
| 65 | + url = `${endpoint}ai/models/:modelId:/train`; |
| 66 | + mock.post(url, () => ({ body: modelTrainingFile, ok: true })); |
| 67 | + sa.aiModel(':modelId:').train({}, (err, model) => { |
| 68 | + if (err) throw err; |
| 69 | + assert.ok(model.constructor.name === 'AIModel'); |
| 70 | + assert.ok(model.training_status === 'TRAINING'); |
| 71 | + done(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('POST ai/models/<modelId>/invoke', () => { |
| 77 | + it('Should return recommendation for a given source based on a specific model', (done) => { |
| 78 | + url = `${endpoint}ai/models/:modelId/invoke`; |
| 79 | + mock.post(url, () => ({ body: recommendationsFile, ok: true })); |
| 80 | + sa.aiModel(':modelId:').getRecommendations({ source_id: '5aec176d1f7cdc0008848f87', size: 4 }, (err, recommendations) => { |
| 81 | + if (err) throw err; |
| 82 | + assert.ok(Array.isArray(recommendations.response)); |
| 83 | + done(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('POST ai/models/invoke', () => { |
| 89 | + it('Should return recommendation for a given source based on set model type, source and destination', (done) => { |
| 90 | + url = `${endpoint}ai/models/invoke`; |
| 91 | + mock.post(url, () => ({ body: recommendationsFile, ok: true })); |
| 92 | + sa.aiModel().getRecommendations({ |
| 93 | + size: 4, model_type: 'content_recommender', source: 'product', destination: 'product', source_id: '5aec176d1f7cdc0008848f87', |
| 94 | + }, (err, recommendations) => { |
| 95 | + if (err) throw err; |
| 96 | + assert.ok(Array.isArray(recommendations.response)); |
| 97 | + done(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments