Skip to content

Commit 316d188

Browse files
authored
feat(chat): Prompt to connect when a disconnected user tries to run code from participant VSCODE-618 (#872)
1 parent 6f57ce5 commit 316d188

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

src/editors/playgroundController.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,13 @@ export default class PlaygroundController {
583583
.get('confirmRunCopilotCode');
584584

585585
if (!this._connectionController.isCurrentlyConnected()) {
586-
// TODO(VSCODE-618): Prompt user to connect when clicked.
587-
void vscode.window.showErrorMessage(connectBeforeRunningMessage);
586+
const successfullyConnected =
587+
await this._connectionController.changeActiveConnection();
588588

589-
return false;
589+
if (!successfullyConnected) {
590+
void vscode.window.showErrorMessage(connectBeforeRunningMessage);
591+
return false;
592+
}
590593
}
591594

592595
if (shouldConfirmRunCopilotCode === true) {

src/test/suite/editors/playgroundController.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,60 @@ suite('Playground Controller Test Suite', function () {
270270
expectedMessage
271271
);
272272
});
273+
274+
suite('running code from the participant', function () {
275+
beforeEach(function () {
276+
sinon
277+
.stub(testPlaygroundController, '_evaluateWithCancelModal')
278+
.resolves({ result: '123' } as any);
279+
sinon.stub(testPlaygroundController, '_openInResultPane').resolves();
280+
281+
showInformationMessageStub.resolves('Yes');
282+
});
283+
284+
afterEach(() => sinon.restore());
285+
286+
test('prompts to connect to a database and succeeds with selection', async () => {
287+
const changeActiveConnectionStub = sinon.stub(
288+
testPlaygroundController._connectionController,
289+
'changeActiveConnection'
290+
);
291+
// Mocks the user selecting a connection.
292+
changeActiveConnectionStub.resolves(true);
293+
294+
const result = await testPlaygroundController.evaluateParticipantCode(
295+
'console.log("test");'
296+
);
297+
298+
expect(showErrorMessageStub.notCalled).is.true;
299+
300+
expect(changeActiveConnectionStub.calledOnce).is.true;
301+
302+
expect(result).is.true;
303+
});
304+
305+
test('prompts to connect to a database and errors if not selected', async () => {
306+
const changeActiveConnectionStub = sinon.stub(
307+
testPlaygroundController._connectionController,
308+
'changeActiveConnection'
309+
);
310+
// Mocks the user selecting a connection.
311+
changeActiveConnectionStub.resolves(false);
312+
313+
const result = await testPlaygroundController.evaluateParticipantCode(
314+
'console.log("test");'
315+
);
316+
317+
const expectedMessage =
318+
'Please connect to a database before running a playground.';
319+
await testPlaygroundController.runAllOrSelectedPlaygroundBlocks();
320+
expect(showErrorMessageStub.firstCall.args[0]).to.be.equal(
321+
expectedMessage
322+
);
323+
324+
expect(result).is.false;
325+
});
326+
});
273327
});
274328

275329
suite('user is connected', () => {
@@ -431,6 +485,35 @@ suite('Playground Controller Test Suite', function () {
431485
expect(result).to.be.false;
432486
});
433487
});
488+
489+
suite('running code from the participant', function () {
490+
beforeEach(function () {
491+
sinon
492+
.stub(testPlaygroundController, '_evaluateWithCancelModal')
493+
.resolves({ result: '123' } as any);
494+
sinon.stub(testPlaygroundController, '_openInResultPane').resolves();
495+
496+
showInformationMessageStub.resolves('Yes');
497+
});
498+
499+
afterEach(() => sinon.restore());
500+
501+
test('does not prompt to connect to the database', async () => {
502+
const changeActiveConnectionStub = sinon.stub(
503+
testPlaygroundController._connectionController,
504+
'changeActiveConnection'
505+
);
506+
const result = await testPlaygroundController.evaluateParticipantCode(
507+
'console.log("test");'
508+
);
509+
510+
expect(showErrorMessageStub.notCalled).is.true;
511+
512+
expect(changeActiveConnectionStub.notCalled).is.true;
513+
514+
expect(result).is.true;
515+
});
516+
});
434517
});
435518
});
436519
});

0 commit comments

Comments
 (0)