{"size": 5620, "ext": "js", "max_stars_repo_path": "test/lib/worker.spec.js", "max_stars_repo_name": "gustavnikolaj/linter-js-standard-engine", "max_stars_repo_stars_event_min_datetime": "2016-01-09T12:51:05.000Z", "max_stars_repo_stars_event_max_datetime": "2018-07-15T14:14:45.000Z", "max_issues_repo_path": "test/lib/worker.spec.js", "max_issues_repo_name": "leomp12/linter-js-standard-engine", "max_issues_repo_issues_event_min_datetime": "2015-11-15T18:37:44.000Z", "max_issues_repo_issues_event_max_datetime": "2020-06-20T06:39:52.000Z", "max_forks_repo_path": "test/lib/worker.spec.js", "max_forks_repo_name": "leomp12/linter-js-standard-engine", "max_forks_repo_forks_event_min_datetime": "2016-02-12T17:20:56.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-03T11:13:47.000Z"}
{"max_stars_count": 19, "max_issues_count": 54, "max_forks_count": 9, "avg_line_length": 31.0497237569, "max_line_length": 112, "alphanum_fraction": 0.5660142349}
/* global describe, it */

const expect = require('unexpected')
const childProcess = require('child_process')
const path = require('path')

const workerPath = require.resolve('../../lib/worker')

describe('lib/worker', () => {
  it('should only load linter once', () => {
    const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker')])
    const messages = []
    const promise = new Promise(resolve => {
      child.on('message', m => {
        messages.push(m)
        if (messages.length === 2) {
          resolve()
        }
      })
    })

    child.send({ id: 1, source: '' })
    child.send({ id: 2, source: '' })

    return promise.then(() => {
      expect(messages[0].results[0], 'to have property', 'count', 1)
      expect(messages[1].results[0], 'to have property', 'count', 2)
    })
  })
  it('should report crashes in linter loading', () => {
    const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/crashing')])
    const promise = new Promise(resolve => {
      child.on('message', m => resolve(m))
    })

    child.send({ id: 1, source: '' })

    return promise.then((message) => {
      expect(message.error, 'to have property', 'message', 'crash in linter loading')
    })
  })
  it('should report missing linters as a linter message', () => {
    const child = childProcess.fork(workerPath, ['non-existent-for-sure-or-so-we-hope'])
    const promise = new Promise(resolve => {
      child.on('message', m => resolve(m))
    })

    child.send({ id: 1, source: '' })

    return promise.then((message) => {
      expect(message, 'to satisfy', {
        results: [
          {
            messages: [
              {
                message: 'Could not load linter "non-existent-for-sure-or-so-we-hope"',
                fatal: true
              }
            ]
          }
        ]
      })
    })
  })
  it('should resolve linter from working directory', () => {
    const cwd = path.resolve(__dirname, '..', 'fixtures', 'localLinter')
    const child = childProcess.fork(workerPath, ['my-linter'], { cwd })
    const promise = new Promise(resolve => {
      child.on('message', m => resolve(m))
    })

    child.send({ id: 1, source: '' })

    return promise.then((message) => {
      expect(message.results[0], 'to have property', 'hello', 'world')
    })
  })
  it('should report crashes when linting', () => {
    const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/crashOnLint')])
    const promise = new Promise(resolve => {
      child.on('message', m => resolve(m))
    })

    child.send({ id: 1, source: '' })

    return promise.then((message) => {
      expect(message.error, 'to have property', 'message', 'threw when linting')
    })
  })
  it('should report errors when linting', () => {
    const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/errOnLint')])
    const promise = new Promise(resolve => {
      child.on('message', m => resolve(m))
    })

    child.send({ id: 1, source: '' })

    return promise.then((message) => {
      expect(message.error, 'to have property', 'message', 'err when linting')
    })
  })
  it('should serialize errors', () => {
    const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/errOnLint')])
    const promise = new Promise(resolve => {
      child.on('message', m => resolve(m))
    })

    child.send({ id: 1, source: '' })

    return promise.then((message) => {
      expect(message.error, 'to have property', 'stack')
      expect(message.error.stack, 'to contain', `${require.resolve('../fixtures/stubForWorker/errOnLint')}:2`)
    })
  })
})

describe('handle both async and sync lintText', () => {
  describe('sync', () => {
    it('should get a lint result', () => {
      const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/lintText-sync')])
      const promise = new Promise(resolve => {
        child.on('message', m => resolve(m))
      })

      child.send({ id: 1, source: '' })

      return expect(promise, 'when fulfilled', 'to satisfy', {
        id: 1,
        results: []
      })
    })
    it('should get a lint error', () => {
      const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/errOnLint-sync')])
      const promise = new Promise(resolve => {
        child.on('message', m => resolve(m))
      })

      child.send({ id: 1, source: '' })

      return expect(promise, 'when fulfilled', 'to satisfy', {
        id: 1,
        error: {
          name: 'Error',
          message: 'err when linting'
        }
      })
    })
  })
  describe('async', () => {
    it('should get a lint result', () => {
      const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/lintText-async')])
      const promise = new Promise(resolve => {
        child.on('message', m => resolve(m))
      })

      child.send({ id: 1, source: '' })

      return expect(promise, 'when fulfilled', 'to satisfy', {
        id: 1,
        results: []
      })
    })
    it('should get a lint error', () => {
      const child = childProcess.fork(workerPath, [require.resolve('../fixtures/stubForWorker/errOnLint')])
      const promise = new Promise(resolve => {
        child.on('message', m => resolve(m))
      })

      child.send({ id: 1, source: '' })

      return expect(promise, 'when fulfilled', 'to satisfy', {
        id: 1,
        error: {
          name: 'Error',
          message: 'err when linting'
        }
      })
    })
  })
})
