Skip to content

Commit

Permalink
align tests and use a stub
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Nov 1, 2024
1 parent 8582193 commit a8bc30d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Place your settings in this file to overwrite default and user settings.
{
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
Expand Down
79 changes: 44 additions & 35 deletions src/test/suite/participant/participant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,26 @@ suite('Participant Controller Test Suite', function () {

suite('when connected', function () {
let sampleStub;
let mockedCollectionList = [
{ name: 'collOne' },
{ name: 'notifications' },
{ name: 'products' },
{ name: 'orders' },
{ name: 'categories' },
{ name: 'invoices' },
{ name: 'transactions' },
{ name: 'logs' },
{ name: 'messages' },
{ name: 'sessions' },
{ name: 'feedback' },
];
let listCollectionsStub;

beforeEach(function () {
sampleStub = sinon.stub();
listCollectionsStub = sinon
.stub()
.resolves([
{ name: 'collOne' },
{ name: 'notifications' },
{ name: 'products' },
{ name: 'orders' },
{ name: 'categories' },
{ name: 'invoices' },
{ name: 'transactions' },
{ name: 'logs' },
{ name: 'messages' },
{ name: 'sessions' },
{ name: 'feedback' },
]);

sinon.replace(
testParticipantController._connectionController,
'getActiveDataService',
Expand All @@ -439,7 +443,7 @@ suite('Participant Controller Test Suite', function () {
{ name: 'analytics' },
{ name: '123' },
]),
listCollections: () => Promise.resolve(mockedCollectionList),
listCollections: listCollectionsStub,
getMongoClientConnectionOptions: () => ({
url: TEST_DATABASE_URI,
options: {},
Expand Down Expand Up @@ -1373,7 +1377,30 @@ suite('Participant Controller Test Suite', function () {
};
});

test('prompts to select collection name', async function () {
afterEach(function () {
sinon.restore();
});

test('collection name gets picked automatically if there is only', async function () {
listCollectionsStub.resolves([{ name: 'onlyOneColl' }]);

const chatResult = await invokeChatHandler(chatRequestMock);

const responses = chatStreamStub.markdown
.getCalls()
.map((call) => call.args[0]);
expect(responses.length).equals(1);
expect(responses[0]).to.include(
'Ask anything about MongoDB, from writing queries to questions about your cluster.'
);

expect(chatResult?.metadata).deep.equals({
intent: 'emptyRequest',
chatId: 'pineapple',
});
});

test('prompts for collection name if there are multiple available', async function () {
const chatResult = await invokeChatHandler(chatRequestMock);

const emptyMessage = chatStreamStub.markdown.getCall(0).args[0];
Expand Down Expand Up @@ -1413,25 +1440,6 @@ suite('Participant Controller Test Suite', function () {
chatId: undefined,
});
});

test('shows the empty request message if there is only 1 collection in the database', async function () {
mockedCollectionList = [{ name: 'onlyColl' }];

const chatResult = await invokeChatHandler(chatRequestMock);

const responses = chatStreamStub.markdown
.getCalls()
.map((call) => call.args[0]);
expect(responses.length).equals(1);
expect(responses[0]).to.include(
'Ask anything about MongoDB, from writing queries to questions about your cluster.'
);

expect(chatResult?.metadata).deep.equals({
intent: 'emptyRequest',
chatId: 'pineapple',
});
});
});
});
});
Expand Down Expand Up @@ -1553,7 +1561,7 @@ suite('Participant Controller Test Suite', function () {
});

test('collection name gets picked automatically if there is only 1', async function () {
mockedCollectionList = [{ name: 'onlyOneColl' }];
listCollectionsStub.resolves([{ name: 'onlyOneColl' }]);
const renderCollectionsTreeSpy = sinon.spy(
testParticipantController,
'renderCollectionsTree'
Expand Down Expand Up @@ -1608,6 +1616,7 @@ suite('Participant Controller Test Suite', function () {
intent: 'askForNamespace',
chatId: testChatId,
databaseName: 'dbOne',
collectionName: undefined,
});
});
});
Expand Down

0 comments on commit a8bc30d

Please sign in to comment.