diff --git a/.scripts/wpt-harness.mjs b/.scripts/wpt-harness.mjs index 88bd246f..b0ae4261 100644 --- a/.scripts/wpt-harness.mjs +++ b/.scripts/wpt-harness.mjs @@ -7,6 +7,7 @@ import * as nodeWebAudioAPI from '../index.mjs'; // mocks import createXMLHttpRequest from './wpt-mock/XMLHttpRequest.js'; +import createFetch from './wpt-mock/fetch.js'; import { DOMException } from '../js/lib/errors.js'; program @@ -31,6 +32,7 @@ function indent(string, times) { // ------------------------------------------------------- // WPT Runner configuration options // ------------------------------------------------------- +const wptRootPath = path.join('wpt'); const testsPath = path.join('wpt','webaudio'); const rootURL = 'webaudio'; @@ -47,6 +49,7 @@ const setup = window => { // e.g. 'resources/audiobuffersource-multi-channels-expected.wav' window.XMLHttpRequest = createXMLHttpRequest(testsPath); + window.fetch = createFetch(wptRootPath); // window.requestAnimationFrame = func => setInterval(func, 16); // errors window.TypeError = TypeError; diff --git a/.scripts/wpt-mock/fetch.js b/.scripts/wpt-mock/fetch.js new file mode 100644 index 00000000..717636cf --- /dev/null +++ b/.scripts/wpt-mock/fetch.js @@ -0,0 +1,25 @@ +// required in node_modules/wpt_runner/testharness/idlharness.js, cf `fetch_spec` +const fs = require('node:fs'); +const path = require('node:path'); + +module.exports = function createFetch(basePath) { + return function fetch(pathname) { + pathname = path.join(basePath, pathname); + + return new Promise(resolve => { + if (!fs.existsSync(pathname)) { + resolve({ + ok: false, + msg: `file ${pathname} not found`, + }); + } else { + const buffer = fs.readFileSync(pathname); + + resolve({ + ok: true, + text: () => buffer.toString(), + }); + } + }); + } +};