Skip to content

Commit 70168e6

Browse files
committed
port some tests to autocomplete
1 parent 88edcd3 commit 70168e6

File tree

6 files changed

+129
-82
lines changed

6 files changed

+129
-82
lines changed

packages/browser-runtime-core/src/open-context-runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface InterpreterEnvironment {
2929
*/
3030
export class OpenContextRuntime implements Runtime {
3131
private interpreterEnvironment: InterpreterEnvironment;
32+
// TODO: we have to also port this to the new autocomplete
3233
private autocompleter: ShellApiAutocompleter | null = null;
3334
private shellEvaluator: ShellEvaluator;
3435
private instanceState: ShellInstanceState;

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,6 +2599,7 @@ describe('CliRepl', function () {
25992599
expect(output).to.include('db.movies.find({year: {$gte');
26002600
});
26012601

2602+
// TODO: port to the new autocomplete
26022603
it('completes properties of shell API result types', async function () {
26032604
if (!hasCollectionNames) return this.skip();
26042605

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 122 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/* eslint-disable no-control-regex */
22
import { MongoshCommandFailed } from '@mongosh/errors';
3-
import type { ServiceProvider } from '@mongosh/service-provider-core';
3+
import type {
4+
AggregationCursor,
5+
ServiceProvider,
6+
} from '@mongosh/service-provider-core';
47
import { bson } from '@mongosh/service-provider-core';
58
import { ADMIN_DB } from '@mongosh/shell-api/lib/enums';
69
import { CliUserConfig } from '@mongosh/types';
@@ -89,6 +92,14 @@ describe('MongoshNodeRepl', function () {
8992
},
9093
});
9194
sp.runCommandWithCheck.resolves({ ok: 1 });
95+
96+
if (process.env.USE_NEW_AUTOCOMPLETE) {
97+
sp.listCollections.resolves([{ name: 'coll' }]);
98+
const aggCursor = stubInterface<AggregationCursor>();
99+
aggCursor.toArray.resolves([{ foo: 1, bar: 2 }]);
100+
sp.aggregate.returns(aggCursor);
101+
}
102+
92103
serviceProvider = sp;
93104
calledServiceProviderFunctions = () =>
94105
Object.fromEntries(
@@ -352,6 +363,14 @@ describe('MongoshNodeRepl', function () {
352363
};
353364
const tabtab = async () => {
354365
await tab();
366+
if (process.env.USE_NEW_AUTOCOMPLETE) {
367+
// TODO: This is because autocomplete() is async and will either list
368+
// databases or collections or sample documents, any of which takes time
369+
// to complete and can time out. There is probably a better way.
370+
await new Promise((resolve) => {
371+
setTimeout(resolve, 210);
372+
});
373+
}
355374
await tab();
356375
};
357376

@@ -379,19 +398,28 @@ describe('MongoshNodeRepl', function () {
379398
expect(output).to.include('65537');
380399
});
381400

382-
it('does not stop input when autocompleting during .editor', async function () {
383-
input.write('.editor\n');
384-
await tick();
385-
expect(output).to.include('Entering editor mode');
386-
output = '';
387-
input.write('db.');
388-
await tabtab();
389-
await tick();
390-
input.write('version()\n');
391-
input.write('\u0004'); // Ctrl+D
392-
await waitEval(bus);
393-
expect(output).to.include('Error running command serverBuildInfo');
394-
});
401+
context(
402+
`autocompleting during .editor [${
403+
process.env.USE_NEW_AUTOCOMPLETE ?? 'old'
404+
}]`,
405+
function () {
406+
it('does not stop input when autocompleting during .editor', async function () {
407+
input.write('.editor\n');
408+
await tick();
409+
expect(output).to.include('Entering editor mode');
410+
output = '';
411+
input.write('db.');
412+
await tabtab();
413+
await tick();
414+
input.write('version()\n');
415+
input.write('\u0004'); // Ctrl+D
416+
await waitEval(bus);
417+
expect(output, output).to.include(
418+
'Error running command serverBuildInfo'
419+
);
420+
});
421+
}
422+
);
395423

396424
it('can enter multiline code', async function () {
397425
for (const line of multilineCode.split('\n')) {
@@ -449,73 +477,86 @@ describe('MongoshNodeRepl', function () {
449477
expect(code).to.equal(undefined);
450478
});
451479

452-
context('autocompletion', function () {
453-
it('autocompletes collection methods', async function () {
454-
input.write('db.coll.');
455-
await tabtab();
456-
await tick();
457-
expect(output).to.include('db.coll.updateOne');
458-
});
459-
it('autocompletes shell-api methods (once)', async function () {
460-
input.write('vers');
461-
await tabtab();
462-
await tick();
463-
expect(output).to.include('version');
464-
expect(output).to.not.match(/version[ \t]+version/);
465-
});
466-
it('autocompletes async shell api methods', async function () {
467-
input.write('db.coll.find().');
468-
await tabtab();
469-
await tick();
470-
expect(output).to.include('db.coll.find().close');
471-
});
472-
it('autocompletes local variables', async function () {
473-
input.write('let somelongvariable = 0\n');
474-
await waitEval(bus);
475-
output = '';
476-
input.write('somelong');
477-
await tabtab();
478-
await tick();
479-
expect(output).to.include('somelongvariable');
480-
});
481-
it('autocompletes partial repl commands', async function () {
482-
input.write('.e');
483-
await tabtab();
484-
await tick();
485-
expect(output).to.include('editor');
486-
expect(output).to.include('exit');
487-
});
488-
it('autocompletes full repl commands', async function () {
489-
input.write('.ed');
490-
await tabtab();
491-
await tick();
492-
expect(output).to.include('.editor');
493-
expect(output).not.to.include('exit');
494-
});
495-
it('autocompletion during .editor does not reset the prompt', async function () {
496-
input.write('.editor\n');
497-
await tick();
498-
output = '';
499-
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal('');
500-
input.write('db.');
501-
await tabtab();
502-
await tick();
503-
input.write('foo\nbar\n');
504-
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal('');
505-
input.write('\u0003'); // Ctrl+C for abort
506-
await tick();
507-
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal(
508-
'test> '
509-
);
510-
expect(stripAnsi(output)).to.equal('db.foo\r\nbar\r\n\r\ntest> ');
511-
});
512-
it('does not autocomplete tab-indented code', async function () {
513-
output = '';
514-
input.write('\t\tfoo');
515-
await tick();
516-
expect(output).to.equal('\t\tfoo');
517-
});
518-
});
480+
context(
481+
`autocompletion [${process.env.USE_NEW_AUTOCOMPLETE ?? 'old'}]`,
482+
function () {
483+
it('autocompletes collection methods', async function () {
484+
input.write('db.coll.');
485+
await tabtab();
486+
await tick();
487+
expect(output, output).to.include('db.coll.updateOne');
488+
});
489+
it('autocompletes collection schema fields', async function () {
490+
if (!process.env.USE_NEW_AUTOCOMPLETE) {
491+
// not supported in the old autocomplete
492+
this.skip();
493+
}
494+
input.write('db.coll.find({');
495+
await tabtab();
496+
await tick();
497+
expect(output, output).to.include('db.coll.find({foo');
498+
});
499+
it('autocompletes shell-api methods (once)', async function () {
500+
input.write('vers');
501+
await tabtab();
502+
await tick();
503+
expect(output, output).to.include('version');
504+
expect(output, output).to.not.match(/version[ \t]+version/);
505+
});
506+
it('autocompletes async shell api methods', async function () {
507+
input.write('db.coll.find().');
508+
await tabtab();
509+
await tick();
510+
expect(output, output).to.include('db.coll.find().toArray');
511+
});
512+
it('autocompletes local variables', async function () {
513+
input.write('let somelongvariable = 0\n');
514+
await waitEval(bus);
515+
output = '';
516+
input.write('somelong');
517+
await tabtab();
518+
await tick();
519+
expect(output, output).to.include('somelongvariable');
520+
});
521+
it('autocompletes partial repl commands', async function () {
522+
input.write('.e');
523+
await tabtab();
524+
await tick();
525+
expect(output, output).to.include('editor');
526+
expect(output, output).to.include('exit');
527+
});
528+
it('autocompletes full repl commands', async function () {
529+
input.write('.ed');
530+
await tabtab();
531+
await tick();
532+
expect(output, output).to.include('.editor');
533+
expect(output, output).not.to.include('exit');
534+
});
535+
it('autocompletion during .editor does not reset the prompt', async function () {
536+
input.write('.editor\n');
537+
await tick();
538+
output = '';
539+
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal('');
540+
input.write('db.');
541+
await tabtab();
542+
await tick();
543+
input.write('foo\nbar\n');
544+
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal('');
545+
input.write('\u0003'); // Ctrl+C for abort
546+
await tick();
547+
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal(
548+
'test> '
549+
);
550+
expect(stripAnsi(output)).to.equal('db.foo\r\nbar\r\n\r\ntest> ');
551+
});
552+
it('does not autocomplete tab-indented code', async function () {
553+
output = '';
554+
input.write('\t\tfoo');
555+
await tick();
556+
expect(output, output).to.equal('\t\tfoo');
557+
});
558+
}
559+
);
519560

520561
context('history support', function () {
521562
const arrowUp = '\x1b[A';

packages/e2e-tests/test/e2e-direct.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ describe('e2e direct connection', function () {
193193
expect(await shell.executeLine('show dbs')).to.include('admin');
194194
});
195195

196+
// TODO: port to the new autocomplete
196197
it('autocompletes collection names', async function () {
197198
if (process.arch === 's390x') {
198199
return this.skip(); // https://jira.mongodb.org/browse/MONGOSH-746

packages/e2e-tests/test/e2e-snippet.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe('snippet integration tests', function () {
7272
shell.assertNoErrors();
7373
});
7474

75+
// TODO: port to the new autocomplete
7576
it('autocompletes snippet commands', async function () {
7677
if (process.arch === 's390x') {
7778
return this.skip(); // https://jira.mongodb.org/browse/MONGOSH-746

packages/shell-api/src/collection.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2500,7 +2500,9 @@ export default class Collection extends ShellApiWithMongoClass {
25002500
}
25012501

25022502
async _getSampleDocs(): Promise<Document[]> {
2503-
this._cachedSampleDocs = await (await this.aggregate([])).toArray();
2503+
this._cachedSampleDocs = await (
2504+
await this.aggregate([{ $sample: { size: 10 } }])
2505+
).toArray();
25042506
return this._cachedSampleDocs;
25052507
}
25062508

0 commit comments

Comments
 (0)