From c7bed7c7e6ea1cba495df9ed9ebb484907043c23 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Wed, 16 Aug 2023 15:07:56 -0500 Subject: [PATCH] test: ut --- .mocharc.json | 1 + test/commands/discover.test.ts | 79 ++++++++++++++++++++ test/commands/discoverQuery.test.ts | 112 ++++++++++++++++++++++++++++ test/commands/hello/world.test.ts | 51 ------------- 4 files changed, 192 insertions(+), 51 deletions(-) create mode 100644 test/commands/discover.test.ts create mode 100644 test/commands/discoverQuery.test.ts delete mode 100644 test/commands/hello/world.test.ts diff --git a/.mocharc.json b/.mocharc.json index d78869d..eaecb10 100644 --- a/.mocharc.json +++ b/.mocharc.json @@ -1,6 +1,7 @@ { "require": "ts-node/register,source-map-support/register", "watch-extensions": "ts", + "watch-files": ["src/**/*.ts", "test/**/*.ts"], "recursive": true, "reporter": "spec", "timeout": 600000 diff --git a/test/commands/discover.test.ts b/test/commands/discover.test.ts new file mode 100644 index 0000000..78b819b --- /dev/null +++ b/test/commands/discover.test.ts @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2023, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ +import { TestContext } from '@salesforce/core/lib/testSetup'; +import { expect, config } from 'chai'; +import { stubSfCommandUx } from '@salesforce/sf-plugins-core'; +import Discover from '../../src/commands/plugins/discover'; +import * as queryStubs from '../../src/shared/discoverQuery'; + +const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. ============================= Phasellus a magna odio. Nullam bibendum magna a lectus faucibus, et viverra neque ornare. Vestibulum tellus augue, rutrum et semper id, pellentesque eget lacus. Nunc ac ultricies nulla, in sagittis arcu. Maecenas accumsan ac dui eget pharetra. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque ac nisi pretium, suscipit metus ac, sodales nisl. Quisque eget finibus nunc, id iaculis quam. Fusce a ultrices leo. Morbi eu est condimentum sapien semper porttitor non ac nisl. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. + +Praesent viverra interdum enim commodo ullamcorper. Suspendisse potenti. Suspendisse tristique dignissim vestibulum. Maecenas non velit faucibus, dapibus tellus sit amet, hendrerit lacus. Nullam metus nunc, suscipit venenatis tempor eu, facilisis pulvinar tellus. Proin venenatis pretium justo, at venenatis sapien pharetra porttitor. Cras egestas eget mi tristique ullamcorper. Etiam pretium non risus quis maximus. Fusce in pulvinar ipsum, in elementum magna. Donec nec lacus sit amet est fringilla vehicula sit amet ac ante. Fusce non laoreet arcu. Sed nisi dolor, tempus nec tortor non, placerat tincidunt ligula. +`; + +config.truncateThreshold = 0; +describe('plugins discover', () => { + const $$ = new TestContext(); + let sfCommandStubs: ReturnType; + + beforeEach(() => { + sfCommandStubs = stubSfCommandUx($$.SANDBOX); + $$.SANDBOX.stub(queryStubs, 'query').resolves([ + [ + { name: '@salesforce/foo', description: 'FooDesc', homepage: 'https://www.salesforce.com' }, + { downloads: '10' }, + { + objects: [ + { package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } }, + // the search results yield a less optimal + { package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } }, + ], + }, + ], + [ + { name: '@salesforce/bar', description: lorem, homepage: 'https://www.salesforce.com' }, + { downloads: '100' }, + { + objects: [ + { package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } }, + // the search results yield a less optimal + { package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } }, + ], + }, + ], + [ + { name: '@salesforce/nodate', description: lorem, homepage: 'https://www.salesforce.com' }, + { downloads: '1' }, + { + objects: [ + { package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } }, + // the search results yield a less optimal + { package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } }, + ], + }, + ], + ]); + }); + + afterEach(() => { + $$.restore(); + }); + + it('plugins:discover returns a table', async () => { + await Discover.run([]); + + expect(sfCommandStubs.table.callCount).to.equal(1); + }); + + it('plugins:discover returns proper json', async () => { + const result = await Discover.run(['--json']); + expect(result.length).to.equal(3); + result.map((pkg) => { + expect(pkg).to.have.all.keys('name', 'description', 'homepage', 'downloads', 'date'); + }); + }); +}); diff --git a/test/commands/discoverQuery.test.ts b/test/commands/discoverQuery.test.ts new file mode 100644 index 0000000..d481c87 --- /dev/null +++ b/test/commands/discoverQuery.test.ts @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2023, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ +import { expect, config } from 'chai'; +import { transform, descriptionTransform, query } from '../../src/shared/discoverQuery'; +config.truncateThreshold = 0; + +describe('query tranformations', () => { + it('sorted by downloads', () => { + const result = [ + [ + { name: '@salesforce/foo', description: 'blah', homepage: 'https://www.salesforce.com' }, + { downloads: '10' }, + { + objects: [ + { package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } }, + // the search results yield a less optimal + { package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } }, + ], + }, + ], + [ + { name: '@salesforce/bar', description: 'blah', homepage: 'https://www.salesforce.com' }, + { downloads: '100' }, + { + objects: [ + { package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } }, + // the search results yield a less optimal + { package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } }, + ], + }, + ], + [ + { name: '@salesforce/nodate', description: 'blah', homepage: 'https://www.salesforce.com' }, + { downloads: '1' }, + { + objects: [ + { package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } }, + // the search results yield a less optimal + { package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } }, + ], + }, + ], + ] satisfies Awaited>; + const out = transform(result); + const barIndex = out.findIndex((r) => r.name === '@salesforce/bar'); + const fooIndex = out.findIndex((r) => r.name === '@salesforce/foo'); + const nodateIndex = out.findIndex((r) => r.name === '@salesforce/nodate'); + + expect(nodateIndex).to.be.greaterThan(-1); + expect(barIndex).to.be.greaterThan(-1); + expect(fooIndex).to.be.greaterThan(-1); + + expect(barIndex).to.be.lessThan(fooIndex); + expect(fooIndex).to.be.lessThan(nodateIndex); + }); + + it('handles no matching search results', () => { + expect( + transform([ + [ + { name: '@salesforce/nodate', description: 'blah', homepage: 'https://www.salesforce.com' }, + { downloads: '1' }, + { + objects: [ + { package: { name: '@salesforce/bar', date: '2021-11-02T17:34:14.637Z' } }, + { package: { name: '@salesforce/foo', date: '2020-11-02T17:34:14.637Z' } }, + ], + }, + ], + ])[0].date + ).to.equal(''); + }); + it('handles no search results', () => { + expect( + transform([ + [ + { name: '@salesforce/nodate', description: 'blah', homepage: 'https://www.salesforce.com' }, + { downloads: '1' }, + { + objects: [], + }, + ], + ])[0].date + ).to.equal(''); + }); + + describe('description transform', () => { + it('removes markdown dividers', () => { + expect(descriptionTransform('blah ================blah regular 2=2')).to.not.include('=='); + expect(descriptionTransform('blah ================blah regular 2=2')).to.include('2=2'); + }); + it('removes empty lines', () => { + expect( + descriptionTransform(`blah + + + blah`) + ).to.not.include('\n\n'); + }); + it('handles long descriptions search results', () => { + const lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus a magna odio. Nullam bibendum magna a lectus faucibus, et viverra neque ornare. Vestibulum tellus augue, rutrum et semper id, pellentesque eget lacus. Nunc ac ultricies nulla, in sagittis arcu. Maecenas accumsan ac dui eget pharetra. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque ac nisi pretium, suscipit metus ac, sodales nisl. Quisque eget finibus nunc, id iaculis quam. Fusce a ultrices leo. Morbi eu est condimentum sapien semper porttitor non ac nisl. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. + +Praesent viverra interdum enim commodo ullamcorper. Suspendisse potenti. Suspendisse tristique dignissim vestibulum. Maecenas non velit faucibus, dapibus tellus sit amet, hendrerit lacus. Nullam metus nunc, suscipit venenatis tempor eu, facilisis pulvinar tellus. Proin venenatis pretium justo, at venenatis sapien pharetra porttitor. Cras egestas eget mi tristique ullamcorper. Etiam pretium non risus quis maximus. Fusce in pulvinar ipsum, in elementum magna. Donec nec lacus sit amet est fringilla vehicula sit amet ac ante. Fusce non laoreet arcu. Sed nisi dolor, tempus nec tortor non, placerat tincidunt ligula. +`; + expect(Array.from(descriptionTransform(lorem).matchAll(/\n/g))).to.have.length.greaterThanOrEqual(10); + }); + }); +}); diff --git a/test/commands/hello/world.test.ts b/test/commands/hello/world.test.ts deleted file mode 100644 index eede335..0000000 --- a/test/commands/hello/world.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2023, salesforce.com, inc. - * All rights reserved. - * Licensed under the BSD 3-Clause license. - * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause - */ -import { TestContext } from '@salesforce/core/lib/testSetup'; -import { expect } from 'chai'; -import { stubSfCommandUx } from '@salesforce/sf-plugins-core'; -import World from '../../../src/commands/plugins/discover'; - -describe('plugins world', () => { - const $$ = new TestContext(); - let sfCommandStubs: ReturnType; - - beforeEach(() => { - sfCommandStubs = stubSfCommandUx($$.SANDBOX); - }); - - afterEach(() => { - $$.restore(); - }); - - it('runs plugins world', async () => { - await World.run([]); - const output = sfCommandStubs.log - .getCalls() - .flatMap((c) => c.args) - .join('\n'); - expect(output).to.include('Hello World'); - }); - - it('runs plugins world with --json and no provided name', async () => { - const result = await World.run([]); - expect(result.name).to.equal('World'); - }); - - it('runs plugins world --name Astro', async () => { - await World.run(['--name', 'Astro']); - const output = sfCommandStubs.log - .getCalls() - .flatMap((c) => c.args) - .join('\n'); - expect(output).to.include('Hello Astro'); - }); - - it('runs plugins world --name Astro --json', async () => { - const result = await World.run(['--name', 'Astro', '--json']); - expect(result.name).to.equal('Astro'); - }); -});