Skip to content

Commit cfb9d61

Browse files
committed
Add error info and tests
1 parent 54885d6 commit cfb9d61

File tree

2 files changed

+220
-25
lines changed

2 files changed

+220
-25
lines changed

src/participant/participant.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -853,26 +853,37 @@ export default class ParticipantController {
853853
stream: vscode.ChatResponseStream;
854854
}): Promise<string | undefined> {
855855
const collections = await this._getCollections({ stream, databaseName });
856-
857-
if (collections !== undefined) {
858-
if (collections.length === 1) {
859-
return collections[0].name;
860-
}
861-
856+
if (collections === undefined) {
862857
stream.markdown(
863-
`Which collection would you like to use within ${databaseName}?\n\n`
858+
vscode.l10n.t(
859+
`An error occurred when getting the collections from the database ${databaseName}.`
860+
)
864861
);
865-
866-
this.renderCollectionsTree({
867-
collections,
868-
command,
869-
databaseName,
870-
context,
871-
stream,
872-
});
862+
return;
863+
}
864+
if (collections.length === 0) {
865+
stream.markdown(
866+
vscode.l10n.t(
867+
`No collections were found in the database ${databaseName}.`
868+
)
869+
);
870+
return;
871+
}
872+
if (collections.length === 1) {
873+
return collections[0].name;
873874
}
874875

875-
return;
876+
stream.markdown(
877+
`Which collection would you like to use within ${databaseName}?\n\n`
878+
);
879+
880+
this.renderCollectionsTree({
881+
collections,
882+
command,
883+
databaseName,
884+
context,
885+
stream,
886+
});
876887
}
877888

878889
/** Gets the database name if there is only one collection.
@@ -888,8 +899,14 @@ export default class ParticipantController {
888899
}): Promise<string | undefined> {
889900
const databases = await this._getDatabases({ stream });
890901

891-
if (databases === undefined || databases.length === 0) {
892-
log.error('No databases found');
902+
if (databases === undefined) {
903+
stream.markdown(
904+
vscode.l10n.t('An error occurred when getting the databases.')
905+
);
906+
return;
907+
}
908+
if (databases.length === 0) {
909+
stream.markdown(vscode.l10n.t('No databases were found.'));
893910
return;
894911
}
895912

src/test/suite/participant/participant.test.ts

Lines changed: 185 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import { beforeEach, afterEach } from 'mocha';
33
import { expect } from 'chai';
4-
import type { SinonSpy } from 'sinon';
4+
import type { SinonSpy, SinonStub } from 'sinon';
55
import sinon from 'sinon';
66
import type { DataService } from 'mongodb-data-service';
77
import { ObjectId, Int32 } from 'bson';
@@ -403,9 +403,9 @@ suite('Participant Controller Test Suite', function () {
403403
});
404404

405405
suite('when connected', function () {
406-
let sampleStub;
407-
let listCollectionsStub;
408-
let listDatabasesStub;
406+
let sampleStub: SinonStub;
407+
let listCollectionsStub: SinonStub;
408+
let listDatabasesStub: SinonStub;
409409

410410
beforeEach(function () {
411411
sampleStub = sinon.stub();
@@ -1321,6 +1321,49 @@ suite('Participant Controller Test Suite', function () {
13211321
sinon.restore();
13221322
});
13231323

1324+
test('shows an error if something goes wrong with getting databases', async function () {
1325+
listDatabasesStub.rejects();
1326+
1327+
const chatResult = await invokeChatHandler({
1328+
prompt: 'find all docs by a name example',
1329+
command: 'query',
1330+
references: [],
1331+
});
1332+
1333+
expect(
1334+
chatStreamStub.markdown.getCalls().map((call) => call.args[0])
1335+
).deep.equals(['An error occurred when getting the databases.']);
1336+
1337+
expect(chatResult?.metadata).deep.equals({
1338+
chatId: testChatId,
1339+
intent: 'askForNamespace',
1340+
databaseName: undefined,
1341+
collectionName: undefined,
1342+
});
1343+
});
1344+
1345+
test('shows an error if there are no databases found', async function () {
1346+
// No databases
1347+
listDatabasesStub.resolves([]);
1348+
1349+
const chatResult = await invokeChatHandler({
1350+
prompt: 'find all docs by a name example',
1351+
command: 'query',
1352+
references: [],
1353+
});
1354+
1355+
expect(
1356+
chatStreamStub.markdown.getCalls().map((call) => call.args[0])
1357+
).deep.equals(['No databases were found.']);
1358+
1359+
expect(chatResult?.metadata).deep.equals({
1360+
chatId: testChatId,
1361+
intent: 'askForNamespace',
1362+
databaseName: undefined,
1363+
collectionName: undefined,
1364+
});
1365+
});
1366+
13241367
test('database name gets picked automatically if there is only 1', async function () {
13251368
listDatabasesStub.resolves([{ name: 'onlyOneDb' }]);
13261369

@@ -1334,7 +1377,7 @@ suite('Participant Controller Test Suite', function () {
13341377
);
13351378

13361379
const chatResult = await invokeChatHandler({
1337-
prompt: 'what is this',
1380+
prompt: 'find all docs by a name example',
13381381
command: 'query',
13391382
references: [],
13401383
});
@@ -1394,6 +1437,52 @@ suite('Participant Controller Test Suite', function () {
13941437
sinon.restore();
13951438
});
13961439

1440+
test('shows an error if something goes wrong with getting collections', async function () {
1441+
listCollectionsStub.rejects();
1442+
1443+
const chatResult = await invokeChatHandler({
1444+
prompt: 'find all docs by a name example',
1445+
command: 'query',
1446+
references: [],
1447+
});
1448+
1449+
expect(
1450+
chatStreamStub.markdown.getCalls().map((call) => call.args[0])
1451+
).deep.equals([
1452+
'An error occurred when getting the collections from the database dbOne.',
1453+
]);
1454+
1455+
expect(chatResult?.metadata).deep.equals({
1456+
chatId: testChatId,
1457+
intent: 'askForNamespace',
1458+
databaseName: 'dbOne',
1459+
collectionName: undefined,
1460+
});
1461+
});
1462+
1463+
test('shows an error if there are no collections found', async function () {
1464+
listCollectionsStub.resolves([]);
1465+
1466+
const chatResult = await invokeChatHandler({
1467+
prompt: 'find all docs by a name example',
1468+
command: 'query',
1469+
references: [],
1470+
});
1471+
1472+
expect(
1473+
chatStreamStub.markdown.getCalls().map((call) => call.args[0])
1474+
).deep.equals([
1475+
'No collections were found in the database dbOne.',
1476+
]);
1477+
1478+
expect(chatResult?.metadata).deep.equals({
1479+
chatId: testChatId,
1480+
intent: 'askForNamespace',
1481+
databaseName: 'dbOne',
1482+
collectionName: undefined,
1483+
});
1484+
});
1485+
13971486
test('collection name gets picked automatically if there is only 1', async function () {
13981487
listCollectionsStub.resolves([{ name: 'onlyOneColl' }]);
13991488
const renderCollectionsTreeSpy = sinon.spy(
@@ -1406,7 +1495,7 @@ suite('Participant Controller Test Suite', function () {
14061495
);
14071496

14081497
const chatResult = await invokeChatHandler({
1409-
prompt: 'what is this',
1498+
prompt: 'find all docs by a name example',
14101499
command: 'query',
14111500
references: [],
14121501
});
@@ -1577,6 +1666,49 @@ suite('Participant Controller Test Suite', function () {
15771666
sinon.restore();
15781667
});
15791668

1669+
test('shows an error if something goes wrong with getting databases', async function () {
1670+
listDatabasesStub.rejects();
1671+
1672+
const chatResult = await invokeChatHandler({
1673+
prompt: 'what is my schema',
1674+
command: 'schema',
1675+
references: [],
1676+
});
1677+
1678+
expect(
1679+
chatStreamStub.markdown.getCalls().map((call) => call.args[0])
1680+
).deep.equals(['An error occurred when getting the databases.']);
1681+
1682+
expect(chatResult?.metadata).deep.equals({
1683+
chatId: testChatId,
1684+
intent: 'askForNamespace',
1685+
databaseName: undefined,
1686+
collectionName: undefined,
1687+
});
1688+
});
1689+
1690+
test('shows an error if there are no databases found', async function () {
1691+
// No databases
1692+
listDatabasesStub.resolves([]);
1693+
1694+
const chatResult = await invokeChatHandler({
1695+
prompt: 'what is my schema',
1696+
command: 'schema',
1697+
references: [],
1698+
});
1699+
1700+
expect(
1701+
chatStreamStub.markdown.getCalls().map((call) => call.args[0])
1702+
).deep.equals(['No databases were found.']);
1703+
1704+
expect(chatResult?.metadata).deep.equals({
1705+
chatId: testChatId,
1706+
intent: 'askForNamespace',
1707+
databaseName: undefined,
1708+
collectionName: undefined,
1709+
});
1710+
});
1711+
15801712
test('database name gets picked automatically if there is only 1', async function () {
15811713
listDatabasesStub.resolves([{ name: 'onlyOneDb' }]);
15821714

@@ -1590,7 +1722,7 @@ suite('Participant Controller Test Suite', function () {
15901722
);
15911723

15921724
const chatResult = await invokeChatHandler({
1593-
prompt: 'what is this',
1725+
prompt: 'find all docs by a name example',
15941726
command: 'schema',
15951727
references: [],
15961728
});
@@ -1677,6 +1809,52 @@ suite('Participant Controller Test Suite', function () {
16771809
});
16781810
});
16791811

1812+
test('shows an error if something goes wrong with getting collections', async function () {
1813+
listCollectionsStub.rejects();
1814+
1815+
const chatResult = await invokeChatHandler({
1816+
prompt: 'what is my schema',
1817+
command: 'schema',
1818+
references: [],
1819+
});
1820+
1821+
expect(
1822+
chatStreamStub.markdown.getCalls().map((call) => call.args[0])
1823+
).deep.equals([
1824+
'An error occurred when getting the collections from the database dbOne.',
1825+
]);
1826+
1827+
expect(chatResult?.metadata).deep.equals({
1828+
chatId: testChatId,
1829+
intent: 'askForNamespace',
1830+
databaseName: 'dbOne',
1831+
collectionName: undefined,
1832+
});
1833+
});
1834+
1835+
test('shows an error if there are no collections found', async function () {
1836+
listCollectionsStub.resolves([]);
1837+
1838+
const chatResult = await invokeChatHandler({
1839+
prompt: 'what is my schema',
1840+
command: 'schema',
1841+
references: [],
1842+
});
1843+
1844+
expect(
1845+
chatStreamStub.markdown.getCalls().map((call) => call.args[0])
1846+
).deep.equals([
1847+
'No collections were found in the database dbOne.',
1848+
]);
1849+
1850+
expect(chatResult?.metadata).deep.equals({
1851+
chatId: testChatId,
1852+
intent: 'askForNamespace',
1853+
databaseName: 'dbOne',
1854+
collectionName: undefined,
1855+
});
1856+
});
1857+
16801858
test('prompts for collection name if there are multiple available', async function () {
16811859
const renderCollectionsTreeSpy = sinon.spy(
16821860
testParticipantController,

0 commit comments

Comments
 (0)