11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_cicommon.requireNoPackageJSONAbove();
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst dep = fixtures.path('policy', 'parent.js');
141cb0ef41Sopenharmony_ci{
151cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
161cb0ef41Sopenharmony_ci    'policy',
171cb0ef41Sopenharmony_ci    'dependencies',
181cb0ef41Sopenharmony_ci    'dependencies-redirect-policy.json');
191cb0ef41Sopenharmony_ci  const { status, stderr, stdout } = spawnSync(
201cb0ef41Sopenharmony_ci    process.execPath,
211cb0ef41Sopenharmony_ci    [
221cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
231cb0ef41Sopenharmony_ci    ]
241cb0ef41Sopenharmony_ci  );
251cb0ef41Sopenharmony_ci  console.log('%s\n%s', stderr, stdout);
261cb0ef41Sopenharmony_ci  assert.strictEqual(status, 0);
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
301cb0ef41Sopenharmony_ci    'policy',
311cb0ef41Sopenharmony_ci    'dependencies',
321cb0ef41Sopenharmony_ci    'dependencies-redirect-builtin-policy.json');
331cb0ef41Sopenharmony_ci  const { status } = spawnSync(
341cb0ef41Sopenharmony_ci    process.execPath,
351cb0ef41Sopenharmony_ci    [
361cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
371cb0ef41Sopenharmony_ci    ]
381cb0ef41Sopenharmony_ci  );
391cb0ef41Sopenharmony_ci  assert.strictEqual(status, 0);
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci{
421cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
431cb0ef41Sopenharmony_ci    'policy',
441cb0ef41Sopenharmony_ci    'dependencies',
451cb0ef41Sopenharmony_ci    'dependencies-redirect-unknown-builtin-policy.json');
461cb0ef41Sopenharmony_ci  const { status } = spawnSync(
471cb0ef41Sopenharmony_ci    process.execPath,
481cb0ef41Sopenharmony_ci    [
491cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
501cb0ef41Sopenharmony_ci    ]
511cb0ef41Sopenharmony_ci  );
521cb0ef41Sopenharmony_ci  assert.strictEqual(status, 1);
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci{
551cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
561cb0ef41Sopenharmony_ci    'policy',
571cb0ef41Sopenharmony_ci    'dependencies',
581cb0ef41Sopenharmony_ci    'dependencies-wildcard-policy.json');
591cb0ef41Sopenharmony_ci  const { status, stderr, stdout } = spawnSync(
601cb0ef41Sopenharmony_ci    process.execPath,
611cb0ef41Sopenharmony_ci    [
621cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
631cb0ef41Sopenharmony_ci    ]
641cb0ef41Sopenharmony_ci  );
651cb0ef41Sopenharmony_ci  console.log('%s\n%s', stderr, stdout);
661cb0ef41Sopenharmony_ci  assert.strictEqual(status, 0);
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci{
691cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
701cb0ef41Sopenharmony_ci    'policy',
711cb0ef41Sopenharmony_ci    'dependencies',
721cb0ef41Sopenharmony_ci    'dependencies-empty-policy.json');
731cb0ef41Sopenharmony_ci  const { status } = spawnSync(
741cb0ef41Sopenharmony_ci    process.execPath,
751cb0ef41Sopenharmony_ci    [
761cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
771cb0ef41Sopenharmony_ci    ]
781cb0ef41Sopenharmony_ci  );
791cb0ef41Sopenharmony_ci  assert.strictEqual(status, 1);
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci{
821cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
831cb0ef41Sopenharmony_ci    'policy',
841cb0ef41Sopenharmony_ci    'dependencies',
851cb0ef41Sopenharmony_ci    'dependencies-missing-policy-default-true.json');
861cb0ef41Sopenharmony_ci  const { status } = spawnSync(
871cb0ef41Sopenharmony_ci    process.execPath,
881cb0ef41Sopenharmony_ci    [
891cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
901cb0ef41Sopenharmony_ci    ]
911cb0ef41Sopenharmony_ci  );
921cb0ef41Sopenharmony_ci  assert.strictEqual(status, 0);
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci{
951cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
961cb0ef41Sopenharmony_ci    'policy',
971cb0ef41Sopenharmony_ci    'dependencies',
981cb0ef41Sopenharmony_ci    'dependencies-missing-policy.json');
991cb0ef41Sopenharmony_ci  const { status } = spawnSync(
1001cb0ef41Sopenharmony_ci    process.execPath,
1011cb0ef41Sopenharmony_ci    [
1021cb0ef41Sopenharmony_ci      '--experimental-policy', depPolicy, dep,
1031cb0ef41Sopenharmony_ci    ]
1041cb0ef41Sopenharmony_ci  );
1051cb0ef41Sopenharmony_ci  assert.strictEqual(status, 1);
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci{
1081cb0ef41Sopenharmony_ci  // Regression test for https://github.com/nodejs/node/issues/37812
1091cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
1101cb0ef41Sopenharmony_ci    'policy',
1111cb0ef41Sopenharmony_ci    'dependencies',
1121cb0ef41Sopenharmony_ci    'dependencies-missing-export-policy.json');
1131cb0ef41Sopenharmony_ci  const { status, stderr } = spawnSync(
1141cb0ef41Sopenharmony_ci    process.execPath,
1151cb0ef41Sopenharmony_ci    [
1161cb0ef41Sopenharmony_ci      '--experimental-policy',
1171cb0ef41Sopenharmony_ci      depPolicy,
1181cb0ef41Sopenharmony_ci      fixtures.path('policy', 'bad-main.mjs'),
1191cb0ef41Sopenharmony_ci    ]
1201cb0ef41Sopenharmony_ci  );
1211cb0ef41Sopenharmony_ci  assert.strictEqual(status, 1);
1221cb0ef41Sopenharmony_ci  assert.match(
1231cb0ef41Sopenharmony_ci    `${stderr}`,
1241cb0ef41Sopenharmony_ci    /SyntaxError: Named export 'doesNotExist' not found\./,
1251cb0ef41Sopenharmony_ci    new Error('Should give the real SyntaxError and position'));
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci{
1281cb0ef41Sopenharmony_ci  const depPolicy = fixtures.path(
1291cb0ef41Sopenharmony_ci    'policy',
1301cb0ef41Sopenharmony_ci    'dependencies',
1311cb0ef41Sopenharmony_ci    'dependencies-scopes-relative-specifier.json');
1321cb0ef41Sopenharmony_ci  const { status } = spawnSync(
1331cb0ef41Sopenharmony_ci    process.execPath,
1341cb0ef41Sopenharmony_ci    [
1351cb0ef41Sopenharmony_ci      '--experimental-policy',
1361cb0ef41Sopenharmony_ci      depPolicy,
1371cb0ef41Sopenharmony_ci      fixtures.path('policy', 'canonicalize.mjs'),
1381cb0ef41Sopenharmony_ci    ]
1391cb0ef41Sopenharmony_ci  );
1401cb0ef41Sopenharmony_ci  assert.strictEqual(
1411cb0ef41Sopenharmony_ci    status,
1421cb0ef41Sopenharmony_ci    0,
1431cb0ef41Sopenharmony_ci    new Error(
1441cb0ef41Sopenharmony_ci      'policies should canonicalize specifiers by default prior to matching')
1451cb0ef41Sopenharmony_ci  );
1461cb0ef41Sopenharmony_ci}
147