{"size": 1610, "ext": "js", "max_stars_repo_path": "testing/out/tests/test_utils.js", "max_stars_repo_name": "arthuredelstein/browser-privacy", "max_stars_repo_stars_event_min_datetime": "2020-07-03T19:15:39.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-03T19:15:39.000Z", "max_issues_repo_path": "testing/out/tests/test_utils.js", "max_issues_repo_name": "arthuredelstein/browser-privacy", "max_issues_repo_issues_event_min_datetime": "2018-09-06T22:07:19.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-05T04:11:00.000Z", "max_forks_repo_path": "testing/out/tests/test_utils.js", "max_forks_repo_name": "arthuredelstein/browser-privacy", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 32, "max_forks_count": null, "avg_line_length": 27.2881355932, "max_line_length": 77, "alphanum_fraction": 0.6229813665}
let runTests = async (tests, mode, params) => {
  let results = {};
  for (let test of Object.keys(tests)) {
    let result;
    console.log(`running ${test}...`);
    try {
      let input = params["sessionId"];
      console.log("input", input);
      result = await tests[test][mode](input);
    } catch (e) {
      result = "Error: " + e.message;
    }
    console.log(`  ... finished ${test} with result: ${result}.`);
    results[test] = {
      write: tests[test].write.toString(),
      read: tests[test].read.toString(),
      description: tests[test].description,
      result,
    };
  }
  return results;
};

let queryParams = (urlString) => {
  let searchParams = new URL(urlString).searchParams;
  return Object.fromEntries(searchParams.entries());
};

// Remove any lingering service workers.
const removeAllServiceWorkers = async () => {
  if (navigator.serviceWorker) {
    try {
      const registrations = await navigator.serviceWorker.getRegistrations();
      for (const registration of registrations) {
        registration.unregister()
      }
    } catch (e) {
      console.log(e);
    }
  }
};

export let sleepMs = (timeMs) => new Promise(
  (resolve, reject) => setTimeout(resolve, timeMs)
);

export let runAllTests = async (tests) => {
  let params = queryParams(document.URL);
  if (params["mode"] === "write") {
    await removeAllServiceWorkers();
  }
  let results = await runTests(tests, params["mode"], params);
  console.log("results", results);
  if (window.location !== parent.location) {
    console.log("results:",results);
    parent.postMessage(results, "*");
  }
};
