const fs = require('fs-extra')
const path = require('path')
const { execSync } = require('child_process')

beforeEach(() => {
  fs.emptyDirSync('tests/__data__/temp')
  fs.copyFileSync(
    'tests/__data__/input/database/save-results.streams.db',
    'tests/__data__/temp/streams.db'
  )

  const stdout = execSync(
    'DB_DIR=tests/__data__/temp LOGS_DIR=tests/__data__/input/logs/load-cluster node scripts/commands/save-results.js',
    { encoding: 'utf8' }
  )
})

it('can save results', () => {
  expect(content('tests/__data__/temp/streams.db')).toEqual(
    content('tests/__data__/expected/save-results.streams.db')
  )
})

function content(filepath) {
  const data = fs.readFileSync(path.resolve(filepath), {
    encoding: 'utf8'
  })

  return data
    .split('\n')
    .filter(l => l)
    .map(l => {
      return JSON.parse(l)
    })
}