11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm')
31cb0ef41Sopenharmony_ciconst { cleanCwd } = require('../../fixtures/clean-snapshot.js')
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cit.cleanSnapshot = (str) => cleanCwd(str)
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cit.test('simple query', async t => {
81cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
91cb0ef41Sopenharmony_ci    prefixDir: {
101cb0ef41Sopenharmony_ci      node_modules: {
111cb0ef41Sopenharmony_ci        a: {
121cb0ef41Sopenharmony_ci          name: 'a',
131cb0ef41Sopenharmony_ci          version: '1.0.0',
141cb0ef41Sopenharmony_ci        },
151cb0ef41Sopenharmony_ci        b: {
161cb0ef41Sopenharmony_ci          name: 'b',
171cb0ef41Sopenharmony_ci          version: '^2.0.0',
181cb0ef41Sopenharmony_ci        },
191cb0ef41Sopenharmony_ci      },
201cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
211cb0ef41Sopenharmony_ci        name: 'project',
221cb0ef41Sopenharmony_ci        dependencies: {
231cb0ef41Sopenharmony_ci          a: '^1.0.0',
241cb0ef41Sopenharmony_ci          b: '^1.0.0',
251cb0ef41Sopenharmony_ci        },
261cb0ef41Sopenharmony_ci        peerDependencies: {
271cb0ef41Sopenharmony_ci          c: '1.0.0',
281cb0ef41Sopenharmony_ci        },
291cb0ef41Sopenharmony_ci      }),
301cb0ef41Sopenharmony_ci    },
311cb0ef41Sopenharmony_ci  })
321cb0ef41Sopenharmony_ci  await npm.exec('query', [':root, :root > *'])
331cb0ef41Sopenharmony_ci  t.matchSnapshot(joinedOutput(), 'should return root object and direct children')
341cb0ef41Sopenharmony_ci})
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cit.test('recursive tree', async t => {
371cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
381cb0ef41Sopenharmony_ci    prefixDir: {
391cb0ef41Sopenharmony_ci      node_modules: {
401cb0ef41Sopenharmony_ci        a: {
411cb0ef41Sopenharmony_ci          name: 'a',
421cb0ef41Sopenharmony_ci          version: '1.0.0',
431cb0ef41Sopenharmony_ci        },
441cb0ef41Sopenharmony_ci        b: {
451cb0ef41Sopenharmony_ci          name: 'b',
461cb0ef41Sopenharmony_ci          version: '^2.0.0',
471cb0ef41Sopenharmony_ci          dependencies: {
481cb0ef41Sopenharmony_ci            a: '1.0.0',
491cb0ef41Sopenharmony_ci          },
501cb0ef41Sopenharmony_ci        },
511cb0ef41Sopenharmony_ci      },
521cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
531cb0ef41Sopenharmony_ci        name: 'project',
541cb0ef41Sopenharmony_ci        dependencies: {
551cb0ef41Sopenharmony_ci          a: '^1.0.0',
561cb0ef41Sopenharmony_ci          b: '^1.0.0',
571cb0ef41Sopenharmony_ci        },
581cb0ef41Sopenharmony_ci      }),
591cb0ef41Sopenharmony_ci    },
601cb0ef41Sopenharmony_ci  })
611cb0ef41Sopenharmony_ci  await npm.exec('query', ['*'])
621cb0ef41Sopenharmony_ci  t.matchSnapshot(joinedOutput(), 'should return everything in the tree, accounting for recursion')
631cb0ef41Sopenharmony_ci})
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_cit.test('workspace query', async t => {
661cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
671cb0ef41Sopenharmony_ci    config: {
681cb0ef41Sopenharmony_ci      workspace: ['c'],
691cb0ef41Sopenharmony_ci    },
701cb0ef41Sopenharmony_ci    prefixDir: {
711cb0ef41Sopenharmony_ci      node_modules: {
721cb0ef41Sopenharmony_ci        a: {
731cb0ef41Sopenharmony_ci          name: 'a',
741cb0ef41Sopenharmony_ci          version: '1.0.0',
751cb0ef41Sopenharmony_ci        },
761cb0ef41Sopenharmony_ci        b: {
771cb0ef41Sopenharmony_ci          name: 'b',
781cb0ef41Sopenharmony_ci          version: '^2.0.0',
791cb0ef41Sopenharmony_ci        },
801cb0ef41Sopenharmony_ci        c: t.fixture('symlink', '../c'),
811cb0ef41Sopenharmony_ci      },
821cb0ef41Sopenharmony_ci      c: {
831cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
841cb0ef41Sopenharmony_ci          name: 'c',
851cb0ef41Sopenharmony_ci          version: '1.0.0',
861cb0ef41Sopenharmony_ci        }),
871cb0ef41Sopenharmony_ci      },
881cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
891cb0ef41Sopenharmony_ci        name: 'project',
901cb0ef41Sopenharmony_ci        workspaces: ['c'],
911cb0ef41Sopenharmony_ci        dependencies: {
921cb0ef41Sopenharmony_ci          a: '^1.0.0',
931cb0ef41Sopenharmony_ci          b: '^1.0.0',
941cb0ef41Sopenharmony_ci        },
951cb0ef41Sopenharmony_ci      }),
961cb0ef41Sopenharmony_ci    },
971cb0ef41Sopenharmony_ci  })
981cb0ef41Sopenharmony_ci  await npm.exec('query', [':scope'])
991cb0ef41Sopenharmony_ci  t.matchSnapshot(joinedOutput(), 'should return workspace object')
1001cb0ef41Sopenharmony_ci})
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_cit.test('include-workspace-root', async t => {
1031cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
1041cb0ef41Sopenharmony_ci    config: {
1051cb0ef41Sopenharmony_ci      'include-workspace-root': true,
1061cb0ef41Sopenharmony_ci      workspace: ['c'],
1071cb0ef41Sopenharmony_ci    },
1081cb0ef41Sopenharmony_ci    prefixDir: {
1091cb0ef41Sopenharmony_ci      node_modules: {
1101cb0ef41Sopenharmony_ci        a: {
1111cb0ef41Sopenharmony_ci          name: 'a',
1121cb0ef41Sopenharmony_ci          version: '1.0.0',
1131cb0ef41Sopenharmony_ci        },
1141cb0ef41Sopenharmony_ci        b: {
1151cb0ef41Sopenharmony_ci          name: 'b',
1161cb0ef41Sopenharmony_ci          version: '^2.0.0',
1171cb0ef41Sopenharmony_ci        },
1181cb0ef41Sopenharmony_ci        c: t.fixture('symlink', '../c'),
1191cb0ef41Sopenharmony_ci      },
1201cb0ef41Sopenharmony_ci      c: {
1211cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
1221cb0ef41Sopenharmony_ci          name: 'c',
1231cb0ef41Sopenharmony_ci          version: '1.0.0',
1241cb0ef41Sopenharmony_ci        }),
1251cb0ef41Sopenharmony_ci      },
1261cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
1271cb0ef41Sopenharmony_ci        name: 'project',
1281cb0ef41Sopenharmony_ci        workspaces: ['c'],
1291cb0ef41Sopenharmony_ci        dependencies: {
1301cb0ef41Sopenharmony_ci          a: '^1.0.0',
1311cb0ef41Sopenharmony_ci          b: '^1.0.0',
1321cb0ef41Sopenharmony_ci        },
1331cb0ef41Sopenharmony_ci      }),
1341cb0ef41Sopenharmony_ci    },
1351cb0ef41Sopenharmony_ci  })
1361cb0ef41Sopenharmony_ci  await npm.exec('query', [':scope'])
1371cb0ef41Sopenharmony_ci  t.matchSnapshot(joinedOutput(), 'should return workspace object and root object')
1381cb0ef41Sopenharmony_ci})
1391cb0ef41Sopenharmony_cit.test('linked node', async t => {
1401cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
1411cb0ef41Sopenharmony_ci    prefixDir: {
1421cb0ef41Sopenharmony_ci      node_modules: {
1431cb0ef41Sopenharmony_ci        a: t.fixture('symlink', '../a'),
1441cb0ef41Sopenharmony_ci      },
1451cb0ef41Sopenharmony_ci      a: {
1461cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
1471cb0ef41Sopenharmony_ci          name: 'a',
1481cb0ef41Sopenharmony_ci          version: '1.0.0',
1491cb0ef41Sopenharmony_ci        }),
1501cb0ef41Sopenharmony_ci      },
1511cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
1521cb0ef41Sopenharmony_ci        name: 'project',
1531cb0ef41Sopenharmony_ci        dependencies: {
1541cb0ef41Sopenharmony_ci          a: 'file:./a',
1551cb0ef41Sopenharmony_ci        },
1561cb0ef41Sopenharmony_ci      }),
1571cb0ef41Sopenharmony_ci    },
1581cb0ef41Sopenharmony_ci  })
1591cb0ef41Sopenharmony_ci  await npm.exec('query', ['[name=a]'])
1601cb0ef41Sopenharmony_ci  t.matchSnapshot(joinedOutput(), 'should return linked node res')
1611cb0ef41Sopenharmony_ci})
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_cit.test('global', async t => {
1641cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
1651cb0ef41Sopenharmony_ci    config: {
1661cb0ef41Sopenharmony_ci      global: true,
1671cb0ef41Sopenharmony_ci    },
1681cb0ef41Sopenharmony_ci    globalPrefixDir: {
1691cb0ef41Sopenharmony_ci      node_modules: {
1701cb0ef41Sopenharmony_ci        lorem: {
1711cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
1721cb0ef41Sopenharmony_ci            name: 'lorem',
1731cb0ef41Sopenharmony_ci            version: '2.0.0',
1741cb0ef41Sopenharmony_ci          }),
1751cb0ef41Sopenharmony_ci        },
1761cb0ef41Sopenharmony_ci      },
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci    },
1791cb0ef41Sopenharmony_ci  })
1801cb0ef41Sopenharmony_ci  await npm.exec('query', ['[name=lorem]'])
1811cb0ef41Sopenharmony_ci  t.matchSnapshot(joinedOutput(), 'should return global package')
1821cb0ef41Sopenharmony_ci})
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_cit.test('package-lock-only', t => {
1851cb0ef41Sopenharmony_ci  t.test('no package lock', async t => {
1861cb0ef41Sopenharmony_ci    const { npm } = await loadMockNpm(t, {
1871cb0ef41Sopenharmony_ci      config: {
1881cb0ef41Sopenharmony_ci        'package-lock-only': true,
1891cb0ef41Sopenharmony_ci      },
1901cb0ef41Sopenharmony_ci      prefixDir: {
1911cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
1921cb0ef41Sopenharmony_ci          name: 'project',
1931cb0ef41Sopenharmony_ci          dependencies: {
1941cb0ef41Sopenharmony_ci            a: '^1.0.0',
1951cb0ef41Sopenharmony_ci          },
1961cb0ef41Sopenharmony_ci        }),
1971cb0ef41Sopenharmony_ci      },
1981cb0ef41Sopenharmony_ci    })
1991cb0ef41Sopenharmony_ci    await t.rejects(npm.exec('query', [':root, :root > *']), { code: 'EUSAGE' })
2001cb0ef41Sopenharmony_ci  })
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci  t.test('with package lock', async t => {
2031cb0ef41Sopenharmony_ci    const { npm, joinedOutput } = await loadMockNpm(t, {
2041cb0ef41Sopenharmony_ci      config: {
2051cb0ef41Sopenharmony_ci        'package-lock-only': true,
2061cb0ef41Sopenharmony_ci      },
2071cb0ef41Sopenharmony_ci      prefixDir: {
2081cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
2091cb0ef41Sopenharmony_ci          name: 'project',
2101cb0ef41Sopenharmony_ci          dependencies: {
2111cb0ef41Sopenharmony_ci            a: '^1.0.0',
2121cb0ef41Sopenharmony_ci          },
2131cb0ef41Sopenharmony_ci        }),
2141cb0ef41Sopenharmony_ci        'package-lock.json': JSON.stringify({
2151cb0ef41Sopenharmony_ci          name: 'project',
2161cb0ef41Sopenharmony_ci          lockfileVersion: 3,
2171cb0ef41Sopenharmony_ci          requires: true,
2181cb0ef41Sopenharmony_ci          packages: {
2191cb0ef41Sopenharmony_ci            '': {
2201cb0ef41Sopenharmony_ci              dependencies: {
2211cb0ef41Sopenharmony_ci                a: '^1.0.0',
2221cb0ef41Sopenharmony_ci              },
2231cb0ef41Sopenharmony_ci            },
2241cb0ef41Sopenharmony_ci            'node_modules/a': {
2251cb0ef41Sopenharmony_ci              version: '1.2.3',
2261cb0ef41Sopenharmony_ci              resolved: 'https://dummy.npmjs.org/a/-/a-1.2.3.tgz',
2271cb0ef41Sopenharmony_ci              integrity: 'sha512-dummy',
2281cb0ef41Sopenharmony_ci              engines: {
2291cb0ef41Sopenharmony_ci                node: '>=14.17',
2301cb0ef41Sopenharmony_ci              },
2311cb0ef41Sopenharmony_ci            },
2321cb0ef41Sopenharmony_ci          },
2331cb0ef41Sopenharmony_ci        }),
2341cb0ef41Sopenharmony_ci      },
2351cb0ef41Sopenharmony_ci    })
2361cb0ef41Sopenharmony_ci    await npm.exec('query', ['*'])
2371cb0ef41Sopenharmony_ci    t.matchSnapshot(joinedOutput(), 'should return valid response with only lock info')
2381cb0ef41Sopenharmony_ci  })
2391cb0ef41Sopenharmony_ci  t.end()
2401cb0ef41Sopenharmony_ci})
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_cit.test('expect entries', t => {
2431cb0ef41Sopenharmony_ci  const { exitCode } = process
2441cb0ef41Sopenharmony_ci  t.afterEach(() => process.exitCode = exitCode)
2451cb0ef41Sopenharmony_ci  const prefixDir = {
2461cb0ef41Sopenharmony_ci    node_modules: {
2471cb0ef41Sopenharmony_ci      a: { name: 'a', version: '1.0.0' },
2481cb0ef41Sopenharmony_ci    },
2491cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
2501cb0ef41Sopenharmony_ci      name: 'project',
2511cb0ef41Sopenharmony_ci      dependencies: { a: '^1.0.0' },
2521cb0ef41Sopenharmony_ci    }),
2531cb0ef41Sopenharmony_ci  }
2541cb0ef41Sopenharmony_ci  t.test('false, has entries', async t => {
2551cb0ef41Sopenharmony_ci    const { logs, npm, joinedOutput } = await loadMockNpm(t, {
2561cb0ef41Sopenharmony_ci      prefixDir,
2571cb0ef41Sopenharmony_ci    })
2581cb0ef41Sopenharmony_ci    npm.config.set('expect-results', false)
2591cb0ef41Sopenharmony_ci    await npm.exec('query', ['#a'])
2601cb0ef41Sopenharmony_ci    t.not(joinedOutput(), '[]', 'has entries')
2611cb0ef41Sopenharmony_ci    t.same(logs.warn, [['query', 'Expected no results, got 1']])
2621cb0ef41Sopenharmony_ci    t.ok(process.exitCode, 'exits with code')
2631cb0ef41Sopenharmony_ci  })
2641cb0ef41Sopenharmony_ci  t.test('false, no entries', async t => {
2651cb0ef41Sopenharmony_ci    const { npm, joinedOutput } = await loadMockNpm(t, {
2661cb0ef41Sopenharmony_ci      prefixDir,
2671cb0ef41Sopenharmony_ci    })
2681cb0ef41Sopenharmony_ci    npm.config.set('expect-results', false)
2691cb0ef41Sopenharmony_ci    await npm.exec('query', ['#b'])
2701cb0ef41Sopenharmony_ci    t.equal(joinedOutput(), '[]', 'does not have entries')
2711cb0ef41Sopenharmony_ci    t.notOk(process.exitCode, 'exits without code')
2721cb0ef41Sopenharmony_ci  })
2731cb0ef41Sopenharmony_ci  t.test('true, has entries', async t => {
2741cb0ef41Sopenharmony_ci    const { npm, joinedOutput } = await loadMockNpm(t, {
2751cb0ef41Sopenharmony_ci      prefixDir,
2761cb0ef41Sopenharmony_ci    })
2771cb0ef41Sopenharmony_ci    npm.config.set('expect-results', true)
2781cb0ef41Sopenharmony_ci    await npm.exec('query', ['#a'])
2791cb0ef41Sopenharmony_ci    t.not(joinedOutput(), '[]', 'has entries')
2801cb0ef41Sopenharmony_ci    t.notOk(process.exitCode, 'exits without code')
2811cb0ef41Sopenharmony_ci  })
2821cb0ef41Sopenharmony_ci  t.test('true, no entries', async t => {
2831cb0ef41Sopenharmony_ci    const { logs, npm, joinedOutput } = await loadMockNpm(t, {
2841cb0ef41Sopenharmony_ci      prefixDir,
2851cb0ef41Sopenharmony_ci    })
2861cb0ef41Sopenharmony_ci    npm.config.set('expect-results', true)
2871cb0ef41Sopenharmony_ci    await npm.exec('query', ['#b'])
2881cb0ef41Sopenharmony_ci    t.equal(joinedOutput(), '[]', 'does not have entries')
2891cb0ef41Sopenharmony_ci    t.same(logs.warn, [['query', 'Expected results, got 0']])
2901cb0ef41Sopenharmony_ci    t.ok(process.exitCode, 'exits with code')
2911cb0ef41Sopenharmony_ci  })
2921cb0ef41Sopenharmony_ci  t.test('count, matches', async t => {
2931cb0ef41Sopenharmony_ci    const { npm, joinedOutput } = await loadMockNpm(t, {
2941cb0ef41Sopenharmony_ci      prefixDir,
2951cb0ef41Sopenharmony_ci    })
2961cb0ef41Sopenharmony_ci    npm.config.set('expect-result-count', 1)
2971cb0ef41Sopenharmony_ci    await npm.exec('query', ['#a'])
2981cb0ef41Sopenharmony_ci    t.not(joinedOutput(), '[]', 'has entries')
2991cb0ef41Sopenharmony_ci    t.notOk(process.exitCode, 'exits without code')
3001cb0ef41Sopenharmony_ci  })
3011cb0ef41Sopenharmony_ci  t.test('count 1, does not match', async t => {
3021cb0ef41Sopenharmony_ci    const { logs, npm, joinedOutput } = await loadMockNpm(t, {
3031cb0ef41Sopenharmony_ci      prefixDir,
3041cb0ef41Sopenharmony_ci    })
3051cb0ef41Sopenharmony_ci    npm.config.set('expect-result-count', 1)
3061cb0ef41Sopenharmony_ci    await npm.exec('query', ['#b'])
3071cb0ef41Sopenharmony_ci    t.equal(joinedOutput(), '[]', 'does not have entries')
3081cb0ef41Sopenharmony_ci    t.same(logs.warn, [['query', 'Expected 1 result, got 0']])
3091cb0ef41Sopenharmony_ci    t.ok(process.exitCode, 'exits with code')
3101cb0ef41Sopenharmony_ci  })
3111cb0ef41Sopenharmony_ci  t.test('count 3, does not match', async t => {
3121cb0ef41Sopenharmony_ci    const { logs, npm, joinedOutput } = await loadMockNpm(t, {
3131cb0ef41Sopenharmony_ci      prefixDir,
3141cb0ef41Sopenharmony_ci    })
3151cb0ef41Sopenharmony_ci    npm.config.set('expect-result-count', 3)
3161cb0ef41Sopenharmony_ci    await npm.exec('query', ['#b'])
3171cb0ef41Sopenharmony_ci    t.equal(joinedOutput(), '[]', 'does not have entries')
3181cb0ef41Sopenharmony_ci    t.same(logs.warn, [['query', 'Expected 3 results, got 0']])
3191cb0ef41Sopenharmony_ci    t.ok(process.exitCode, 'exits with code')
3201cb0ef41Sopenharmony_ci  })
3211cb0ef41Sopenharmony_ci  t.end()
3221cb0ef41Sopenharmony_ci})
323