File size: 2,845 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* eslint-env jest */
import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'

export async function validateAMP(/** @type {string} */ html) {
  const validatorPath = getBundledAmpValidatorFilepath()
  const validator = await getAmpValidatorInstance(validatorPath)
  const result = validator.validateString(html)
  const errors = result.errors.filter((error) => {
    if (error.severity === 'ERROR') {
      // Unclear yet if these actually prevent the page from being indexed by the AMP cache.
      // These are coming from React so all we can do is ignore them for now.

      // <link rel="expect" blocking="render" />
      // https://github.com/ampproject/amphtml/issues/40279
      if (
        error.code === 'DISALLOWED_ATTR' &&
        error.params[0] === 'blocking' &&
        error.params[1] === 'link'
      ) {
        return false
      }
      // <template> without type
      // https://github.com/ampproject/amphtml/issues/40280
      if (
        error.code === 'MANDATORY_ATTR_MISSING' &&
        error.params[0] === 'type' &&
        error.params[1] === 'template'
      ) {
        return false
      }
      // <template> without type
      // https://github.com/ampproject/amphtml/issues/40280
      if (
        error.code === 'MISSING_REQUIRED_EXTENSION' &&
        error.params[0] === 'template' &&
        error.params[1] === 'amp-mustache'
      ) {
        return false
      }
      return true
    }
    return false
  })
  const warnings = result.errors.filter((error) => {
    return error.severity !== 'ERROR'
  })

  expect({ errors, warnings }).toEqual({
    errors: [],
    warnings: [],
  })
}

/** @typedef {import('next/dist/compiled/amphtml-validator').Validator} Validator */

/** @type {Map<string | undefined, Promise<Validator>>} */
const instancePromises = new Map()

/**
 * This is a workaround for issues with concurrent `AmpHtmlValidator.getInstance()` calls,
 * duplicated from 'packages/next/src/export/helpers/get-amp-html-validator.ts'.
 * see original code for explanation.
 *
 * @returns {Promise<Validator>}
 * */
function getAmpValidatorInstance(
  /** @type {string | undefined} */ validatorPath
) {
  let promise = instancePromises.get(validatorPath)
  if (!promise) {
    // NOTE: if `validatorPath` is undefined, `AmpHtmlValidator` will load the code from its default URL
    promise = AmpHtmlValidator.getInstance(validatorPath)
    instancePromises.set(validatorPath, promise)
  }
  return promise
}

/**
 * Use the same validator that we use for builds.
 * This avoids trying to load one from the network, which can cause random test flakiness.
 * (duplicated from 'packages/next/src/export/helpers/get-amp-html-validator.ts')
 */
function getBundledAmpValidatorFilepath() {
  return require.resolve(
    'next/dist/compiled/amphtml-validator/validator_wasm.js'
  )
}