-
Notifications
You must be signed in to change notification settings - Fork 14
[NO-REF] - introduce pia http module #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
39689da
4ebbbdd
107617f
de96242
f610990
f8ab9ac
2337772
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,347 @@ | ||
| /* eslint-disable no-unused-expressions, import/no-unresolved, func-names */ | ||
| const dotenv = require('dotenv'); | ||
| const chai = require('chai'); | ||
| const chaiAsPromised = require('chai-as-promised'); | ||
| const sinon = require('sinon'); | ||
| const sinonChai = require('sinon-chai'); | ||
| const fs = require('fs'); | ||
| const helpers = require('../../mocha.helpers'); | ||
| const jsdom = require('../utils/jsdom-global'); | ||
| let ConstructorIO = require('../../../test/constructorio'); // eslint-disable-line import/extensions | ||
|
|
||
| chai.use(chaiAsPromised); | ||
| chai.use(sinonChai); | ||
| dotenv.config(); | ||
|
|
||
| const piaApiKey = process.env.TEST_PIA_REQUEST_API_KEY; | ||
| const clientVersion = 'cio-mocha'; | ||
| const bundled = process.env.BUNDLED === 'true'; | ||
| const skipNetworkTimeoutTests = process.env.SKIP_NETWORK_TIMEOUT_TESTS === 'true'; | ||
| const bundledDescriptionSuffix = bundled ? ' - Bundled' : ''; | ||
|
|
||
| describe(`ConstructorIO - Pia${bundledDescriptionSuffix}`, () => { | ||
| const validItemId = '149100215'; | ||
| const validQuestion = 'What material is this made of?'; | ||
| const jsdomOptions = { url: 'http://localhost' }; | ||
| let fetchSpy; | ||
| let cleanup; | ||
|
|
||
| if (bundled) { | ||
| jsdomOptions.src = fs.readFileSync(`./dist/constructorio-client-javascript-${process.env.PACKAGE_VERSION}.js`, 'utf-8'); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| cleanup = jsdom(jsdomOptions); | ||
| global.CLIENT_VERSION = clientVersion; | ||
| window.CLIENT_VERSION = clientVersion; | ||
| fetchSpy = sinon.spy(fetch); | ||
|
|
||
| if (bundled) { | ||
| ConstructorIO = window.ConstructorioClient; | ||
| } | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| delete global.CLIENT_VERSION; | ||
| delete window.CLIENT_VERSION; | ||
| cleanup(); | ||
|
|
||
| fetchSpy = null; | ||
| }); | ||
|
|
||
| describe('getSuggestedQuestions', () => { | ||
| it('Should return a result provided a valid apiKey and itemId', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return pia.getSuggestedQuestions(validItemId).then((res) => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(res).to.have.property('questions').to.be.an('array'); | ||
| expect(res.questions.length).to.be.greaterThan(0); | ||
| expect(res.questions[0]).to.have.property('value').to.be.a('string'); | ||
| expect(fetchSpy).to.have.been.called; | ||
| expect(requestedUrlParams).to.have.property('key'); | ||
| expect(requestedUrlParams).to.have.property('i'); | ||
| expect(requestedUrlParams).to.have.property('s'); | ||
| expect(requestedUrlParams).to.have.property('c').to.equal(clientVersion); | ||
| expect(requestedUrlParams).to.have.property('_dt'); | ||
| expect(requestedUrlParams).to.have.property('item_id').to.equal(validItemId); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should return a result provided a valid apiKey, itemId and variationId', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems like variationId doesn't need to be valid to return a result. Is this expected?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is expected. The test is verifying that the parameter gets passed through to the API correctly |
||
| const variationId = 'variation-123'; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return pia.getSuggestedQuestions(validItemId, { variationId }).then(() => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(requestedUrlParams).to.have.property('variation_id').to.equal(variationId); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should return a result provided a valid apiKey, itemId and numResults', () => { | ||
| const numResults = 2; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return pia.getSuggestedQuestions(validItemId, { numResults }).then((res) => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(res).to.have.property('questions').to.be.an('array'); | ||
| expect(requestedUrlParams).to.have.property('num_results').to.equal(numResults.toString()); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should return a result provided a valid apiKey, itemId and user id', () => { | ||
| const userId = 'user-id'; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| userId, | ||
| }); | ||
|
|
||
| return pia.getSuggestedQuestions(validItemId).then(() => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(requestedUrlParams).to.have.property('ui').to.equal(userId); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should return a result provided a valid apiKey, itemId and segments', () => { | ||
| const segments = ['foo', 'bar']; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| segments, | ||
| }); | ||
|
|
||
| return pia.getSuggestedQuestions(validItemId).then(() => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(requestedUrlParams).to.have.property('us').to.deep.equal(segments); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should pass threadId as a query parameter when provided', () => { | ||
| const threadId = '550e8400-e29b-41d4-a716-446655440000'; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return pia.getSuggestedQuestions(validItemId, { threadId }).then(() => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(requestedUrlParams).to.have.property('thread_id').to.equal(threadId); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should be rejected if response is malformed', () => { | ||
| const malformedFetch = () => Promise.resolve({ | ||
| ok: true, | ||
| json: () => Promise.resolve({ bad: 'response' }), | ||
| }); | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: malformedFetch, | ||
| }); | ||
|
|
||
| return expect(pia.getSuggestedQuestions(validItemId)).to.eventually.be.rejectedWith('getSuggestedQuestions response data is malformed'); | ||
| }); | ||
|
|
||
| it('Should be rejected if no itemId is provided', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return expect(pia.getSuggestedQuestions(null)).to.eventually.be.rejected; | ||
| }); | ||
|
|
||
| it('Should be rejected if an invalid apiKey is provided', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: 'invalidKey', | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return expect(pia.getSuggestedQuestions(validItemId)).to.eventually.be.rejected; | ||
| }); | ||
|
|
||
| if (!skipNetworkTimeoutTests) { | ||
| it('Should be rejected when network request timeout is provided and reached', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return expect(pia.getSuggestedQuestions(validItemId, {}, { timeout: 20 })).to.eventually.be.rejectedWith('This operation was aborted'); | ||
| }); | ||
|
|
||
| it('Should be rejected when global network request timeout is provided and reached', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| networkParameters: { timeout: 20 }, | ||
| }); | ||
|
|
||
| return expect(pia.getSuggestedQuestions(validItemId)).to.eventually.be.rejectedWith('This operation was aborted'); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| describe('getAnswerResults', () => { | ||
| it('Should return a result provided a valid apiKey, itemId and question', function () { | ||
| this.timeout(10000); | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return pia.getAnswerResults(validItemId, validQuestion).then((res) => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(res).to.have.property('qna_result_id').to.be.a('string'); | ||
| expect(res).to.have.property('value').to.be.a('string'); | ||
| expect(fetchSpy).to.have.been.called; | ||
| expect(requestedUrlParams).to.have.property('key'); | ||
| expect(requestedUrlParams).to.have.property('i'); | ||
| expect(requestedUrlParams).to.have.property('s'); | ||
| expect(requestedUrlParams).to.have.property('c').to.equal(clientVersion); | ||
| expect(requestedUrlParams).to.have.property('_dt'); | ||
| expect(requestedUrlParams).to.have.property('item_id').to.equal(validItemId); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should return a result provided a valid apiKey, itemId, question and variationId', function () { | ||
| this.timeout(10000); | ||
| const variationId = 'variation-123'; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return pia.getAnswerResults(validItemId, validQuestion, { variationId }).then(() => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(requestedUrlParams).to.have.property('variation_id').to.equal(variationId); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should return a result provided a valid apiKey, itemId, question and user id', function () { | ||
| this.timeout(10000); | ||
| const userId = 'user-id'; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| userId, | ||
| }); | ||
|
|
||
| return pia.getAnswerResults(validItemId, validQuestion).then(() => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(requestedUrlParams).to.have.property('ui').to.equal(userId); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should return a result provided a valid apiKey, itemId, question and segments', function () { | ||
| this.timeout(10000); | ||
| const segments = ['foo', 'bar']; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| segments, | ||
| }); | ||
|
|
||
| return pia.getAnswerResults(validItemId, validQuestion).then(() => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(requestedUrlParams).to.have.property('us').to.deep.equal(segments); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should pass threadId as a query parameter when provided', function () { | ||
| this.timeout(10000); | ||
| const threadId = '550e8400-e29b-41d4-a716-446655440000'; | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return pia.getAnswerResults(validItemId, validQuestion, { threadId }).then(() => { | ||
| const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy); | ||
|
|
||
| expect(requestedUrlParams).to.have.property('thread_id').to.equal(threadId); | ||
| }); | ||
| }); | ||
|
|
||
| it('Should be rejected if response is malformed', () => { | ||
| const malformedFetch = () => Promise.resolve({ | ||
| ok: true, | ||
| json: () => Promise.resolve({ bad: 'response' }), | ||
| }); | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: malformedFetch, | ||
| }); | ||
|
|
||
| return expect(pia.getAnswerResults(validItemId, validQuestion)).to.eventually.be.rejectedWith('getAnswerResults response data is malformed'); | ||
| }); | ||
|
|
||
| it('Should be rejected if no itemId is provided', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return expect(pia.getAnswerResults(null, validQuestion)).to.eventually.be.rejected; | ||
| }); | ||
|
|
||
| it('Should be rejected if no question is provided', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return expect(pia.getAnswerResults(validItemId, null)).to.eventually.be.rejected; | ||
| }); | ||
|
|
||
| it('Should be rejected if an invalid apiKey is provided', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: 'invalidKey', | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return expect(pia.getAnswerResults(validItemId, validQuestion)).to.eventually.be.rejected; | ||
| }); | ||
|
|
||
| if (!skipNetworkTimeoutTests) { | ||
| it('Should be rejected when network request timeout is provided and reached', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| }); | ||
|
|
||
| return expect(pia.getAnswerResults(validItemId, validQuestion, {}, { timeout: 20 })).to.eventually.be.rejectedWith('This operation was aborted'); | ||
| }); | ||
|
|
||
| it('Should be rejected when global network request timeout is provided and reached', () => { | ||
| const { agent: { pia } } = new ConstructorIO({ | ||
| apiKey: piaApiKey, | ||
| fetch: fetchSpy, | ||
| networkParameters: { timeout: 20 }, | ||
| }); | ||
|
|
||
| return expect(pia.getAnswerResults(validItemId, validQuestion)).to.eventually.be.rejectedWith('This operation was aborted'); | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.