Repository: puppeteer/puppeteer
File: tools/sort-test-expectations.mjs (JavaScript)

Code before:
  return isSubset;
}

const toBeRemoved = new Set();
for (let i = testExpectations.length - 1; i >= 0; i--) {
  const expectation = testExpectations[i];
  const params = new Set(expectation.parameters);
  const labels = new Set(expectation.expectations);
  const platforms = new Set(expectation.platforms);

  let foundMatch = false;
  for (let j = i - 1; j >= 0; j--) {
    const candidate = testExpectations[j];
    const candidateParams = new Set(candidate.parameters);
    const candidateLabels = new Set(candidate.expectations);
    const candidatePlatforms = new Set(candidate.platforms);

    if (
      testIdMatchesExpectationPattern(
        expectation.testIdPattern,
        candidate.testIdPattern
      )
    ) {
      foundMatch = true;
      if (
        isSubset(candidateParams, params) &&
        isSubset(candidatePlatforms, platforms)
      ) {
        if (isSubset(candidateLabels, labels)) {
          console.log('removing', expectation, 'already covered by', candidate);
          toBeRemoved.add(expectation);
        }
      }
      break;
    }
  }

  if (!foundMatch && isSubset(new Set(['PASS']), labels)) {
    console.log(
      'removing',
      expectation,
      'because the default expectation is to pass'
    );
    toBeRemoved.add(expectation);
  }
}

testExpectations = testExpectations.filter(item => {
  return !toBeRemoved.has(item);
});


Review comment (bug):
We need to mark that there is a match even if the other parts don't match.
```json
  {
    "testIdPattern": "[network.spec] network Network Events Page.Events.RequestServedFromCache",
    "platforms": ["darwin", "linux", "win32"],
    "parameters": ["chrome", "webDriverBiDi"],
    "expectations": ["PASS"]
  },
  ```
  Was being remove due to 
  ```json
    {
    "testIdPattern": "[network.spec] network Network Events *",
    "platforms": ["darwin", "linux", "win32"],
    "parameters": ["webDriverBiDi"],
    "expectations": ["FAIL"],
    "comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
  },
  ```

Code after:
  return isSubset;
}

const toBeRemoved = new Set();
for (let i = testExpectations.length - 1; i >= 0; i--) {
  const expectation = testExpectations[i];
  const params = new Set(expectation.parameters);
  const expectations = new Set(expectation.expectations);
  const platforms = new Set(expectation.platforms);

  let foundMatch = false;
  for (let j = i - 1; j >= 0; j--) {
    const candidate = testExpectations[j];
    const candidateParams = new Set(candidate.parameters);
    const candidateExpectations = new Set(candidate.expectations);
    const candidatePlatforms = new Set(candidate.platforms);

    if (
      testIdMatchesExpectationPattern(
        expectation.testIdPattern,
        candidate.testIdPattern
      ) &&
      isSubset(candidatePlatforms, platforms) &&
      (isSubset(params, candidateParams) || isSubset(candidateParams, params))
    ) {
      foundMatch = true;
      if (isSubset(candidateExpectations, expectations)) {
        console.log('removing', expectation, 'already covered by', candidate);
        toBeRemoved.add(expectation);
      }
      break;
    }
  }

  if (!foundMatch && isSubset(new Set(['PASS']), expectations)) {
    console.log(
      'removing',
      expectation,
      'because the default expectation is to pass'
    );
    toBeRemoved.add(expectation);
  }
}

testExpectations = testExpectations.filter(item => {
  return !toBeRemoved.has(item);
});

if (process.argv.includes('--lint')) {
  const missingComments = [];
  testExpectations.forEach(item => {