11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst realFetch = require('npm-registry-fetch')
31cb0ef41Sopenharmony_ciconst mockNpm = require('../../fixtures/mock-npm')
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst noop = () => {}
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst mockStars = async (t, { npmFetch = noop, exec = true, ...opts }) => {
81cb0ef41Sopenharmony_ci  const mock = await mockNpm(t, {
91cb0ef41Sopenharmony_ci    command: 'stars',
101cb0ef41Sopenharmony_ci    exec,
111cb0ef41Sopenharmony_ci    mocks: {
121cb0ef41Sopenharmony_ci      'npm-registry-fetch': Object.assign(noop, realFetch, { json: npmFetch }),
131cb0ef41Sopenharmony_ci      '{LIB}/utils/get-identity.js': async () => 'foo',
141cb0ef41Sopenharmony_ci    },
151cb0ef41Sopenharmony_ci    ...opts,
161cb0ef41Sopenharmony_ci  })
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  return {
191cb0ef41Sopenharmony_ci    ...mock,
201cb0ef41Sopenharmony_ci    result: mock.stars.output,
211cb0ef41Sopenharmony_ci    logs: () => mock.logs.filter(l => l[1] === 'stars').map(l => l[2]),
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cit.test('no args', async t => {
261cb0ef41Sopenharmony_ci  t.plan(3)
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const npmFetch = async (uri, opts) => {
291cb0ef41Sopenharmony_ci    t.equal(uri, '/-/_view/starredByUser', 'should fetch from expected uri')
301cb0ef41Sopenharmony_ci    t.equal(opts.query.key, '"foo"', 'should match logged in username')
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    return {
331cb0ef41Sopenharmony_ci      rows: [
341cb0ef41Sopenharmony_ci        { value: '@npmcli/arborist' },
351cb0ef41Sopenharmony_ci        { value: '@npmcli/map-workspaces' },
361cb0ef41Sopenharmony_ci        { value: 'libnpmfund' },
371cb0ef41Sopenharmony_ci        { value: 'libnpmpublish' },
381cb0ef41Sopenharmony_ci        { value: 'ipt' },
391cb0ef41Sopenharmony_ci      ],
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  const { result } = await mockStars(t, { npmFetch })
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  t.matchSnapshot(
461cb0ef41Sopenharmony_ci    result,
471cb0ef41Sopenharmony_ci    'should output a list of starred packages'
481cb0ef41Sopenharmony_ci  )
491cb0ef41Sopenharmony_ci})
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cit.test('npm star <user>', async t => {
521cb0ef41Sopenharmony_ci  t.plan(3)
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  const npmFetch = async (uri, opts) => {
551cb0ef41Sopenharmony_ci    t.equal(uri, '/-/_view/starredByUser', 'should fetch from expected uri')
561cb0ef41Sopenharmony_ci    t.equal(opts.query.key, '"ruyadorno"', 'should match username')
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    return {
591cb0ef41Sopenharmony_ci      rows: [{ value: '@npmcli/arborist' }],
601cb0ef41Sopenharmony_ci    }
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  const { result } = await mockStars(t, { npmFetch, exec: ['ruyadorno'] })
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  t.match(
661cb0ef41Sopenharmony_ci    result,
671cb0ef41Sopenharmony_ci    '@npmcli/arborist',
681cb0ef41Sopenharmony_ci    'should output expected list of starred packages'
691cb0ef41Sopenharmony_ci  )
701cb0ef41Sopenharmony_ci})
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cit.test('unauthorized request', async t => {
731cb0ef41Sopenharmony_ci  const npmFetch = async () => {
741cb0ef41Sopenharmony_ci    throw Object.assign(
751cb0ef41Sopenharmony_ci      new Error('Not logged in'),
761cb0ef41Sopenharmony_ci      { code: 'ENEEDAUTH' }
771cb0ef41Sopenharmony_ci    )
781cb0ef41Sopenharmony_ci  }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  const { joinedOutput, stars, logs } = await mockStars(t, { npmFetch, exec: false })
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  await t.rejects(
831cb0ef41Sopenharmony_ci    stars.exec([]),
841cb0ef41Sopenharmony_ci    /Not logged in/,
851cb0ef41Sopenharmony_ci    'should throw unauthorized request msg'
861cb0ef41Sopenharmony_ci  )
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  t.strictSame(
891cb0ef41Sopenharmony_ci    logs(),
901cb0ef41Sopenharmony_ci    ['auth is required to look up your username'],
911cb0ef41Sopenharmony_ci    'should warn auth required msg'
921cb0ef41Sopenharmony_ci  )
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  t.equal(
951cb0ef41Sopenharmony_ci    joinedOutput(),
961cb0ef41Sopenharmony_ci    '',
971cb0ef41Sopenharmony_ci    'should have empty output'
981cb0ef41Sopenharmony_ci  )
991cb0ef41Sopenharmony_ci})
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_cit.test('unexpected error', async t => {
1021cb0ef41Sopenharmony_ci  const npmFetch = async () => {
1031cb0ef41Sopenharmony_ci    throw new Error('ERROR')
1041cb0ef41Sopenharmony_ci  }
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci  const { stars, logs } = await mockStars(t, { npmFetch, exec: false })
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  await t.rejects(
1091cb0ef41Sopenharmony_ci    stars.exec([]),
1101cb0ef41Sopenharmony_ci    /ERROR/,
1111cb0ef41Sopenharmony_ci    'should throw unexpected error message'
1121cb0ef41Sopenharmony_ci  )
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  t.strictSame(logs(), [], 'no logs')
1151cb0ef41Sopenharmony_ci})
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_cit.test('no pkg starred', async t => {
1181cb0ef41Sopenharmony_ci  const npmFetch = async () => ({ rows: [] })
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  const { logs } = await mockStars(t, { npmFetch })
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  t.strictSame(
1231cb0ef41Sopenharmony_ci    logs(),
1241cb0ef41Sopenharmony_ci    ['user has not starred any packages'],
1251cb0ef41Sopenharmony_ci    'should warn no starred packages msg'
1261cb0ef41Sopenharmony_ci  )
1271cb0ef41Sopenharmony_ci})
128