11cb0ef41Sopenharmony_ciimport * as common from '../common/index.mjs';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciimport assert from 'assert';
41cb0ef41Sopenharmony_ciimport fs from 'fs';
51cb0ef41Sopenharmony_ciimport path from 'path';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (common.isWindows) {
81cb0ef41Sopenharmony_ci  common.skip('`make doc` does not run on Windows');
91cb0ef41Sopenharmony_ci}
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// This tests that `make doc` generates the documentation properly.
121cb0ef41Sopenharmony_ci// Note that for this test to pass, `make doc` must be run first.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst apiURL = new URL('../../out/doc/api/', import.meta.url);
151cb0ef41Sopenharmony_ciconst mdURL = new URL('../../doc/api/', import.meta.url);
161cb0ef41Sopenharmony_ciconst allMD = fs.readdirSync(mdURL);
171cb0ef41Sopenharmony_ciconst allDocs = fs.readdirSync(apiURL);
181cb0ef41Sopenharmony_ciassert.ok(allDocs.includes('index.html'));
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst actualDocs = allDocs.filter(
211cb0ef41Sopenharmony_ci  (name) => {
221cb0ef41Sopenharmony_ci    const extension = path.extname(name);
231cb0ef41Sopenharmony_ci    return extension === '.html' || extension === '.json';
241cb0ef41Sopenharmony_ci  },
251cb0ef41Sopenharmony_ci);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cifor (const name of actualDocs) {
281cb0ef41Sopenharmony_ci  if (name.startsWith('all.')) continue;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  assert.ok(
311cb0ef41Sopenharmony_ci    allMD.includes(name.replace(/\.\w+$/, '.md')),
321cb0ef41Sopenharmony_ci    `Unexpected output: out/doc/api/${name}, remove and rerun.`,
331cb0ef41Sopenharmony_ci  );
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst toc = fs.readFileSync(new URL('./index.html', apiURL), 'utf8');
371cb0ef41Sopenharmony_ciconst re = /href="([^/]+\.html)"/;
381cb0ef41Sopenharmony_ciconst globalRe = new RegExp(re, 'g');
391cb0ef41Sopenharmony_ciconst links = toc.match(globalRe);
401cb0ef41Sopenharmony_ciassert.notStrictEqual(links, null);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Filter out duplicate links, leave just filenames, add expected JSON files.
431cb0ef41Sopenharmony_ciconst linkedHtmls = [...new Set(links)].map((link) => link.match(re)[1])
441cb0ef41Sopenharmony_ci                      .concat(['index.html']);
451cb0ef41Sopenharmony_ciconst expectedJsons = linkedHtmls
461cb0ef41Sopenharmony_ci                       .map((name) => name.replace('.html', '.json'));
471cb0ef41Sopenharmony_ciconst expectedDocs = linkedHtmls.concat(expectedJsons);
481cb0ef41Sopenharmony_ciconst renamedDocs = ['policy.json', 'policy.html'];
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci// Test that all the relative links in the TOC match to the actual documents.
511cb0ef41Sopenharmony_cifor (const expectedDoc of expectedDocs) {
521cb0ef41Sopenharmony_ci  assert.ok(actualDocs.includes(expectedDoc), `${expectedDoc} does not exist`);
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// Test that all the actual documents match to the relative links in the TOC
561cb0ef41Sopenharmony_ci// and that they are not empty files.
571cb0ef41Sopenharmony_cifor (const actualDoc of actualDocs) {
581cb0ef41Sopenharmony_ci  // When renaming the documentation, the old url is lost
591cb0ef41Sopenharmony_ci  // Unless the old file is still available pointing to the correct location
601cb0ef41Sopenharmony_ci  // 301 redirects are not yet automated. So keeping the old URL is a
611cb0ef41Sopenharmony_ci  // reasonable workaround.
621cb0ef41Sopenharmony_ci  if (renamedDocs.includes(actualDoc)) continue;
631cb0ef41Sopenharmony_ci  assert.ok(
641cb0ef41Sopenharmony_ci    expectedDocs.includes(actualDoc), `${actualDoc} does not match TOC`);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  assert.notStrictEqual(
671cb0ef41Sopenharmony_ci    fs.statSync(new URL(`./${actualDoc}`, apiURL)).size,
681cb0ef41Sopenharmony_ci    0,
691cb0ef41Sopenharmony_ci    `${actualDoc} is empty`,
701cb0ef41Sopenharmony_ci  );
711cb0ef41Sopenharmony_ci}
72