|
| 1 | +/* |
| 2 | + This test depends on the languagetool server to be available. See |
| 3 | + https://languagetool.org/de. A `compose.yml` file is available in the |
| 4 | + repository root to start an instance. |
| 5 | + */ |
| 6 | + |
| 7 | +import Ajv from 'ajv/dist/jtd.js' |
| 8 | +import bcp47 from 'bcp47' |
| 9 | +import { context } from '../../context.js' |
| 10 | + |
| 11 | +const ajv = new Ajv() |
| 12 | + |
| 13 | +const inputSchema = /** @type {const} */ ({ |
| 14 | + additionalProperties: true, |
| 15 | + optionalProperties: { |
| 16 | + document: { |
| 17 | + additionalProperties: true, |
| 18 | + optionalProperties: { |
| 19 | + lang: { type: 'string' }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | +}) |
| 24 | + |
| 25 | +const validateInput = ajv.compile(inputSchema) |
| 26 | + |
| 27 | +/** |
| 28 | + * If the document language is given it MUST be tested that a grammar check for |
| 29 | + * the given language does not find any mistakes. The test SHALL be skipped if |
| 30 | + * the document language is not set. It SHALL fail if the given language is not |
| 31 | + * supported. |
| 32 | + * |
| 33 | + * @param {unknown} doc |
| 34 | + * @returns |
| 35 | + */ |
| 36 | +export async function informativeTest_6_3_16(doc) { |
| 37 | + const ctx = { |
| 38 | + infos: /** @type {Array<{ message: string; instancePath: string }>} */ ([]), |
| 39 | + } |
| 40 | + |
| 41 | + if (!validateInput(doc)) { |
| 42 | + return ctx |
| 43 | + } |
| 44 | + |
| 45 | + const lang = |
| 46 | + (doc.document?.lang && |
| 47 | + bcp47.parse(doc.document.lang)?.langtag.language.language) ?? |
| 48 | + 'en' |
| 49 | + |
| 50 | + /* |
| 51 | + Check if the language is supported by the languagetool server. |
| 52 | + */ |
| 53 | + { |
| 54 | + /** |
| 55 | + * @typedef {object} Language |
| 56 | + * @property {string} code |
| 57 | + */ |
| 58 | + |
| 59 | + /** @typedef {Language[]} Response */ |
| 60 | + |
| 61 | + const res = await fetch(new URL('/v2/languages', context.languageToolUrl), { |
| 62 | + headers: { |
| 63 | + accept: 'application/json', |
| 64 | + }, |
| 65 | + }) |
| 66 | + if (!res.ok) throw new Error('request to languagetool failed') |
| 67 | + |
| 68 | + const json = /** @type {Response} */ (await res.json()) |
| 69 | + |
| 70 | + if (!json.some((l) => l.code === lang)) { |
| 71 | + ctx.infos.push({ |
| 72 | + instancePath: '/document/lang', |
| 73 | + message: 'language is not supported', |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for (const path of [ |
| 79 | + '/document/acknowledgments[]/summary', |
| 80 | + '/document/aggregate_severity/text', |
| 81 | + '/document/distribution/text', |
| 82 | + '/document/notes[]/audience', |
| 83 | + '/document/notes[]/text', |
| 84 | + '/document/notes[]/title', |
| 85 | + '/document/publisher/issuing_authority', |
| 86 | + '/document/references[]/summary', |
| 87 | + '/document/title', |
| 88 | + '/document/tracking/revision_history[]/summary', |
| 89 | + '/product_tree/product_groups[]/summary', |
| 90 | + '/vulnerabilities[]/acknowledgments[]/summary', |
| 91 | + '/vulnerabilities[]/involvements[]/summary', |
| 92 | + '/vulnerabilities[]/notes[]/audience', |
| 93 | + '/vulnerabilities[]/notes[]/text', |
| 94 | + '/vulnerabilities[]/notes[]/title', |
| 95 | + '/vulnerabilities[]/references[]/summary', |
| 96 | + '/vulnerabilities[]/remediations[]/details', |
| 97 | + '/vulnerabilities[]/remediations[]/entitlements[]', |
| 98 | + '/vulnerabilities[]/remediations[]/restart_required/details', |
| 99 | + '/vulnerabilities[]/threats[]/details', |
| 100 | + '/vulnerabilities[]/title', |
| 101 | + ]) { |
| 102 | + await checkPath( |
| 103 | + [], |
| 104 | + path.split('/').slice(1), |
| 105 | + doc, |
| 106 | + async (instancePath, text) => { |
| 107 | + if (typeof text !== 'string') return |
| 108 | + const result = await checkString(text, lang) |
| 109 | + if (result.length) { |
| 110 | + ctx.infos.push({ |
| 111 | + instancePath, |
| 112 | + message: result.map((r) => r.message).join(' '), |
| 113 | + }) |
| 114 | + } |
| 115 | + } |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + return ctx |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Checks the value behind `path` using the given `onCheck` function. This is a |
| 124 | + * recursive helper function to loop through the list of paths in the spec. |
| 125 | + * |
| 126 | + * @param {string[]} reminder |
| 127 | + * @param {string[]} path |
| 128 | + * @param {unknown} value |
| 129 | + * @param {(instancePath: string, value: string) => Promise<void>} onCheck |
| 130 | + */ |
| 131 | +async function checkPath(reminder, path, value, onCheck) { |
| 132 | + if (value == null) return |
| 133 | + const currentSegment = path.at(0) |
| 134 | + |
| 135 | + if (!currentSegment) { |
| 136 | + // We've reached the end. Now the `onCheck` function can be called to check |
| 137 | + // the actual value. |
| 138 | + if (typeof value === 'string') { |
| 139 | + await onCheck('/' + reminder.join('/'), value) |
| 140 | + } |
| 141 | + } else if (currentSegment.endsWith('[]')) { |
| 142 | + // The value is supposed to be an array for which every element needs to be |
| 143 | + // checked ... |
| 144 | + const arrayName = currentSegment.split('[')[0] |
| 145 | + const array = Reflect.get(value, arrayName) |
| 146 | + |
| 147 | + if (Array.isArray(array)) { |
| 148 | + // ... But only if it's really an array. |
| 149 | + for (const [elementIndex, element] of array.entries() ?? []) { |
| 150 | + await checkPath( |
| 151 | + [...reminder, arrayName, String(elementIndex)], |
| 152 | + [...path.slice(1)], |
| 153 | + element, |
| 154 | + onCheck |
| 155 | + ) |
| 156 | + } |
| 157 | + } |
| 158 | + } else { |
| 159 | + // Otherwise it's something object-ish which we traverse recursively. |
| 160 | + await checkPath( |
| 161 | + [...reminder, currentSegment], |
| 162 | + path.slice(1), |
| 163 | + Reflect.get(value, currentSegment), |
| 164 | + onCheck |
| 165 | + ) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Check the given string using the languagetool server. |
| 171 | + * |
| 172 | + * @param {string} str |
| 173 | + * @param {string} lng |
| 174 | + * @returns |
| 175 | + */ |
| 176 | +async function checkString(str, lng) { |
| 177 | + /** |
| 178 | + * @typedef {object} Match |
| 179 | + * @property {string} message |
| 180 | + */ |
| 181 | + |
| 182 | + /** |
| 183 | + * @typedef {object} Response |
| 184 | + * @property {Match[]} matches |
| 185 | + */ |
| 186 | + |
| 187 | + const res = await fetch(new URL('/v2/check', context.languageToolUrl), { |
| 188 | + method: 'POST', |
| 189 | + body: new URLSearchParams([ |
| 190 | + ['language', lng], |
| 191 | + ['text', str], |
| 192 | + ]), |
| 193 | + }) |
| 194 | + if (!res.ok) throw new Error('request to languagetool failed') |
| 195 | + |
| 196 | + const json = /** @type {Response} */ (await res.json()) |
| 197 | + return json.matches |
| 198 | +} |
0 commit comments