const sfw = require('../lib')

const path = require('path')
const fs = require('fs-extra')

describe('entry point', function () {
  let subs, fixtureDir, watchDir, mainLogFile, workerLogFile

  beforeEach(async function () {
    subs = []

    fixtureDir = path.join(__dirname, 'fixture')
    watchDir = path.join(fixtureDir, 'watched')

    mainLogFile = path.join(fixtureDir, 'main.test.log')
    workerLogFile = path.join(fixtureDir, 'worker.test.log')

    await fs.mkdir(watchDir)
  })

  afterEach(async function () {
    if (this.currentTest.state === 'failed') {
      const [mainLog, workerLog] = await Promise.all(
        [mainLogFile, workerLogFile].map(fname => fs.readFile(fname, {encoding: 'utf8'}).catch(() => ''))
      )

      console.log(`main log:\n${mainLog}`)
      console.log(`worker log:\n${workerLog}`)
    }

    const promises = [mainLogFile, workerLogFile].map(fname => fs.unlink(fname).catch(() => {}))
    promises.push(fs.remove(watchDir))
    promises.push(...subs.map(sub => sub.unwatch()))

    await Promise.all(promises)
  })

  describe('configuration', function () {
    it('validates its arguments', async function () {
      await assert.isRejected(sfw.configure(), /requires an option object/)
    })

    it('configures the main thread logger', async function () {
      await sfw.configure({mainLogFile})

      const contents = await fs.readFile(mainLogFile)
      assert.match(contents, /FileLogger opened/)
    })

    it('configures the worker thread logger', async function () {
      await sfw.configure({workerLogFile})

      const contents = await fs.readFile(workerLogFile)
      assert.match(contents, /FileLogger opened/)
    })
  })

  describe('watching a directory', function () {
    beforeEach(async function () {
      await sfw.configure({mainLogFile, workerLogFile})
    })

    it('begins receiving events within that directory', async function () {
      let error = null
      const events = []

      subs.push(await sfw.watch(watchDir, (err, es) => {
        error = err
        events.push(...es)
      }))

      await fs.writeFile(path.join(watchDir, 'file.txt'), 'indeed')

      await until('an event arrives', () => events.length > 0)
      assert.isNull(error)
    })

    it('can watch multiple directories at once and dispatch events appropriately')

    describe('events', function () {
      it('when a file is created')
      it('when a file is modified')
      it('when a file is renamed')
      it('when a file is deleted')
      it('when a directory is created')
      it('when a directory is renamed')
      it('when a directory is deleted')
    })
  })

  describe('unwatching a directory', function () {
    it('unwatches a previously watched directory')
    it('is a no-op if the directory is not being watched')
  })
})

@@ -2320,16 +2320,1198 @@
 riately'
+, async function () %7B%0A      let errors = %5B%5D%0A      const eventsA = %5B%5D%0A      const eventsB = %5B%5D%0A%0A      const watchDirA = path.join(watchDir, 'dir_a')%0A      const watchDirB = path.join(watchDir, 'dir_b')%0A      await Promise.all(%0A        %5BwatchDirA, watchDirB%5D.map(subdir =%3E fs.mkdir(subdir))%0A      )%0A%0A      subs.push(await sfw.watch(watchDirA, (err, es) =%3E %7B%0A        errors.push(err)%0A        eventsA.push(...es)%0A      %7D))%0A      subs.push(await sfw.watch(watchDirB, (err, es) =%3E %7B%0A        errors.push(err)%0A        eventsB.push(...es)%0A      %7D))%0A%0A      const fileA = path.join(watchDirA, 'a.txt')%0A      await fs.writeFile(fileA, 'file a')%0A      await until('watcher A picks up its event', () =%3E eventsA.some(event =%3E event.oldPath === fileA))%0A%0A      const fileB = path.join(watchDirB, 'b.txt')%0A      await fs.writeFile(fileB, 'file b')%0A      await until('watcher B picks up its event', () =%3E eventsB.some(event =%3E event.oldPath === fileB))%0A%0A      // Assert that the streams weren't crossed%0A      assert.isTrue(errors.every(err =%3E err === null))%0A      assert.isTrue(eventsA.every(event =%3E event.oldPath !== fileB))%0A      assert.isTrue(eventsB.every(event =%3E event.oldPath !== fileA))%0A    %7D
 )%0A%0A    d

Test event multiplexing to multiple watchers