11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm.js') 31cb0ef41Sopenharmony_ciconst MockRegistry = require('@npmcli/mock-registry') 41cb0ef41Sopenharmony_ciconst libnpmsearchResultFixture = 51cb0ef41Sopenharmony_ci require('../../fixtures/libnpmsearch-stream-result.js') 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_cit.test('no args', async t => { 81cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t) 91cb0ef41Sopenharmony_ci await t.rejects( 101cb0ef41Sopenharmony_ci npm.exec('search', []), 111cb0ef41Sopenharmony_ci /search must be called with arguments/, 121cb0ef41Sopenharmony_ci 'should throw usage instructions' 131cb0ef41Sopenharmony_ci ) 141cb0ef41Sopenharmony_ci}) 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_cit.test('search <name> text', async t => { 171cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t) 181cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 191cb0ef41Sopenharmony_ci tap: t, 201cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 211cb0ef41Sopenharmony_ci }) 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 241cb0ef41Sopenharmony_ci await npm.exec('search', ['libnpm']) 251cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'should have expected search results') 261cb0ef41Sopenharmony_ci}) 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cit.test('search <name> --json', async t => { 291cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { json: true } }) 301cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 311cb0ef41Sopenharmony_ci tap: t, 321cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 331cb0ef41Sopenharmony_ci }) 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci await npm.exec('search', ['libnpm']) 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci t.same( 401cb0ef41Sopenharmony_ci JSON.parse(joinedOutput()), 411cb0ef41Sopenharmony_ci libnpmsearchResultFixture, 421cb0ef41Sopenharmony_ci 'should have expected search results as json' 431cb0ef41Sopenharmony_ci ) 441cb0ef41Sopenharmony_ci}) 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cit.test('search <name> --parseable', async t => { 471cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { parseable: true } }) 481cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 491cb0ef41Sopenharmony_ci tap: t, 501cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 511cb0ef41Sopenharmony_ci }) 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 541cb0ef41Sopenharmony_ci await npm.exec('search', ['libnpm']) 551cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'should have expected search results as parseable') 561cb0ef41Sopenharmony_ci}) 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_cit.test('search <name> --color', async t => { 591cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { color: 'always' } }) 601cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 611cb0ef41Sopenharmony_ci tap: t, 621cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 631cb0ef41Sopenharmony_ci }) 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 661cb0ef41Sopenharmony_ci await npm.exec('search', ['libnpm']) 671cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'should have expected search results with color') 681cb0ef41Sopenharmony_ci}) 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_cit.test('search /<name>/--color', async t => { 711cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { color: 'always' } }) 721cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 731cb0ef41Sopenharmony_ci tap: t, 741cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 751cb0ef41Sopenharmony_ci }) 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 781cb0ef41Sopenharmony_ci await npm.exec('search', ['/libnpm/']) 791cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'should have expected search results with color') 801cb0ef41Sopenharmony_ci}) 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_cit.test('search <name>', async t => { 831cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t) 841cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 851cb0ef41Sopenharmony_ci tap: t, 861cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 871cb0ef41Sopenharmony_ci }) 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci registry.search({ results: [{ 901cb0ef41Sopenharmony_ci name: 'foo', 911cb0ef41Sopenharmony_ci scope: 'unscoped', 921cb0ef41Sopenharmony_ci version: '1.0.0', 931cb0ef41Sopenharmony_ci description: '', 941cb0ef41Sopenharmony_ci keywords: [], 951cb0ef41Sopenharmony_ci date: null, 961cb0ef41Sopenharmony_ci author: { name: 'Foo', email: 'foo@npmjs.com' }, 971cb0ef41Sopenharmony_ci publisher: { name: 'Foo', email: 'foo@npmjs.com' }, 981cb0ef41Sopenharmony_ci maintainers: [ 991cb0ef41Sopenharmony_ci { username: 'foo', email: 'foo@npmjs.com' }, 1001cb0ef41Sopenharmony_ci ], 1011cb0ef41Sopenharmony_ci }, { 1021cb0ef41Sopenharmony_ci name: 'libnpmversion', 1031cb0ef41Sopenharmony_ci scope: 'unscoped', 1041cb0ef41Sopenharmony_ci version: '1.0.0', 1051cb0ef41Sopenharmony_ci description: '', 1061cb0ef41Sopenharmony_ci keywords: [], 1071cb0ef41Sopenharmony_ci date: null, 1081cb0ef41Sopenharmony_ci author: { name: 'Foo', email: 'foo@npmjs.com' }, 1091cb0ef41Sopenharmony_ci publisher: { name: 'Foo', email: 'foo@npmjs.com' }, 1101cb0ef41Sopenharmony_ci maintainers: [ 1111cb0ef41Sopenharmony_ci { username: 'foo', email: 'foo@npmjs.com' }, 1121cb0ef41Sopenharmony_ci ], 1131cb0ef41Sopenharmony_ci }] }) 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci await npm.exec('search', ['foo']) 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'should have filtered expected search results') 1181cb0ef41Sopenharmony_ci}) 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_cit.test('empty search results', async t => { 1211cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t) 1221cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1231cb0ef41Sopenharmony_ci tap: t, 1241cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 1251cb0ef41Sopenharmony_ci }) 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci registry.search({ results: [] }) 1281cb0ef41Sopenharmony_ci await npm.exec('search', ['foo']) 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'should have expected search results') 1311cb0ef41Sopenharmony_ci}) 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_cit.test('empty search results --json', async t => { 1341cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { json: true } }) 1351cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1361cb0ef41Sopenharmony_ci tap: t, 1371cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 1381cb0ef41Sopenharmony_ci }) 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci registry.search({ results: [] }) 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci await npm.exec('search', ['foo']) 1431cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '\n[]\n', 'should have expected empty square brackets') 1441cb0ef41Sopenharmony_ci}) 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_cit.test('search api response error', async t => { 1471cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t) 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1501cb0ef41Sopenharmony_ci tap: t, 1511cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 1521cb0ef41Sopenharmony_ci }) 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci registry.search({ error: 'ERR' }) 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci await t.rejects( 1571cb0ef41Sopenharmony_ci npm.exec('search', ['foo']), 1581cb0ef41Sopenharmony_ci /ERR/, 1591cb0ef41Sopenharmony_ci 'should throw response error' 1601cb0ef41Sopenharmony_ci ) 1611cb0ef41Sopenharmony_ci}) 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_cit.test('search exclude string', async t => { 1641cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { searchexclude: 'libnpmversion' } }) 1651cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1661cb0ef41Sopenharmony_ci tap: t, 1671cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 1681cb0ef41Sopenharmony_ci }) 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 1711cb0ef41Sopenharmony_ci await npm.exec('search', ['libnpm']) 1721cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'results should not have libnpmversion') 1731cb0ef41Sopenharmony_ci}) 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_cit.test('search exclude username with upper case letters', async t => { 1761cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { searchexclude: 'NLF' } }) 1771cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1781cb0ef41Sopenharmony_ci tap: t, 1791cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 1801cb0ef41Sopenharmony_ci }) 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 1831cb0ef41Sopenharmony_ci await npm.exec('search', ['libnpm']) 1841cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'results should not have nlf') 1851cb0ef41Sopenharmony_ci}) 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_cit.test('search exclude regex', async t => { 1881cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { searchexclude: '/version/' } }) 1891cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1901cb0ef41Sopenharmony_ci tap: t, 1911cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 1921cb0ef41Sopenharmony_ci }) 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 1951cb0ef41Sopenharmony_ci await npm.exec('search', ['libnpm']) 1961cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'results should not have libnpmversion') 1971cb0ef41Sopenharmony_ci}) 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_cit.test('search exclude forward slash', async t => { 2001cb0ef41Sopenharmony_ci const { npm, joinedOutput } = await loadMockNpm(t, { config: { searchexclude: '/version' } }) 2011cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 2021cb0ef41Sopenharmony_ci tap: t, 2031cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 2041cb0ef41Sopenharmony_ci }) 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci registry.search({ results: libnpmsearchResultFixture }) 2071cb0ef41Sopenharmony_ci await npm.exec('search', ['libnpm']) 2081cb0ef41Sopenharmony_ci t.matchSnapshot(joinedOutput(), 'results should not have libnpmversion') 2091cb0ef41Sopenharmony_ci}) 210