/* eslint "key-spacing": ["error", {"align": "colon"}] */

import fs   from 'fs-extra';
import test from 'ava';

import replaceWithHashed, {ERROR_CONF_MSG, ERROR_REPLACEMENT_MSG} from '../dist/lib/replace';
import helpers from './helpers/integration';

const NAME = 'replace';

const CONFIG = {
  buildFolder : `./test/integration/${helpers.dist(NAME)}`,
  assetsFolder: 'assets',
  buildCssFile: 'css/style.css'
};

const REPLACEMENT = {
  from: ['bundle.js', 'assets/css/style.css'],
  to  : ['bundle-asdasd.js', 'assets/css/style-asdasd.css']
};

test.before(async () => {
  await helpers.distFromTpl(NAME);
});

test('replaceWithHashed', t => {
  t.is(typeof replaceWithHashed, 'function', 'should be a function');
});

test('replaceWithHashed(config)(replacement)', async t => {
  const replaceMap = await replaceWithHashed(CONFIG)(REPLACEMENT);
  const result     = await fs.readFile(replaceMap[0], 'utf8');

  const jsSnippet  = '<script src="bundle-asdasd.js"></script>';
  const cssSnippet = '<link rel="stylesheet" href="assets/css/style-asdasd.css" />';

  t.true(result.includes(jsSnippet), 'should replace bundle file');
  t.true(result.includes(cssSnippet), 'should replace styles file');
});

test('replaceWithHashed(undefined)(replacement)', async t => {
  const err = await t.throws(replaceWithHashed(undefined)(REPLACEMENT));

  t.is(err.message, ERROR_CONF_MSG, 'should reject with an error');
});

test('replaceWithHashed(config, undefined)', async t => {
  const err = await t.throws(replaceWithHashed(CONFIG)(undefined));

  t.is(err.message, ERROR_REPLACEMENT_MSG, 'should reject with an error');
});

test.after.always(async () => {
  await helpers.clean(NAME);
});

@@ -52,16 +52,41 @@
 %22%7D%5D */%0A%0A
+import path from 'path';%0A
 import f
@@ -236,147 +236,158 @@
 ort 
-helpers from './helpers/integration';%0A%0Aconst NAME = 'replace';%0A%0Aconst CONFIG = %7B%0A  buildFolder : %60./test/integration/$%7Bhelpers.dist(NAME)%7D%60
+tmpDir                                                     from './helpers/tmp-dir';%0A%0Aconst _config = dir =%3E (%7B%0A  buildFolder : path.join(dir, 'dist')
 ,%0A  
@@ -443,16 +443,17 @@
 e.css'%0A%7D
+)
 ;%0A%0Aconst
@@ -591,33 +591,36 @@
 t.before
+Each
 (async 
-()
+t
  =%3E %7B%0A  
 await he
@@ -615,38 +615,201 @@
 %7B%0A  
-await helpers.distFromTpl(NAME
+// eslint-disable-next-line ava/use-t-well%0A  const workDir = await tmpDir(path.join(__dirname, 'integration/dist'), t.title);%0A%0A  t.context.tmpDir = workDir;%0A  t.context.config = _config(workDir
 );%0A%7D
@@ -972,32 +972,64 @@
 ', async t =%3E %7B%0A
+  const %7Bconfig%7D   = t.context;%0A
   const replaceM
@@ -1057,22 +1057,22 @@
 hHashed(
-CONFIG
+config
 )(REPLAC
@@ -1704,35 +1704,70 @@
  t =%3E %7B%0A  const 
-err
+%7Bconfig%7D = t.context;%0A  const err     
  = await t.throw
@@ -1790,14 +1790,14 @@
 hed(
-CONFIG
+config
 )(un
@@ -1898,16 +1898,20 @@
 st.after
+Each
 .always(
@@ -1920,10 +1920,9 @@
 ync 
-()
+t
  =%3E 
@@ -1935,26 +1935,34 @@
 ait 
-helpers.clean(NAME
+fs.remove(t.context.tmpDir
 );%0A%7D

use tmp-dir helper in replace.test