Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions packages/mongodb-constants/src/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import type { Completion } from './filter';
import { wrapField, getFilteredCompletions } from './filter';
import { wrapField, getFilteredCompletions, ALL_CONSTANTS } from './filter';
import semver from 'semver';
import util from 'util';

describe('completer', function () {
const simpleConstants: Completion[] = [
Expand All @@ -14,6 +16,16 @@ describe('completer', function () {
version: '>=2.0.0 <3.0.0 || >=3.5.0',
meta: 'expr:set',
},
{
value: 'woof',
version: '>=0.200.300 <0.200.301',
meta: 'accumulator',
},
{
value: 'honk',
version: '999.999.999',
meta: 'variable:system',
},
];

function getFilteredValues(
Expand All @@ -30,7 +42,13 @@ describe('completer', function () {
'Foo',
'bar',
]);
expect(getFilteredValues({ serverVersion: '0.0.1-alpha0' })).to.deep.eq([
});

it('should clean up version before comparing', function () {
expect(getFilteredValues({ serverVersion: '0.200.300-alpha0' })).to.deep.eq(
['foo', 'Foo', 'woof']
);
expect(getFilteredValues({ serverVersion: '0.0.0---what???' })).to.deep.eq([
'foo',
'Foo',
]);
Expand All @@ -44,14 +62,18 @@ describe('completer', function () {
);
});

it('should ignore version when version is not valid', function () {
expect(getFilteredValues({ serverVersion: '1' })).to.deep.eq([
it('should use default version when provided version is not valid', function () {
expect(
getFilteredValues({ serverVersion: 'one dot one dot zero' })
).to.deep.eq(['foo', 'Foo', 'bar', 'buz', 'barbar', 'meow', 'honk']);
expect(getFilteredValues({ serverVersion: '1.2' })).to.deep.eq([
'foo',
'Foo',
'bar',
'buz',
'barbar',
'meow',
'honk',
]);
});

Expand Down Expand Up @@ -170,4 +192,21 @@ describe('completer', function () {
);
});
});

describe('all completions', function () {
it('should have valid semver version or range specified', function () {
ALL_CONSTANTS.forEach(({ name, version }) => {
const errMessage = `Expected completion ${util.inspect({
name,
version,
})} to have valid version`;

try {
expect(semver.valid(version)).to.not.eq(null, errMessage);
} catch {
expect(semver.validRange(version)).to.not.eq(null, errMessage);
}
});
});
});
});
6 changes: 3 additions & 3 deletions packages/mongodb-constants/src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { QUERY_OPERATORS } from './query-operators';
import { STAGE_OPERATORS } from './stage-operators';
import { SYSTEM_VARIABLES } from './system-variables';

const ALL_CONSTANTS = [
export const ALL_CONSTANTS = [
...ACCUMULATORS,
...BSON_TYPES,
...BSON_TYPE_ALIASES,
Expand Down Expand Up @@ -111,7 +111,7 @@ function isIn<T extends string | number>(
* @param v2 Either a single version number or a range to match against
*/
function satisfiesVersion(v1: string, v2: string): boolean {
const isGTECheck = /^\d+?\.\d+?\.\d+?$/.test(v2);
const isGTECheck = /^\d+\.\d+\.\d+$/.test(v2);
return satisfies(v1, isGTECheck ? `>=${v2}` : v2);
}

Expand All @@ -123,7 +123,7 @@ export function createConstantFilter({
completion: Completion
) => boolean {
const currentServerVersion =
/^(?<version>\d+?\.\d+?\.\d+?)/.exec(serverVersion)?.groups?.version ??
/^(?<version>\d+\.\d+\.\d+)/.exec(serverVersion)?.groups?.version ??
// Fallback to default server version if provided version doesn't match
// regex so that semver doesn't throw when checking
DEFAULT_SERVER_VERSION;
Expand Down