<commit_before>export { tester };

import * as ValueMax from "../util/ValueMax.js";
import { AsyncWorker_Proxy_tester } from "./AsyncWorker_Proxy_tester.js";

/**
 * @return {Promise}
 *   Return a promise which will be resolved as specified value after specified
 * milliseconds.
 */
function delayedValue( milliseconds, value ) {
  return new Promise( ( resolve /*, reject*/ ) => {
    setTimeout( () => resolve( value ), milliseconds );
  } );
}

/**
 * Test a WorkerProxy's initialization.
 *
 * @param {AsyncWorker_Proxy_tester} workerProxy
 */
async function test_WorkerProxy_init( { workerProxy }, workerId ) {
  let initWorkerPromise = workerProxy.initWorker_async( workerId );
  let initWorkerOk = await initWorkerPromise;
  if ( initWorkerOk == false )
    throw Error( `AsyncWorker_tester.testWorkerProxy(): `
      `workerId=${workerId}, initWorker failed.`
    );

  //!!! (2022/09/14 Remarked)
  // // Test: unknown command.
  // let not_existed_command_result;
  // try {
  //   not_existed_command_result
  //     = await workerProxy.createPromise_by_postCommandArgs( [
  //     "not_existed_command"
  //   ] );
  // } catch ( e ) {
  //   //debugger;
  //   console.log( e );
  // }
}

/**
 * Test a WorkerProxy's processing queue size whether is zero.
 *
 * @param {AsyncWorker_Proxy_tester} workerProxy
 */
async function test_WorkerProxy_processingQueueSize_zero( { workerProxy } ) {
  let processingQueueSize = workerProxy.the_processingId_Resulter_Map.size;
  if ( processingQueueSize != 0 )
    throw Error( `AsyncWorker_tester.test_WorkerProxy_processingQueueSize(): `
      + `workerId=${workerProxy.workerId}, `
      + `processingQueueSize ( ${processingQueueSize} ) should be 0.`
    );
}

/**
 * Test a WorkerProxy for generating number sequence.
 *
 * @param {AsyncWorker_Proxy_tester} workerProxy
 */
async function test_WorkerProxy_numberSequence( {
    sequenceName,
    intervalMilliseconds,
    valueBegin,
    valueCountTotal,
    valueCountPerBoost,
    workerProxy,
} ) {
  const workerId = workerProxy.workerId;

  let numberResulter = workerProxy.number_sequence_asyncGenerator(
    intervalMilliseconds, valueBegin, valueCountTotal, valueCountPerBoost );

  let valueIndex = 0;
  let valueTest = valueBegin;
  let numberResulterNext, done, value;
  do {
    numberResulterNext = await numberResulter.next();
    ( { done, value } = numberResulterNext );

    if ( valueTest != value )
      throw Error( `AsyncWorker_tester.test_WorkerProxy_numberSequence(): `
        + `sequenceName="${sequenceName}", workerId=${workerId}, `
        + `valueIndex=${valueIndex}, `
        + `value ( ${value} ) should be the same as valueTest ( ${valueTest} ).`
      );

    ++valueIndex;
    ++valueTest;

  } while ( !done );

  let valueTestFinal = valueBegin + valueCountTotal - 1;
  if ( valueTestFinal != value )
      throw Error( `AsyncWorker_tester.test_WorkerProxy_numberSequence(): `
        + `sequenceName="${sequenceName}", workerId=${workerId}, `
        + `value ( ${value} ) should be the same as valueTestFinal ( ${valueTestFinal} ).`
    );
}

/**
 *
 * @param {ValueMax.Percentage.Aggregate} progressParent
 *   Some new progressToAdvance will be created and added to progressParent. The
 * created progressToAdvance will be increased when every time advanced. The
 * progressParent.root_get() will be returned when every time yield.
 *
 */<commit_msg>NumberSequenceInfo<commit_after>export { tester };

import * as ValueMax from "../util/ValueMax.js";
import { AsyncWorker_Proxy_tester } from "./AsyncWorker_Proxy_tester.js";

/** */
class NumberSequenceInfo {

  constructor(
    sequenceName: "allBoost",
    intervalMilliseconds: 10,
    valueBegin: 0,
    valueCountTotal: valueCountTotal,
    valueCountPerBoost: valueCountTotal,
    workerProxy: undefined,
  ) {
    
  }
}

/**
 * @return {Promise}
 *   Return a promise which will be resolved as specified value after specified
 * milliseconds.
 */
function delayedValue( milliseconds, value ) {
  return new Promise( ( resolve /*, reject*/ ) => {
    setTimeout( () => resolve( value ), milliseconds );
  } );
}

/**
 * Test a WorkerProxy's initialization.
 *
 * @param {AsyncWorker_Proxy_tester} workerProxy
 */
async function test_WorkerProxy_init( { workerProxy }, workerId ) {
  let initWorkerPromise = workerProxy.initWorker_async( workerId );
  let initWorkerOk = await initWorkerPromise;
  if ( initWorkerOk == false )
    throw Error( `AsyncWorker_tester.testWorkerProxy(): `
      `workerId=${workerId}, initWorker failed.`
    );

  //!!! (2022/09/14 Remarked)
  // // Test: unknown command.
  // let not_existed_command_result;
  // try {
  //   not_existed_command_result
  //     = await workerProxy.createPromise_by_postCommandArgs( [
  //     "not_existed_command"
  //   ] );
  // } catch ( e ) {
  //   //debugger;
  //   console.log( e );
  // }
}

/**
 * Test a WorkerProxy's processing queue size whether is zero.
 *
 * @param {AsyncWorker_Proxy_tester} workerProxy
 */
async function test_WorkerProxy_processingQueueSize_zero( { workerProxy } ) {
  let processingQueueSize = workerProxy.the_processingId_Resulter_Map.size;
  if ( processingQueueSize != 0 )
    throw Error( `AsyncWorker_tester.test_WorkerProxy_processingQueueSize(): `
      + `workerId=${workerProxy.workerId}, `
      + `processingQueueSize ( ${processingQueueSize} ) should be 0.`
    );
}

/**
 * Test a WorkerProxy for generating number sequence.
 *
 * @param {AsyncWorker_Proxy_tester} workerProxy
 * 
 * @param {number} nextMilliseconds
 *   - Negative: No await before call numberResulter.next().
 *   - Other value: await so many milliseconds before call numberResulter.next().
 */
async function test_WorkerProxy_numberSequence(
  {
    sequenceName,
    intervalMilliseconds,
    valueBegin,
    valueCountTotal,
    valueCountPerBoost,
    workerProxy,
  },
  nextMilliseconds,
) {
  const workerId = workerProxy.workerId;

  let numberResulter = workerProxy.number_sequence_asyncGenerator(
    intervalMilliseconds, valueBegin, valueCountTotal, valueCountPerBoost );

  let valueIndex = 0;
  let valueTest = valueBegin;
  let numberResulterNext, done, value;
  do {
    if ( nextMilliseconds >= 0 )
      delayedValue( nextMilliseconds );

    numberResulterNext = await numberResulter.next();
    ( { done, value } = numberResulterNext );

    if ( valueTest != value )
      throw Error( `AsyncWorker_tester.test_WorkerProxy_numberSequence(): `
        + `sequenceName="${sequenceName}", workerId=${workerId}, `
        + `valueIndex=${valueIndex}, `
        + `value ( ${value} ) should be the same as valueTest ( ${valueTest} ).`
      );

    ++valueIndex;
    ++valueTest;

  } while ( !done );

  let valueTestFinal = valueBegin + valueCountTotal - 1;
  if ( valueTestFinal != value )
      throw Error( `AsyncWorker_tester.test_WorkerProxy_numberSequence(): `
        + `sequenceName="${sequenceName}", workerId=${workerId}, `
        + `value ( ${value} ) should be the same as valueTestFinal ( ${valueTestFinal} ).`
    );
}

async function test_WorkerProxy_numberSequence(

/**
 *
 * @param {ValueMax.Percentage.Aggregate} progressParent
 *   Some new progressToAdvance will be created and added to progressParent. The
 * created progressToAdvance will be increased when every time advanced. The
 * progressParent.root_get() will be returned when every time yield.
 *
 */
{'fraction_non_alphanumeric': 0.1176948051948052, 'fraction_numerical': 0.003787878787878788, 'mean_word_length': 3.673198482932996, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '341016', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2172, 'n_words': 518}