<commit_before>import { createServer } from 'service-mocker/server';

const server = createServer();

export function serverRunner(register: () => void) {
  // register tests right away on legacy mode
  if (server.isLegacy) {
    return register();
  }

  // wrap `self.onerror`,
  // remove the annoying warning caused by mocha
  wrapErrorEvent();

  // avoid polluting client env
  require('mocha/mocha');
  require('source-map-support/sw-source-map-support').install();

  // wait for client request on modern mode
  // DON'T use `server.on()`, this listener should be permanently added
  self.addEventListener('message', (evt: ExtendableMessageEvent) => {
    const {
      data,
      ports,
    } = evt;

    if (data && data.request === 'MOCHA_TASKS') {
      ports[0].postMessage({
        suites: runTests(register),
      });
    }
  });
}

//////// THE FOLLOWING WILL ONLY BE EXECUTED IN MODERN MODE ////////

// make sure test results are update-to-dated
function runTests(register: () => void) {
  const mocha: any = new Mocha();

  mocha.ui('bdd');
  mocha.slow(200);
  mocha.timeout(10 * 1e3);
  mocha.reporter(swReporter);

  mocha.suite.emit('pre-require', self, null, mocha);

  // remove previous routers
  (server.router.constructor as any).routers.length = 0;

  register();

  mocha.run();

  return getAllSuites(mocha.suite.suites);
}

function swReporter(runner) {
  runner
    .on('pass', (test) => {
      // broadcast result to connected clients
      broadcast({
        mochaTest: true,
        composedTitle: composeTestTitle(test),
      });
    })
    .on('pending', (test) => {
      broadcast({
        skip: true,
        mochaTest: true,
        composedTitle: composeTestTitle(test),
      });
    })
    .on('fail', async (test, error) => {
      let fault: any;

      if (error) {
        // async error remap
        const stack = await error.stack;

        if (typeof error.toJSON === 'function') {
          // `AssertionError`
          fault = error.toJSON();
          fault.stack = stack;

          // err.actual is a function
          delete fault.actual;
        } else if (error instanceof Error) {
          fault = {
            stack,
            message: error.message,
          };
        }
      }

      const result = {
        error: fault,
        mochaTest: true,
        composedTitle: composeTestTitle(test),
      };

      broadcast(result);
    });
}

// get minimal suites
function getAllSuites(parent) {
  return parent.map(({ suites, tests, title }) => {
    const allSuites = getAllSuites(suites);
    const allTests = tests.map((t) => {
      const { body, title, pending } = t;
<commit_msg>chore(test): update server error reporter<commit_after>import { createServer } from 'service-mocker/server';

const server = createServer();

export function serverRunner(register: () => void) {
  // register tests right away on legacy mode
  if (server.isLegacy) {
    return register();
  }

  // wrap `self.onerror`,
  // remove the annoying warning caused by mocha
  wrapErrorEvent();

  // avoid polluting client env
  require('mocha/mocha');
  require('source-map-support/sw-source-map-support').install();

  // wait for client request on modern mode
  // DON'T use `server.on()`, this listener should be permanently added
  self.addEventListener('message', (evt: ExtendableMessageEvent) => {
    const {
      data,
      ports,
    } = evt;

    if (data && data.request === 'MOCHA_TASKS') {
      ports[0].postMessage({
        suites: runTests(register),
      });
    }
  });
}

//////// THE FOLLOWING WILL ONLY BE EXECUTED IN MODERN MODE ////////

// make sure test results are update-to-dated
function runTests(register: () => void) {
  const mocha: any = new Mocha();

  mocha.ui('bdd');
  mocha.slow(200);
  mocha.timeout(10 * 1e3);
  mocha.reporter(swReporter);

  mocha.suite.emit('pre-require', self, null, mocha);

  // remove previous routers
  (server.router.constructor as any).routers.length = 0;

  register();

  mocha.run();

  return getAllSuites(mocha.suite.suites);
}

function swReporter(runner) {
  runner
    .on('pass', (test) => {
      // broadcast result to connected clients
      broadcast({
        mochaTest: true,
        composedTitle: composeTestTitle(test),
      });
    })
    .on('pending', (test) => {
      broadcast({
        skip: true,
        mochaTest: true,
        composedTitle: composeTestTitle(test),
      });
    })
    .on('fail', async (test, error) => {
      let fault: any;

      if (error) {
        // async error remap
        const stack = await error.stack;

        if (typeof error.toJSON === 'function') {
          // `AssertionError`
          fault = JSON.stringify(error);
          fault.stack = stack;
        } else if (error instanceof Error) {
          fault = {
            stack,
            message: error.message,
          };
        }
      }

      const result = {
        error: fault,
        mochaTest: true,
        composedTitle: composeTestTitle(test),
      };

      broadcast(result);
    });
}

// get minimal suites
function getAllSuites(parent) {
  return parent.map(({ suites, tests, title }) => {
    const allSuites = getAllSuites(suites);
    const allTests = tests.map((t) => {
      const { body, title, pending } = t;

{'fraction_non_alphanumeric': 0.1408344345856145, 'fraction_numerical': 0.003398149896167642, 'mean_word_length': 2.36808645899555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2946029', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1611, 'n_words': 408}