<commit_before>/* eslint-env jest */
'use strict'

const ResolveEntity = require('./resolve-entity')
const ReducerEntity = require('../../reducer-entity')

const FixtureStore = require('../../../test/utils/fixture-store')
const helpers = require('../../helpers')
const utils = require('../../utils')

let dataPoint
let resolveTransform

beforeAll(() => {
  dataPoint = FixtureStore.create()
  resolveTransform = helpers.createResolveTransform(dataPoint)
})

afterEach(() => {
  dataPoint.middleware.clear()
})

describe('ResolveEntity.resolveErrorReducers', () => {
  test('It should reject if no transform set', () => {
    const err = new Error('Test')
    const accumulator = helpers.createAccumulator(
      {},
      {
        context: {
          error: helpers.createTransform([])
        }
      }
    )
    return ResolveEntity.resolveErrorReducers(
      err,
      accumulator,
      resolveTransform
    )
      .catch(reason => reason)
      .then(acc => {
        expect(acc).toBeInstanceOf(Error)
        expect(acc).toHaveProperty('message', 'Test')
      })
  })

  test('It should handle if transform set', () => {
    const err = new Error('Test')
    const accumulator = helpers.createAccumulator(
      {},
      {
        context: {
          error: helpers.createTransform((acc, next) => next(null, 'pass'))
        }
      }
    )
    return ResolveEntity.resolveErrorReducers(
      err,
      accumulator,
      resolveTransform
    ).then(acc => {
      expect(acc.value).toEqual('pass')
    })
  })
})

describe('ResolveEntity.createCurrentAccumulator', () => {
  let acc
  beforeAll(() => {
    const reducerEntity = ReducerEntity.create('hash:base')
    const accumulator = helpers.createAccumulator({
      foo: 'bar'
    })
    acc = ResolveEntity.createCurrentAccumulator(
      dataPoint,
      accumulator,
      reducerEntity
    )
  })
  test('It should set reducer property', () => {
    expect(acc).toHaveProperty('reducer.spec.id', 'hash:base')
  })
  test('It should context as the current Entity', () => {
    expect(acc).toHaveProperty('context.id', 'hash:base')
  })
  test('It should initialValue acc.value', () => {
    expect(acc).toHaveProperty('initialValue', {
      foo: 'bar'
    })
  })
  test('It should initialValue acc.value', () => {
    expect(acc).toHaveProperty('params', {
      base: true
    })
  })
})

describe('ResolveEntity.resolveMiddleware', () => {
  test('It should execute a middleware', () => {
    dataPoint.middleware.use('request:before', (acc, next) => {
      acc.value = 'bar'
      next(null)
    })

    const racc = helpers.createAccumulator('foo')
    return ResolveEntity.resolveMiddleware(
      dataPoint,
      'request:before',
      racc
    ).then(acc => {
      expect(acc.value).toEqual('bar')
    })
  })

  test('It should execute a middleware', () => {
    dataPoint.middleware.use('request:before', (acc, next) => {
      acc.resolve('bar')
      next(null)
    })

    const racc = helpers.createAccumulator('foo')
    return ResolveEntity.resolveMiddleware(dataPoint, 'request:before', racc)
      .catch(reason => reason)
      .then(reason => {
        expect(reason).toBeInstanceOf(Error)
        expect(reason).toHaveProperty('name', 'bypass')
        expect(reason).toHaveProperty('bypass', true)
        expect(reason).toHaveProperty('bypassValue.value', 'bar')
      })
  })
})

const Resolve = require('../../resolve/reducer')

describe('ResolveEntity.resolveEntity', () => {
  const defaultResolver = (acc, resolveTransform) => Promise.resolve(acc)

  const resolveEntity = (
    entityId,
    input,
    options,
    resolver = defaultResolver
  ) => {
    const racc = helpers.createAccumulator.call(null, input, options)
    const reducer = ReducerEntity.create(entityId)
    return ResolveEntity.resolveEntity(
      dataPoint,
      resolveTransform,
      racc,
      reducer,
      resolver
    )
  }<commit_msg>fix(entity-types/*): remove es6 syntax<commit_after>/* eslint-env jest */
'use strict'

const ResolveEntity = require('./resolve-entity')
const ReducerEntity = require('../../reducer-entity')

const FixtureStore = require('../../../test/utils/fixture-store')
const helpers = require('../../helpers')
const utils = require('../../utils')

let dataPoint
let resolveTransform

beforeAll(() => {
  dataPoint = FixtureStore.create()
  resolveTransform = helpers.createResolveTransform(dataPoint)
})

afterEach(() => {
  dataPoint.middleware.clear()
})

describe('ResolveEntity.resolveErrorReducers', () => {
  test('It should reject if no transform set', () => {
    const err = new Error('Test')
    const accumulator = helpers.createAccumulator(
      {},
      {
        context: {
          error: helpers.createTransform([])
        }
      }
    )
    return ResolveEntity.resolveErrorReducers(
      err,
      accumulator,
      resolveTransform
    )
      .catch(reason => reason)
      .then(acc => {
        expect(acc).toBeInstanceOf(Error)
        expect(acc).toHaveProperty('message', 'Test')
      })
  })

  test('It should handle if transform set', () => {
    const err = new Error('Test')
    const accumulator = helpers.createAccumulator(
      {},
      {
        context: {
          error: helpers.createTransform((acc, next) => next(null, 'pass'))
        }
      }
    )
    return ResolveEntity.resolveErrorReducers(
      err,
      accumulator,
      resolveTransform
    ).then(acc => {
      expect(acc.value).toEqual('pass')
    })
  })
})

describe('ResolveEntity.createCurrentAccumulator', () => {
  let acc
  beforeAll(() => {
    const reducerEntity = ReducerEntity.create('hash:base')
    const accumulator = helpers.createAccumulator({
      foo: 'bar'
    })
    acc = ResolveEntity.createCurrentAccumulator(
      dataPoint,
      accumulator,
      reducerEntity
    )
  })
  test('It should set reducer property', () => {
    expect(acc).toHaveProperty('reducer.spec.id', 'hash:base')
  })
  test('It should context as the current Entity', () => {
    expect(acc).toHaveProperty('context.id', 'hash:base')
  })
  test('It should initialValue acc.value', () => {
    expect(acc).toHaveProperty('initialValue', {
      foo: 'bar'
    })
  })
  test('It should initialValue acc.value', () => {
    expect(acc).toHaveProperty('params', {
      base: true
    })
  })
})

describe('ResolveEntity.resolveMiddleware', () => {
  test('It should execute a middleware', () => {
    dataPoint.middleware.use('request:before', (acc, next) => {
      acc.value = 'bar'
      next(null)
    })

    const racc = helpers.createAccumulator('foo')
    return ResolveEntity.resolveMiddleware(
      dataPoint,
      'request:before',
      racc
    ).then(acc => {
      expect(acc.value).toEqual('bar')
    })
  })

  test('It should execute a middleware', () => {
    dataPoint.middleware.use('request:before', (acc, next) => {
      acc.resolve('bar')
      next(null)
    })

    const racc = helpers.createAccumulator('foo')
    return ResolveEntity.resolveMiddleware(dataPoint, 'request:before', racc)
      .catch(reason => reason)
      .then(reason => {
        expect(reason).toBeInstanceOf(Error)
        expect(reason).toHaveProperty('name', 'bypass')
        expect(reason).toHaveProperty('bypass', true)
        expect(reason).toHaveProperty('bypassValue.value', 'bar')
      })
  })
})

const Resolve = require('../../resolve/reducer')

describe('ResolveEntity.resolveEntity', () => {
  const defaultResolver = (acc, resolveTransform) => Promise.resolve(acc)

  const resolveEntity = (entityId, input, options, resolver) => {
    const racc = helpers.createAccumulator.call(null, input, options)
    const reducer = ReducerEntity.create(entityId)
    return ResolveEntity.resolveEntity(
      dataPoint,
      resolveTransform,
      racc,
      reducer,
      resolver || defaultResolver
    )
  }
{'fraction_non_alphanumeric': 0.1536014252990583, 'fraction_numerical': 0.00012725884448969204, 'mean_word_length': 2.9732052578361983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, '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': '3951263', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2373, 'n_words': 429}