summaryrefslogtreecommitdiff
path: root/test/parallel/test-policy-dependency-conditions.js
blob: dec17aa22984b0202e111cc0c360dfadb7779acf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
// Flags: --expose-internals

const common = require('../common');

if (!common.hasCrypto) common.skip('missing crypto');
common.requireNoPackageJSONAbove();

const Manifest = require('internal/policy/manifest').Manifest;


const assert = require('assert');

const { debuglog } = require('util');
const debug = debuglog('test');

const conditionTreePermutations = [
  ['default'],
  ['import'],
  ['node'],
  ['require'],
  ['require', 'import'],
  ['import', 'require'],
  ['default', 'require'],
  ['require', 'default'],
  ['node', 'require'],
  ['require', 'node'],
];
for (const totalDepth of [1, 2, 3]) {
  const conditionTrees = [];
  function calc(depthLeft = 0, path = []) {
    if (depthLeft) {
      for (const conditions of conditionTreePermutations) {
        calc(depthLeft - 1, [...path, conditions]);
      }
    } else {
      conditionTrees.push(path);
    }
  }
  calc(totalDepth, []);
  let nextURLId = 1;
  function getUniqueHREF() {
    const id = nextURLId++;
    return `test:${id}`;
  }
  for (const tree of conditionTrees) {
    const root = {};
    const targets = [root];
    const offsets = [-1];
    const order = [];
    while (offsets.length) {
      const depth = offsets.length - 1;
      offsets[depth]++;
      const conditionOffset = offsets[depth];
      const conditionsForDepth = tree[depth];
      if (conditionOffset >= conditionsForDepth.length) {
        offsets.pop();
        continue;
      }
      let target;
      if (depth === tree.length - 1) {
        target = getUniqueHREF();
        order.push({
          target,
          conditions: new Set(
            offsets.map(
              (conditionOffset, depth) => tree[depth][conditionOffset]
            )
          )
        });
      } else {
        target = {};
        targets[depth + 1] = target;
        offsets.push(-1);
      }
      const condition = tree[depth][conditionOffset];
      targets[depth][condition] = target;
    }
    const manifest = new Manifest({
      resources: {
        'test:_': {
          dependencies: {
            _: root
          }
        }
      }
    });
    const redirector = manifest.getDependencyMapper('test:_');
    for (const { target, conditions } of order) {
      const result = redirector.resolve('_', conditions).href;
      if (result !== target) {
        // If we didn't hit the target, make sure a target prior to this one
        // matched, including conditions
        searchPriorTargets:
        for (const { target: preTarget, conditions: preConditions } of order) {
          if (result === preTarget) {
            // Ensure that the current conditions are a super set of the
            // prior target
            for (const preCondition of preConditions) {
              if (conditions.has(preCondition) !== true) {
                continue searchPriorTargets;
              }
            }
            break searchPriorTargets;
          }
          if (preTarget === target) {
            debug(
              'dependencies %O expected ordering %O and trying for %j ' +
              'no prior targets matched',
              root,
              order,
              target
            );
            // THIS WILL ALWAYS FAIL, but we want that error message
            assert.strictEqual(
              result, target
            );
          }
        }
      }
    }
  }
}