1const t = require('tap')
2const installed = require('../../../../lib/utils/completion/installed-shallow.js')
3const mockNpm = require('../../../fixtures/mock-npm')
4
5const mockShallow = async (t, config) => {
6  const res = await mockNpm(t, {
7    globalPrefixDir: {
8      node_modules: {
9        x: {},
10        '@scope': {
11          y: {},
12        },
13      },
14    },
15    prefixDir: {
16      node_modules: {
17        a: {},
18        '@scope': {
19          b: {},
20        },
21      },
22    },
23    config: { global: false, ...config },
24  })
25  return res
26}
27
28t.test('global not set, include globals with -g', async t => {
29  const { npm } = await mockShallow(t)
30  const opt = { conf: { argv: { remain: [] } } }
31  const res = await installed(npm, opt)
32  t.strictSame(res.sort(), [
33    '@scope/y -g',
34    'x -g',
35    'a',
36    '@scope/b',
37  ].sort())
38})
39
40t.test('global set, include globals and not locals', async t => {
41  const { npm } = await mockShallow(t, { global: true })
42  const opt = { conf: { argv: { remain: [] } } }
43  const res = await installed(npm, opt)
44  t.strictSame(res.sort(), [
45    '@scope/y',
46    'x',
47  ].sort())
48})
49
50t.test('more than 3 items in argv, skip it', async t => {
51  const { npm } = await mockShallow(t)
52  const opt = { conf: { argv: { remain: [1, 2, 3, 4, 5, 6] } } }
53  const res = await installed(npm, opt)
54  t.strictSame(res, null)
55})
56