11cb0ef41Sopenharmony_ciimport '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport * as fixtures from '../common/fixtures.mjs';
31cb0ef41Sopenharmony_ciimport tmpdir from '../common/tmpdir.js';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciimport assert from 'assert';
61cb0ef41Sopenharmony_ciimport { execFileSync } from 'child_process';
71cb0ef41Sopenharmony_ciimport fs from 'fs';
81cb0ef41Sopenharmony_ciimport path from 'path';
91cb0ef41Sopenharmony_ciimport { fileURLToPath } from 'url';
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst script = fileURLToPath(
121cb0ef41Sopenharmony_ci  new URL('../../tools/doc/apilinks.mjs', import.meta.url));
131cb0ef41Sopenharmony_ciconst apilinks = fixtures.path('apilinks');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_citmpdir.refresh();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifs.readdirSync(apilinks).forEach((fixture) => {
181cb0ef41Sopenharmony_ci  if (!fixture.endsWith('.js')) return;
191cb0ef41Sopenharmony_ci  const input = path.join(apilinks, fixture);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  const expectedContent = fs.readFileSync(`${input}on`, 'utf8');
221cb0ef41Sopenharmony_ci  const outputPath = path.join(tmpdir.path, `${fixture}on`);
231cb0ef41Sopenharmony_ci  execFileSync(
241cb0ef41Sopenharmony_ci    process.execPath,
251cb0ef41Sopenharmony_ci    [script, outputPath, input],
261cb0ef41Sopenharmony_ci    { encoding: 'utf-8' },
271cb0ef41Sopenharmony_ci  );
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  const expectedLinks = JSON.parse(expectedContent);
301cb0ef41Sopenharmony_ci  const actualLinks = JSON.parse(fs.readFileSync(outputPath));
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  for (const [k, v] of Object.entries(expectedLinks)) {
331cb0ef41Sopenharmony_ci    assert.ok(k in actualLinks, `link not found: ${k}`);
341cb0ef41Sopenharmony_ci    assert.ok(actualLinks[k].endsWith('/' + v),
351cb0ef41Sopenharmony_ci              `link ${actualLinks[k]} expected to end with ${v}`);
361cb0ef41Sopenharmony_ci    delete actualLinks[k];
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  assert.strictEqual(
401cb0ef41Sopenharmony_ci    Object.keys(actualLinks).length, 0,
411cb0ef41Sopenharmony_ci    `unexpected links returned ${JSON.stringify(actualLinks)}`,
421cb0ef41Sopenharmony_ci  );
431cb0ef41Sopenharmony_ci});
44