-
Notifications
You must be signed in to change notification settings - Fork 180
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
chore(compass-e2e-tests): add generative query ai e2e tests with mock server COMPASS-6978 #4728
Merged
Anemy
merged 8 commits into
main
from
COMPASS-6978-add-generative-ai-e2e-tests-mock-server
Aug 15, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ae05b0
add e2e test for ai query success
Anemy 95ea242
Merge branch 'main' into COMPASS-6978-add-generative-ai-e2e-tests-moc…
Anemy 273a51b
add basic e2e test for ai query experience with mock server
Anemy b0d68e0
remove extra newline
Anemy 5b2fef5
update comment
Anemy de15275
retry with waitUntil for ipc events
Anemy 4dda51e
Merge branch 'main' into COMPASS-6978-add-generative-ai-e2e-tests-moc…
Anemy 5f75711
remove unneeded process env skip
Anemy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import http from 'http'; | ||
import { once } from 'events'; | ||
import type { AddressInfo } from 'net'; | ||
|
||
export type MockAtlasServerResponse = { | ||
status: number; | ||
body: any; | ||
}; | ||
|
||
export async function startMockAtlasServiceServer( | ||
{ | ||
response: _response, | ||
}: { | ||
response: MockAtlasServerResponse; | ||
} = { | ||
response: { | ||
status: 200, | ||
body: { | ||
content: { | ||
query: { | ||
filter: { | ||
test: 'pineapple', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
): Promise<{ | ||
clearRequests: () => void; | ||
getRequests: () => { | ||
content: any; | ||
req: any; | ||
}[]; | ||
setMockAtlasServerResponse: (response: MockAtlasServerResponse) => void; | ||
endpoint: string; | ||
server: http.Server; | ||
stop: () => Promise<void>; | ||
}> { | ||
let requests: { | ||
content: any; | ||
req: any; | ||
}[] = []; | ||
let response = _response; | ||
const server = http | ||
.createServer((req, res) => { | ||
let body = ''; | ||
req | ||
.setEncoding('utf8') | ||
.on('data', (chunk) => { | ||
body += chunk; | ||
}) | ||
.on('end', () => { | ||
const jsonObject = JSON.parse(body); | ||
requests.push({ | ||
req, | ||
content: jsonObject, | ||
}); | ||
|
||
res.setHeader('Content-Type', 'application/json'); | ||
if (response.status !== 200) { | ||
res.writeHead(response.status); | ||
} | ||
return res.end(JSON.stringify(response.body)); | ||
}); | ||
}) | ||
.listen(0); | ||
await once(server, 'listening'); | ||
|
||
// address() returns either a string or AddressInfo. | ||
const address = server.address() as AddressInfo; | ||
|
||
const endpoint = `http://localhost:${address.port}`; | ||
|
||
async function stop() { | ||
server.close(); | ||
await once(server, 'close'); | ||
} | ||
|
||
function clearRequests() { | ||
requests = []; | ||
} | ||
|
||
function getRequests() { | ||
return requests; | ||
} | ||
|
||
function setMockAtlasServerResponse(newResponse: MockAtlasServerResponse) { | ||
response = newResponse; | ||
} | ||
|
||
return { | ||
clearRequests, | ||
getRequests, | ||
endpoint, | ||
server, | ||
setMockAtlasServerResponse, | ||
stop, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
packages/compass-e2e-tests/tests/collection-ai-query.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import chai from 'chai'; | ||
|
||
import type { CompassBrowser } from '../helpers/compass-browser'; | ||
import { startTelemetryServer } from '../helpers/telemetry'; | ||
import type { Telemetry } from '../helpers/telemetry'; | ||
import { beforeTests, afterTests, afterTest } from '../helpers/compass'; | ||
import type { Compass } from '../helpers/compass'; | ||
import * as Selectors from '../helpers/selectors'; | ||
import { createNumbersCollection } from '../helpers/insert-data'; | ||
import { startMockAtlasServiceServer } from '../helpers/atlas-service'; | ||
import type { MockAtlasServerResponse } from '../helpers/atlas-service'; | ||
import { getFirstListDocument } from '../helpers/read-first-document-content'; | ||
|
||
const { expect } = chai; | ||
|
||
describe('Collection ai query', function () { | ||
let compass: Compass; | ||
let browser: CompassBrowser; | ||
let telemetry: Telemetry; | ||
let setMockAtlasServerResponse: (response: MockAtlasServerResponse) => void; | ||
let stopMockAtlasServer: () => Promise<void>; | ||
let getRequests: () => any[]; | ||
let clearRequests: () => void; | ||
|
||
before(async function () { | ||
process.env.COMPASS_E2E_SKIP_ATLAS_SIGNIN = 'true'; | ||
|
||
// Start a mock server to pass an ai response. | ||
const { | ||
endpoint, | ||
getRequests: _getRequests, | ||
clearRequests: _clearRequests, | ||
setMockAtlasServerResponse: _setMockAtlasServerResponse, | ||
stop, | ||
} = await startMockAtlasServiceServer(); | ||
|
||
stopMockAtlasServer = stop; | ||
getRequests = _getRequests; | ||
clearRequests = _clearRequests; | ||
setMockAtlasServerResponse = _setMockAtlasServerResponse; | ||
|
||
process.env.COMPASS_ATLAS_SERVICE_BASE_URL_OVERRIDE = endpoint; | ||
|
||
telemetry = await startTelemetryServer(); | ||
compass = await beforeTests({ | ||
extraSpawnArgs: ['--enableAIExperience'], | ||
}); | ||
browser = compass.browser; | ||
}); | ||
|
||
beforeEach(async function () { | ||
await createNumbersCollection(); | ||
await browser.connectWithConnectionString(); | ||
await browser.navigateToCollectionTab('test', 'numbers', 'Documents'); | ||
}); | ||
|
||
after(async function () { | ||
await stopMockAtlasServer(); | ||
|
||
delete process.env.COMPASS_ATLAS_SERVICE_BASE_URL_OVERRIDE; | ||
delete process.env.COMPASS_E2E_SKIP_ATLAS_SIGNIN; | ||
|
||
await afterTests(compass, this.currentTest); | ||
await telemetry.stop(); | ||
}); | ||
|
||
afterEach(async function () { | ||
clearRequests(); | ||
await afterTest(compass, this.currentTest); | ||
}); | ||
|
||
describe('when the ai model response is valid', function () { | ||
beforeEach(function () { | ||
setMockAtlasServerResponse({ | ||
status: 200, | ||
body: { | ||
content: { | ||
query: { | ||
filter: { | ||
i: { | ||
$gt: 50, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('makes request to the server and updates the query bar with the response', async function () { | ||
// Click the ask ai button. | ||
await browser.clickVisible(Selectors.QueryBarAskAIButton); | ||
|
||
// Enter the ai prompt. | ||
await browser.clickVisible(Selectors.QueryBarAITextInput); | ||
|
||
const testUserInput = 'find all documents where i is greater than 50'; | ||
await browser.setOrClearValue( | ||
Selectors.QueryBarAITextInput, | ||
testUserInput | ||
); | ||
|
||
// Click generate. | ||
await browser.clickVisible(Selectors.QueryBarAIGenerateQueryButton); | ||
|
||
// Wait for the ipc events to succeed. | ||
await browser.waitUntil(async function () { | ||
// Make sure the query bar was updated. | ||
const queryBarFilterContent = await browser.getCodemirrorEditorText( | ||
Selectors.queryBarOptionInputFilter('Documents') | ||
); | ||
return queryBarFilterContent === '{i: {$gt: 50}}'; | ||
}); | ||
|
||
// Check that the request was made with the correct parameters. | ||
const requests = getRequests(); | ||
expect(requests.length).to.equal(1); | ||
expect(requests[0].content.userInput).to.equal(testUserInput); | ||
expect(requests[0].content.collectionName).to.equal('numbers'); | ||
expect(requests[0].content.databaseName).to.equal('test'); | ||
expect(requests[0].content.schema).to.exist; | ||
|
||
// Run it and check that the correct documents are shown. | ||
await browser.runFind('Documents', true); | ||
const modifiedResult = await getFirstListDocument(browser); | ||
expect(modifiedResult.i).to.be.equal('51'); | ||
}); | ||
}); | ||
|
||
describe('when the Atlas service request errors', function () { | ||
beforeEach(function () { | ||
setMockAtlasServerResponse({ | ||
status: 500, | ||
body: { | ||
content: 'error', | ||
}, | ||
}); | ||
}); | ||
|
||
it('the error is shown to the user', async function () { | ||
// Click the ask ai button. | ||
await browser.clickVisible(Selectors.QueryBarAskAIButton); | ||
|
||
// Enter the ai prompt. | ||
await browser.clickVisible(Selectors.QueryBarAITextInput); | ||
|
||
const testUserInput = 'find all documents where i is greater than 50'; | ||
await browser.setOrClearValue( | ||
Selectors.QueryBarAITextInput, | ||
testUserInput | ||
); | ||
|
||
// Click generate. | ||
await browser.clickVisible(Selectors.QueryBarAIGenerateQueryButton); | ||
|
||
// Check that the error is shown. | ||
const errorBanner = await browser.$( | ||
Selectors.QueryBarAIErrorMessageBanner | ||
); | ||
await errorBanner.waitForDisplayed(); | ||
expect(await errorBanner.getText()).to.equal('500 Internal Server Error'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to keep it, but this is something we already cover with functional tests and I don't think redundancy is needed here. From our e2e tests I would mostly expect testing that the UI is correctly handling user interactions
I know that we do a similar thing for analytics, but this is not something we usually cover with unit / functional, so there it makes more sense to check specifically what got pushed to the backend
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I'll remove those extra checks in another pr to avoid another test ci run.