<commit_before>const fs = require('fs')
const bulbo = require('../src/')

const {expect} = require('chai')
const rimraf = require('rimraf')
const request = require('superagent')
const vinylServe = require('vinyl-serve')
const through2 = require('through2')
const browserify = require('browserify')

const SERVER_LAUNCH_WAIT = 800
const BUILD_WAIT = 400
const WATCH_BUILD_WAIT = 1000

describe('bulbo', () => {
  beforeEach(() => {
    bulbo.clear()
    bulbo.setLogger(require('../src/util/logger')('bulbo'))
  })

  describe('asset', () => {
    it('registers the asset', () => {
      bulbo.asset('test/fixture/**/*.js')
      expect(bulbo.isEmpty()).to.be.false
    })

    it('returns the asset\'s modifier', () => {
      expect(bulbo.asset('test/fixture/**/*.js')).to.be.an('object')
      expect(bulbo.asset('test/fixture/**/*.js').watch).to.be.a('function')
      expect(bulbo.asset('test/fixture/**/*.js').watchOptions).to.be.a('function')
      expect(bulbo.asset('test/fixture/**/*.js').base).to.be.a('function')
    })

    describe('.pipe', () => {
      it('sets the transform to the asset', done => {
        bulbo
        .asset('test/fixture/js/{foo,bar}.js')
        .base('test/fixture')
        .pipe(through2.obj(function (file, enc, cb) {
          file.contents = browserify(file.path).bundle()

          cb(null, file)
        }))

        bulbo.build().then(() => {
          const contents = fs.readFileSync('build/js/foo.js').toString()

          expect(contents).to.contain('This is foo.js')
          expect(contents).to.contain('This is bar.js')

          rimraf('build', done)
        }).catch(done)
      })
    })
  })

  describe('build', () => {
    it('builds the assets and put them in build/ dir', done => {
      bulbo.asset('test/fixture/**/*.js')

      bulbo.build().then(() => {
        expect(fs.readFileSync('build/js/foo.js').toString()).to.have.length.above(1)

        rimraf('build', done)
      })
    })

    it('does not stop at highWaterMark(=16) files', done => {
      bulbo.asset('test/fixture/**/*.js')

      bulbo.build().then(() => {
        expect(fs.readFileSync('build/js/0.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/1.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/2.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/3.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/4.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/5.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/6.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/7.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/8.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/9.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/10.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/11.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/12.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/13.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/14.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/15.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/16.js').toString()).to.have.length.above(1)

        rimraf('build', done)
      })
    })

    it('does not throw when assets are empty', done => {
      bulbo.build()

      setTimeout(done, BUILD_WAIT)
    })
  })

  describe('watchAndBuild', () => {
    it('builds the assets', done => {
      bulbo.asset('test/fixture/**/*.js')

      bulbo.watchAndBuild()

      setTimeout(() => {
        expect(fs.readFileSync('build/js/0.js').toString()).to.have.length.above(1)

        rimraf('build', done)
      }, WATCH_BUILD_WAIT)
    })
  })

  describe('serve', () => {
    it('serves the assets at port 7100', done => {
      bulbo.asset('test/fixture/**/*.js')

      bulbo.serve().then(() => {
        setTimeout(() => {
          request.get('0.0.0.0:7100/js/foo.js').buffer().end((err, res) => {
            if (err) { done(err) }

            expect(res.text).to.contain('This is foo.js')

            vinylServe.stop(7100)

            done()
          })
        }, SERVER_LAUNCH_WAIT)
      })
    })
<commit_msg>test(watch-build): add test of watchAndBuild<commit_after>const fs = require('fs')
const bulbo = require('../src/')

const {expect} = require('chai')
const rimraf = require('rimraf')
const request = require('superagent')
const vinylServe = require('vinyl-serve')
const through2 = require('through2')
const browserify = require('browserify')

const SERVER_LAUNCH_WAIT = 800
const BUILD_WAIT = 400
const WATCH_BUILD_WAIT = 100
const WATCH_BUILD_WAIT_CHANGED = 300

describe('bulbo', () => {
  beforeEach(() => {
    bulbo.clear()
    bulbo.setLogger(require('../src/util/logger')('bulbo'))
  })

  describe('asset', () => {
    it('registers the asset', () => {
      bulbo.asset('test/fixture/**/*.js')
      expect(bulbo.isEmpty()).to.be.false
    })

    it('returns the asset\'s modifier', () => {
      expect(bulbo.asset('test/fixture/**/*.js')).to.be.an('object')
      expect(bulbo.asset('test/fixture/**/*.js').watch).to.be.a('function')
      expect(bulbo.asset('test/fixture/**/*.js').watchOptions).to.be.a('function')
      expect(bulbo.asset('test/fixture/**/*.js').base).to.be.a('function')
    })

    describe('.pipe', () => {
      it('sets the transform to the asset', done => {
        bulbo
        .asset('test/fixture/js/{foo,bar}.js')
        .base('test/fixture')
        .pipe(through2.obj(function (file, enc, cb) {
          file.contents = browserify(file.path).bundle()

          cb(null, file)
        }))

        bulbo.build().then(() => {
          const contents = fs.readFileSync('build/js/foo.js').toString()

          expect(contents).to.contain('This is foo.js')
          expect(contents).to.contain('This is bar.js')

          rimraf('build', done)
        }).catch(done)
      })
    })
  })

  describe('build', () => {
    it('builds the assets and put them in build/ dir', done => {
      bulbo.asset('test/fixture/**/*.js')

      bulbo.build().then(() => {
        expect(fs.readFileSync('build/js/foo.js').toString()).to.have.length.above(1)

        rimraf('build', done)
      })
    })

    it('does not stop at highWaterMark(=16) files', done => {
      bulbo.asset('test/fixture/**/*.js')

      bulbo.build().then(() => {
        expect(fs.readFileSync('build/js/0.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/1.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/2.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/3.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/4.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/5.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/6.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/7.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/8.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/9.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/10.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/11.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/12.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/13.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/14.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/15.js').toString()).to.have.length.above(1)
        expect(fs.readFileSync('build/js/16.js').toString()).to.have.length.above(1)

        rimraf('build', done)
      })
    })

    it('does not throw when assets are empty', done => {
      bulbo.build()

      setTimeout(done, BUILD_WAIT)
    })
  })

  describe('watchAndBuild', () => {
    it('builds the assets', done => {
      bulbo.asset('test/fixture/js/0.js').base('test/fixture')

      bulbo.watchAndBuild()

      setTimeout(() => {
        expect(fs.readFileSync('build/js/0.js').toString()).to.equal("console.log('hello')\n")

        rimraf('build', done)
      }, WATCH_BUILD_WAIT)
    })

    it('watches the assets', done => {
      bulbo.asset('test/fixture/js/0.js').base('test/fixture')

      bulbo.watchAndBuild()

      setTimeout(() => {
        expect(fs.readFileSync('build/js/0.js').toString()).to.equal("console.log('hello')\n")

        fs.writeFileSync('test/fixture/js/0.js', "console.log('spam')\n")

        setTimeout(() => {
          expect(fs.readFileSync('build/js/0.js').toString()).to.equal("console.log('spam')\n")

          fs.writeFileSync('test/fixture/js/0.js', "console.log('hello')\n")

          rimraf('build', done)
        }, WATCH_BUILD_WAIT_CHANGED)
      }, WATCH_BUILD_WAIT)
    })
  })

  describe('serve', () => {
    it('serves the assets at port 7100', done => {
      bulbo.asset('test/fixture/**/*.js')

      bulbo.serve().then(() => {
        setTimeout(() => {
          request.get('0.0.0.0:7100/js/foo.js').buffer().end((err, res) => {
            if (err) { done(err) }

            expect(res.text).to.contain('This is foo.js')

            vinylServe.stop(7100)

            done()
          })
        }, SERVER_LAUNCH_WAIT)
      })
    })

{'fraction_non_alphanumeric': 0.21133545147073896, 'fraction_numerical': 0.016091011581428716, 'mean_word_length': 2.8146989835809224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2827065', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3302, 'n_words': 369}